I load multiple pandas dataframes in a dictionary each representing a different timeframe of data. I then loop through each dataframe adding columns with calculated values(from other columns). The last column i have written out the logic in psuedo code but it will get quite complex with all the nested if statements so I was looking for some help as to structure the code and logic.
Here is a sample of the for loop to starting adding columns to the dataframes based on calculations of df fields.
for key in df.hist:
df.hist[key] = df.hist[key].assign(Target=(df.hist[key]['Spike'] == True))
df.hist[key] = df.hist[key].assign(StopLow=np.where(df.hist[key]['Target']==True, df.hist[key]['low'],None))
df.hist[key] = df.hist[key].assign(targetLow=np.where(df.hist[key]['Target']==True, df.hist[key]['low'],None))
df.hist[key] = df.hist[key].assign(MomentumVal=np.where(df.hist[key]['Target']==True,'Initiated',None))
..... ..... Now here is the pseudo logic that I would like to basically update the column 'MomentumVal' that i've created above. Anything with a [1] or [2] means one or two rows previously. Any help as what the most efficient way to do this would be appreciated.
if MomentumVal[1] == 'Initiated':
if shortLR > shortLR[1] and shortLR >0:
if midLR > midLR[1] and midLR >0:
MomentumVal = 'midtMoMo'
else:
MomentumVal = 'shrtMoMo'
elseif midLR > midLR[1] and midLR >0:
if lngLR > lngLR[1] and lngLR >0:
MomentumVal = 'lngtMoMo'
else:
MomentumVal = 'midtMoMo'
elseif low < targetLow:
MomentumVal = 'Exit'
else:
Momentum = Initiated
elsif MomentumVal[1] == shrtMoMo:
if midLR > midLR[1] and midLR >0:
if lngLR > lngLR[1] and lngLR >0:
MomentumVal = 'lngtMoMo'
else:
MomentumVal = 'midtMoMo'
elsif shortLR[2]> shortLR[1] and shortLR[1] > shortLR:
MomentumVal = 'Exit'
else:
MomentumVal = shrtMomo
elseif MomentumVal[1] == midMoMo:
if lngLR > lngLR[1] and lngLR >0:
MomentumVal = 'lngtMoMo'
elsif midLR[1] > midLR and close < close[1]:
MomentumVal = 'Exit'
else:
MomentumVal = 'midMomo'
elseif MomentumVal[1] == 'lngMoMo':
if lngLR[1] > lngLR:
MomentumVal = 'Exit'
else:
MomentumVal = 'lngMomo'
else:
MomentumField = None
Aucun commentaire:
Enregistrer un commentaire