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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

d4fuQQ opened this issue · comments

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:

'stop_loss': -STOP_LOSS,
'take_profit': TAKE_PROFIT,


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.

# define stop loss and take profit
TP = float(coins_bought[coin]['bought_at']) + (float(coins_bought[coin]['bought_at']) * coins_bought[coin]['take_profit']) / 100
SL = float(coins_bought[coin]['bought_at']) + (float(coins_bought[coin]['bought_at']) * coins_bought[coin]['stop_loss']) / 100

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.

# increasing TP by TRAILING_TAKE_PROFIT (essentially next time to readjust SL)
coins_bought[coin]['take_profit'] = PriceChange + TRAILING_TAKE_PROFIT
coins_bought[coin]['stop_loss'] = coins_bought[coin]['take_profit'] - TRAILING_STOP_LOSS

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.

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)

commented

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