For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

On-close orders

  • Hi, Backtraders!

    I want to test an intraday strategy that involves close positions at the close of each day. Interactive brokers have a type of order to achieve this: Market-on-Close (MOC) Orders. Reading the documentation of backtrader it seems that it doesn't have this feature in backtest mode.

    From a point o view, it will not consider appropriate to ask a question while rising a solution because it could induce bias but I will make an exception looking for feedback of my approach in case no other possible solution arrives:

    To achieve this kind of behavior I thought to modify my data feed to include two additional bars after the last bar of the day (16:00:00). One will be at 16:00:01 to create a market order and the other one will be at 16:00:02 to execute the order. Both will have in the OHLC data the close price of the 16:00:00 bar.

    I'm not simply close my positions at 15:49:00 (supposing minute bars) in order to be executed at 16:00 because I'm not using time bars. So, I have uncertainty about at what time it will be the previous bar to the close of the day.

    Could you think in another way to incorporate (or simulate) on-close orders in backtesting with backtrader? Or could you share an inconvenience that you see in my approach?


  • Since you are trying to come up with some tricks, than you don't use intraday data, am I right?

    If this is a case, than check out the data filters, in particular Docs - Daysplitter_Close.

  • administrators

    @J-Javier-Gálvez said in On-close orders:

    Reading the documentation of backtrader it seems that it doesn't have this feature in backtest mode.

    It is in the docs as Close and uses the known (or implied) end of session price to execute.

  • Hi, @backtrader, I find quite a similar question in my case. In fact, I want to invest the OTC fund which I buy and sell at the everyday close price so I have to make decisions based on the current 'predicted' index signal and use the close price of the current bar when it closes.

    I read and tried the Docs-Close which says using the close price of the next bar when the next bar actually CLOSES, as well as the Docs-Daysplitter_Close scheme splitting intraday ticks into OHLO and CCCC mentioned by @ab_trader. Finally, both are found not desirable and I didn't find a proper way to implement. Do you have any suggestions?


  • administrators

    That's called cheat-on-close. See: Docs - Broker -

  • @backtrader Thanks for your prompt reply and it does solve my problem. Sorry that I missed it in my first reading. Thanks again for your support.