You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The equity should not change when buying on the last day with trade_on_close = True.
Actual Behavior
The equity increases by data.Close[-1]/data.Close[-2] (assuming data.Close[-1] > data.Close[-2])
Steps to Reproduce
Make the strategy that buys at the last candle
Set trade_on_close = True
Additional info
For example, my strategy buys at the last candle and trades on close. The last closing price is 170.33 and data.Close[-2] is 159.22. And the equity was 862.479%, but it incorrectly becomes 922.584% at the last candle, which is approximately equal to 862.479%*170.33/159.22 (the error is presumably the remaining cash that is not sufficient to buy a share).
This should not cause any problem if the strategy is not buying at the last candle. But when it buys at the last candle, it incorrectly takes the previous closing price as the buying price on the day it decides to buy.
I tentatively fixed it by modifying the code as follows.
Expected Behavior
The equity should not change when buying on the last day with trade_on_close = True.
Actual Behavior
The equity increases by data.Close[-1]/data.Close[-2] (assuming data.Close[-1] > data.Close[-2])
Steps to Reproduce
Additional info
For example, my strategy buys at the last candle and trades on close. The last closing price is 170.33 and data.Close[-2] is 159.22. And the equity was 862.479%, but it incorrectly becomes 922.584% at the last candle, which is approximately equal to 862.479%*170.33/159.22 (the error is presumably the remaining cash that is not sufficient to buy a share).

The possible reason for this behavior might be
in backtesting.py.
This should not cause any problem if the strategy is not buying at the last candle. But when it buys at the last candle, it incorrectly takes the previous closing price as the buying price on the day it decides to buy.
I tentatively fixed it by modifying the code as follows.
But I am not sure whether that would cause any other issue. I appreciate any help you can provide!
The text was updated successfully, but these errors were encountered: