Advertisement
Maurizio-Ciullo

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

Jan 13th, 2022 (edited)
201
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 Ver-5 (Strategia Breakout) - parte 2
  4. // (Exchange= FTX) (Sottostante ETH-PERP) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= SI) (Esclusione Ore=NO) (Esclusione Giorni=LUNEDI') (Esclusione Mesi=SETTEMBRE)
  5. // (Take Profit Long/Short Market = Non Definito) (Take Profit Limit Long/Short= +20% Tradingview-Hub) (Stop Loss Limit Long/Short= -11% Tradingview-Hub) (Stop Loss Market Long/Short= -11%) (Stop Emergenza= No)
  6. //@version=5
  7. //1. Strategia
  8. strategy(title='Bot Break-in-New Ver-5 ETH/PERP FTX 4H LONG E SHORT', overlay=true,
  9.      pyramiding=0,
  10.      initial_capital=150,
  11.      commission_type=strategy.commission.percent,
  12.      commission_value=0.1,
  13.      slippage=3,
  14.      default_qty_type=strategy.percent_of_equity,
  15.      default_qty_value=27.5)
  16.  
  17.  
  18. //input SL e TP Long e Short
  19. input_stop_loss_long = input.float(title='stop_loss_long', defval=11, minval=0, maxval=100, step=0.1, group='Stop')
  20. input_take_profit_long = input.float(title='take_profit_long', defval=20, minval=0, maxval=100, step=0.1, group='Stop')
  21. input_stop_loss_short = input.float(title='stop_loss_short', defval=11, minval=0, maxval=100, step=0.1, group='Stop')
  22. input_take_profit_short = input.float(title='take_profit_short', defval=20, minval=0, maxval=100, step=0.1, group='Stop')
  23. input_lunghezza_ema = input.int(title='lunghezza ema', defval=125, minval=0, maxval=300, step=5)
  24.  
  25. riskPerTrade = input.float(title='Risk Per Trade %', minval=0.1, maxval=100, defval=27.5, step=0.5)
  26.  
  27.  
  28. length = input.int(title="Length", defval=10, minval=1)
  29. smoothing = input.string(title="Smoothing", defval="SMA", options=["RMA", "SMA", "EMA", "WMA"])
  30. ma_function(source, length) =>
  31.     switch smoothing
  32.         "RMA" => ta.rma(source, length)
  33.         "SMA" => ta.sma(source, length)
  34.         "EMA" => ta.ema(source, length)
  35.         => ta.wma(source, length)
  36. plot(ma_function(ta.tr(true), length), title = "ATR", color=color.new(#B71C1C, 0))
  37.  
  38.  
  39. in_atr_Mult = input.float(title='atr_Mult', minval=1, maxval=5, defval=1, step=0.5)
  40. //in_tpPips = input(title="TP", type=input.integer, defval=40)
  41. //in_slPips = input(title="SL", type=input.integer, defval=30)
  42.  
  43. //Input solo long o solo short
  44. in_solo_long = input.bool(title='Solo long', defval=false, inline='1', group='Direzione')
  45. in_solo_short = input.bool(title='Solo short', defval=false, inline='1', group='Direzione')
  46.  
  47. atr = ma_function(ta.tr(true), length)
  48. media = ta.ema(close, input_lunghezza_ema)
  49.  
  50. plot(media, title = "media", color=color.blue)
  51.  
  52. hourTrading = input(title='sessione valida di trading', defval='0600-2300:23456')
  53.  
  54. rangeTrading = time(timeframe.period, hourTrading)
  55.  
  56.  
  57. //calcolo della size per lo SL e TP
  58. size = strategy.equity * riskPerTrade / 100
  59. nr_contratti = size / close
  60. stop_loss_long = math.round(size / syminfo.mintick / 100 * input_stop_loss_long) / nr_contratti
  61. take_profit_long = math.round(size / syminfo.mintick / 100 * input_take_profit_long) / nr_contratti
  62. stop_loss_short = math.round(size / syminfo.mintick / 100 * input_stop_loss_short) / nr_contratti
  63. take_profit_short = math.round(size / syminfo.mintick / 100 * input_take_profit_short) / nr_contratti
  64.  
  65.  
  66. 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
  67. plot(strategy.position_size > 0 ?  strategy.position_avg_price + take_profit_long * 0.01 * 10: strategy.position_size < 0 ? strategy.position_avg_price - take_profit_short * 0.01 * 10: na, color=color.green, style=plot.style_cross, linewidth=2, title="tk_limit")
  68. plot(strategy.position_size > 0 ?  strategy.position_avg_price - stop_loss_long * 0.01 * 10: strategy.position_size < 0 ? strategy.position_avg_price + stop_loss_short * 0.01 * 10 : na, color=color.red, style=plot.style_cross, linewidth=2, title="sl_limit")
  69.  
  70. 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
  71.  
  72. // giorni da 1 a 7 1 Γ¨ domenica
  73.  
  74. //Condizione Long
  75. cond_long = math.abs(open - close) > atr * in_atr_Mult and close > open and close > media and month != 9 and dayofweek != 2 and not in_solo_short
  76.  
  77. //Condizione Short
  78. cond_short = math.abs(open - close) > atr * in_atr_Mult and close < open and  close < media and month != 9 and dayofweek != 2 and not in_solo_long
  79.  
  80.  
  81.  
  82. //entrata e uscita Long
  83. if cond_long// and strategy.opentrades == 0
  84.     strategy.entry('operazioneLong', strategy.long)
  85.     strategy.exit('SL e TP', from_entry='operazioneLong', loss=stop_loss_long, profit=take_profit_long)
  86.    
  87. //entrata e uscita Short
  88. if cond_short// and strategy.opentrades == 0
  89.     strategy.entry('operazioneShort', strategy.short)
  90.     strategy.exit('SL e TP', from_entry='operazioneShort', loss=stop_loss_short, profit=take_profit_short)
  91.  
  92. // Emergency!!!
  93.  
  94. lastEntryPrice = strategy.opentrades.entry_price(0)
  95.  
  96. //chiusura di sicurezza Long
  97. prezzo_uscita_emergenza_long = lastEntryPrice - math.round(size / 100 * input_stop_loss_long) / nr_contratti
  98.  
  99. plot(prezzo_uscita_emergenza_long,color=color.blue,linewidth=3, title="Stop_Emerg_Long")
  100.  
  101. condExitLong = close < prezzo_uscita_emergenza_long
  102.  
  103. if strategy.opentrades ==1 and condExitLong
  104.     strategy.close(id = 'operazioneLong')
  105.  
  106. barcolor(cond_long ? color.lime : cond_short ? color.purple : na)
  107.  
  108. //chiusura di sicurezza Short
  109.  
  110. //plot(lastEntryPrice, color=color.fuchsia, style=plot.style_circles, linewidth=3, title="Last entry price")
  111.  
  112. prezzo_uscita_emergenza_short = lastEntryPrice + math.round(size / 100 * input_stop_loss_short) / nr_contratti
  113.  
  114. plot(prezzo_uscita_emergenza_short,color=color.red,linewidth=3, title="Stop_Emerg_Short")
  115.  
  116. condExitShort = close > prezzo_uscita_emergenza_short
  117.  
  118. if strategy.opentrades ==1 and condExitShort
  119.     strategy.close(id = 'operazioneShort')
  120.    
  121. // Tabella risultati mensili by QuantNomad (TSC Boilerplate) Per visualizzare andare nelle impostazioni proprietΓ  e spuntare ad ogni tick
  122.  
  123. new_month = month(time) != month(time[1])
  124. new_year  = year(time)  != year(time[1])
  125.  
  126. eq = strategy.equity
  127.  
  128. bar_pnl = eq / eq[1] - 1
  129.  
  130. cur_month_pnl = 0.0
  131. cur_year_pnl  = 0.0
  132.  
  133. // Current Monthly P&L
  134. cur_month_pnl := new_month ? 0.0 :
  135.                  (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
  136.  
  137. // Current Yearly P&L
  138. cur_year_pnl := new_year ? 0.0 :
  139.                  (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1  
  140.  
  141. // Arrays to store Yearly and Monthly P&Ls
  142. var month_pnl  = array.new_float(0)
  143. var month_time = array.new_int(0)
  144.  
  145. var year_pnl  = array.new_float(0)
  146. var year_time = array.new_int(0)
  147.  
  148. last_computed = false
  149.  
  150. if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
  151.     if (last_computed[1])
  152.         array.pop(month_pnl)
  153.         array.pop(month_time)
  154.        
  155.     array.push(month_pnl , cur_month_pnl[1])
  156.     array.push(month_time, time[1])
  157.  
  158. if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
  159.     if (last_computed[1])
  160.         array.pop(year_pnl)
  161.         array.pop(year_time)
  162.        
  163.     array.push(year_pnl , cur_year_pnl[1])
  164.     array.push(year_time, time[1])
  165.  
  166. last_computed := barstate.islast ? true : nz(last_computed[1])
  167.  
  168. // Monthly P&L Table    
  169. var monthly_table = table(na)
  170. prec      = input(2, title = "Return Precision")
  171.  
  172. if (barstate.islast)
  173.     monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_width=1,border_color=#000000)
  174.  
  175.     table.cell(monthly_table, 0,  0, "",     text_color=#D3D3D3, bgcolor=#0F0F0F)
  176.     table.cell(monthly_table, 1,  0, "Jan",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  177.     table.cell(monthly_table, 2,  0, "Feb",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  178.     table.cell(monthly_table, 3,  0, "Mar",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  179.     table.cell(monthly_table, 4,  0, "Apr",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  180.     table.cell(monthly_table, 5,  0, "May",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  181.     table.cell(monthly_table, 6,  0, "Jun",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  182.     table.cell(monthly_table, 7,  0, "Jul",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  183.     table.cell(monthly_table, 8,  0, "Aug",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  184.     table.cell(monthly_table, 9,  0, "Sep",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  185.     table.cell(monthly_table, 10, 0, "Oct",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  186.     table.cell(monthly_table, 11, 0, "Nov",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  187.     table.cell(monthly_table, 12, 0, "Dec",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  188.     table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
  189.  
  190.  
  191.     for yi = 0 to array.size(year_pnl) - 1
  192.         table.cell(monthly_table, 0,  yi + 1, str.tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
  193.        
  194.         y_color = array.get(year_pnl, yi) > 0 ? color.lime : color.red
  195.         table.cell(monthly_table, 13, yi + 1, str.tostring(math.round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
  196.        
  197.     for mi = 0 to array.size(month_time) - 1
  198.         m_row   = year(array.get(month_time, mi))  - year(array.get(year_time, 0)) + 1
  199.         m_col   = month(array.get(month_time, mi))
  200.         m_color = array.get(month_pnl, mi) > 0 ? color.lime : color.red
  201.        
  202.         table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement