Im new to quantopian, I have some experience in manual trading techniques and I want to learn how to code them. I was formerly coding them on pandas dataframes by my self and doing simple backtest and now I would like to use the quantopian backtester.
I know how to create everithing I want in dfs, but not i dont know hot to do it in the pipe line.
What I want is, Starting form the ZigZag indicator (https://www.quantopian.com/posts/zigzag-implementation-example) get the following factors:
- the low bounce trend ( example one low at 7,next at 5, next at 6, next at 8 , current at 9. The current low trend value would be +3 because there were 3 consecutive higher bounces in a row)
- the high bounce trend (same but for the higher bounces)
- current state of wave ( if it is going up or down)
- the moment y realize the wave change ( zigzag is a delayed indicator, you realice that there is a bounce once the price is x% abobe the bounce)
- the level of Fibonacci of the retracement
My current code working with Dfs is :
def ZigZagBuild(dfSeries, minSegSize=2): #function for building the indicator
minRetrace = minSegSize
curVal = dfSeries[0]
curPos = dfSeries.index[0]
curDir = 1
dfRes = pd.DataFrame(index=dfSeries.index, columns=["Dir", "Value",'Change','LastBounce'])
for ln in dfSeries.index:
if((dfSeries[ln] - curVal)*curDir >= 0):
curVal = dfSeries[ln]
curPos = ln
else:
retracePrc = abs((dfSeries[ln]-curVal)/curVal*100)
if(retracePrc >= minRetrace):
dfRes.ix[curPos, 'Value'] = curVal
dfRes.ix[curPos, 'Dir'] = curDir
if curDir == 1:
dfRes.at[ln, 'Change'] = 1
dfRes.at[ln, 'LastBounce'] = curVal
if curDir == -1:
dfRes.at[ln, 'Change'] = -1
dfRes.at[ln, 'LastBounce'] = curVal
curVal = dfSeries[ln]
curPos = ln
curDir = -1*curDir
dfRes[['Value']] = dfRes[['Value']].astype(float)
dfRes = dfRes.interpolate(method='linear')
return(dfRes.Dir, dfRes.Value, dfRes.Change, dfRes.LastBounce)
#Dir is the direction of the bounce
#Value is the value of the line
#Change is the direction of the bounce but placed in the row where I realice the change was made ####### one of the factor I want
#LasBounce , the value of the bounce in the row where I realice the change was made
def ZigZagVals(df):
zigzagVals= [2]
for z in zigzagVals :
dir = 'Z{:1}Dir'.format(z)
val = 'Z{:1}Val'.format(z)
change = 'Z{:1}Change'.format(z)
lastbounce = 'Z{:1}LastBounce'.format(z)
df[dir],df[val],df[change], df[lastbounce] = ZigZagBuild(df['Close'], z)
######################## trends ######################
HT = 'Z{:1}HT'.format(z)
LT = 'Z{:1}LT'.format(z)
h1 = [df[dir] == 1] ## Highs
df['h1'] = np.select(h1, [df[val]], default= 0)
df['h1'].replace(to_replace=0, method='ffill', inplace = True)
h2 = [df['h1'] > df['h1'].shift(), df['h1'] < df['h1'].shift() ]
df['h2'] = np.select(h2, [1,-1], default= 0)
h = df.groupby(df.h2.mask(df.h2==0).ffill().diff().ne(0).cumsum()).h2
df[HT] = h.cumsum()-h.transform('first') ################ This is the high bounce trend one of the factor I want
l1 = [df[dir] == -1] ## Lows
df['l1'] = np.select(l1, [df[val]], default= 0)
df['l1'].replace(to_replace=0, method='ffill', inplace = True)
l2 = [df['l1'] > df['l1'].shift(), df['l1'] < df['l1'].shift() ]
df['l2'] = np.select(l2, [1,-1], default= 0)
l = df.groupby(df.l2.mask(df.l2==0).ffill().diff().ne(0).cumsum()).l2
df[LT] = l.cumsum()-l.transform('first') ################ This is the low bounce trend one of the factor I want
##############################
prevLow = 'Z{:1}PrevLow'.format(z)
df[prevLow] = np.select([df[dir] == -1],[df['Close']], default = np.nan )
df[prevLow].fillna(method='ffill', inplace = True)
df[prevLow] = df[prevLow].shift()
###########
wave = 'Z{:1}wave'.format(z)
df[wave] = np.select([df[dir] == -1, df[dir] == 1],[1,-1], default = np.nan )
df[wave].fillna(method='ffill', inplace = True)
df[wave] = df[wave].shift() ##########################This is the current state of wave one of the factor I want
###########
bounce = 'Z{:1}Bounce'.format(z)
df[bounce] = np.select([(df[dir] == -1)|(df[dir] == 1)],[df[val]], default = np.nan )
df[bounce].fillna(method='ffill', inplace = True)
df[bounce] = df[bounce].shift()
##################################
prevWaveZize = 'Z{:1}prevWaveZize'.format(z)
df[prevWaveZize] = np.select([(df[dir] == -1)|(df[dir] == 1)],[df[bounce].shift(-1) -df[bounce] ], default = np.nan )
df[prevWaveZize] = df[prevWaveZize].shift()
df[prevWaveZize].fillna(method='ffill', inplace = True)
####################################
fib = 'Z{:1}Fibonacci'.format(z)
df[fib] = -(df['Close']-df[bounce])*100.0/ df[prevWaveZize]
###########
fibBounce = 'Z{:1}FibBounce'.format(z)
df[fibBounce] = np.select([(df[dir] == -1)|(df[dir] == 1)],[df[fib]], default = np.nan )
df[fibBounce].fillna(method='ffill', inplace = True)
df[fibBounce] = df[fibBounce].shift() ####################This is the level of Fibonacci one of the factor I want
df = df.drop(['h1','h2','l1','l2'],1)
Sorry that everithing is too messy, im just learniing how to code and maybe the techniques i use are not the best.
I aprecate any recomendations,
thanks,
Marcos