Advertisement
Maurizio-Ciullo

Bot Weekly-Mean-Trend ETH/USDT.P BYBIT 4H LONG E SHORT

Mar 4th, 2023 (edited)
782
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. // No Repainting: https://www.youtube.com/watch?v=-ZvGC0wUNAI IN QUESTA STRATEGIA E' STATA USATA LA TECNICA NO REPAINTING
  5.  
  6. // Il trading system completo - Weekly Mean Trend ETH/USDT.P BYBIT 4H LONG E SHORT (Strategia Breakout)
  7. // (Sviluppo Dati Exchange = FTX)
  8. // (Exchange= BYBIT) (Sottostante ETH-USDT.P) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= NO) (Esclusione Ore=NO) (Esclusione Giorni=NO) (Esclusione Mesi=NO)
  9. // (Take Profit Long/Short Market = NO) (Take Profit Limit Long/Short= NO)
  10. // (Stop Loss Limit Long= NO) (Stop Loss Limit Short= NO) (Stop Loss Market Long/Short= SI) (Trailing Stop=SI) (Stop Emergenza= NO)
  11. // (Rischio Operazione 2% Perdita Media) (Max Drawdown Permesso 10,94%)
  12. // (In Sample Dal=18/08/17 Al 30/11/21) (Out Of Sample Dal=30/11/21 Al 31/01/22)
  13. // (Progettatta Il=31/01/22)
  14.  
  15.  
  16. //@version=5
  17. strategy(title='Bot Weekly-Mean-Trend ETH/USDT.P BYBIT 4H LONG E SHORT', overlay=true,
  18.      pyramiding=0,
  19.      initial_capital=1000,
  20.      commission_type=strategy.commission.percent,
  21.      commission_value=0.1,
  22.      slippage=3,
  23.      default_qty_type=strategy.percent_of_equity,
  24.      default_qty_value=34)
  25.  
  26.  
  27. // >>>>>>>>>>>>>> Start Detecting Inputs <<<<<<<<<<<<<<<<<
  28.  
  29. swt = input(true, title='Show This Weeks OHLC?')
  30. input_media_W_high = input.int(title='media_W_high', defval=10, minval=1, maxval=500, group='Medie_Settimanali')
  31. input_media_W_low = input.int(title='media_W_low', defval=19, minval=1, maxval=500, group='Medie_Settimanali')
  32. input_media_W_high_smoothed = input.int(title='media_W_high_smoothed', defval=38, minval=1, maxval=500, group='Medie_Settimanali_Smoothed')
  33. input_media_W_low_smoothed = input.int(title='media_W_low_smoothed', defval=38, minval=1, maxval=500, group='Medie_Settimanali_Smoothed')
  34. onlyLong = input.bool(title='Solo long trade', defval=false, inline='1', group='Direzione')
  35. onlyShort = input.bool(title='Solo short trade', defval=false, inline='1', group='Direzione')
  36. in_atr_period_long = input.int(title='Periodicità ATR long', minval=5, maxval=500, defval=14, step=1)
  37. in_atr_period_short = input.int(title='Periodicità ATR short', minval=5, maxval=500, defval=9, step=1)
  38. in_atr_mult_long = input.float(title='atr_Mult_long', minval=0.1, maxval=5, defval=0.75, step=0.5)
  39. in_atr_mult_short = input.float(title='atr_Mult_short', minval=0.1, maxval=5, defval=1, step=0.5)
  40. dmi_min_diff_input_long = input.float(title='Distanza min dim_long', minval=-100, maxval=1000000, defval=18, step=0.1)
  41. dmi_min_diff_input_short = input.float(title='Distanza min dim_short', minval=-100, maxval=1000000, defval=25, step=0.1)
  42.  
  43. // >>>>>>>>>>>>>> End Detecting Inputs <<<<<<<<<<<<<<<<<
  44.  
  45.  
  46. // >>>>>>>>>>>>>> Start Detecting Calcolo del range del backtest <<<<<<<<<<<<<<<<<
  47.  
  48. startDate = input.int(title='Start Date', defval=17, minval=1, maxval=31, group='Periodo')
  49. startMonth = input.int(title='Start Month', defval=08, minval=1, maxval=12, group='Periodo')
  50. startYear = input.int(title='Start Year', defval=2000, minval=1800, maxval=2100, group='Periodo')
  51.  
  52. endDate = input.int(title='End Date', defval=01, minval=1, maxval=31, group='Periodo')
  53. endMonth = input.int(title='End Month', defval=01, minval=1, maxval=12, group='Periodo')
  54. endYear = input.int(title='End Year', defval=2121, minval=1800, maxval=2150, group='Periodo')
  55.  
  56. inDateRange = time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0) and time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0)
  57.  
  58. // >>>>>>>>>>>>>> End Detecting Calcolo del range del backtest <<<<<<<<<<<<<<<<<
  59.  
  60.  
  61. // >>>>>>>>>>>>>> Start Detecting Hour Range Trading <<<<<<<<<<<<<<<<<
  62.  
  63. // hourTrading = input(title='sessione valida di trading', defval='0600-2300:23456')
  64. // hourRangeTrading = time(timeframe.period, hourTrading)
  65.  
  66. // >>>>>>>>>>>>>> EndDetecting Hour Range Trading <<<<<<<<<<<<<<<<<
  67.  
  68.  
  69. // >>>>>>>>>>>>>> Start Detecting Weekly Highs/Lows <<<<<<<<<<<<<<<<<
  70.  
  71. wtdo = request.security(syminfo.tickerid, 'W', open) //[barstate.isconfirmed ? 0:1]
  72. wpdo = request.security(syminfo.tickerid, 'W', open[1])
  73. wpc = request.security(syminfo.tickerid, 'W', close)
  74. wpdc = request.security(syminfo.tickerid, 'W', close[1])
  75. wph = request.security(syminfo.tickerid, 'W', high[barstate.isconfirmed ? 0:1]) // NO REPAINTING [barstate.isconfirmed ? 0:1]
  76. wpdh = request.security(syminfo.tickerid, 'W', high[1])
  77. wpl = request.security(syminfo.tickerid, 'W', low[barstate.isconfirmed ? 0:1])  // NO REPAINTING [barstate.isconfirmed ? 0:1]
  78. wpdl = request.security(syminfo.tickerid, 'W', low[1])
  79.  
  80. // Weekly Plots
  81. // plot(swt and wtdo ? wtdo : na, title="Weekly Open", style=plot.style_circles, linewidth=1, color=color.silver)
  82. // plot(swt and wpc ? wpc : na, title="Weekly Close", style=plot.style_circles, linewidth=1, color=color.fuchsia)
  83. // plot(swt and wph ? wph : na, title="Weekly High", style=plot.style_circles, linewidth=1, color=color.green)
  84. // plot(swt and wpl ? wpl : na, title="Weekly Low", style=plot.style_circles, linewidth=1, color=color.red)
  85.  
  86. // >>>>>>>>>>>>>> End Detecting Weekly Highs/Lows <<<<<<<<<<<<<<<<<
  87.  
  88.  
  89. // >>>>>>>>>>>>>> Start Detecting Variables <<<<<<<<<<<<<<<<<
  90.  
  91. media_high = ta.sma(wph, input_media_W_high)
  92. plot(close > media_high ? media_high : close < media_high ? media_high : na, color=close > media_high ? color.green : close < media_high ? color.gray :na, linewidth=1, title='media_high', style=plot.style_cross)
  93. media_low = ta.sma(wpl, input_media_W_low)
  94. plot(close < media_low ? media_low : close > media_low ? media_low : na, color=close < media_low ? color.red : close > media_low ? color.gray :na, linewidth=1, title='media_low', style=plot.style_cross)
  95.  
  96. media_high_smoothed = ta.sma(wph, input_media_W_high_smoothed)
  97. plot(media_high_smoothed, color=color.new(color.green, 0), linewidth=1, title='media_high_smoothed')
  98. media_low_smoothed = ta.sma(wpl, input_media_W_low_smoothed)
  99. plot(media_low_smoothed, color=color.new(color.red, 0), linewidth=1, title='media_low_smoothed')
  100.  
  101. // >>>>>>>>>>>>>> End Detecting Variables <<<<<<<<<<<<<<<<<
  102.  
  103.  
  104. // >>>>>>>>>>>>>> Start Detecting Atr Long E Short <<<<<<<<<<<<<<<<<
  105. // Per il calcolo dell'indicatore è stato richiamato l'indicatore originale e l'indicatore di 1na barra fa per non avere repainting usando l'operatore ternario
  106.  
  107. valore_atr_long_origin =  ta.atr(in_atr_period_long)
  108. valore_atr_long1 = valore_atr_long_origin[1]
  109. valore_atr_long =  barstate.isconfirmed ? valore_atr_long_origin : valore_atr_long1   // NO REPAINTING
  110.  
  111. atr_per_mult_long = valore_atr_long * in_atr_mult_long
  112. plot(atr_per_mult_long, title='atr_mult_long')
  113.  
  114.  
  115. valore_atr_short_origin = ta.atr(in_atr_period_short)
  116. valore_atr_short1 = valore_atr_short_origin[1]
  117. valore_atr_short =  barstate.isconfirmed ? valore_atr_short_origin : valore_atr_short1 // NO REPAINTING
  118.  
  119. atr_per_mult_short = valore_atr_short * in_atr_mult_short
  120. plot(atr_per_mult_short, title='atr_mult_short')
  121.  
  122. // >>>>>>>>>>>>>> End Detecting Atr Long E Short <<<<<<<<<<<<<<<<<
  123.  
  124.  
  125. // >>>>>>>>>>>>>> Start Detecting DMI <<<<<<<<<<<<<<<<<
  126.  
  127. // Filtro voglio che sia true se il +DI è sopra sia il -DI ed anche dell'ADX //21,3587
  128. // Per il calcolo dell'indicatore è stato richiamato l'indicatore originale e l'indicatore di 1na barra fa per non avere repainting usando l'operatore ternario
  129.  
  130. //[di_pos, di_neg, adx] = ta.dmi(14, 14)
  131. [di_pos_origin, di_neg_origin, adx] = ta.dmi(14, 14)
  132.  
  133. di_pos1 = di_pos_origin[1]
  134. di_neg1 = di_neg_origin[1]
  135.  
  136. di_pos = barstate.isconfirmed ? di_pos_origin : di_pos1
  137. di_neg = barstate.isconfirmed ? di_neg_origin : di_neg1
  138.  
  139.  
  140. //dmi_total_green_cross = di_pos > di_neg and di_pos > adx? true : false
  141. //dmi_total_green_cross = (di_pos - di_neg) > dma_min_diff_input ? true : false
  142. //dmi_total_green_cross = (di_neg - di_pos) > dmi_min_diff_input ? true : false
  143. //dmi_total_green_cross_short = (di_neg > adx + dmi_min_diff_input_short)  ? true : false
  144. //dmi_total_green_cross_long = (di_pos > adx + dmi_min_diff_input_long)  ? true : false
  145. //dmi_total_green_cross = (di_pos - di_neg) < dmi_min_diff_input  ? true : false
  146.  
  147. //plot(dmi_total_green_cross ? 1 : 0, title="dmi_total_green_cross")
  148.  
  149. // >>>>>>>>>>>>>> End Detecting DMI <<<<<<<<<<<<<<<<<
  150.  
  151.  
  152. // >>>>>>>>>>>>>> Start Plot Ingresso In Posizione <<<<<<<<<<<<<<<<<
  153.  
  154. 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
  155. 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
  156.  
  157. // >>>>>>>>>>>>>> End Plot Ingresso In Posizione <<<<<<<<<<<<<<<<<
  158.  
  159.  
  160. // >>>>>>>>>>>>>> Start Detecting No Trading Allowed <<<<<<<<<<<<<<<<<
  161.  
  162. // Plot No Trading Allowed giorni da 1 a 7 1 è domenica. Mesi da 1 a 12 1 è Gennaio.
  163.  
  164. //giorni_esclusi = dayofweek(time)
  165. //plotshape(giorni_esclusi[1] == 2 ? giorni_esclusi : na, color=color.green, title="giorni_esclusi")
  166. //mesi_esclusi = month(time)
  167. //plotshape(mesi_esclusi[1] == 9 ? mesi_esclusi : na, color=color.yellow, title="mesi_esclusi")
  168.  
  169. // >>>>>>>>>>>>>> Start Detecting No Trading Allowed <<<<<<<<<<<<<<<<<
  170.  
  171.  
  172. // >>>>>>>>>>>>>> Start Entry/Exit Conditions <<<<<<<<<<<<<<<<<
  173.  
  174. // Condizione Entry Long: Crossover chiusura media e media maggiore altra media
  175. condEntryLong = ta.crossover(close, media_high) and math.abs(open - close) > valore_atr_long * in_atr_mult_long and close > open and di_pos > dmi_min_diff_input_long and not onlyShort and inDateRange
  176. // Condizione Exit Long: Crossunder chiusura media
  177. condExitLong = ta.crossunder(close, media_high)
  178.  
  179. // Condizione Entry Short: Crossounder chiusura media e media minore altra media
  180. condEntryShort = ta.crossunder(close, media_low) and math.abs(open - close) > valore_atr_short * in_atr_mult_short and close < open and di_neg > dmi_min_diff_input_short and not onlyLong and inDateRange
  181. // Condizione Exit Short: Crossover chiusura media
  182. condExitShort = ta.crossover(close, media_low)
  183.  
  184. // >>>>>>>>>>>>>> End Entry/Exit Conditions <<<<<<<<<<<<<<<<<
  185.  
  186.  
  187. // >>>>>>>>>>>>>> Start Buy/Sell/Close Command <<<<<<<<<<<<<<<<<
  188.  
  189. buy_command = 'buy_command '
  190. sell_command = 'sell_command '
  191. close_command = 'close_command '
  192. // cancel_command = 'cancel_command'
  193.  
  194. // >>>>>>>>>>>>>> End Buy/Sell/Close Command <<<<<<<<<<<<<<<<<
  195.  
  196.  
  197. // >>>>>>>>>>>>>> Start Execute Long And Short Open/Close Positions <<<<<<<<<<<<<<<<<
  198.  
  199. if condEntryLong
  200.     strategy.entry('long', strategy.long, alert_message = "Open Long Position", comment = buy_command)
  201.  
  202. if condExitLong
  203.     strategy.close(id='long', alert_message = "Close Long Position", comment = close_command)
  204.  
  205. if condEntryShort
  206.     strategy.entry('short', strategy.short, alert_message = "Open Short position", comment = sell_command)
  207.  
  208. if condExitShort
  209.     strategy.close(id='short', alert_message = "Close Short Position", comment = close_command)
  210.  
  211. // >>>>>>>>>>>>>> End Execute Long And Short Open/Close Positions <<<<<<<<<<<<<<<<<
  212.  
  213. // Nome Alert: Weekly-Mean-Trend ETH/USDT.P BYBIT 4H
  214. // Commento Alert: {{strategy.order.comment}}
  215.  
  216.  
  217. // ----------------- Inizio Tabella risultati mensili. Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick -----------------
  218.  
  219. // new_month = month(time) != month(time[1])
  220. // new_year  = year(time)  != year(time[1])
  221.  
  222. // eq = strategy.equity
  223.  
  224. // bar_pnl = eq / eq[1] - 1
  225.  
  226. // cur_month_pnl = 0.0
  227. // cur_year_pnl  = 0.0
  228.  
  229. // // Current Monthly P&L
  230. // cur_month_pnl := new_month ? 0.0 :
  231. //                  (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
  232.  
  233. // // Current Yearly P&L
  234. // cur_year_pnl := new_year ? 0.0 :
  235. //                  (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1  
  236.  
  237. // // Arrays to store Yearly and Monthly P&Ls
  238. // var month_pnl  = array.new_float(0)
  239. // var month_time = array.new_int(0)
  240.  
  241. // var year_pnl  = array.new_float(0)
  242. // var year_time = array.new_int(0)
  243.  
  244. // last_computed = false
  245.  
  246. // if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
  247. //     if (last_computed[1])
  248. //         array.pop(month_pnl)
  249. //         array.pop(month_time)
  250.  
  251. //     array.push(month_pnl , cur_month_pnl[1])
  252. //     array.push(month_time, time[1])
  253.  
  254. // if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
  255. //     if (last_computed[1])
  256. //         array.pop(year_pnl)
  257. //         array.pop(year_time)
  258.  
  259. //     array.push(year_pnl , cur_year_pnl[1])
  260. //     array.push(year_time, time[1])
  261.  
  262. // last_computed := barstate.islast ? true : nz(last_computed[1])
  263.  
  264. // // Monthly P&L Table    
  265. // var monthly_table = table(na)
  266. // prec      = input(2, title = "Return Precision")
  267.  
  268. // if (barstate.islast)
  269. //     monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_width=1,border_color=#000000)
  270.  
  271. //     table.cell(monthly_table, 0,  0, "",     text_color=#D3D3D3, bgcolor=#0F0F0F)
  272. //     table.cell(monthly_table, 1,  0, "Jan",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  273. //     table.cell(monthly_table, 2,  0, "Feb",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  274. //     table.cell(monthly_table, 3,  0, "Mar",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  275. //     table.cell(monthly_table, 4,  0, "Apr",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  276. //     table.cell(monthly_table, 5,  0, "May",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  277. //     table.cell(monthly_table, 6,  0, "Jun",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  278. //     table.cell(monthly_table, 7,  0, "Jul",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  279. //     table.cell(monthly_table, 8,  0, "Aug",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  280. //     table.cell(monthly_table, 9,  0, "Sep",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  281. //     table.cell(monthly_table, 10, 0, "Oct",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  282. //     table.cell(monthly_table, 11, 0, "Nov",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  283. //     table.cell(monthly_table, 12, 0, "Dec",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  284. //     table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
  285.  
  286.  
  287. //     for yi = 0 to array.size(year_pnl) - 1
  288. //         table.cell(monthly_table, 0,  yi + 1, str.tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
  289.  
  290. //         y_color = array.get(year_pnl, yi) > 0 ? color.lime : color.red
  291. //         table.cell(monthly_table, 13, yi + 1, str.tostring(math.round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
  292.  
  293. //     for mi = 0 to array.size(month_time) - 1
  294. //         m_row   = year(array.get(month_time, mi))  - year(array.get(year_time, 0)) + 1
  295. //         m_col   = month(array.get(month_time, mi))
  296. //         m_color = array.get(month_pnl, mi) > 0 ? color.lime : color.red
  297.  
  298. //         table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
  299.  
  300. // ----------------- Fine Tabella risultati mensili. Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick -----------------
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement