Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Backtesting on the closing price

Hi,
I am trying to do something that should be very simple, but I am really struggling to find the good method.

How can I program an algorithm that only buy/sell on the closing price of a stock?

For instance, the backtest I am performing here is to buy when the closing price is above the MVG10 and sell if it gets bellow. The buy/sell signal trigged on Intraday prices and that is not the behaviour I am trying to get.

Thank you,

5 responses

A few questions...

You simply want to:
buy when close_price > mean_close_price_10
sell when close_price < mean_close_price_10

How often do you want to trade? Every minute or every day or less often? What time of day do you want to trade? When you say 'close price' is it the minute close or the daily close price?

The handle_data function is called every minute so the code is looking at the current close price of every one minute tick. An easy solution would be to do a schedule function which calls it once every day(or however often you want) so you are looking at daily data rather than minute data.

Thank you for your reply.

Here is my reply to your question, Dan :

You simply want to:
buy when close_price > mean_close_price_10
sell when close_price < mean_close_price_10
Yes

How often do you want to trade? Every minute or every day or less often? Every day
What time of day do you want to trade? At the open ( I should thus maybe use open_price instead of close_price)
When you say 'close price' is it the minute close or the daily close price? The daily close price

Thank you Andy; I will also study the schedule function.

The schedule function solved indeed all my problems.

I have found a good explanation here : https://www.youtube.com/watch?v=8HG1DGDKpPM

Thanks for your support!

Maybe something like this...

Notice that the algorithm can easily be adjusted to trade any number of stocks by simply adding them to 'MY_STOCK'. The buy/sell rules can also easily be changed by changing the two 'rules' variables. For example, if you want to trade based upon the previous day close price then simply change the rules to these:

    open_rules = 'close_price > sma_10'  
    close_rules = 'close_price < sma_10'