Maurizio-Ciullo

Study Trendwide ETH-PERP Ftx 4H Long E Short

Dec 4th, 2021 (edited)
153
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.                                                                                                                            
  8. // Se gli hub permettono di andare solo long o solo short cambiare le impostazioni onlyLong true e onlyshort false nei parametri di imput.
  9. // Se permette long e short insieme setterli tutti e due true.
  10. // Modificare anche il true o false dello strategy entry.
  11.  
  12. // Collegamento a wunderbit: 1) Inserire la stringa di entry long di wunderbit a strategy.entry aggiungendo ,comment="stringa-wunderbit per entry long".
  13. //                              Stessa cosa per short con exti short. Se ordini in short anche per gli short entry ed exit.
  14. //                           2) Aggiungere webhook di wunderbit sull'allert di tradingview
  15. //                           3) Nelle note di tradingview cancellare tutto ed aggiungere solo: {{strategy.order.comment}}
  16. //                           4) Inserire l'allert sempre alla fine dopo le impostazioni e tutto il resto
  17. //                           5) Ordine minimo wunderbit 20$ altrimenti il bot non entra
  18. //                           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
  19. //                           7) Se non si vuole usare la leva, impostare leva 1 su wunderbit altrimenti userà la leva che in quel momento è impostata sull'exchange
  20. //                           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
  21.  
  22.                                                                  // I migliori settaggi //
  23.                                                          //ETH/USD // TIMFRAME 4H LONG E SHORT BITFINEX//
  24.                                                                  
  25. study(title="Study Trendwide ETH-PERP Ftx 4H Long E Short", overlay=false)
  26.      
  27.  
  28. maInputSlowLong = input(title="MA SlowLong", type=input.integer, defval=74, minval=0, maxval=500, group="Medie")
  29. maInputSlowShort = input(title="MA SlowShort", type=input.integer, defval=49, minval=0, maxval=500, group="Medie")
  30. maInputFastLong = input(title="MA FastLong", type=input.integer, defval=15, minval=0, maxval=500, group="Medie")
  31. maInputFastShort = input(title="MA FastShort", type=input.integer, defval=15, minval=0, maxval=500, group="Medie")
  32. smaInputMediumShort = input(title="SMA MediumShort", type=input.integer, defval=50, minval=0, maxval=500, group="Medie")
  33. maInputExitLong = input(title="MA Exit Long", type=input.integer, defval=43, minval=0, maxval=500, group="Medie")
  34. smaInputExitShort = input(title="SMA Exit Short", type=input.integer, defval=11, minval=0, maxval=500, group="Medie")
  35. maMinDiffLong = input(title="Distanza min medie Long", type=input.float, defval=8.31, step=0.01, group="Filtri Medie")
  36. maMinDiffShort = input(title="Distanza min medie Short", type=input.float, defval=7.23, step=0.01, group="Filtri Medie")
  37. hourTrading = input(title="Sessione valida di trading", type=input.string, defval="24x7", group="Sessioni")
  38. onlyLong = input(title="Solo long trade", type=input.bool, defval=false, inline="1", group="Direzione")
  39. onlyShort = input(title="Solo short trade", type=input.bool, defval=false, inline="1", group="Direzione")
  40.  
  41. // STEP 1 DATARANGE:
  42. // Make input options that configure backtest date range
  43.  
  44. startDate = input(title="Start Date", type=input.integer,
  45.      defval=01, minval=1, maxval=31, group="Periodo")
  46. startMonth = input(title="Start Month", type=input.integer,
  47.      defval=1, minval=1, maxval=12, group="Periodo")
  48. startYear = input(title="Start Year", type=input.integer,
  49.      defval=2000, minval=1800, maxval=2100, group="Periodo")
  50.  
  51.  
  52. endDate = input(title="End Date", type=input.integer,
  53.      defval=01, minval=1, maxval=31, group="Periodo")
  54. endMonth = input(title="End Month", type=input.integer,
  55.      defval=01, minval=1, maxval=12, group="Periodo")
  56. endYear = input(title="End Year", type=input.integer,
  57.      defval=2121, minval=1800, maxval=2100, group="Periodo")
  58.  
  59. // STEP 2 DATARANGE:
  60. // Look if the close time of the current bar
  61. // falls inside the date range
  62.  
  63. inDateRange = (time >= timestamp(syminfo.timezone, startYear,
  64.          startMonth, startDate, 0, 0)) and
  65.      (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
  66.      
  67. // Include or Exclude Days of the Week
  68. // Trade_long_on_dow_1 is Sunday/Domenica
  69. // Trade_long_on_dow_2 is Monday/Lunedì Etc...
  70. // If a position is opened it will be closed as srategy without excluding the month
  71.  
  72. trade_long_on_dow_1 = input(true, group="Include/Esclude Giorni Long")
  73. trade_long_on_dow_2 = input(true, group="Include/Esclude Giorni Long")
  74. trade_long_on_dow_3 = input(true, group="Include/Esclude Giorni Long")
  75. trade_long_on_dow_4 = input(true, group="Include/Esclude Giorni Long")
  76. trade_long_on_dow_5 = input(true, group="Include/Esclude Giorni Long")
  77. trade_long_on_dow_6 = input(true, group="Include/Esclude Giorni Long")
  78. trade_long_on_dow_7 = input(true, group="Include/Esclude Giorni Long")
  79.  
  80. trade_short_on_dow_1 = input(true, group="Include/Esclude Giorni Short")
  81. trade_short_on_dow_2 = input(true, group="Include/Esclude Giorni Short")
  82. trade_short_on_dow_3 = input(true, group="Include/Esclude Giorni Short")
  83. trade_short_on_dow_4 = input(true, group="Include/Esclude Giorni Short")
  84. trade_short_on_dow_5 = input(true, group="Include/Esclude Giorni Short")
  85. trade_short_on_dow_6 = input(true, group="Include/Esclude Giorni Short")
  86. trade_short_on_dow_7 = input(true, group="Include/Esclude Giorni Short")
  87.  
  88. 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)
  89. 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)
  90.  
  91. // Exit open market position when date range ends
  92.  
  93. //if (not trade_long_today)
  94. //    strategy.close_all()
  95.  
  96. //if (not trade_short_today)
  97. //    strategy.close_all()
  98.  
  99.  
  100. // Include or Exclude Moths of the Year
  101. // Trade_long_on_dow_1 is Sunday/Domenica
  102. // Trade_long_on_dow_2 is Monday/Lunedì Etc...
  103. // If a position is opened it will be closed as srategy without excluding the month
  104.  
  105. trade_long_on_dow_month_1 = input(true, group="Include/Esclude Mesi Long")
  106. trade_long_on_dow_month_2 = input(true, group="Include/Esclude Mesi Long")
  107. trade_long_on_dow_month_3 = input(true, group="Include/Esclude Mesi Long")
  108. trade_long_on_dow_month_4 = input(true, group="Include/Esclude Mesi Long")
  109. trade_long_on_dow_month_5 = input(true, group="Include/Esclude Mesi Long")
  110. trade_long_on_dow_month_6 = input(true, group="Include/Esclude Mesi Long")
  111. trade_long_on_dow_month_7 = input(true, group="Include/Esclude Mesi Long")
  112. trade_long_on_dow_month_8 = input(true, group="Include/Esclude Mesi Long")
  113. trade_long_on_dow_month_9 = input(true, group="Include/Esclude Mesi Long")
  114. trade_long_on_dow_month_10 = input(true, group="Include/Esclude Mesi Long")
  115. trade_long_on_dow_month_11 = input(true, group="Include/Esclude Mesi Long")
  116. trade_long_on_dow_month_12 = input(true, group="Include/Esclude Mesi Long")
  117.  
  118.  
  119. trade_short_on_dow_month_1 = input(true, group="Include/Esclude Mesi Short")
  120. trade_short_on_dow_month_2 = input(true, group="Include/Esclude Mesi Short")
  121. trade_short_on_dow_month_3 = input(true, group="Include/Esclude Mesi Short")
  122. trade_short_on_dow_month_4 = input(true, group="Include/Esclude Mesi Short")
  123. trade_short_on_dow_month_5 = input(true, group="Include/Esclude Mesi Short")
  124. trade_short_on_dow_month_6 = input(true, group="Include/Esclude Mesi Short")
  125. trade_short_on_dow_month_7 = input(true, group="Include/Esclude Mesi Short")
  126. trade_short_on_dow_month_8 = input(true, group="Include/Esclude Mesi Short")
  127. trade_short_on_dow_month_9 = input(true, group="Include/Esclude Mesi Short")
  128. trade_short_on_dow_month_10 = input(true, group="Include/Esclude Mesi Short")
  129. trade_short_on_dow_month_11 = input(true, group="Include/Esclude Mesi Short")
  130. trade_short_on_dow_month_12 = input(true, group="Include/Esclude Mesi Short")
  131.  
  132.  
  133. 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)
  134. 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)
  135.  
  136. // Exit open market position when date range ends
  137. //if (trade_long_this_month)
  138. //    strategy.close_all()
  139.  
  140. //if (trade_short_this_month)
  141. //    strategy.close_all()
  142.  
  143.  
  144. maSlowLong = ema(close, maInputSlowLong)
  145. maSlowShort = ema(close, maInputSlowShort)
  146. maFastLong = ema(close, maInputFastLong)
  147. maFastShort = ema(close, maInputFastShort)
  148. smaMediumShort = sma(close, smaInputMediumShort)
  149. maExitLong = ema(close, maInputExitLong)
  150. smaExitShort = sma(close, smaInputExitShort)
  151. rangeTrading = time(timeframe.period, hourTrading)
  152.  
  153.  
  154. //plot(maSlowLong, color=color.orange, title="maSlowLong")
  155. //plot(maSlowShort, color=color.green, title="maSlowShort")
  156. //plot(maFastLong, color=color.blue, title="maFastLong")
  157. //plot(maFastShort, color=color.purple, title="maFastShort")
  158. //plot(smaMediumShort, color=color.gray, title="smaMediumShort")
  159. //plot(maExitLong, color=color.black, title="maExitLong")
  160. //plot(smaExitShort, color=color.red, title="smaExitShort")
  161.  
  162. // 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ù
  163.  
  164. //if (close > maSlowShort)
  165.   //  if (smaExitShort > maSlowShort)
  166. //        if (strategy.position_size < 0)
  167.   //          strategy.close(id="short")
  168.  
  169.  
  170. //Condizione Entry Long: Chiusura candela sopra media lenta con differenziale medie e media veloce maggiore media ExitLong
  171. 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
  172. //Condizione Exit Long: Crossunder di due medie
  173. condExitLong = crossunder(maFastLong, maExitLong)
  174. //condExitLong = maFastLong < maExitLong
  175.  
  176.  
  177. // Condizione Entry Short: Crossunder di due medie con il prezzo di chiusura che è comunque sotto una terza media con differenziale delle medie
  178. 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
  179. // Condizione Exit Short: Crossover di medie con il prezzo di chiusura che è comunque sopra una terza media
  180. condExitShort = crossover(smaExitShort, smaMediumShort) and close > maSlowShort or (smaExitShort > maSlowShort) and close > maSlowShort
  181.  
  182.  
  183. //definizione variabili posizioni aperte
  184.  
  185. IsLongOpen = false
  186. IsLongOpen := condEntryLong[1] ? true : condExitLong[1] ? false : IsLongOpen[1]
  187.  
  188. IsShortOpen = false
  189. IsShortOpen := condEntryShort[1] ? true : condExitShort[1] ? false : IsShortOpen[1]
  190.  
  191. IsFlat = true
  192. IsFlat := not IsLongOpen and not IsShortOpen
  193.  
  194. //conversione bool -> float, per debug
  195.  
  196. IsLongOpenFloat = if IsLongOpen == true
  197.     1
  198. else
  199.     0
  200.  
  201. IsShortOpenFloat = if IsShortOpen == true
  202.     1
  203. else
  204.     0
  205.  
  206. IsFlatFloat = if IsFlat == true
  207.     1
  208. else
  209.     0
  210.  
  211. //plot posizioni aperte, per debug
  212.  
  213. //plot (IsLongOpenFloat)
  214. //plot (IsShortOpenFloat,color=color.yellow)
  215. //plot (IsFlatFloat,color=color.red)
  216.  
  217. //variabili apertura e chiusura posizione
  218.  
  219. OpenLong = condEntryLong and not IsLongOpen
  220. CloseLong = condExitLong and IsLongOpen
  221.  
  222. OpenShort = condEntryShort and not IsShortOpen
  223. CloseShort = condExitShort and IsShortOpen
  224.  
  225. //conversione bool -> float, per debug
  226.  
  227. OpenShortFloat = if OpenShort == true
  228.     1
  229. else
  230.     0
  231.  
  232. CloseShortFloat = if CloseShort == true
  233.     1
  234. else
  235.     0
  236.  
  237. OpenLongFloat = if OpenLong == true
  238.     1
  239. else
  240.     0
  241.  
  242. CloseLongFloat = if CloseLong == true
  243.     1
  244. else
  245.     0
  246.  
  247. //plot aperture ordini, per debug
  248.  
  249. plot (OpenShortFloat,color=color.red)
  250. plot (CloseShortFloat,color=color.yellow)
  251. plot (OpenLongFloat,color=color.blue)
  252. plot (CloseLongFloat,color=color.black)
  253.  
  254. //alert
  255.  
  256. alertcondition(OpenLong,title="Open Long")
  257. alertcondition(CloseLong,title="Close Long")
  258. alertcondition(OpenShort,title="Open Short")
  259. alertcondition(CloseShort,title="Close Short")
  260.  
  261.  
  262. //Entry/Exit Orders
  263.  
  264. //strategy.entry("long", true, when = condEntryLong)
  265. //strategy.close("long", when=condExitLong)
  266.  
  267. //strategy.entry("short", false, when = condEntryShort)
  268. //strategy.close("short", when=condExitShort)
  269.  
  270.  
  271.  
  272.  
  273. //SETTAGGI FTX FUTURE SOLO LONG:
  274. //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ù.
  275. //MaFastLong: 15 entra ed esce bene le altre più alte fanno meno operazioni ma entrano ed escono dopo.
  276. //Continuare ad usare i settaggi: MaSlowLong: 66.....MaFastLong: 15
  277.  
  278. //SETTAGGI ETH/USD FTX FUTURE IDENTICI A BITFINEX COME INGRESSI E USCITE ECCEZIONE MINIMA DELLA CANDELA SPORADICA LONG E SHORT:
  279. // maSlowLong 74
  280. // maSlowShort 49
  281. // maFastLong 15
  282. // maFastShort 15
  283. // smaMediumShort 50
  284. // maExitLong 43
  285. // maExitShort 11
  286. // Distanza min Medie Long 8.31
  287. // Distanza min Medie Short 7.23
Add Comment
Please, Sign In to add comment