Maurizio-Ciullo

Bot Scalping-Exponential-Bot ETH/PERP Ver-5 FTX 5M LONG E SHORT

May 10th, 2022 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2.  
  3. //Il trading system completo - Scalping Exponential Bot Ver-5 (Strategia Breakout) - parte 1
  4. // (Exchange= FTX) (Sottostante ETH-PERP) (Timeframe= 5M) (Direzione= LONG E SHORT) (Swing Posizione= NO) (Esclusione Ore=NO) (Esclusione Giorni=NO') (Esclusione Mesi=NO)
  5. // (Take Profit Long/Short Market = NO) (Take Profit Limit Long/Short= 5% Tradingview-Hub)
  6. // (Stop Loss Limit Long= -1% Tradingview-Hub) (Stop Loss Limit Short= -2% Tradingview-Hub) (Stop Loss Market Long/Short= NO) (Trailing Stop=NO) (Stop Emergenza= NO)
  7. // (Rischio Operazione 2% Perdita Media)
  8. // (In Sample Dal=01/5/21 Al 01/10/21) (Out Of Sample Dal=01/10/21 Al 28/04/22)
  9. // (Progettatta Il=06/05/2022)
  10.  
  11.  
  12. //@version=5
  13. //1. Strategia
  14. strategy(title='Bot Scalping-Exponential-Bot ETH/PERP Ver-5 FTX 5M LONG E SHORT', overlay=true, precision = 2,
  15.      max_bars_back=5000, // Serve Per Caricare Più Storico Per Il Trailing Stop
  16.      pyramiding=0,
  17.      initial_capital=150,
  18.      commission_type=strategy.commission.percent,
  19.      commission_value=0.1,
  20.      slippage=3,
  21.      default_qty_type=strategy.percent_of_equity,
  22.      default_qty_value=46)
  23.      
  24.      
  25. // Input
  26. input_target_long = input.float(title="input_take_profit_long", defval=5.0, minval=0.1, maxval=1000, group='Target')
  27. input_stop_loss_long = input.float(title="input_stop_loss_long", defval=1.0, minval=0.1, maxval=1000, group='Stop Loss')
  28. input_target_short = input.float(title="input_take_profit_short", defval=5.0, minval=0.1, maxval=1000, group='Target')
  29. input_stop_loss_short = input.float(title="input_stop_loss_short", defval=2.0, minval=0.1, maxval=1000, group='Stop Loss')
  30. lunghezza_adx = input.int(title="lunghezza_adx", defval=9, minval=0, maxval=1000, group='Differenziale Dmi')
  31. differenziale_adx_long_basso = input.float(title="differenziale_adx_basso_long", defval=5.0, minval=0.1, maxval=1000, group='Differenziale Adx', tooltip="Opera In Base Alla Tendenza")
  32. differenziale_adx_long_alto = input.float(title="differenziale_adx_alto_long", defval=25.0, minval=0.1, maxval=1000, group='Differenziale Adx', tooltip="Opera In Base Alla Tendenza")
  33. differenziale_adx_short_basso = input.float(title="differenziale_adx_basso_short", defval=30.0, minval=0.1, maxval=1000, group='Differenziale Adx', tooltip="Opera In Base Alla Tendenza")
  34. differenziale_adx_short_alto = input.float(title="differenziale_adx_alto_short", defval=60.0, minval=0.1, maxval=1000, group='Differenziale Adx', tooltip="Opera In Base Alla Tendenza")
  35. lunghezza_rsi = input.int(title="lunghezza_rsi", defval=9, minval=1, maxval=1000, group='Differenziale Rsi', tooltip="Opera In Base Ai Range Rsi")
  36. differenziale_rsi_long = input.float(title="differenziale_rsi_long", defval=65.0, minval=0.1, maxval=1000, group='Differenziale Rsi', tooltip="Opera In Base Ai Range Rsi")
  37. differenziale_rsi_short = input.float(title="differenziale_rsi_short", defval=35.0, minval=0.1, maxval=1000, group='Differenziale Rsi', tooltip="Opera In Base Ai Range Rsi")
  38. lunghezza_media_long = input.int(title="media_ema_long", defval=140, minval=1, maxval=1000, group='Medie')
  39. lunghezza_media_short = input.int(title="media_ema_short", defval=100, minval=1, maxval=1000, group='Medie')
  40. lunghezza_media_tendenza_veloce = input.int(title="media_ema_tendenza_veloce", defval=60, minval=1, maxval=1000, group='Medie', tooltip="Rende Più O Meno Dinamiche Le Medie")
  41. lunghezza_media_tendenza_lenta  = input.int(title="media_ema_tendenza_lenta", defval=180, minval=1, maxval=1000, group='Medie', tooltip="Rende Più O Meno Dinamiche Le Medie")
  42. input_trailing_stop_trigger_long1 = input.int(title="trigger_long 1", defval=45, minval=1, maxval=1000, group='2 Trailing Stop')
  43. input_trailing_stop_close_long1 = input.int(title="close_long 1", defval=40, minval=1, maxval=1000, group='2 Trailing Stop')
  44. input_trailing_stop_trigger_long2 = input.int(title="trigger_long 2", defval=80, minval=1, maxval=1000, group='2 Trailing Stop')
  45. input_trailing_stop_close_long2 = input.int(title="close_long 2", defval=60, minval=1, maxval=1000, group='2 Trailing Stop')
  46. input_trailing_stop_trigger_short1 = input.int(title="trigger_short 1", defval=50, minval=1, maxval=1000, group='2 Trailing Stop')
  47. input_trailing_stop_close_short1 = input.int(title="close_short 1", defval=20, minval=1, maxval=1000, group='2 Trailing Stop')
  48. input_trailing_stop_trigger_short2 = input.int(title="trigger_short 2", defval=80, minval=1, maxval=1000, group='2 Trailing Stop')
  49. input_trailing_stop_close_short2 = input.int(title="close_short 2", defval=55, minval=1, maxval=1000, group='2 Trailing Stop')
  50. perc_apertura_minima_long = input.float(title="perc_apertura_minima_long", defval=0.09, minval=0.01, maxval=1000, group='Apertura Minima Close Media')
  51. perc_apertura_minima_short = input.float(title="perc_apertura_minima_short", defval=0.08, minval=0.01, maxval=1000, group='Apertura Minima Close Media')
  52. in_solo_long = input.bool(title='Solo long', defval=false, inline='1', group='Direzione')
  53. in_solo_short = input.bool(title='Solo short', defval=false, inline='1', group='Direzione')
  54.  
  55.  
  56. // Calcolo del range del backtest
  57. startDate = input.int(title="Start Date",
  58.      defval=1, minval=1, maxval=31, group="Periodo")
  59. startMonth = input.int(title="Start Month",
  60.      defval=02, minval=1, maxval=12, group="Periodo")
  61. startYear = input.int(title="Start Year",
  62.      defval=2021, minval=1800, maxval=2150, group="Periodo")
  63.  
  64. endDate = input.int(title="End Date",
  65.      defval=01, minval=1, maxval=31, group="Periodo")
  66. endMonth = input.int(title="End Month",
  67.      defval=01, minval=1, maxval=12, group="Periodo")
  68. endYear = input.int(title="End Year",
  69.      defval=2121, minval=1800, maxval=2150, group="Periodo")
  70.  
  71.  
  72. inDateRange = (time >= timestamp(syminfo.timezone, startYear,
  73.          startMonth, startDate, 0, 0)) and
  74.      (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
  75.  
  76.  
  77. // Start Hour Range Trading Non Attivo /////////////////////
  78. //hourTrading = input(title='sessione valida di trading', defval='0600-2300:23456')
  79. //hourRangeTrading = time(timeframe.period, hourTrading)
  80.  
  81.  
  82. //Calcolo degli indicatori
  83.  
  84. // Medie Mobili
  85. media_long = ta.ema(close, lunghezza_media_long)
  86. media_short = ta.ema(close, lunghezza_media_short)
  87. plot(media_long, color=color.new(color.blue, 0), title='Ema Long', linewidth=2)
  88. plot(media_short, color=color.new(color.red, 0), title='Ema Short', linewidth=2)
  89. media_tendenza_lenta = ta.ema(close, lunghezza_media_tendenza_lenta)
  90. media_tendenza_veloce = ta.ema(close, lunghezza_media_tendenza_veloce)
  91. plot(media_tendenza_lenta, color=color.new(color.purple, 0), title='media_tendenza_lenta', linewidth=2)
  92. plot(media_tendenza_veloce, color=color.new(color.aqua, 0), title='media_tendenza_veloce', linewidth=2)
  93.  
  94.  
  95. // Variabili Rsi Long e Short
  96. valore_rsi = ta.rsi(close, lunghezza_rsi)
  97. plot(valore_rsi, title = "valore_rsi", color=color.blue)
  98.  
  99.  
  100. // Variabili Adx
  101. [di_pos, di_neg, adx] = ta.dmi(lunghezza_adx, lunghezza_adx)
  102. valore_adx = adx
  103. plot(adx, title="adx_long")
  104.  
  105.  
  106. //Apertura minima
  107. apertura_minima_long   = (media_long / 100) * perc_apertura_minima_long
  108. apertura_minima_short   = (media_short / 100) * perc_apertura_minima_short
  109.  
  110.  
  111. // Money Menagment e stop loss
  112.  
  113. // size = strategy.equity * input_risk / 100
  114. // nr_contratti = size / close
  115. // stop_loss_long = math.round(size / syminfo.mintick / 100 * input_stop_loss_long) / nr_contratti
  116. // stop_loss_short = math.round(size / syminfo.mintick / 100 * input_stop_loss_short) / nr_contratti
  117. // target_long = math.round(size / syminfo.mintick / 100 * input_target_long) / nr_contratti
  118. // target_short = math.round(size / syminfo.mintick / 100 * input_target_short) / nr_contratti
  119.  
  120.  
  121. //Variabili Stop Long e Short
  122. stop_loss_long_price =math.round(strategy.opentrades.entry_price(0) - (strategy.opentrades.entry_price(0) * input_stop_loss_long) / 100)
  123. stop_loss_short_price = math.round(strategy.opentrades.entry_price(0) + (strategy.opentrades.entry_price(0) * input_stop_loss_short) / 100)
  124. take_profit_long_price = math.round(strategy.opentrades.entry_price(0) + (strategy.opentrades.entry_price(0) * input_target_long) / 100)
  125. take_profit_short_price = math.round(strategy.opentrades.entry_price(0) - (strategy.opentrades.entry_price(0) * input_target_short) / 100)
  126.  
  127.  
  128. // Calcolo Trailing Stop
  129. trailing_stop_trigger_long1 = strategy.opentrades.entry_price(0) + (strategy.opentrades.entry_price(0) * input_target_long /100) * (input_trailing_stop_trigger_long1/100)
  130. trailing_stop_close_long1 = strategy.opentrades.entry_price(0) + (strategy.opentrades.entry_price(0) * input_target_long /100) * (input_trailing_stop_close_long1/100)
  131. trailing_stop_trigger_short1 = strategy.opentrades.entry_price(0) - (strategy.opentrades.entry_price(0) * input_target_short /100) * (input_trailing_stop_trigger_short1/100)
  132. trailing_stop_close_short1 = strategy.opentrades.entry_price(0) - (strategy.opentrades.entry_price(0) * input_target_short /100) * (input_trailing_stop_close_short1/100)
  133. trailing_stop_trigger_long2 = strategy.opentrades.entry_price(0) + (strategy.opentrades.entry_price(0) * input_target_long /100) * (input_trailing_stop_trigger_long2/100)
  134. trailing_stop_close_long2 = strategy.opentrades.entry_price(0) + (strategy.opentrades.entry_price(0) * input_target_long /100) * (input_trailing_stop_close_long2/100)
  135. trailing_stop_trigger_short2 = strategy.opentrades.entry_price(0) - (strategy.opentrades.entry_price(0) * input_target_short /100) * (input_trailing_stop_trigger_short2/100)
  136. trailing_stop_close_short2 = strategy.opentrades.entry_price(0) - (strategy.opentrades.entry_price(0) * input_target_short /100) * (input_trailing_stop_close_short2/100)
  137.  
  138.  
  139. // Trigger Trailing Stop: Quando la posizione è aperta, contando indietro dall'ultima chiusura fino a max 49 candele indietro fino ad arrivare all'ingresso della posizione, se gli high sono >= dei trailing_stop_trigger e le close sono <= dei trailing_stop_close la condizione è valida
  140. // trailing_long  =  ( ((high >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close >= strategy.opentrades.entry_time(0)) or (high >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close >= strategy.opentrades.entry_time(0))) or ((high[1] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[1] > strategy.opentrades.entry_time(0)) or (high[1] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[1] > strategy.opentrades.entry_time(0))) or ((high[2] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[2] > strategy.opentrades.entry_time(0)) or (high[2] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[2] > strategy.opentrades.entry_time(0))) or ((high[3] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[3] > strategy.opentrades.entry_time(0)) or (high[3] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[3] > strategy.opentrades.entry_time(0))) or ((high[4] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[4] > strategy.opentrades.entry_time(0)) or (high[4] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[4] > strategy.opentrades.entry_time(0)))
  141. //       or  ((high[5] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[5] > strategy.opentrades.entry_time(0)) or (high[5] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[5] > strategy.opentrades.entry_time(0))) or ((high[6] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[6] > strategy.opentrades.entry_time(0)) or (high[6] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[6] > strategy.opentrades.entry_time(0))) or ((high[7] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[7] > strategy.opentrades.entry_time(0)) or (high[7] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[7] > strategy.opentrades.entry_time(0))) or ((high[8] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[8] > strategy.opentrades.entry_time(0)) or (high[8] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[8] > strategy.opentrades.entry_time(0))) or ((high[9] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[9] > strategy.opentrades.entry_time(0)) or (high[9] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[9] > strategy.opentrades.entry_time(0)))
  142. //       or  ((high[10] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[10] > strategy.opentrades.entry_time(0)) or (high[10] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[10] > strategy.opentrades.entry_time(0))) or ((high[11] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[11] > strategy.opentrades.entry_time(0)) or (high[11] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[11] > strategy.opentrades.entry_time(0))) or ((high[12] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[12] > strategy.opentrades.entry_time(0)) or (high[12] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[12] > strategy.opentrades.entry_time(0))) or ((high[13] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[13] > strategy.opentrades.entry_time(0)) or (high[13] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[13] > strategy.opentrades.entry_time(0))) or ((high[14] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[14] > strategy.opentrades.entry_time(0)) or (high[14] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[14] > strategy.opentrades.entry_time(0)))
  143. //       or  ((high[15] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[15] > strategy.opentrades.entry_time(0)) or (high[15] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[15] > strategy.opentrades.entry_time(0))) or ((high[16] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[16] > strategy.opentrades.entry_time(0)) or (high[16] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[16] > strategy.opentrades.entry_time(0))) or ((high[17] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[17] > strategy.opentrades.entry_time(0)) or (high[17] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[17] > strategy.opentrades.entry_time(0))) or ((high[18] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[18] > strategy.opentrades.entry_time(0)) or (high[18] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[18] > strategy.opentrades.entry_time(0))) or ((high[19] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[19] > strategy.opentrades.entry_time(0)) or (high[19] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[19] > strategy.opentrades.entry_time(0)))
  144. //       or  ((high[20] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[20] > strategy.opentrades.entry_time(0)) or (high[20] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[20] > strategy.opentrades.entry_time(0))) or ((high[21] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[21] > strategy.opentrades.entry_time(0)) or (high[21] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[21] > strategy.opentrades.entry_time(0))) or ((high[22] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[22] > strategy.opentrades.entry_time(0)) or (high[22] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[22] > strategy.opentrades.entry_time(0))) or ((high[23] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[23] > strategy.opentrades.entry_time(0)) or (high[23] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[23] > strategy.opentrades.entry_time(0))) or ((high[24] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[24] > strategy.opentrades.entry_time(0)) or (high[24] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[24] > strategy.opentrades.entry_time(0)))
  145. //       or  ((high[25] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[25] > strategy.opentrades.entry_time(0)) or (high[25] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[25] > strategy.opentrades.entry_time(0))) or ((high[26] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[26] > strategy.opentrades.entry_time(0)) or (high[26] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[26] > strategy.opentrades.entry_time(0))) or ((high[27] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[27] > strategy.opentrades.entry_time(0)) or (high[27] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[27] > strategy.opentrades.entry_time(0))) or ((high[28] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[28] > strategy.opentrades.entry_time(0)) or (high[28] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[28] > strategy.opentrades.entry_time(0))) or ((high[29] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[29] > strategy.opentrades.entry_time(0)) or (high[29] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[29] > strategy.opentrades.entry_time(0)))
  146. //       or  ((high[30] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[30] > strategy.opentrades.entry_time(0)) or (high[30] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[30] > strategy.opentrades.entry_time(0))) or ((high[31] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[31] > strategy.opentrades.entry_time(0)) or (high[31] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[31] > strategy.opentrades.entry_time(0))) or ((high[32] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[32] > strategy.opentrades.entry_time(0)) or (high[32] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[32] > strategy.opentrades.entry_time(0))) or ((high[33] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[33] > strategy.opentrades.entry_time(0)) or (high[33] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[33] > strategy.opentrades.entry_time(0))) or ((high[34] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[34] > strategy.opentrades.entry_time(0)) or (high[34] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[34] > strategy.opentrades.entry_time(0)))
  147. //       or  ((high[35] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[35] > strategy.opentrades.entry_time(0)) or (high[35] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[35] > strategy.opentrades.entry_time(0))) or ((high[36] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[36] > strategy.opentrades.entry_time(0)) or (high[36] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[36] > strategy.opentrades.entry_time(0))) or ((high[37] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[37] > strategy.opentrades.entry_time(0)) or (high[37] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[37] > strategy.opentrades.entry_time(0))) or ((high[38] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[38] > strategy.opentrades.entry_time(0)) or (high[38] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[38] > strategy.opentrades.entry_time(0))) or ((high[39] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[39] > strategy.opentrades.entry_time(0)) or (high[39] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[39] > strategy.opentrades.entry_time(0)))
  148. //       or  ((high[40] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[40] > strategy.opentrades.entry_time(0)) or (high[40] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[40] > strategy.opentrades.entry_time(0))) or ((high[41] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[41] > strategy.opentrades.entry_time(0)) or (high[41] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[41] > strategy.opentrades.entry_time(0))) or ((high[42] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[42] > strategy.opentrades.entry_time(0)) or (high[42] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[42] > strategy.opentrades.entry_time(0))) or ((high[43] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[43] > strategy.opentrades.entry_time(0)) or (high[43] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[43] > strategy.opentrades.entry_time(0))) or ((high[44] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[44] > strategy.opentrades.entry_time(0)) or (high[44] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[44] > strategy.opentrades.entry_time(0)))
  149. //       or  ((high[45] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[45] > strategy.opentrades.entry_time(0)) or (high[45] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[45] > strategy.opentrades.entry_time(0))) or ((high[46] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[46] > strategy.opentrades.entry_time(0)) or (high[46] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[46] > strategy.opentrades.entry_time(0))) or ((high[47] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[47] > strategy.opentrades.entry_time(0)) or (high[47] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[47] > strategy.opentrades.entry_time(0))) or ((high[48] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[48] > strategy.opentrades.entry_time(0)) or (high[48] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[48] > strategy.opentrades.entry_time(0))) or ((high[49] >= trailing_stop_trigger_long1 and close <= trailing_stop_close_long1 and time_close[49] > strategy.opentrades.entry_time(0)) or (high[49] >= trailing_stop_trigger_long2 and close <= trailing_stop_close_long2 and time_close[49] > strategy.opentrades.entry_time(0))))
  150.  
  151. // Trigger Trailing Stop: Quando la posizione è aperta, contando indietro dall'ultima chiusura fino a max 49 candele indietro fino ad arrivare all'ingresso della posizione, se i low sono <= dei trailing_stop_trigger e le close sono >= dei trailing_stop_close la condizione è valida
  152. // trailing_short  =  ( ((low <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close >= strategy.opentrades.entry_time(0)) or (low <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close >= strategy.opentrades.entry_time(0))) or ((low[1] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[1] > strategy.opentrades.entry_time(0)) or (low[1] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[1] > strategy.opentrades.entry_time(0))) or ((low[2] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[2] > strategy.opentrades.entry_time(0)) or (low[2] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[2] > strategy.opentrades.entry_time(0))) or ((low[3] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[3] > strategy.opentrades.entry_time(0)) or (low[3] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[3] > strategy.opentrades.entry_time(0))) or ((low[4] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[4] > strategy.opentrades.entry_time(0)) or (low[4] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[4] > strategy.opentrades.entry_time(0)))
  153. //       or ((low[5] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[5] > strategy.opentrades.entry_time(0)) or (low[5] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[5] > strategy.opentrades.entry_time(0))) or ((low[6] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[6] > strategy.opentrades.entry_time(0)) or (low[6] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[6] > strategy.opentrades.entry_time(0))) or ((low[7] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[7] > strategy.opentrades.entry_time(0)) or (low[7] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[7] > strategy.opentrades.entry_time(0))) or ((low[8] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[8] > strategy.opentrades.entry_time(0)) or (low[8] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[8] > strategy.opentrades.entry_time(0))) or ((low[9] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[9] > strategy.opentrades.entry_time(0)) or (low[9] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[9] > strategy.opentrades.entry_time(0)))
  154. //       or ((low[10] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[10] > strategy.opentrades.entry_time(0)) or (low[10] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[10] > strategy.opentrades.entry_time(0))) or ((low[11] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[11] > strategy.opentrades.entry_time(0)) or (low[11] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[11] > strategy.opentrades.entry_time(0))) or ((low[12] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[12] > strategy.opentrades.entry_time(0)) or (low[12] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[12] > strategy.opentrades.entry_time(0))) or ((low[13] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[13] > strategy.opentrades.entry_time(0)) or (low[13] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[13] > strategy.opentrades.entry_time(0))) or ((low[14] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[14] > strategy.opentrades.entry_time(0)) or (low[14] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[14] > strategy.opentrades.entry_time(0)))
  155. //       or ((low[15] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[15] > strategy.opentrades.entry_time(0)) or (low[15] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[15] > strategy.opentrades.entry_time(0))) or ((low[16] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[16] > strategy.opentrades.entry_time(0)) or (low[16] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[16] > strategy.opentrades.entry_time(0))) or ((low[17] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[17] > strategy.opentrades.entry_time(0)) or (low[17] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[17] > strategy.opentrades.entry_time(0))) or ((low[18] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[18] > strategy.opentrades.entry_time(0)) or (low[18] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[18] > strategy.opentrades.entry_time(0))) or ((low[19] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[19] > strategy.opentrades.entry_time(0)) or (low[19] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[19] > strategy.opentrades.entry_time(0)))
  156. //       or ((low[20] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[20] > strategy.opentrades.entry_time(0)) or (low[20] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[20] > strategy.opentrades.entry_time(0))) or ((low[21] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[21] > strategy.opentrades.entry_time(0)) or (low[21] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[21] > strategy.opentrades.entry_time(0))) or ((low[22] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[22] > strategy.opentrades.entry_time(0)) or (low[22] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[22] > strategy.opentrades.entry_time(0))) or ((low[23] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[23] > strategy.opentrades.entry_time(0)) or (low[23] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[23] > strategy.opentrades.entry_time(0))) or ((low[24] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[24] > strategy.opentrades.entry_time(0)) or (low[24] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[24] > strategy.opentrades.entry_time(0)))
  157. //       or ((low[25] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[25] > strategy.opentrades.entry_time(0)) or (low[25] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[25] > strategy.opentrades.entry_time(0))) or ((low[26] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[26] > strategy.opentrades.entry_time(0)) or (low[26] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[26] > strategy.opentrades.entry_time(0))) or ((low[27] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[27] > strategy.opentrades.entry_time(0)) or (low[27] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[27] > strategy.opentrades.entry_time(0))) or ((low[28] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[28] > strategy.opentrades.entry_time(0)) or (low[28] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[28] > strategy.opentrades.entry_time(0))) or ((low[29] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[29] > strategy.opentrades.entry_time(0)) or (low[29] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[29] > strategy.opentrades.entry_time(0)))
  158. //       or ((low[30] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[30] > strategy.opentrades.entry_time(0)) or (low[30] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[30] > strategy.opentrades.entry_time(0))) or ((low[31] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[31] > strategy.opentrades.entry_time(0)) or (low[31] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[31] > strategy.opentrades.entry_time(0))) or ((low[32] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[32] > strategy.opentrades.entry_time(0)) or (low[32] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[32] > strategy.opentrades.entry_time(0))) or ((low[33] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[33] > strategy.opentrades.entry_time(0)) or (low[33] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[33] > strategy.opentrades.entry_time(0))) or ((low[34] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[34] > strategy.opentrades.entry_time(0)) or (low[34] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[34] > strategy.opentrades.entry_time(0)))
  159. //       or ((low[35] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[35] > strategy.opentrades.entry_time(0)) or (low[35] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[35] > strategy.opentrades.entry_time(0))) or ((low[36] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[36] > strategy.opentrades.entry_time(0)) or (low[36] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[36] > strategy.opentrades.entry_time(0))) or ((low[37] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[37] > strategy.opentrades.entry_time(0)) or (low[37] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[37] > strategy.opentrades.entry_time(0))) or ((low[38] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[38] > strategy.opentrades.entry_time(0)) or (low[38] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[38] > strategy.opentrades.entry_time(0))) or ((low[39] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[39] > strategy.opentrades.entry_time(0)) or (low[39] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[39] > strategy.opentrades.entry_time(0)))
  160. //       or ((low[40] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[40] > strategy.opentrades.entry_time(0)) or (low[40] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[40] > strategy.opentrades.entry_time(0))) or ((low[41] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[41] > strategy.opentrades.entry_time(0)) or (low[41] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[41] > strategy.opentrades.entry_time(0))) or ((low[42] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[42] > strategy.opentrades.entry_time(0)) or (low[42] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[42] > strategy.opentrades.entry_time(0))) or ((low[43] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[43] > strategy.opentrades.entry_time(0)) or (low[43] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[43] > strategy.opentrades.entry_time(0))) or ((low[44] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[44] > strategy.opentrades.entry_time(0)) or (low[44] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[44] > strategy.opentrades.entry_time(0)))
  161. //       or ((low[45] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[45] > strategy.opentrades.entry_time(0)) or (low[45] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[45] > strategy.opentrades.entry_time(0))) or ((low[46] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[46] > strategy.opentrades.entry_time(0)) or (low[46] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[46] > strategy.opentrades.entry_time(0))) or ((low[47] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[47] > strategy.opentrades.entry_time(0)) or (low[47] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[47] > strategy.opentrades.entry_time(0))) or ((low[48] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[48] > strategy.opentrades.entry_time(0)) or (low[48] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[48] > strategy.opentrades.entry_time(0))) or ((low[49] <= trailing_stop_trigger_short1 and close >= trailing_stop_close_short1 and time_close[49] > strategy.opentrades.entry_time(0)) or (low[49] <= trailing_stop_trigger_short2 and close >= trailing_stop_close_short2 and time_close[49] > strategy.opentrades.entry_time(0))))
  162.  
  163. plot(strategy.position_size != 0 ? strategy.opentrades.entry_price(0) : na , color=strategy.position_size > 0 ? color.blue : strategy.position_size < 0 ? color.red : na, style=plot.style_linebr, title="entry_price") // stampa l'entry price in rosso se short in blu se long
  164. plot(strategy.position_size > 0 ?  take_profit_long_price : strategy.position_size < 0 ? take_profit_short_price: na, color=color.green, style=plot.style_cross, linewidth=2, title="tk_limit")
  165. plot(strategy.position_size > 0 ?  stop_loss_long_price : strategy.position_size < 0 ? stop_loss_short_price: na, color=color.red, style=plot.style_cross, linewidth=2, title="sl_limit")
  166. plot(strategy.position_size > 0 ?  trailing_stop_trigger_long1 : strategy.position_size < 0 ? trailing_stop_trigger_short1: na, color=strategy.position_size > 0 ? color.blue : strategy.position_size < 0 ? color.red : na, style=plot.style_cross, linewidth=2, transp=0, title="trigger1")
  167. plot(strategy.position_size > 0 ?  trailing_stop_close_long1 : strategy.position_size < 0 ? trailing_stop_close_short1: na, color=strategy.position_size > 0 ? color.blue : strategy.position_size < 0 ? color.red : na, style=plot.style_cross, linewidth=2,transp=0, title="trailing 1")
  168. plot(strategy.position_size > 0 ?  trailing_stop_trigger_long2 : strategy.position_size < 0 ? trailing_stop_trigger_short2: na, color=strategy.position_size > 0 ? color.blue : strategy.position_size < 0 ? color.red : na, style=plot.style_cross, linewidth=2, transp=50, title="trigger2")
  169. plot(strategy.position_size > 0 ?  trailing_stop_close_long2 : strategy.position_size < 0 ? trailing_stop_close_short2: na, color=strategy.position_size > 0 ? color.blue : strategy.position_size < 0 ? color.red : na, style=plot.style_cross, linewidth=2, transp=50, title="trailing2")
  170.  
  171. bgcolor(strategy.position_size > 0 ? color.green : strategy.position_size < 0 ? color.red : na, transp=90) // sfondo verde quando siamo long, sfondo rosso quando siamo short, no sfondo quando non siamo in posizione
  172.  
  173.  
  174. // Calcolo Uscita Trailing Stop Long E Short
  175. // Bar_index = bar_index
  176. // plot(Bar_index, title = "Bar_index")
  177.  
  178. // entry_bar_index = strategy.opentrades.entry_bar_index(0) +1
  179. // plot(entry_bar_index, title = "entry_bar_index")
  180.  
  181. range_barre_trailing = bar_index - strategy.opentrades.entry_bar_index(0) +1
  182. //plot(lunghezza_trailing, title = 'lunghezza')
  183.  
  184. highesthigh = strategy.opentrades ==1 ? ta.highest(high, range_barre_trailing): na
  185. //plot(highesthigh)
  186.  
  187. lowestlow = strategy.opentrades ==1 ? ta.lowest(low, range_barre_trailing): na
  188. //plot(lowestlow)
  189.  
  190.  
  191. // Plot No Trading Allowed giorni da 1 a 7 1 è domenica. Mesi da 1 a 12 1 è Gennaio.
  192.  
  193. //giorni_esclusi = dayofweek(time)
  194. //plotshape(giorni_esclusi[1] == 2 ? giorni_esclusi : na, color=color.green, title="giorni_esclusi")
  195. //mesi_esclusi = month(time)
  196. //plotshape(mesi_esclusi[1] == 9 ? mesi_esclusi : na, color=color.yellow, title="mesi_esclusi")
  197.  
  198.  
  199. //Condizione Entrata Long
  200. condEntryLong = ta.crossover(close, media_long) and close > (media_long + apertura_minima_long) and media_tendenza_veloce > media_tendenza_lenta and valore_adx > differenziale_adx_long_basso and valore_adx < differenziale_adx_long_alto  and valore_rsi < differenziale_rsi_long and not in_solo_short
  201.  
  202. // Condizione Uscita Trailing Stop Long
  203. trailing_long =  (highesthigh > trailing_stop_trigger_long1 and close <= trailing_stop_close_long1) or (highesthigh > trailing_stop_trigger_long2 and close <= trailing_stop_close_long2)
  204.  
  205. //Condizione Entrata Short
  206. condEntryShort = ta.crossunder(close, media_short) and close < (media_short - apertura_minima_short)  and media_tendenza_lenta > media_tendenza_veloce and valore_adx > differenziale_adx_short_basso and valore_adx < differenziale_adx_short_alto  and valore_rsi > differenziale_rsi_short and not in_solo_long
  207.  
  208. // Condizione Uscita Trailing Stop Short
  209. trailing_short = (lowestlow < trailing_stop_trigger_short1 and close >= trailing_stop_close_short1) or (lowestlow < trailing_stop_trigger_short2 and close >= trailing_stop_close_short2)
  210.  
  211. barcolor(condEntryLong ? color.lime : condEntryShort ? color.purple : na)
  212.  
  213.  
  214. // Allerts
  215. buy_command = '{"BUY COMMAND"}'
  216. sell_command = '{"SELL COMMAND"}'
  217. close_command = '{"CLOSE COMMAND"}'
  218.  
  219.  
  220. //entrata e uscita Long (stop/limit x prezzo) (loss/profit x ticks)
  221. if condEntryLong and strategy.opentrades == 0
  222.     strategy.entry('operazioneLong', strategy.long, alert_message = "Open Long Position", comment = buy_command)
  223.    
  224. if strategy.opentrades ==1
  225.     strategy.exit('SL e TP', from_entry='operazioneLong', stop=stop_loss_long_price, limit=take_profit_long_price, alert_message = "Your Long SL-TP Has Been Triggered.", comment = close_command)
  226.  
  227. if trailing_long and strategy.opentrades == 1
  228.     strategy.close(id='operazioneLong', alert_message = "Close Long Position", comment = close_command)
  229.  
  230. //entrata e uscita Short    
  231. if condEntryShort and strategy.opentrades == 0
  232.     strategy.entry('operazioneShort', strategy.short, alert_message = "Open Short position", comment = sell_command)
  233.  
  234. if strategy.opentrades ==1
  235.     strategy.exit('SL e TP', from_entry='operazioneShort', stop=stop_loss_short_price, limit=take_profit_short_price, alert_message = "Your Short SL-TP Has Been Triggered.", comment = close_command)
  236.  
  237. if trailing_short and strategy.opentrades == 1
  238.     strategy.close(id='operazioneShort', alert_message = "Close Short Position", comment = close_command)
  239.  
  240. // Nome Alert: Scalping-Exponential-Bot ETH/PERP Ver-5 FTX 5M
  241. // Commento Alert: {{strategy.order.comment}}
  242.  
  243.  
  244. // ----------------- Inizio Tabella risultati mensili. Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick -----------------
  245.  
  246. // new_month = month(time) != month(time[1])
  247. // new_year  = year(time)  != year(time[1])
  248.  
  249. // eq = strategy.equity
  250.  
  251. // bar_pnl = eq / eq[1] - 1
  252.  
  253. // cur_month_pnl = 0.0
  254. // cur_year_pnl  = 0.0
  255.  
  256. // // Current Monthly P&L
  257. // cur_month_pnl := new_month ? 0.0 :
  258. //                  (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
  259.  
  260. // // Current Yearly P&L
  261. // cur_year_pnl := new_year ? 0.0 :
  262. //                  (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1  
  263.  
  264. // // Arrays to store Yearly and Monthly P&Ls
  265. // var month_pnl  = array.new_float(0)
  266. // var month_time = array.new_int(0)
  267.  
  268. // var year_pnl  = array.new_float(0)
  269. // var year_time = array.new_int(0)
  270.  
  271. // last_computed = false
  272.  
  273. // if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
  274. //     if (last_computed[1])
  275. //         array.pop(month_pnl)
  276. //         array.pop(month_time)
  277.  
  278. //     array.push(month_pnl , cur_month_pnl[1])
  279. //     array.push(month_time, time[1])
  280.  
  281. // if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
  282. //     if (last_computed[1])
  283. //         array.pop(year_pnl)
  284. //         array.pop(year_time)
  285.  
  286. //     array.push(year_pnl , cur_year_pnl[1])
  287. //     array.push(year_time, time[1])
  288.  
  289. // last_computed := barstate.islast ? true : nz(last_computed[1])
  290.  
  291. // // Monthly P&L Table    
  292. // var monthly_table = table(na)
  293. // prec      = input(2, title = "Return Precision")
  294.  
  295. // if (barstate.islast)
  296. //     monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_width=1,border_color=#000000)
  297.  
  298. //     table.cell(monthly_table, 0,  0, "",     text_color=#D3D3D3, bgcolor=#0F0F0F)
  299. //     table.cell(monthly_table, 1,  0, "Jan",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  300. //     table.cell(monthly_table, 2,  0, "Feb",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  301. //     table.cell(monthly_table, 3,  0, "Mar",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  302. //     table.cell(monthly_table, 4,  0, "Apr",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  303. //     table.cell(monthly_table, 5,  0, "May",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  304. //     table.cell(monthly_table, 6,  0, "Jun",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  305. //     table.cell(monthly_table, 7,  0, "Jul",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  306. //     table.cell(monthly_table, 8,  0, "Aug",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  307. //     table.cell(monthly_table, 9,  0, "Sep",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  308. //     table.cell(monthly_table, 10, 0, "Oct",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  309. //     table.cell(monthly_table, 11, 0, "Nov",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  310. //     table.cell(monthly_table, 12, 0, "Dec",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  311. //     table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
  312.  
  313.  
  314. //     for yi = 0 to array.size(year_pnl) - 1
  315. //         table.cell(monthly_table, 0,  yi + 1, str.tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
  316.  
  317. //         y_color = array.get(year_pnl, yi) > 0 ? color.lime : color.red
  318. //         table.cell(monthly_table, 13, yi + 1, str.tostring(math.round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
  319.  
  320. //     for mi = 0 to array.size(month_time) - 1
  321. //         m_row   = year(array.get(month_time, mi))  - year(array.get(year_time, 0)) + 1
  322. //         m_col   = month(array.get(month_time, mi))
  323. //         m_color = array.get(month_pnl, mi) > 0 ? color.lime : color.red
  324.  
  325. //         table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
  326.  
  327. // ----------------- Fine Tabella risultati mensili. Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick -----------------
Add Comment
Please, Sign In to add comment