Maurizio-Ciullo

Bot Swing-Trend ETH/PERP FTX 4H LONG E SHORT

Jan 13th, 2022 (edited)
168
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. // © Maurizio-Ciullo
  3.  
  4. //Il trading system completo - Swing-Trend (Strategia Trend Following Con Swing Di Posizione) - parte 1
  5. // (Exchange= FTX) (Sottostante ETH-PERP) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= SI) (Esclusione Giorni=Sabato) (Esclusione Mesi=Ottobre)
  6. // (Take Profit Long/Short= Market Close Sopra/Sotto Medie) (Stop Loss Limit= -2,5% Tradingview-Hub) (Stop Loss Emergenza Long/Short = Market Close Sopra/Sotto Medie)
  7. //@version=4
  8.  
  9. strategy(title="Bot Swing-Trend ETH/PERP FTX 4H LONG E SHORT", overlay=true,
  10.      pyramiding=0, initial_capital=150,
  11.      commission_type=strategy.commission.percent,
  12.      commission_value=0.1, slippage=3,
  13.      default_qty_type=strategy.percent_of_equity,
  14.      default_qty_value=27.5)
  15.  
  16. input_ema_long_apertura = input(title="Media Long Apertura", type=input.integer, defval=45, minval=0, maxval=500, group="Medie")
  17. input_ema_long_chiusura = input(title="Media Long Chiusura", type=input.integer, defval=35, minval=0, maxval=500, group="Medie")
  18. input_ema_short_apertura = input(title="Media Short Apertura", type=input.integer, defval=40, minval=0, maxval=500, group="Medie")
  19. input_ema_short_chiusura = input(title="Media Short Chiusura", type=input.integer, defval=30, minval=0, maxval=500, group="Medie")
  20. perc_apertura_minima_long = input(title="perc_apertura_minima_long", type=input.float, defval=0, step=0.1, group="Filtri Posizione")
  21. perc_apertura_massima_long = input(title="perc_apertura_massima_long", type=input.float, defval=2, step=0.1, group="Filtri Posizione ")
  22. perc_chiusura_minima_long = input(title="perc_chiusura_minima_long", type=input.float,defval=1.5, step=0.1, group="Filtri Posizione")
  23. perc_apertura_minima_short = input(title="perc_apertura_minima_short", type=input.float, defval=0.75, step=0.01, group="Filtri Posizione")
  24. perc_apertura_massima_short = input(title="perc_apertura_massima_short", type=input.float, defval=2, step=0.1, group="Filtri Posizione")
  25. perc_chiusura_minima_short = input(title="perc_chiusura_minima_short", type=input.float, defval=1, step=0.1, group="Filtri Posizione")
  26. onlyLong = input(title="Solo long trade", type=input.bool, defval=false, inline="1", group="Direzione")
  27. onlyShort = input(title="Solo short trade", type=input.bool, defval=false, inline="1", group="Direzione")
  28. input_stop_loss = input(title="stop_loss_long", type=input.float, defval=2.5, minval=0, maxval=100, step=0.1, group="Stop")
  29. input_risk = input(title="Percentuale rischio", type=input.float, defval=27.5, minval=0, maxval=100, step = 0.01, group="Stop")
  30. //dmi_min_diff_input_long = input(title="Distanza min dim", type=input.float, minval=-100, maxval=1000000, defval=19, step=0.1)
  31. //dmi_min_diff_input_short = input(title="Distanza min dim", type=input.float, minval=-100, maxval=1000000, defval=7, step=0.1)
  32.  
  33.  
  34. // STEP 1 DATARANGE:
  35. // Make input options that configure backtest date range
  36.  
  37. startDate = input(title="Start Date", type=input.integer,
  38.      defval=18, minval=1, maxval=31, group="Periodo")
  39. startMonth = input(title="Start Month", type=input.integer,
  40.      defval=08, minval=1, maxval=12, group="Periodo")
  41. startYear = input(title="Start Year", type=input.integer,
  42.      defval=2017, minval=1800, maxval=2100, group="Periodo")
  43.  
  44. endDate = input(title="End Date", type=input.integer,
  45.      defval=01, minval=1, maxval=31, group="Periodo")
  46. endMonth = input(title="End Month", type=input.integer,
  47.      defval=01, minval=1, maxval=12, group="Periodo")
  48. endYear = input(title="End Year", type=input.integer,
  49.      defval=2121, minval=1800, maxval=2100, group="Periodo")
  50.  
  51. // STEP 2 DATARANGE:
  52. // Look if the close time of the current bar
  53. // falls inside the date range
  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.  
  61. ema_long_apertura = ema(close, input_ema_long_apertura)
  62. ema_long_chiusura = ema(close, input_ema_long_chiusura)
  63. ema_short_apertura = ema(close,input_ema_short_apertura)
  64. ema_short_chiusura = ema(close,input_ema_short_chiusura)
  65.  
  66. //Calcolo filtri e stop loss
  67.  
  68.  
  69. apertura_minima_long = (ema_long_apertura / 100) * perc_apertura_minima_long
  70. apertura_massima_long = (ema_long_apertura / 100) * perc_apertura_massima_long
  71. chiusura_minima_long = (ema_long_chiusura / 100) * perc_chiusura_minima_long
  72. apertura_minima_short = (ema_short_apertura / 100) * perc_apertura_minima_short
  73. apertura_massima_short = (ema_short_apertura / 100) * perc_apertura_massima_short
  74. chiusura_minima_short = (ema_short_chiusura / 100) * perc_chiusura_minima_short
  75. size = ((strategy.equity * input_risk) / 100)
  76. nr_contratti = (size) / close
  77. //stop_loss =round(((size /syminfo.mintick) / 100) * input_stop_loss)/nr_contratti
  78.  
  79.  
  80.  
  81. plot(ema_long_apertura, color=color.blue, title="Ema Long Apertura", linewidth = 2)
  82. plot(ema_long_chiusura, color=color.blue, title="Ema Long Chiusura", linewidth = 2)
  83. plot(ema_short_apertura, color=color.red, title="Ema Short Apertura", linewidth = 2)
  84. plot(ema_short_chiusura, color=color.red, title="Ema Short Chiusura", linewidth = 2)
  85.  
  86.  
  87. filtro_long_basso = (ema_long_apertura + apertura_minima_long)
  88. filtro_long_alto = (ema_long_apertura + apertura_massima_long)
  89.  
  90. plot_filtro_long_basso = plot(filtro_long_basso, color=color.green, title="filtro_long_basso")
  91. plot_filtro_long_alto = plot(filtro_long_alto, color=color.green, title="filtro_long_alto")
  92. fill(plot_filtro_long_basso, plot_filtro_long_alto, color=color.green)
  93.  
  94. filtro_short_basso = (ema_short_apertura - apertura_minima_short)
  95. filtro_short_alto = (ema_short_apertura - apertura_massima_short)
  96.  
  97. plot_filtro_short_basso = plot(filtro_short_basso, color=color.yellow, title="filtro_short_basso")
  98. plot_filtro_short_alto = plot(filtro_short_alto, color=color.yellow, title="filtro_short_alto")
  99. fill(plot_filtro_short_basso, plot_filtro_short_alto, color=color.red)
  100.  
  101. plot(ema_long_chiusura - chiusura_minima_long, color=color.green, style=plot.style_circles, linewidth=3, title="Emergency_Stop_Long")
  102. plot(ema_short_chiusura + chiusura_minima_short, color=color.red, style=plot.style_circles, linewidth=3, title="Emergency_Stop_Short")
  103.  
  104. stop_loss =round(((size /syminfo.mintick) / 100) * input_stop_loss)/nr_contratti
  105.  
  106.  
  107. plot(strategy.position_size != 0 ? strategy.position_avg_price : 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
  108. plot(strategy.position_size > 0 ?  stop_loss: strategy.position_size < 0 ? stop_loss : na, color=color.green, style=plot.style_linebr, title="stop_loss_limit")
  109. plot(strategy.position_size > 0 ?  stop_loss: strategy.position_size < 0 ? stop_loss : na, color=color.red, style=plot.style_linebr, title="stop_loss_limit")
  110. bgcolor(strategy.position_size > 0 ? color.green : strategy.position_size < 0 ? color.red : na) // sfondo verde quando siamo long, sfondo rosso quando siamo short, no sfondo quando non siamo in posizione
  111.  
  112.                    
  113. // Filtro voglio che sia true se il +DI è sopra sia il -DI ed anche dell'ADX //21,3587
  114.  
  115. //[di_pos, di_neg, adx] = dmi(14, 14)
  116.  
  117. //dmi_total_green_cross = di_pos > di_neg and di_pos > adx? true : false
  118. //dmi_total_green_cross = (di_pos - di_neg) > dma_min_diff_input ? true : false
  119. //dmi_total_green_cross = (di_neg - di_pos) > dmi_min_diff_input ? true : false
  120. //dmi_total_green_cross_short = (di_neg > adx + dmi_min_diff_input_short)  ? true : false
  121. //dmi_total_green_cross_long = (di_pos > adx + dmi_min_diff_input_long)  ? true : false
  122. //dmi_total_green_cross = (di_pos - di_neg) < dmi_min_diff_input  ? true : false
  123.  
  124. //plot(dmi_total_green_cross ? 1 : 0, title="dmi_total_green_cross")
  125.  
  126.  
  127. //Entry/Exit Conditions
  128.  
  129.  
  130. //Condizione Entry Long: Chiusura candela sopra media lenta con differenziale medie e media veloce maggiore media ExitLong
  131. condEntryLong = close > (ema_long_apertura + apertura_minima_long) and close < (ema_long_apertura + apertura_massima_long) and close > open and not onlyShort and dayofweek != 7 and month !=10 and inDateRange
  132. //Condizione Exit Long: Crossunder di due medie
  133. condExitLong = close < (ema_long_chiusura - chiusura_minima_long)
  134.  
  135. // Condizione Entry Short: Crossunder di due medie con il prezzo di chiusura che è comunque sotto una terza media con differenziale delle medie
  136. condEntryShort = close < (ema_short_apertura - apertura_minima_short) and close > (ema_short_apertura - apertura_massima_short) and close < open and not onlyLong and dayofweek != 7 and month !=10 and inDateRange
  137. // Condizione Exit Short: Crossover di medie con il prezzo di chiusura che è comunque sopra una terza media
  138. condExitShort = close > (ema_short_chiusura + chiusura_minima_short)
  139.  
  140.  
  141. plot(strategy.position_avg_price - stop_loss * 0.01 * 10, title="stop_loss_limit_long", color=color.silver, transp=3, linewidth=4)
  142. plot(strategy.position_avg_price + stop_loss * 0.01 * 10, title="stop_loss_limit_short", color=color.purple, transp=3, linewidth=4)
  143.  
  144. //Entry/Exit Orders
  145.  
  146.  
  147. //strategy.entry("long", true, when = condEntryLong)
  148. //strategy.close("long", when=condExitLong)
  149. //strategy.entry("short", false, when = condEntryShort)
  150. //strategy.close("short", when=condExitShort)
  151.  
  152. if (condEntryLong)
  153.     strategy.entry("long", true)
  154.     strategy.exit("stop loss", from_entry = "long", loss = stop_loss)
  155.    
  156. if (condExitLong)    
  157.     strategy.close(id="long")
  158.    
  159. if (condEntryShort)
  160.     strategy.entry("short", false)
  161.     strategy.exit("stop loss", from_entry = "short", loss = stop_loss)
  162.    
  163. if (condExitShort)    
  164.     strategy.close(id="short")
  165.  
  166. // Tabella risultati mensili by QuantNomad (TSC Boilerplate) Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick
  167.  
  168. new_month = month(time) != month(time[1])
  169. new_year  = year(time)  != year(time[1])
  170.  
  171. eq = strategy.equity
  172.  
  173. bar_pnl = eq / eq[1] - 1
  174.  
  175. cur_month_pnl = 0.0
  176. cur_year_pnl  = 0.0
  177.  
  178. // Current Monthly P&L
  179. cur_month_pnl := new_month ? 0.0 :
  180.                  (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
  181.  
  182. // Current Yearly P&L
  183. cur_year_pnl := new_year ? 0.0 :
  184.                  (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1  
  185.  
  186. // Arrays to store Yearly and Monthly P&Ls
  187. var month_pnl  = array.new_float(0)
  188. var month_time = array.new_int(0)
  189.  
  190. var year_pnl  = array.new_float(0)
  191. var year_time = array.new_int(0)
  192.  
  193. last_computed = false
  194.  
  195. if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
  196.     if (last_computed[1])
  197.         array.pop(month_pnl)
  198.         array.pop(month_time)
  199.        
  200.     array.push(month_pnl , cur_month_pnl[1])
  201.     array.push(month_time, time[1])
  202.  
  203. if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
  204.     if (last_computed[1])
  205.         array.pop(year_pnl)
  206.         array.pop(year_time)
  207.        
  208.     array.push(year_pnl , cur_year_pnl[1])
  209.     array.push(year_time, time[1])
  210.  
  211. last_computed := barstate.islast ? true : nz(last_computed[1])
  212.  
  213. // Monthly P&L Table    
  214. var monthly_table = table(na)
  215. prec      = input(2, title = "Return Precision")
  216.  
  217. if (barstate.islast)
  218.     monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_color=#000000, border_width=1) //frame_width=1 tabella più piccola sostituisce border_width=1
  219.  
  220.     table.cell(monthly_table, 0,  0, "",     text_color=#D3D3D3, bgcolor=#0F0F0F)
  221.     table.cell(monthly_table, 1,  0, "Jan",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  222.     table.cell(monthly_table, 2,  0, "Feb",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  223.     table.cell(monthly_table, 3,  0, "Mar",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  224.     table.cell(monthly_table, 4,  0, "Apr",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  225.     table.cell(monthly_table, 5,  0, "May",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  226.     table.cell(monthly_table, 6,  0, "Jun",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  227.     table.cell(monthly_table, 7,  0, "Jul",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  228.     table.cell(monthly_table, 8,  0, "Aug",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  229.     table.cell(monthly_table, 9,  0, "Sep",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  230.     table.cell(monthly_table, 10, 0, "Oct",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  231.     table.cell(monthly_table, 11, 0, "Nov",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  232.     table.cell(monthly_table, 12, 0, "Dec",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  233.     table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
  234.  
  235.  
  236.     for yi = 0 to array.size(year_pnl) - 1
  237.         table.cell(monthly_table, 0,  yi + 1, tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
  238.        
  239.         y_color = array.get(year_pnl, yi) > 0 ? color.lime : color.red
  240.         table.cell(monthly_table, 13, yi + 1, tostring(round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
  241.        
  242.     for mi = 0 to array.size(month_time) - 1
  243.         m_row   = year(array.get(month_time, mi))  - year(array.get(year_time, 0)) + 1
  244.         m_col   = month(array.get(month_time, mi))
  245.         m_color = array.get(month_pnl, mi) > 0 ? color.lime : color.red
  246.        
  247.         table.cell(monthly_table, m_col, m_row, tostring(round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
Add Comment
Please, Sign In to add comment