conor19w / Binance-Futures-Trading-Bot

A Technical Analysis Bot that trades leveraged USDT futures markets on Binance.
513 stars 173 forks source link

Incorrect trailing stop calculation for long positions #21

Closed luki009 closed 2 years ago

luki009 commented 2 years ago

Trailing stop is calculating long position profits as short position and therefore calculation on the end is wrong . Please see marked issue and suggested correction

https://github.com/conor19w/Binance-Futures-Trading-Bot/blob/3ff7f4ea86ac564a2b613c718677d34707355949/Backtester.py#L568

Suggestion

cashout.append({'x': i, 'y': trailing_stop_value, 'type': "win", 'position': 'long', 'Profit': positionSize * (trailing_stop_value - positionPrice)})

conor19w commented 2 years ago

I've removed the typo, still need to fix trailing stop in the back tester seems to result in inflated profits the logic is messed up somehow.

luki009 commented 2 years ago

I've got idea why profits are inflated or maybe even deflated. I see that in backtester is calculation if SL, TP, TSL is triggered based on candle close. This might be issue because if you look on candles there are swings up and down. In real time data those swings could trigger either SL or TP/TSL but in backtest will not because are not catched. Considering 1 minute candle as you do in backtest was great idea and it make good precision for calculation. What can make it even more precise is to consider also 1 minute candle High and Low values for decision if SL, TP, TSL was triggered or not .. Let see example:

takeprofitvalue = 0.2 entry_price = 4.3 take_profit_price = 4.3 +0.2 = 4.5 backtest 1 minute candle close = 4.49 - in backtest this will not be considered / calculated as TP triggered. real situation: backtest 1 minute candle high = 4.51 - which should trigger TP but in backtest will not

This can be solved with: LONG POSITION TP condition: High >=TP instead of Close >= TP or SL condition: Low <= SL instead of Close <= SL Same goes for opposite way.

Issue here is how to decide which swing was first .. either Low or High but this is irrelevant in case that you are not doing backtest for 1m interval (really close SL and TP to each other). If interval is set to 5m and higher trigger point can be calculated just based on high values if candle is Bullish or Low value if candle is Bearish In case you would want to solve this also for 1m interval. I would do estimated guess what was first : if candle is Bullish then first swing was Low then High and then Close if candle is Bearish then first swing was high then Low and then Close there is not other option to estimate it without tick in time data.

Correct me if i'm wrong or wrongly understood logic for calculation.... I did not found any other possible error in calculations logic.

conor19w commented 2 years ago

Yeah your logic is sound I had a similar thought recently the highs and lows would be a better price value to consider. Yeah I can't find any errors either, but if you run any of the strategys with the trailing stop it just explodes into the millions currently I'm fairly sure, heres a screenshot I just ran haha no way these results are correct so must be a rounding error. image

It does hint at a bug though, opens a position on doge and doesnt close it but then opens a position on rose which shouldn't happen in the backtester... so maybe we can find the bug. Trailing stop is huge on doge and python is prone to rounding errors, if this is the issue in order to solve I may need to use a decimal library to represent the numbers.