-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
trade_on_close=True uses Open price for contingent orders #1218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There may be an issue with settle prices for contingent orders equalling the Open prices even when backtesting.py/backtesting/backtesting.py Lines 906 to 909 in 14f111a
But your issue isn't it.
https://quant.stackexchange.com/questions/64148/how-to-get-adj-open-high-and-low-prices |
Thanks for replying. Below is a numerical example using real results
that hopefully will clarify my observations. This was generated with the
Trade_on_Close=True. You can see that the buy trade (Signal =1) done on
2/21/2007 was done using a closing price of 9.863242 while the sell price
was done using the next day's (03/02/2007) open price at 10.39125. I feel
the sell trade should have been done using the close price of 9.132671 as
of 03/01/2007. As mentioned, I use adjusted prices for my close price
column but leave the open/High/Low prices unadjusted since I do not use
them. In previous versions this was not an issue.
[image: {5D2FC2AD-85F5-4E6F-9546-D7B4DD510C2E}.png]
Regards
Patrick
…On Sun, 16 Feb 2025 at 22:05, kernc ***@***.***> wrote:
There may be an issue with settle prices for contingent orders equalling
the Open prices even when trade_on_close=True— we even have a comment
warning of it ... 🤔
https://github.com/kernc/backtesting.py/blob/14f111af896b5e1fefb0ff81077c3a72579e6ce0/backtesting/backtesting.py#L906-L909
------------------------------
But your issue isn't it.
in my price file I use adjusted prices for my close prices and all other
prices (open, hi, lo) are left unadjusted. The result is that my buy trades
are done with adjusted prices and my sales are done with unadjusted prices.
Backtest
<https://kernc.github.io/backtesting.py/doc/backtesting/backtesting.html#backtesting.backtesting.Backtest>
expects data= with at least four columns (Open, High, Low, Close) and
relies on all of them internally consistent. If you adjust the close, you
need to adjust the other columns too!
https://quant.stackexchange.com/questions/64148/how-to-get-adj-open-high-and-low-prices
—
Reply to this email directly, view it on GitHub
<#1218 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APNLQRQC2CPUD65JDNH4HML2QFGV7AVCNFSM6AAAAABXAKERZKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNRRHA2TQNRSGM>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
[image: kernc]*kernc* left a comment (kernc/backtesting.py#1218)
<#1218 (comment)>
There may be an issue with settle prices for contingent orders equalling
the Open prices even when trade_on_close=True— we even have a comment
warning of it ... 🤔
https://github.com/kernc/backtesting.py/blob/14f111af896b5e1fefb0ff81077c3a72579e6ce0/backtesting/backtesting.py#L906-L909
------------------------------
But your issue isn't it.
in my price file I use adjusted prices for my close prices and all other
prices (open, hi, lo) are left unadjusted. The result is that my buy trades
are done with adjusted prices and my sales are done with unadjusted prices.
Backtest
<https://kernc.github.io/backtesting.py/doc/backtesting/backtesting.html#backtesting.backtesting.Backtest>
expects data= with at least four columns (Open, High, Low, Close) and
relies on all of them internally consistent. If you adjust the close, you
need to adjust the other columns too!
https://quant.stackexchange.com/questions/64148/how-to-get-adj-open-high-and-low-prices
—
Reply to this email directly, view it on GitHub
<#1218 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APNLQRQC2CPUD65JDNH4HML2QFGV7AVCNFSM6AAAAABXAKERZKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNRRHA2TQNRSGM>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Not sure if my example came through in the e-mail. I have attached the spreadsheet from which it was created. Regards Patrick |
Please try v0.6.2 that's just been released! 🙌 |
Thanks very much. The newest version fixes the issue re trade_on_close.
I've enjoyed using Backtinging.py and think it is a very good app. It has
the right mix of functionality and usability. I've recommended it to
several people.
Regards
Patrick
…On Wed, 19 Feb 2025 at 10:30, kernc ***@***.***> wrote:
Please try v0.6.2 that's just been released! 🙌
—
Reply to this email directly, view it on GitHub
<#1218 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APNLQRS2PW72L2AJZCC4KET2QSPPZAVCNFSM6AAAAABXAKERZKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNRYHE4TCOBTGY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
[image: kernc]*kernc* left a comment (kernc/backtesting.py#1218)
<#1218 (comment)>
Please try v0.6.2 that's just been released! 🙌
—
Reply to this email directly, view it on GitHub
<#1218 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APNLQRS2PW72L2AJZCC4KET2QSPPZAVCNFSM6AAAAABXAKERZKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNRYHE4TCOBTGY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Expected behavior
Hi after downloading the most recent version of Backtesting .py (0.6.1) and running it I noticed material increases in my stats generated. I currently use the backtest module with the trade_on_close set to True (see below). The expected behaviour based on earlier versions is that both the buy (entry price) and sell (exit price) trades are execute using the closing price on the day on which the respective signal is generated.
This functionality had been working as expected in previous versions as I use Backtesting.py to validate my own backtesting code.
Code sample
Actual behavior
After looking at the backtesting.py trades file and comparing this to my own prices dataframe it appears that when I sell/close a position, the price used is the following day's open price not the closing price of the day on which the sell signal was generated. This issue is exasperated due to the fact that in my price file I use adjusted prices for my close prices and all other prices (open, hi, lo) are left unadjusted. The result is that my buy trades are done with adjusted prices and my sales are done with unadjusted prices.
The trade_on_close functionality had been working in previous versions.
Additional info, steps to reproduce, full crash traceback, screenshots
Below are stats run with and without the trade_on_close set to False or True. The "false" setting (left side) appears to be working as both buy and sell trades are executed using the next day open. The "True" (right side) is showing the affect of buy trades done on the close (adjusted prices) and the sell done at the next day open (unadjusted prices).
Trades 99 | | # Trades 99
Win Rate [%] 47.4747 | | Win Rate [%] 76.7677
Best Trade [%] 68.7149 | | Best Trade [%] 72.7316
Worst Trade [%] -18.1411 | | Worst Trade [%] -16.8448
Avg. Trade [%] 2.0143 | | Avg. Trade [%] 6.1904
Max. Trade Duration 357 days 00:00:00 | | Max. Trade Duration 357 days 00:00:00
Avg. Trade Duration 41 days 00:00:00 | | Avg. Trade Duration 41 days 00:00:00
Profit Factor 2.2587 | | Profit Factor 9.7871
Expectancy [%] 2.5082 | | Expectancy [%] 6.7451
SQN 1.4753 | | SQN 1.6812
Kelly Criterion 234.9810m | | Kelly Criterion 520.8895m
_strategy DIY_Signal_Gener... | | _strategy DIY_Signal_Gener...
_equity_curve Eq... | | _equity_curve Eq...
_trades Size EntryB... | | _trades Size Entry...
dtype: object | | dtype: object
Software versions
Currently running Backtesting.py version 0.6.1, Sambo version 1.25.0
The text was updated successfully, but these errors were encountered: