Maurizio-Ciullo

Bot Break-in-New-Trailing-Maurizio Ver-5 ETH/PERP FTX 4H LONG E SHORT

Mar 17th, 2022 (edited)
196
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 - Bot Break-in-New-Trailing-Maurizio Ver-5 (Strategia Breakout) Parte 1
  4. // (Exchange= FTX) (Sottostante ETH-PERP) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= SI) (Esclusione Ore=NO) (Esclusione Giorni=NO') (Esclusione Mesi=NO)
  5. // (Take Profit Long/Short Market = Trailing) (Take Profit Limit Long/Short= +16%) (Stop Loss Limit Long/Short= -13%) (Stop Loss Market Long/Short= NO) (Trailing Stop=SI) (Stop Emergenza= NO)
  6. // (Rischio Operazione 2% Perdita Media)
  7. // (In Sample Dal=17/08/2017 Al 17/10/2020) (Out Of Sample Dal=18/10/2020 Al 15/03/2021)
  8.  
  9.  
  10. //@version=5
  11. //1. Strategia
  12. strategy(title='Bot Break-in-New-Trailing-Maurizio Ver-5 ETH/PERP FTX 4H LONG E SHORT', overlay=true,
  13.      max_bars_back=5000, // Serve Per Caricare Più Storico Per Il Trailing Stop
  14.      pyramiding=0,
  15.      initial_capital=150,
  16.      commission_type=strategy.commission.percent,
  17.      commission_value=0.1,
  18.      slippage=3,
  19.      default_qty_type=strategy.percent_of_equity,
  20.      default_qty_value=8.8)
  21.  
  22.  
  23. // Input
  24. input_stop_loss_long = input.float(title='stop_loss_long', defval=13, minval=0, maxval=100, step=0.1, group='Stop Loss')
  25. input_take_profit_long = input.float(title='take_profit_long', defval=16, minval=0, maxval=100, step=0.1, group='Take Profit')
  26. input_stop_loss_short = input.float(title='stop_loss_short', defval=13, minval=0, maxval=100, step=0.1, group='Stop Loss')
  27. input_take_profit_short = input.float(title='take_profit_short', defval=16, minval=0, maxval=100, step=0.1, group='Take Profit')
  28. input_trailing_trigger_long = input.float(title='trailing_trigger_long', defval=75, minval=0, maxval=100, step=1, group='Traling Stop')
  29. input_trailing_close_long = input.float(title='trailing_close_long', defval=50, minval=0, maxval=100, step=1, group='Traling Stop')
  30. input_trailing_trigger_short = input.float(title='trailing_trigger_short', defval=75, minval=0, maxval=100, step=1, group='Traling Stop')
  31. input_trailing_close_short = input.float(title='trailing_close_short', defval=50, minval=0, maxval=100, step=1, group='Traling Stop')
  32. input_lunghezza_ema_long = input.int(title='lunghezza_ema_long', defval=240, minval=0, maxval=300, step=5, group='Medie')
  33. input_lunghezza_ema_short = input.int(title='lunghezza_ema_short', defval=240, minval=0, maxval=300, step=5, group='Medie')
  34. in_solo_long = input.bool(title='Solo long', defval=false, inline='1', group='Direzione')
  35. in_solo_short = input.bool(title='Solo short', defval=false, inline='1', group='Direzione')
  36. riskPerTrade = input.float(title='Risk Per Trade %', minval=0.1, maxval=100, defval=8.8, step=0.5)
  37.  
  38.  
  39. // Calcolo del range del backtest
  40. startDate = input.int(title="Start Date",
  41.      defval=17, minval=1, maxval=31, group="Periodo")
  42. startMonth = input.int(title="Start Month",
  43.      defval=08, minval=1, maxval=12, group="Periodo")
  44. startYear = input.int(title="Start Year",
  45.      defval=2016, minval=1800, maxval=2100, group="Periodo")
  46.  
  47. endDate = input.int(title="End Date",
  48.      defval=01, minval=1, maxval=31, group="Periodo")
  49. endMonth = input.int(title="End Month",
  50.      defval=01, minval=1, maxval=12, group="Periodo")
  51. endYear = input.int(title="End Year",
  52.      defval=2121, minval=1800, maxval=2150, group="Periodo")
  53.  
  54.  
  55. inDateRange = (time >= timestamp(syminfo.timezone, startYear,
  56.          startMonth, startDate, 0, 0)) and
  57.      (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
  58.      
  59.      
  60. // Start Hour Range Trading Non Attivo /////////////////////
  61. //hourTrading = input(title='sessione valida di trading', defval='0600-2300:23456')
  62. //hourRangeTrading = time(timeframe.period, hourTrading)
  63.  
  64.  
  65. // Start Detecting ATR Long ///////////////////////////////////////////////////////
  66. length_long = input.int(title="Atr Length_long", defval=10, minval=0)
  67. smoothing_long = input.string(title="Smoothing_long", defval="SMA", options=["RMA", "SMA", "EMA", "WMA"])
  68. ma_function_long(source, length_long) =>
  69.     switch smoothing_long
  70.         "RMA" => ta.rma(source, length_long)
  71.         "SMA" => ta.sma(source, length_long)
  72.         "EMA" => ta.ema(source, length_long)
  73.         => ta.wma(source, length_long)
  74. plot(ma_function_long(ta.tr(true), length_long), title = "ATR_long", color=color.new(#B71C1C, 0))
  75. // End Detecting ATR Long ///////////////////////////////////////////////////////
  76.  
  77.  
  78. // Start Detecting ATR Short ///////////////////////////////////////////////////////
  79. length_short = input.int(title="Atr Length_short", defval=10, minval=0)
  80. smoothing_short = input.string(title="Smoothing_short", defval="SMA", options=["RMA", "SMA", "EMA", "WMA"])
  81. ma_function_short(source, length_short) =>
  82.     switch smoothing_short
  83.         "RMA" => ta.rma(source, length_short)
  84.         "SMA" => ta.sma(source, length_short)
  85.         "EMA" => ta.ema(source, length_short)
  86.         => ta.wma(source, length_short)
  87. plot(ma_function_short(ta.tr(true), length_short), title = "ATR_short", color=color.new(#B71C1C, 0))
  88. // End Detecting ATR Short ///////////////////////////////////////////////////////
  89.  
  90.  
  91. // Variabili ATR Long e Short e Variabili Multiplier Long e Short
  92. in_atr_Mult_long = input.float(title='atr_Mult_long', minval=0, maxval=5, defval=1, step=0.5)
  93. in_atr_Mult_short = input.float(title='atr_Mult_short', minval=0, maxval=5, defval=1, step=0.5)
  94. atr_long = ma_function_long(ta.tr(true), length_long)
  95. atr_short = ma_function_short(ta.tr(true), length_short)
  96.  
  97.  
  98. //Variabili Medie Long e Short
  99. media_long = ta.ema(close, input_lunghezza_ema_long)
  100. media_short = ta.ema(close, input_lunghezza_ema_short)
  101.  
  102. plot(media_long, title = "media_long", color=color.blue)
  103. plot(media_short, title = "media_short", color=color.purple)
  104.  
  105.  
  106. // Variabili Stop Loss Long
  107. stop_loss_long_price = strategy.opentrades.entry_price(0) - (strategy.opentrades.entry_price(0) * input_stop_loss_long) / 100
  108. stop_loss_long = (strategy.opentrades.entry_price(0) - stop_loss_long_price) / syminfo.mintick
  109. take_profit_long_price = (strategy.opentrades.entry_price(0) + ((strategy.opentrades.entry_price(0) * input_take_profit_long) / 100))
  110. take_profit_long = (take_profit_long_price - strategy.opentrades.entry_price(0)) / syminfo.mintick
  111.  
  112. //plot(stop_loss_long, title = "sl_long_ticks", color=color.purple)
  113.  
  114. // Variabili Stop Loss Short
  115. stop_loss_short_price = strategy.opentrades.entry_price(0) + (strategy.opentrades.entry_price(0) * input_stop_loss_short) / 100
  116. stop_loss_short = (stop_loss_short_price - strategy.opentrades.entry_price(0)) / syminfo.mintick
  117. take_profit_short_price = (strategy.opentrades.entry_price(0) - ((strategy.opentrades.entry_price(0) * input_take_profit_short) / 100))
  118. take_profit_short =(strategy.opentrades.entry_price(0) - take_profit_short_price) / syminfo.mintick
  119.  
  120. //plot(stop_loss_short, title = "sl_short_ticks", color=color.purple)
  121.  
  122. // Variabili Stop Loss Long
  123. // stop_loss_long_price = strategy.position_avg_price - (strategy.position_avg_price * input_stop_loss_long) / 100
  124. // stop_loss_long = (strategy.position_avg_price - stop_loss_long_price) / syminfo.mintick
  125. // take_profit_long_price = strategy.position_avg_price + (strategy.position_avg_price * input_take_profit_long) / 100
  126. // take_profit_long = (take_profit_long_price - strategy.position_avg_price) / syminfo.mintick
  127.  
  128. // plot(stop_loss_long, title = "sl_long_ticks", color=color.purple)
  129.  
  130. // Variabili Stop Loss Short
  131. // stop_loss_short_price = strategy.position_avg_price + (strategy.position_avg_price * input_stop_loss_short) / 100
  132. // stop_loss_short = (stop_loss_short_price - strategy.position_avg_price) / 0.1
  133. // take_profit_short_price = (strategy.position_avg_price - ((strategy.position_avg_price * input_take_profit_short) / 100))
  134. // take_profit_short =(strategy.position_avg_price - take_profit_short_price) / 0.1
  135.  
  136. // plot(stop_loss_short, title = "sl_short_ticks", color=color.purple)
  137.  
  138.  
  139. // Calcolo Trailing Stop Long e Short
  140. trailing_stop_trigger_long = strategy.opentrades.entry_price(0) + ( strategy.opentrades.entry_price(0) * input_take_profit_long  / 100) * (input_trailing_trigger_long / 100)
  141. trailing_stop_close_long = strategy.opentrades.entry_price(0) + ( strategy.opentrades.entry_price(0) * input_take_profit_long  / 100) * (input_trailing_close_long / 100)
  142. trailing_stop_trigger_short = strategy.opentrades.entry_price(0) - ( strategy.opentrades.entry_price(0) * input_take_profit_short  / 100) * (input_trailing_trigger_short / 100)
  143. trailing_stop_close_short = strategy.opentrades.entry_price(0) - ( strategy.opentrades.entry_price(0) * input_take_profit_short  / 100) * (input_trailing_close_short / 100)
  144.  
  145.  
  146. // Condizione Uscita Cross Trailing Stop Long E Short
  147. //cond_exit_long = high >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[1] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[2] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[3] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or
  148. //                 high[4] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[5] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[6] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[7] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or
  149. //                 high[8] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[9] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or  high[10] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[11] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or
  150. //                 high[12] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[13] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or  high[14] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or high[15] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low) or
  151. //                 high[16] >= trailing_stop_long_trigger_high and ta.crossunder(close, trailing_stop_close_long_low)
  152.  
  153. //cond_exit_short = low <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[1] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[2] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[3] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or
  154. //                  low[4] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[5] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[6] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[7] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or  
  155. //                  low[8] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[9] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[10] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[11] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or
  156. //                  low[12] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[13] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[14] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or low[15] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high) or
  157. //                  low[16] <= trailing_stop_short_trigger_low and ta.crossover(close, trailing_stop_close_short_high)
  158.  
  159.  
  160. // Condizione Uscita Close Trailing Stop Long E Short                  
  161. // condExitLong = high >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[1] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[2] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[3] >= trailing_stop_trigger_long and close < trailing_stop_close_long or
  162. //                  high[4] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[5] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[6] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[7] >= trailing_stop_trigger_long and close < trailing_stop_close_long or
  163. //                  high[8] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[9] >= trailing_stop_trigger_long and close < trailing_stop_close_long or  high[10] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[11] >= trailing_stop_trigger_long and close < trailing_stop_close_long or
  164. //                  high[12] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[13] >= trailing_stop_trigger_long and close < trailing_stop_close_long or  high[14] >= trailing_stop_trigger_long and close < trailing_stop_close_long or high[15] >= trailing_stop_trigger_long and close < trailing_stop_close_long or
  165. //                  high[16] >= trailing_stop_trigger_long and close < trailing_stop_close_long
  166.  
  167. // condExitShort =  low <= trailing_stop_trigger_short and close >= trailing_stop_close_short or low[1] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or  low[2] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or low[3] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or
  168. //                   low[4] <= trailing_stop_trigger_short and close >= trailing_stop_close_short  or low[5] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or low[6] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or low[7] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or
  169. //                   low[8] <= trailing_stop_trigger_short and close >= trailing_stop_close_short  or low[9] <= trailing_stop_trigger_short and close >= trailing_stop_close_short  or low[10] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or low[11] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or
  170. //                   low[12] <= trailing_stop_trigger_short and close >= trailing_stop_close_short  or low[13] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or low[14] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or low[15] <= trailing_stop_trigger_short and close >= trailing_stop_close_short or
  171. //                   low[16] <= trailing_stop_trigger_short and close >= trailing_stop_close_short
  172.  
  173.  
  174. // Condizione Uscita Close Trailing Stop E Close [1] > < Long E Short
  175. // condExitLong =  high >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[1] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long  or  high[2] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[3] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or
  176. //                  high[4] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[5] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[6] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[7] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or
  177. //                  high[8] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[9] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[10] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[11] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or
  178. //                  high[12] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[13] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[14] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or high[15] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long or
  179. //                  high[16] >= trailing_stop_trigger_long and close <= trailing_stop_close_long and close[1] > trailing_stop_close_long
  180.  
  181. // condExitShort =  low <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[1] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short  or  low[2] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[3] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or
  182. //                   low[4] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[5] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[6] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[7] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or
  183. //                   low[8] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[9] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[10] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[11] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or
  184. //                   low[12] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[13] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[14] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or low[15] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short or
  185. //                   low[16] <= trailing_stop_trigger_short and close >= trailing_stop_close_short and close[1] < trailing_stop_close_short
  186.  
  187. 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
  188. 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")
  189. 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")
  190. plot(strategy.position_size > 0 ? trailing_stop_trigger_long : na, color=color.blue, style=plot.style_cross, linewidth=2, title="trail_trigger_long")
  191. plot(strategy.position_size > 0 ? trailing_stop_close_long : na, color=color.blue, style=plot.style_cross, linewidth=2, title="trail_close_long")
  192. plot(strategy.position_size < 0 ? trailing_stop_trigger_short : na, color=color.blue, style=plot.style_cross, linewidth=2, title="trail_trigger_short")
  193. plot(strategy.position_size < 0 ? trailing_stop_close_short : na, color=color.blue, style=plot.style_cross, linewidth=2, title="trail_close_short")
  194.  
  195. 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
  196.  
  197.  
  198. // Calcolo Uscita Trailing Stop Long E Short
  199. // Bar_index = bar_index
  200. // plot(Bar_index, title = "Bar_index")
  201.  
  202. // entry_bar_index = strategy.opentrades.entry_bar_index(0) +1
  203. // plot(entry_bar_index, title = "entry_bar_index")
  204.  
  205. range_barre_trailing = bar_index - strategy.opentrades.entry_bar_index(0) +1
  206. //plot(lunghezza_trailing, title = 'lunghezza')
  207.  
  208. highesthigh = strategy.opentrades ==1 ? ta.highest(high, range_barre_trailing): na
  209. //plot(highesthigh)
  210.  
  211. lowestlow = strategy.opentrades ==1 ? ta.lowest(low, range_barre_trailing): na
  212. //plot(lowestlow)
  213.  
  214.  
  215. // Plot No Trading Allowed giorni da 1 a 7 1 è Domenica. Mesi da 1 a 12 1 è Gennaio
  216.  
  217. //giorni_esclusi = dayofweek(time)
  218. //plotshape(giorni_esclusi[1] == 2 ? giorni_esclusi : na, color=color.green, title="giorni_esclusi")
  219. //mesi_esclusi = month(time)
  220. //plotshape(mesi_esclusi[1] == 9 ? mesi_esclusi : na, color=color.yellow, title="mesi_esclusi")
  221.  
  222.  
  223. // giorni da 1 a 7 1 è domenica. Entrambe le possibilità sono disponibili sia il cross, sia la close del livello trailing stop close
  224.  
  225. //Condizione Long
  226. cond_long = math.abs(open - close) > atr_long * in_atr_Mult_long and close > open and close > media_long and not in_solo_short and inDateRange //and month != 9 and dayofweek != 2
  227.  
  228. //Condizione Short
  229. cond_short = math.abs(open - close) > atr_short * in_atr_Mult_short and close < open and  close < media_short and not in_solo_long and inDateRange // and month != 9 and dayofweek != 2
  230.  
  231. barcolor(cond_long ? color.lime : cond_short ? color.purple : na)
  232.  
  233.  
  234. // Condizione Uscita Trailing Stop Long
  235. condExitLong =  (highesthigh > trailing_stop_trigger_long and close <= trailing_stop_close_long)
  236.  
  237. // Condizione Uscita Trailing Stop Short
  238. condExitShort = (lowestlow < trailing_stop_trigger_short and close >= trailing_stop_close_short)
  239.  
  240.  
  241. // isLong = false
  242. // if strategy.position_size > 0
  243. //     isLong := true
  244.  
  245. // isShort = false
  246. // if strategy.position_size < 0
  247. //     isShort:= true
  248.  
  249. // cond_long2 = cond_long and not isLong[1]// and not isLong[0]
  250. // cond_short2 = cond_short and not isShort[1]// and not isShort[0]
  251.  
  252.  
  253. // Allerts
  254. buy_command = '{"BUY COMMAND"}'
  255. sell_command = '{"SELL COMMAND"}'
  256. close_command = '{"CLOSE COMMAND"}'
  257.  
  258.  
  259. //entrata e uscita Long
  260. if cond_long // strategy.opentrades == 0
  261.     strategy.entry('operazioneLong', strategy.long, alert_message = "Open Long Position", comment = buy_command)
  262.  
  263. if strategy.opentrades ==1
  264.     strategy.exit('SL e TP', from_entry='operazioneLong', loss = stop_loss_long, profit = take_profit_long, alert_message = "Your Long SL-TP Has Been Triggered.", comment = close_command)
  265.  
  266. if condExitLong and strategy.opentrades == 1
  267.     strategy.close('operazioneLong', alert_message = "Close Long Position", comment = close_command)
  268.    
  269. //entrata e uscita Short
  270. if cond_short //and strategy.opentrades == 0
  271.     strategy.entry('operazioneShort', strategy.short, alert_message = "Open Short position", comment = sell_command)
  272.    
  273. if strategy.opentrades ==1
  274.     strategy.exit('SL e TP', from_entry='operazioneShort', loss = stop_loss_short, profit = take_profit_short, alert_message = "Your Short SL-TP Has Been Triggered.", comment = close_command)
  275.  
  276. if condExitShort and strategy.opentrades == 1
  277.     strategy.close('operazioneShort', alert_message = "Close Short Position", comment = close_command)
  278.    
  279. // Nome Alert: Break-In-New-Trailing-Maurizio ETH/PERP FTX-4H
  280. // Commento Alert: {{strategy.order.comment}}
  281.  
  282.  
  283. // ----------------- Inizio Tabella risultati mensili. Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick -----------------
  284.  
  285. // new_month = month(time) != month(time[1])
  286. // new_year  = year(time)  != year(time[1])
  287.  
  288. // eq = strategy.equity
  289.  
  290. // bar_pnl = eq / eq[1] - 1
  291.  
  292. // cur_month_pnl = 0.0
  293. // cur_year_pnl  = 0.0
  294.  
  295. // // Current Monthly P&L
  296. // cur_month_pnl := new_month ? 0.0 :
  297. //                  (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
  298.  
  299. // // Current Yearly P&L
  300. // cur_year_pnl := new_year ? 0.0 :
  301. //                  (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1  
  302.  
  303. // // Arrays to store Yearly and Monthly P&Ls
  304. // var month_pnl  = array.new_float(0)
  305. // var month_time = array.new_int(0)
  306.  
  307. // var year_pnl  = array.new_float(0)
  308. // var year_time = array.new_int(0)
  309.  
  310. // last_computed = false
  311.  
  312. // if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
  313. //     if (last_computed[1])
  314. //         array.pop(month_pnl)
  315. //         array.pop(month_time)
  316.        
  317. //     array.push(month_pnl , cur_month_pnl[1])
  318. //     array.push(month_time, time[1])
  319.  
  320. // if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
  321. //     if (last_computed[1])
  322. //         array.pop(year_pnl)
  323. //         array.pop(year_time)
  324.        
  325. //     array.push(year_pnl , cur_year_pnl[1])
  326. //     array.push(year_time, time[1])
  327.  
  328. // last_computed := barstate.islast ? true : nz(last_computed[1])
  329.  
  330. // // Monthly P&L Table    
  331. // var monthly_table = table(na)
  332. // prec      = input(2, title = "Return Precision")
  333.  
  334. // if (barstate.islast)
  335. //     monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_width=1,border_color=#000000)
  336.  
  337. //     table.cell(monthly_table, 0,  0, "",     text_color=#D3D3D3, bgcolor=#0F0F0F)
  338. //     table.cell(monthly_table, 1,  0, "Jan",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  339. //     table.cell(monthly_table, 2,  0, "Feb",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  340. //     table.cell(monthly_table, 3,  0, "Mar",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  341. //     table.cell(monthly_table, 4,  0, "Apr",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  342. //     table.cell(monthly_table, 5,  0, "May",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  343. //     table.cell(monthly_table, 6,  0, "Jun",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  344. //     table.cell(monthly_table, 7,  0, "Jul",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  345. //     table.cell(monthly_table, 8,  0, "Aug",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  346. //     table.cell(monthly_table, 9,  0, "Sep",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  347. //     table.cell(monthly_table, 10, 0, "Oct",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  348. //     table.cell(monthly_table, 11, 0, "Nov",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  349. //     table.cell(monthly_table, 12, 0, "Dec",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  350. //     table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
  351.  
  352.  
  353. //     for yi = 0 to array.size(year_pnl) - 1
  354. //         table.cell(monthly_table, 0,  yi + 1, str.tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
  355.        
  356. //         y_color = array.get(year_pnl, yi) > 0 ? color.lime : color.red
  357. //         table.cell(monthly_table, 13, yi + 1, str.tostring(math.round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
  358.        
  359. //     for mi = 0 to array.size(month_time) - 1
  360. //         m_row   = year(array.get(month_time, mi))  - year(array.get(year_time, 0)) + 1
  361. //         m_col   = month(array.get(month_time, mi))
  362. //         m_color = array.get(month_pnl, mi) > 0 ? color.lime : color.red
  363.        
  364. //         table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
  365.  
  366. // ----------------- Fine Tabella risultati mensili. Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick -----------------
  367.  
  368.  
Add Comment
Please, Sign In to add comment