kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.39k stars 1.05k forks source link

Sell order being executed with a much lower price than the open/close of next candle. #1003

Closed waemm closed 1 year ago

waemm commented 1 year ago

Expected Behavior

I am entering a sell trade on the 9:14 candle with SL 1.10056 and TP 1.09873. The limit order price should be the next candle open of 1.09079 but instead I get the error

File "", line 1, in self.sell(size=percent_total_position_size, sl=self.short_sl,tp=tp) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtesting/backtesting.py", line 225, in sell return self._broker.new_order(-size, limit, stop, sl, tp) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtesting/backtesting.py", line 718, in new_order raise ValueError( ValueError: Short orders require: TP (1.09873) < LIMIT (1.09872018) < SL (1.10056)

I don't understand why the limit is so far below any of the follow candle open/close prices. Is this an issue with the number of decimal places ?

Thanks!

Additional info

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">

dt_timestamp | Open | High | Low | Close -- | -- | -- | -- | -- 2023-05-10 09:09:00-04:00 | 1.09915 | 1.0992 | 1.09899 | 1.09902 2023-05-10 09:10:00-04:00 | 1.09901 | 1.09913 | 1.09891 | 1.09911 2023-05-10 09:11:00-04:00 | 1.0991 | 1.09947 | 1.09906 | 1.09947 2023-05-10 09:12:00-04:00 | 1.09947 | 1.09956 | 1.09927 | 1.09937 2023-05-10 09:13:00-04:00 | 1.09937 | 1.09958 | 1.09936 | 1.09957 2023-05-10 09:14:00-04:00 | 1.09957 | 1.09997 | 1.09956 | 1.09982 2023-05-10 09:15:00-04:00 | 1.09983 | 1.09984 | 1.09924 | 1.09924 2023-05-10 09:16:00-04:00 | 1.09925 | 1.09952 | 1.09917 | 1.09919 2023-05-10 09:17:00-04:00 | 1.09919 | 1.09923 | 1.09897 | 1.09898 2023-05-10 09:18:00-04:00 | 1.09898 | 1.09898 | 1.09876 | 1.09877 2023-05-10 09:19:00-04:00 | 1.09881 | 1.09918 | 1.09881 | 1.09918 2023-05-10 09:20:00-04:00 | 1.09917 | 1.09926 | 1.09905 | 1.09923 2023-05-10 09:21:00-04:00 | 1.09919 | 1.09932 | 1.09902 | 1.09902 2023-05-10 09:22:00-04:00 | 1.09901 | 1.09912 | 1.09882 | 1.09882 2023-05-10 09:23:00-04:00 | 1.09881 | 1.09881 | 1.09832 | 1.09836 2023-05-10 09:24:00-04:00 | 1.09836 | 1.09846 | 1.09823 | 1.09837

kernc commented 1 year ago

Adjusted price due to Backtest(commission=...). See https://github.com/kernc/backtesting.py/issues/153, https://github.com/kernc/backtesting.py/issues/538, https://github.com/kernc/backtesting.py/issues/633.

waemm commented 1 year ago

thank you! Much appreciated for creating this fantastic tool!