Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calculate MA as of yest close, then trade today at open

Experts, would like to know how you'd modify this simple mean reversion strategy so that it calculates the MA as of yesterday's close, then trades today at open. Right now it calculates the MA at today's open and trades at the open. Thanks in advance!

5 responses

Try this way

Vladimir thanks for the reply! The log shows a 931 timestamp. Is this calculating the 5d MA at 931a when schedule_function runs -- instead of the prior's day close?
I may need another schedule_function to run at the market close, but I'm not sure how to do it. I tried to setup a second "def signal" to calculate the MA at the close, but "def rebalance" didn't recognize the MA defined in "def signal." Hope that makes sense?? Thanks for taking the time to contribute to this project.

2015-12-21 09:31 PRINT 204.77
2015-12-22 09:31 PRINT 203.47
2015-12-23 09:31 PRINT 201.79
2015-12-24 09:31 PRINT 201.72
2015-12-28 09:31 PRINT 203.74
2015-12-29 09:31 PRINT 205.04
2015-12-30 09:31 PRINT 205.6
2015-12-31 09:31 PRINT 206.07

12/18/2015 6:31:00 AM (PST)
prices = data.history(stock, 'price', 6, '1d')
print prices[0:-1]
12/11/2015 00:00:00+00:00 200.666
12/14/2015 00:00:00+00:00 201.79
12/15/2015 00:00:00+00:00 203.877
12/16/2015 00:00:00+00:00 206.73
12/17/2015 00:00:00+00:00 203.698

average (prices) 203.3522
ma =prices.iloc[0:-1].mean()
print ma 203.3522

12/21/2015 6:31:00 AM (PST)
prices = data.history(stock, 'price', 6, '1d')
print prices[0:-1]
12/14/2015 00:00:00+00:00 201.79
12/15/2015 00:00:00+00:00 203.877
12/16/2015 00:00:00+00:00 206.73
12/17/2015 00:00:00+00:00 203.698
12/18/2015 00:00:00+00:00 199.98

average (prices) 203.215
ma =prices.iloc[0:-1].mean()
print ma 203.215

@Vladimir

Do you mind if I ask what the idea behind slicing the history data is?
As in

moving_average = data.history(security, 'price', 5+1, '1d')[0:-2].mean()  

Why are we excluding the last two points?

Got it. Thank you Vlad!