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.55k stars 1.07k forks source link

Didn't get forced liquidation when the "Low Price" is very low. #939

Open peilinnnnnn opened 1 year ago

peilinnnnnn commented 1 year ago
image

As depicted in the picture, the first trade generated a profit, but during the trade, a bar had a significantly low "Low Price", indicating that in reality, the first trade would have been subject to forced liquidation due to my margin setting of 0.01. Is there a way to fix this issue now?

kernc commented 1 year ago

Indeed, equity is computed only once per bar, taking into account only the last_price (i.e. close) for trade P/L computation. Nice catch!

https://github.com/kernc/backtesting.py/blob/0ce24d80b1bcb8120d95d31dc3bb351b1052a27d/backtesting/backtesting.py#L802-L813 https://github.com/kernc/backtesting.py/blob/0ce24d80b1bcb8120d95d31dc3bb351b1052a27d/backtesting/backtesting.py#L788-L790