Maurizio-Ciullo

Strategia trend-wide Eth/PERPETUAL Ftx 4H Long E Short

Oct 27th, 2021 (edited)
193
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 - Trend-wide (Strategia Trend Following) - parte 1
  5. //@version=4    
  6.  
  7.                                                                  // I migliori settaggi //
  8.                                                          //ETH/USD // TIMFRAME 4H LONG E SHORT FTX//
  9.                                                                  
  10. // Se gli hub permettono di andare solo long o solo short cambiare le impostazioni onlyLong true e onlyshort false nei parametri di imput.
  11. // Se permette long e short insieme setterli tutti e due true.
  12. // Modificare anche il true o false dello strategy entry.
  13.  
  14. // Collegamento a wunderbit: 1) Inserire la stringa di entry long di wunderbit a strategy.entry aggiungendo ,comment="stringa-wunderbit per entry long".
  15. //                              Stessa cosa per short con exti short. Se ordini in short anche per gli short entry ed exit.
  16. //                           2) Aggiungere webhook di wunderbit sull'allert di tradingview
  17. //                           3) Nelle note di tradingview cancellare tutto ed aggiungere solo: {{strategy.order.comment}}
  18. //                           4) Inserire l'allert sempre alla fine dopo le impostazioni e tutto il resto
  19. //                           5) Ordine minimo wunderbit 20$ altrimenti il bot non entra
  20. //                           6) Se si ha un solo portafoglio con più strategie su un unico portafoglio i bot potrebbero avere size di ingresso diverse da qualle prestabilite
  21. //                           7) Se non si vuole usare la leva, impostare leva 1 su wunderbit altrimenti userà la leva che in quel momento è impostata sull'exchange
  22. //                           8) Avere un capitale adeguato per il margine di mantenimento e considerare che se il capitale scende la posizione è minore dei 20$ di wunderbit richiesti per entrare in posizione
  23.  
  24. strategy(title="Strategia trend-wide Eth/USD Ftx 4H Long E Short", overlay=true,
  25.      pyramiding=0, initial_capital=266,
  26.      commission_type=strategy.commission.percent,
  27.      commission_value=0.1, slippage=50,
  28.      default_qty_type=strategy.percent_of_equity,
  29.      default_qty_value=15)
  30.  
  31. maInputSlowLong = input(title="MA SlowLong", type=input.integer, defval=74, minval=0, maxval=500, group="Medie")
  32. maInputSlowShort = input(title="MA SlowShort", type=input.integer, defval=49, minval=0, maxval=500, group="Medie")
  33. maInputFastLong = input(title="MA FastLong", type=input.integer, defval=15, minval=0, maxval=500, group="Medie")
  34. maInputFastShort = input(title="MA FastShort", type=input.integer, defval=15, minval=0, maxval=500, group="Medie")
  35. smaInputMediumShort = input(title="SMA MediumShort", type=input.integer, defval=50, minval=0, maxval=500, group="Medie")
  36. maInputExitLong = input(title="MA Exit Long", type=input.integer, defval=43, minval=0, maxval=500, group="Medie")
  37. smaInputExitShort = input(title="SMA Exit Short", type=input.integer, defval=11, minval=0, maxval=500, group="Medie")
  38. maMinDiffLong = input(title="Distanza min medie Long", type=input.float, defval=8.31, step=0.01, group="Filtri Medie")
  39. maMinDiffShort = input(title="Distanza min medie Short", type=input.float, defval=7.23, step=0.01, group="Filtri Medie")
  40. hourTrading = input(title="Sessione valida di trading", type=input.string, defval="24x7", group="Sessioni")
  41. onlyLong = input(title="Solo long trade", type=input.bool, defval=false, inline="1", group="Direzione")
  42. onlyShort = input(title="Solo short trade", type=input.bool, defval=false, inline="1", group="Direzione")
  43.  
  44. // STEP 1 DATARANGE:
  45. // Make input options that configure backtest date range
  46.  
  47. startDate = input(title="Start Date", type=input.integer,
  48.      defval=01, minval=1, maxval=31, group="Periodo")
  49. startMonth = input(title="Start Month", type=input.integer,
  50.      defval=1, minval=1, maxval=12, group="Periodo")
  51. startYear = input(title="Start Year", type=input.integer,
  52.      defval=2000, minval=1800, maxval=2100, group="Periodo")
  53.  
  54. endDate = input(title="End Date", type=input.integer,
  55.      defval=01, minval=1, maxval=31, group="Periodo")
  56. endMonth = input(title="End Month", type=input.integer,
  57.      defval=01, minval=1, maxval=12, group="Periodo")
  58. endYear = input(title="End Year", type=input.integer,
  59.      defval=2121, minval=1800, maxval=2100, group="Periodo")
  60.  
  61. // STEP 2 DATARANGE:
  62. // Look if the close time of the current bar
  63. // falls inside the date range
  64.  
  65. inDateRange = (time >= timestamp(syminfo.timezone, startYear,
  66.          startMonth, startDate, 0, 0)) and
  67.      (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
  68.      
  69. // Include or Exclude Days of the Week
  70. // Trade_long_on_dow_1 is Sunday/Domenica
  71. // Trade_long_on_dow_2 is Monday/Lunedì Etc...
  72. // If a position is opened it will be closed as srategy without excluding the month
  73.  
  74. trade_long_on_dow_1 = input(true, group="Include/Esclude Giorni Long")
  75. trade_long_on_dow_2 = input(true, group="Include/Esclude Giorni Long")
  76. trade_long_on_dow_3 = input(true, group="Include/Esclude Giorni Long")
  77. trade_long_on_dow_4 = input(true, group="Include/Esclude Giorni Long")
  78. trade_long_on_dow_5 = input(true, group="Include/Esclude Giorni Long")
  79. trade_long_on_dow_6 = input(true, group="Include/Esclude Giorni Long")
  80. trade_long_on_dow_7 = input(true, group="Include/Esclude Giorni Long")
  81.  
  82. trade_short_on_dow_1 = input(true, group="Include/Esclude Giorni Short")
  83. trade_short_on_dow_2 = input(true, group="Include/Esclude Giorni Short")
  84. trade_short_on_dow_3 = input(true, group="Include/Esclude Giorni Short")
  85. trade_short_on_dow_4 = input(true, group="Include/Esclude Giorni Short")
  86. trade_short_on_dow_5 = input(true, group="Include/Esclude Giorni Short")
  87. trade_short_on_dow_6 = input(true, group="Include/Esclude Giorni Short")
  88. trade_short_on_dow_7 = input(true, group="Include/Esclude Giorni Short")
  89.  
  90. trade_long_today = (trade_long_on_dow_1 and dayofweek == 1) or (trade_long_on_dow_2 and dayofweek == 2) or (trade_long_on_dow_3 and dayofweek == 3) or (trade_long_on_dow_4 and dayofweek == 4) or (trade_long_on_dow_5 and dayofweek == 5) or (trade_long_on_dow_6 and dayofweek == 6) or (trade_long_on_dow_7 and dayofweek == 7)
  91. trade_short_today = (trade_short_on_dow_1 and dayofweek == 1) or (trade_short_on_dow_2 and dayofweek == 2) or (trade_short_on_dow_3 and dayofweek == 3) or (trade_short_on_dow_4 and dayofweek == 4) or (trade_short_on_dow_5 and dayofweek == 5) or (trade_short_on_dow_6 and dayofweek == 6) or (trade_short_on_dow_7 and dayofweek == 7)
  92.  
  93. // Exit open market position when date range ends
  94.  
  95. //if (not trade_long_today)
  96. //    strategy.close_all()
  97.  
  98. //if (not trade_short_today)
  99. //    strategy.close_all()
  100.  
  101.  
  102. // Include or Exclude Moths of the Year
  103. // Trade_long_on_dow_1 is Sunday/Domenica
  104. // Trade_long_on_dow_2 is Monday/Lunedì Etc...
  105. // If a position is opened it will be closed as srategy without excluding the month
  106.  
  107. trade_long_on_dow_month_1 = input(true, group="Include/Esclude Mesi Long")
  108. trade_long_on_dow_month_2 = input(true, group="Include/Esclude Mesi Long")
  109. trade_long_on_dow_month_3 = input(true, group="Include/Esclude Mesi Long")
  110. trade_long_on_dow_month_4 = input(true, group="Include/Esclude Mesi Long")
  111. trade_long_on_dow_month_5 = input(true, group="Include/Esclude Mesi Long")
  112. trade_long_on_dow_month_6 = input(true, group="Include/Esclude Mesi Long")
  113. trade_long_on_dow_month_7 = input(true, group="Include/Esclude Mesi Long")
  114. trade_long_on_dow_month_8 = input(true, group="Include/Esclude Mesi Long")
  115. trade_long_on_dow_month_9 = input(true, group="Include/Esclude Mesi Long")
  116. trade_long_on_dow_month_10 = input(true, group="Include/Esclude Mesi Long")
  117. trade_long_on_dow_month_11 = input(true, group="Include/Esclude Mesi Long")
  118. trade_long_on_dow_month_12 = input(true, group="Include/Esclude Mesi Long")
  119.  
  120. trade_short_on_dow_month_1 = input(true, group="Include/Esclude Mesi Short")
  121. trade_short_on_dow_month_2 = input(true, group="Include/Esclude Mesi Short")
  122. trade_short_on_dow_month_3 = input(true, group="Include/Esclude Mesi Short")
  123. trade_short_on_dow_month_4 = input(true, group="Include/Esclude Mesi Short")
  124. trade_short_on_dow_month_5 = input(true, group="Include/Esclude Mesi Short")
  125. trade_short_on_dow_month_6 = input(true, group="Include/Esclude Mesi Short")
  126. trade_short_on_dow_month_7 = input(true, group="Include/Esclude Mesi Short")
  127. trade_short_on_dow_month_8 = input(true, group="Include/Esclude Mesi Short")
  128. trade_short_on_dow_month_9 = input(true, group="Include/Esclude Mesi Short")
  129. trade_short_on_dow_month_10 = input(true, group="Include/Esclude Mesi Short")
  130. trade_short_on_dow_month_11 = input(true, group="Include/Esclude Mesi Short")
  131. trade_short_on_dow_month_12 = input(true, group="Include/Esclude Mesi Short")
  132.  
  133.  
  134. trade_long_this_month = (trade_long_on_dow_month_1 and month == 1) or (trade_long_on_dow_month_2 and month == 2) or (trade_long_on_dow_month_3 and month == 3) or (trade_long_on_dow_month_4 and month == 4) or (trade_long_on_dow_month_5 and month == 5) or (trade_long_on_dow_month_5 and month == 5) or (trade_long_on_dow_month_6 and month == 6) or (trade_long_on_dow_month_7 and month == 7) or (trade_long_on_dow_month_8 and month == 8) or (trade_long_on_dow_month_9 and month == 9) or (trade_long_on_dow_month_10 and month == 10) or (trade_long_on_dow_month_11 and month == 11) or (trade_long_on_dow_month_12 and month == 12)
  135. trade_short_this_month = (trade_short_on_dow_month_1 and month == 1) or (trade_short_on_dow_month_2 and month == 2) or (trade_short_on_dow_month_3 and month == 3) or (trade_short_on_dow_month_4 and month == 4) or (trade_short_on_dow_month_5 and month == 5) or (trade_short_on_dow_month_5 and month == 5) or (trade_short_on_dow_month_6 and month == 6) or (trade_short_on_dow_month_7 and month == 7) or (trade_short_on_dow_month_8 and month == 8) or (trade_short_on_dow_month_9 and month == 9) or (trade_short_on_dow_month_10 and month == 10) or (trade_short_on_dow_month_11 and month == 11) or (trade_short_on_dow_month_12 and month == 12)
  136.  
  137. // Exit open market position when date range ends
  138. //if (trade_long_this_month)
  139. //    strategy.close_all()
  140.  
  141. //if (trade_short_this_month)
  142. //    strategy.close_all()
  143.  
  144.  
  145. maSlowLong = ema(close, maInputSlowLong)
  146. maSlowShort = ema(close, maInputSlowShort)
  147. maFastLong = ema(close, maInputFastLong)
  148. maFastShort = ema(close, maInputFastShort)
  149. smaMediumShort = sma(close, smaInputMediumShort)
  150. maExitLong = ema(close, maInputExitLong)
  151. smaExitShort = sma(close, smaInputExitShort)
  152. rangeTrading = time(timeframe.period, hourTrading)
  153. differenziale_attuale_long = maFastLong - maSlowLong
  154. differenziale_attuale_short = maSlowShort - maFastShort
  155.  
  156.  
  157. plot(maSlowLong, color=color.orange, title="maSlowLong")
  158. plot(maSlowShort, color=color.green, title="maSlowShort")
  159. plot(maFastLong, color=color.blue, title="maFastLong")
  160. plot(maFastShort, color=color.purple, title="maFastShort")
  161. plot(smaMediumShort, color=color.gray, title="smaMediumShort")
  162. plot(maExitLong, color=color.black, title="maExitLong")
  163. plot(smaExitShort, color=color.red, title="smaExitShort")
  164. plot(differenziale_attuale_long, title="diff_attuale_long")
  165. plot(differenziale_attuale_short, title="diff_attuale_short")
  166.  
  167. // Filtro di emergenza chiusura posizione solo se aperta solo per lo short perchè se il cross di uscita posizione delle medie sono invertite non esco più
  168.  
  169. if (close > maSlowShort)
  170.     if (smaExitShort > maSlowShort)
  171.         if (strategy.position_size < 0)
  172.             strategy.close(id="short")
  173.  
  174.  
  175. //Entry/Exit Conditions
  176.  
  177. //Condizione Entry Long: Chiusura candela sopra media lenta con differenziale medie e media veloce maggiore media ExitLong
  178. condEntryLong = close > maSlowLong and (maFastLong - maSlowLong) > maMinDiffLong and rangeTrading and inDateRange and trade_long_today and trade_long_this_month and maFastLong > maExitLong and not onlyShort
  179. //Condizione Exit Long: Crossunder di due medie
  180. condExitLong = crossunder(maFastLong, maExitLong)
  181.  
  182. // Condizione Entry Short: Crossunder di due medie con il prezzo di chiusura che è comunque sotto una terza media con differenziale delle medie
  183. condEntryShort = crossunder(smaExitShort, maFastShort) and close < smaMediumShort and (maSlowShort - maFastShort) > maMinDiffShort and rangeTrading and inDateRange and trade_short_today and trade_short_this_month and not onlyLong
  184. // Condizione Exit Short: Crossover di medie con il prezzo di chiusura che è comunque sopra una terza media
  185. condExitShort = crossover(smaExitShort, smaMediumShort) and close > maSlowShort
  186.  
  187.  
  188. //Entry/Exit Orders
  189.  
  190. strategy.entry("long", true, when = condEntryLong, comment="eac4917c_ENTER-LONG_FTX_ETH-PERP_eac4917c-FTX-ETH-PERP4H-LONG-SHORT_4H")
  191. strategy.close("long", when=condExitLong, comment="eac4917c_EXIT-LONG_FTX_ETH-PERP_eac4917c-FTX-ETH-PERP4H-LONG-SHORT_4H")
  192.  
  193. strategy.entry("short", false, when = condEntryShort, comment="eac4917c_ENTER-SHORT_FTX_ETH-PERP_eac4917c-FTX-ETH-PERP4H-LONG-SHORT_4H")
  194. strategy.close("short", when=condExitShort, comment="eac4917c_EXIT-SHORT_FTX_ETH-PERP_eac4917c-FTX-ETH-PERP4H-LONG-SHORT_4H")
  195.  
  196.  
  197. // Tabella risultati mensili by QuantNomad (TSC Boilerplate) Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick
  198.  
  199. new_month = month(time) != month(time[1])
  200. new_year  = year(time)  != year(time[1])
  201.  
  202. eq = strategy.equity
  203.  
  204. bar_pnl = eq / eq[1] - 1
  205.  
  206. cur_month_pnl = 0.0
  207. cur_year_pnl  = 0.0
  208.  
  209. // Current Monthly P&L
  210. cur_month_pnl := new_month ? 0.0 :
  211.                  (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
  212.  
  213. // Current Yearly P&L
  214. cur_year_pnl := new_year ? 0.0 :
  215.                  (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1  
  216.  
  217. // Arrays to store Yearly and Monthly P&Ls
  218. var month_pnl  = array.new_float(0)
  219. var month_time = array.new_int(0)
  220.  
  221. var year_pnl  = array.new_float(0)
  222. var year_time = array.new_int(0)
  223.  
  224. last_computed = false
  225.  
  226. if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
  227.     if (last_computed[1])
  228.         array.pop(month_pnl)
  229.         array.pop(month_time)
  230.        
  231.     array.push(month_pnl , cur_month_pnl[1])
  232.     array.push(month_time, time[1])
  233.  
  234. if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
  235.     if (last_computed[1])
  236.         array.pop(year_pnl)
  237.         array.pop(year_time)
  238.        
  239.     array.push(year_pnl , cur_year_pnl[1])
  240.     array.push(year_time, time[1])
  241.  
  242. last_computed := barstate.islast ? true : nz(last_computed[1])
  243.  
  244. // Monthly P&L Table    
  245. var monthly_table = table(na)
  246. prec      = input(2, title = "Return Precision")
  247.  
  248. if (barstate.islast)
  249.     monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_width=1,border_color=#000000)
  250.  
  251.     table.cell(monthly_table, 0,  0, "",     text_color=#D3D3D3, bgcolor=#0F0F0F)
  252.     table.cell(monthly_table, 1,  0, "Jan",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  253.     table.cell(monthly_table, 2,  0, "Feb",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  254.     table.cell(monthly_table, 3,  0, "Mar",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  255.     table.cell(monthly_table, 4,  0, "Apr",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  256.     table.cell(monthly_table, 5,  0, "May",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  257.     table.cell(monthly_table, 6,  0, "Jun",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  258.     table.cell(monthly_table, 7,  0, "Jul",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  259.     table.cell(monthly_table, 8,  0, "Aug",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  260.     table.cell(monthly_table, 9,  0, "Sep",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  261.     table.cell(monthly_table, 10, 0, "Oct",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  262.     table.cell(monthly_table, 11, 0, "Nov",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  263.     table.cell(monthly_table, 12, 0, "Dec",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  264.     table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
  265.  
  266.  
  267.     for yi = 0 to array.size(year_pnl) - 1
  268.         table.cell(monthly_table, 0,  yi + 1, tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
  269.        
  270.         y_color = array.get(year_pnl, yi) > 0 ? color.lime : color.red
  271.         table.cell(monthly_table, 13, yi + 1, tostring(round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
  272.        
  273.     for mi = 0 to array.size(month_time) - 1
  274.         m_row   = year(array.get(month_time, mi))  - year(array.get(year_time, 0)) + 1
  275.         m_col   = month(array.get(month_time, mi))
  276.         m_color = array.get(month_pnl, mi) > 0 ? color.lime : color.red
  277.        
  278.         table.cell(monthly_table, m_col, m_row, tostring(round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
  279.  
  280.  
  281. //SETTAGGI FTX FUTURE SOLO LONG:
  282. //MaSlowLong: 66/116/114/100 le migliori sono 66 e 116 la 66 miglio equity line e non stalla, la 116 guadagna un pò di più ma stalla di più.
  283. //MaFastLong: 15 entra ed esce bene le altre più alte fanno meno operazioni ma entrano ed escono dopo.
  284. //Continuare ad usare i settaggi: MaSlowLong: 66.....MaFastLong: 15
  285.  
  286. //SETTAGGI ETH/USD FTX FUTURE IDENTICI A BITFINEX COME INGRESSI E USCITE ECCEZIONE MINIMA DELLA CANDELA SPORADICA LONG E SHORT:
  287. // maSlowLong 74
  288. // maSlowShort 49
  289. // maFastLong 15
  290. // maFastShort 15
  291. // smaMediumShort 50
  292. // maExitLong 43
  293. // maExitShort 11
  294. // Distanza min Medie Long 8.31
  295. // Distanza min Medie Short 7.23
  296.  
  297. // Quando valutare se stoppare il trading sytem, Valutare le percentuali perchè il portfoglio è incrementale:
  298.  
  299. //1) Equity line che rompe la retta dei minimi
  300. //2) Report valori passato: Inizio 16/03/2017 Fine 26/11/2021
  301.  
  302. //   Sommario Performance: DD MAx - 31.01$ = 4.89% ----- Perdita Media - 5.41% = - 2.03 Del Portafoglio ------ Peggio Perdita 14.60$ = 13.77%
  303.  
  304. //   Perdita più grandi realizzate per operazione ordine crescente
  305. //                                                      -10.96$ = -13.77%
  306. //                                                      -7.17$  = -11.57%
  307. //                                                      -11.48$ = -11.11%
  308. //                                                      -14.31$ = -10.96%
  309. //                                                      -7.64$  = -10.93%
  310. //                                                      -7.43$  = -10.52%
  311. //                                                      -14.60  = -10.36%
  312.  
  313. // Draw Down Max Non Realizzato In Operazione In Ordine Crescente:
  314. //                                                      -10.92$ = 27.04%
  315. //                                                      -18.53$ = 12.87%
  316. //                                                      -13.03$ = 18.26%
  317. //                                                      -10.34$ = 16.69%
  318. //                                                      -13.20$ = 16.58%
  319. //                                                      -18.16$ = 13.91%
  320. //                                                      - 9.39$ = 13.74%
  321. //                                                      -12.06$ = 13.72%
  322. //                                                      -14.16$ = 13.70%
  323. //                                                      -19.17$ = 13.61%
  324.  
Add Comment
Please, Sign In to add comment