CyberPunkMetalHead / Binance-volatility-trading-bot

This is a fully functioning Binance trading bot that measures the volatility of every coin on Binance and places trades with the highest gaining coins If you like this project consider donating though the Brave browser to allow me to continuously improve the script.
MIT License
3.39k stars 774 forks source link

the Trailing Stop Loss and Trailing Take Profit calculations do not make sense to me #233

Open d4fuQQ opened 1 year ago

d4fuQQ commented 1 year ago

Hi, I'm wondering about the calculation of the readjusted TP and SL values based on the settings coming from the config file.

These are the default values as given in the config:

  # define in % when to sell a coin that's not making a profit.
  STOP_LOSS: 5

  # define in % when to take profit on a profitable coin.
  TAKE_PROFIT: .8

  # when hit TAKE_PROFIT, move STOP_LOSS to TRAILING_STOP_LOSS percentage points below TAKE_PROFIT hence locking in profit
  # when hit TAKE_PROFIT, move TAKE_PROFIT up by TRAILING_TAKE_PROFIT percentage points
  TRAILING_STOP_LOSS: .4
  TRAILING_TAKE_PROFIT: .1

Then the STOP_LOSS is set to -5 and the TAKE_PROFIT value is stored: https://github.com/CyberPunkMetalHead/Binance-volatility-trading-bot/blob/533daf54c0c47390d2d51daa67fec8127da4a829/Binance%20Detect%20Moonings.py#L437-L438


Now, let's assume a simple scenario: A position was opened at a price of coins_bought[coin]['bought_at'] = 50. Based on the following calculations this means TP = 50.4 and SL = 47.5. https://github.com/CyberPunkMetalHead/Binance-volatility-trading-bot/blob/533daf54c0c47390d2d51daa67fec8127da4a829/Binance%20Detect%20Moonings.py#L364-L366 This is not a lot of profit, but okay, perhaps TAKE_PROFIT should just be a higher value.

But that's not the actual issue here.

The next step is to readjust the TAKE_PROFIT and STOP_LOSS values if LastPrice > TP and USE_TRAILING_STOP_LOSS:. So, let's say the last price is LastPrice = 70. This would be a 40% price increase and is calculated correctly in line 371: PriceChange = float((LastPrice - BuyPrice) / BuyPrice * 100), i.e. PriceChange = 40.

https://github.com/CyberPunkMetalHead/Binance-volatility-trading-bot/blob/533daf54c0c47390d2d51daa67fec8127da4a829/Binance%20Detect%20Moonings.py#L376-L378

And now the calculation becomes somewhat obscure to me. Because, looking at the example values, the new TAKE_PROFIT price would then be calculated as coins_bought[coin]['take_profit'] = 40.1 and the new STOP_LOSS coins_bought[coin]['stop_loss'] = 39.7, respectively.

Both are now below the actual entry price. So what am I missing here?

If you ask me, I'd say the following calculations would make a lot more sense, resulting in coins_bought[coin]['take_profit'] = 55.44 and coins_bought[coin]['stop_loss'] = 66.5, respectively:

 coins_bought[coin]['take_profit'] = TP + (TP * TRAILING_TAKE_PROFIT)
 coins_bought[coin]['stop_loss'] = SL + (SL * TRAILING_TAKE_PROFIT)

Of course, then the values in the config should be adjusted accordingly (TRAILING_TAKE_PROFIT > TRAILING_STOP_LOSS), so the new stop loss level is lower than the new take profit level. For TRAILING_TAKE_PROFIT = 0.4 and TRAILING_STOP_LOSS = 0.1, the results would be coins_bought[coin]['take_profit'] = 70.56 and coins_bought[coin]['stop_loss'] = 52.25, for example.

watchman94 commented 1 year ago

coins_bought[coin]['stop_loss'] = 39.7 is define in %. so if enter price is 50, the new stop loss price is: 50 * (1 + 0.397)

d4fuQQ commented 1 year ago

coins_bought[coin]['stop_loss'] = 39.7 is define in %.

no, I don't agree. The only value defined directly in % is actually PriceChange, i.e. PriceChange = 40.

so if enter price is 50, the new stop loss price is: 50 * (1 + 0.397)

where can I find this calculation in the code? I don't see it.

attached is an Excel sheet following the script step by step with the example values: Binance-volatility-trading-bot_TSL_calc.xlsx

celltronic01 commented 9 months ago

Hey OP,

coins_bought[coin]['take_profit'] = TP + (TP TRAILING_TAKE_PROFIT) coins_bought[coin]['stop_loss'] = SL + (SL TRAILING_TAKE_PROFIT)

Did this solution work for you? Please Advise