Close price comparison at specific times to generate signals

Hi Guys

I'm trying to do some backtesting using the notebook to generate a buy signal that updates the signal column at 8:15AM every morning wondering if anyone can shed some light on this.

The idea is that the close price of the stock has to close above the upper Bolinger band and the price also has to be higher than all the previous close prices to 01:00 hours that evening this will generate the buy signal. I cannot figure a short way of doing this as it will iterate back 30 close price points at each price points is 15 minute candles.

df.loc[(df['close'].shift(1)>df['upper'].shift(1))&(df['close'].shift(1)>df['close'].shift(2)) \
& (df['close'].shift(1)>df['close'].shift(3)), ............................................. 'Signal']=1

My other sticky point is how to only check this at 8:15 AM and if above is true update a signal column to 1 at that time.

I can see the data by doing the following but thats about it.
sig_time = dt.time(8, 15, 0)
df.loc[sig_time]

Any help will be much appreciated.

Regards

Al

Hi Alex,



There is an easier way of doing this. If I understand correctly, you want to generate signals when the time is 8:15. You can do this by first calculating the technical indicators such as the Bollinger bands and the ArronUp indicator from the Talib library.

 

df.upper, df.middle, df.lower = talib.BBANDS(df.close, 14)
df.aroondown, df.aroonup = talib.AROON(df.high, df.low, timeperiod=4)

Then convert the index to DateTime using pandas in-built function to_datetime and use the .time property to check the time.

df.index.time==time(8,15) to select the market open times.

Once you have these opening times, you check the if the close price is more than upper Bollinger band and Arronup is equal to 100. Aroonup will give you a score of 100 if the close price is the highest in the last x periods. Assuming that you are using a 15-minute candle then a time period of 4 in Aroonup calculation will give if a new high has been formed in the last one hour.

condition = [(df.index.time==time(8,15)) & (df.close>df.upper) & (df.aroonup==100)]
df.loc[condition,'Signal']=1

You can combine the three conditions: time, Bollinger and Aroon along with .loc property on the entire dataset to get what you want. Hope this helps. 

Thank you quanta team for the prompt answer, I will test this and let you know how I get along.

Hi Guys



I have managed to get a positive result on the Aroonnup code any improvments on will be much appreciated.  Thank you for your input.



I have written a snippet of the code below. any updates will be much appreciated.



Kind Regards



Alex



------------------------------------------------------------------------------------------------------------



df['aroondown'], df['aroonup'] = talib.AROON(df['close'], df['close'], timeperiod=29)

dt.time(8,15,0)

df['Signal']=0

df.loc[(df.index.time==dt.time(8,15,0)) & (df['aroonup']==100 ) & (df['close']>df['upper']),'signal']=1

Hi Alex,



Your code looks good. You can further try to refine the signals by adding some Volume-based indicators from the Talib library. Ex: OBV (On Balance Volume) to check for momentum backed by volume.