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?
-
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 tococ
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.
-
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 tocerebro
withreplaydata
indicating whichtimeframe
andcompression
should be the output in the system (probablyTimeFrame.Days
and1
)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
See: https://www.backtrader.com/docu/data-replay/data-replay.html?highlight=replay
-
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.
-
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.
-
@backtrader said in Convincing strategy to buy at close of current bar in backtest:
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)@ab_trader said in Convincing strategy to buy at close of current bar in backtest:
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.
Hi. I faced the same issue. Does anybody know how to force the plotting to draw sell/buy signals in right place (with cheat-on-close mode enabled) ?