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

Convincing strategy to buy at close of current bar in backtest

  • Curious if there is any way to convince the backtrade analysis to buy at close price of current bar.

    My use case is that in the system I am attempting to port to backtrader, the strategy buys at close of the current day provided the conditions are met. The system is looking at daily data for previous days but monitors minute data to make the decision about trade entry based on the current day's close.

    This strategy appears to be supported provided I have the different resolution of data sources configured. Would like to be able to run a backtest based on the daily data and just pin the entry price to the close price for that day's data.

    I see in the docs where the behavior is currently open of next day.

    Any way to modify this behavior?

  • administrators

    The behavior is to buy the next incoming tick, which in all cases and after examining a closed bar is the opening tick of the next bar.

    If make a decision (as stated here):

    to make the decision about trade entry based on the current day's close.

    on a known price and you buy that price ... you are cheating yourself. There seem to be enough people willing to cheat, that's why the option is supported with the name cheat-on-close. (shorted to coc for a comfortable use)

  • Thank you.

    In my defense, I am "cheating" to avoid having to manage two time-frames of data in this strategy analysis. In a live trading system, I recognize that I would not be able to get the closing price for the bar before it is actually closed. :smirk:

  • administrators

    No need for defense. There are many ways to win the war.

    In this case i would anyhow like to point out that you may be looking for replaying.

    You create the data feed and instead of adddata you pass it to cerebro with replaydata indicating which timeframe and compression should be the output in the system (probably TimeFrame.Days and 1)

    In each iteration you could be checking if the time is close to the end of the session and act accordingly (the indicators will be calculated in this case on a daily basis)

    Thia removes the cheating, let's you operate with a sinhel timeframe and simulate real market conditions


  • With .set_coc(True) the order is executed on the same bar, but the order triangle on the plot is still shown on the next bar similar to regular behavior.

  • administrators

    The observer in charge of plotting only sees the notification in the next cycle.

    Check the order.executed.dt field and you will see the order was properly executed on the right bar (even if the observer doesn't know it)

  • The order is executed as required, it is just the plotting and it confuses a bit. I know the plotting is not the main goal of the backtrader, but sometimes visual check of the signals/executions is really helpful.