0

So Im making a program where the variables fastMA and slowMA below are calculated every minute (below). I am trying to figure out a way to add each instance they are calculated to a series so that I can compare the most recent minute's values with what they were in the last minute. So I only need the two most recent calculations for each.

if len(dfC) >= 75:

    fastMAcloses = pd.DataFrame(dfC.tail(20)).values
    fastMA = fastMAcloses.sum() / 20
    print(fastMA)

    slowMAcloses = pd.DataFrame(dfC.tail(75)).values
    slowMA = slowMAcloses.sum() / 75
    print(slowMA)

else:
    print('... calculating MAs in', 75 - len(dfC), 'min')

What ive come up with so far is:

    columnsMA = ['Fast MA:', 'Slow MA:']
    valuesMA = [fastMA, slowMA]
    listMA = list(valuesMA)
    seriesMA = pd.Series(listMA, columnsMA)
    print(seriesMA)

However with this each time theyre calculated, the new minute's values overwrite the previous ones so they can't be compared. If you can help I thank you so much!

1
  • 1
    The shift() method may help. For e.g. df['a'].shift() will be a new series with the same length, but every row shifted down by one, so say df['a'] + df['a'].shift() will be the sum of the current value and the previous value.
    – alkasm
    Feb 27, 2019 at 21:54

1 Answer 1

1

You could try something similar to the following:

columnsMA = ['Time:','Fast MA:', 'Slow MA:']

#Initiate results
results=pd.DataFrame([[datetime.datetime.now(),fastMA,slowMA]],columns=columnsMA)
                print(results)

#Append to results (say every minute)
dfTemp=pd.DataFrame([[datetime.datetime.now(),fastMA,slowMA]],columns=columnsMA)
results=results.append(dfTemp,ignore_index=True)
print(results)

#Append to results (say every minute)
dfTemp=pd.DataFrame([[datetime.datetime.now(),fastMA,slowMA]],columns=columnsMA)
results=results.append(dfTemp,ignore_index=True)
print(results)

    ... keep appending to the dataframe ....

There is also a concat function which you could investigate further.

I guess that if you wanted to only keep 2 separate minutes of data in memory rather than the full history, you could just build a data frame from the tail and the new calculation results.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.