Since people seem to like Sentdex's tutorial for Quantopian, and they have not been updated for Quantopian 2- I thought updating them would be valuable to people here and a good way to finally learn Quantopian!
This first algorithm is a simple long-only trend following strategy. It highlights two of the major changes between Quantopian 1 and Quantopian 2 First, data[asset].mavg(days)
has been replaced with data.history(asset, 'price', days, '1d').mean()
. Second, his handle_data
function should be replaced with a scheduled rebalance
function.
Potential "Gotchya": When called before trading, like in before_trading_start
, data.history(asset,'price', 10,'1d')
does exactly what you expect: return the closing price for the previous 10 days. However, if it is called during trading, i.e. in either a handle_data
call or scheduled rebalancd
call, then it returns the most recent price observed in trading today and the closing price for the previous 9 days. (If you ever have questions about what's being returned, I found setting a debug-point and using get_datetime()
to be useful to make sure I was working with the correct data.)
OH, and I much prefer my order entry logic and design pattern over his, but let me know if you feel otherwise.