Back to Community
Scanning

I am new here, I have a basic understanding of python and I started by downloading an intraday database and have found multiple algorithms that I believe to be strong. Now I am struggling to implement the algorithms to live trading here. I usually don't post in forums but I have spent countless hours reading with no luck. So this is my problem:

I do not understand the logic behind the classes that must be called and narrowing the universe. This doesn't seem to follow general python language, I have read about the initialize(context), handle_data(context, data) etc. But what I do not understand is what these actually mean on the basic level. What is context the context and data that is being passed?

I am specifically wanting to scan for all stocks over 25% on the day, is there a way to initialize all stocks?
Can I do a nightly scan for all stocks over 25%, if so would that be in a schedule_function, and can schedule function stand alone?

Then, I want to update the universe into classifications based on the gap, days gain percentage, and the opening price in relation to standard bollinger bands (2,20). Also, I'd like to access the open, close, low, and high of the day of gain. I don't want to get rid of the fully scanned universe but just classify them based on these.

I want the buy and sell point to be based on the crossing of various bollinger bands.

Basically....

1) At night, scan all stocks for 25% gainers.
2) Same night, extract data... previous close, open, high, and close to perform calculations.
3) Upon market open the next day, calculate gap, calculate relationship between open price and bollinger bands.
4) Given whatever the categories, if it crosses a specific bollinger band, buy, and a second cross of another band, sell.

I apologize for my lack of experience, what really seems to be standing in my way is the basic understanding of the classes, how to call them, and what they actually mean. I have searched at least 100 hours on these basic problems with no luck at all.

I appreciate any help you can give, and if you don't have much time, mainly I would like to know how to scan the full universe and extract market data into a list. If I could just scan for the specific stocks and get the data for each stock into lists I think I could figure out the rest. A detailed logical overview of how the definitions work would also be greatly appreciated.

If the pipeline is necessary, a basic overview of what exactly is going on when a pipeline is created would also be greatly appreciated. Thank you so much! Hopefully, I will get this figured out with a little help!

1 response

Hello Abby,

The easiest way to get started is to steal the code from someone else who has an algo similar to what you want to do. In your case I think the closest algo you will find is this: https://www.quantopian.com/posts/intraday-mean-reversion-buy-on-gap-model. Specifically you could change a few things in the def enter_positions(context,data). Here are a few examples of code that might give you some ideas:
price_history = data.history(context.security_list, 'price', 5, '1m')
y_close = price_history[-4:-3].mean()
t_open = price_history[-2:-1].mean()
context.output['yclose'] = y_close.values
context.output['topen'] = t_open.values
context.output['gaining_today'] = context.output['topen']/context.output['yclose']

vol_history = data.history(context.security_list, 'volume', 3, '1m')  
vol_avg = vol_history[-2:-1].mean()  
context.output['volavg'] = vol_avg.values  
context.output['volumeavg'] = context.output['topen']*context.output['volavg']  

filtered_uni = context.output[context.output['gaining_today'] > 1.25]  
filtered_vol = filtered_uni['volumeavg'] > 50000.0  
context.longs = filtered_uni[filtered_vol].sort_values(['gaining_today']).tail(context.num_long)

Have a good day.