Advertisement
Maurizio-Ciullo

SniperTrend Vittorio The Quant BTC/PERP 15/30 MIN

Nov 23rd, 2021 (edited)
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Name: SniperTrend
  2.  
  3. //Category: Trend Follower.
  4.  
  5. //Operating mode: futures position long and short
  6.  
  7. //Trades duration: less than a day
  8.  
  9. //Exchange: Binance Future
  10.  
  11. //Pair: BTC/Therer US
  12.  
  13. //Strumento: Future Perpetual
  14.  
  15. //Timeframe: 30M/15M chart to find the best entry price.
  16.  
  17. //Suggested usage: in trend.
  18.  
  19. // Appuntarsi le configurazioni
  20. // BINANCE:BTCUSDTPERP 15M 200 75 200 75 open 70 true true 24x7 close close true 8 4 true 16 7.5 true 16 8 236 237
  21. // BINANCE:ETHUSDTPERP 15M 100 16 100 8 open 10 true true 24x7 close close false 14 4 true 2 5.5 | 3 |  1 2 5
  22. // BINANCE:BTCUSDT  30M 100 75 open 100 true false true 8 4.5 | 2 3 5 6
  23.  
  24. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  25. // © Maurizio-Ciullo
  26.  
  27. // This work is inspired from a strategy find in Quant Trader Academy, modified by TheSocialCryptoClub is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License https://creativecommons.org/licenses/by-nc-sa/4.0/
  28. //@version=4
  29.  
  30. // Definizione della strategia
  31.  
  32. //ATTENZIONE: PER VISUALIZZARE CORRETTAMENTE LA TABELLA E' NECESSARIO SPUNTARE LA VOCE "On Every Tick" DAL SETTING DELLA STRATEGIA
  33.  
  34. strategy(title="SniperTrend v1.0.2",
  35.      shorttitle="ST",
  36.      overlay=true,
  37.      initial_capital=100,
  38.      pyramiding=0,
  39.      default_qty_type=strategy.percent_of_equity,
  40.      default_qty_value=100,
  41.      commission_type=strategy.commission.percent,
  42.      commission_value=0.025,
  43.      slippage=3)
  44.  
  45. // Date per il backtesting by khalidsalomao
  46.  
  47. // Date per il backtesting
  48. fromYear  = input(defval = 2019, title = "Backtesting - Start Year")      
  49. fromMonth = input(defval = 1,    title = "Backtesting - Star Month")    
  50. fromDay   = input(defval = 1,    title = "Backtesting - Star Day")  
  51. thruYear  = input(defval = 2112, title = "Backtesting - End Year")      
  52. thruMonth = input(defval = 1,    title = "Backtesting - End Month")    
  53. thruDay   = input(defval = 1,    title = "Backtesting - End Day")    
  54.    
  55. Start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // inizio backtesting
  56. Finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // fine backtesting
  57. RangeBacktest()  => time >= Start and time <= Finish ? true : false       // funzione che restituisce vero/falso se siamo in range
  58.  
  59. // Variabili e funzioni utili
  60. PeriodoMediaLentaShort=input(defval=200, title="Periodo Media Lenta Short", type=input.integer)
  61. PeriodoMediaVeloceShort=input(defval=75, title="Periodo Media Veloce Short", type=input.integer)
  62.  
  63. PeriodoMediaLentaLong=input(defval=200, title="Periodo Media Lenta Long", type=input.integer)
  64. PeriodoMediaVeloceLong=input(defval=75, title="Periodo Media Veloce Long", type=input.integer)
  65.  
  66. SourceMedie=input(defval=open, title="Sorgente Medie", type=input.source)
  67. DifferenzaMedie=input(defval=70, title="Differenza tra le medie", type=input.integer)
  68.  
  69. TradaLong=input(defval=true, title="Attiva i Long", type=input.bool)
  70. TradaShort=input(defval=true, title="Attiva gli Short", type=input.bool)
  71.  
  72. InputRangeTrading=input(defval="24x7", title="Sessione di Trading", type=input.string)
  73.  
  74. SorgenteEntrataLong=input(defval=close, title="Sorgente Entrata Long", type=input.source)
  75. SorgenteEntrataShort=input(defval=close, title="Sorgente Entrata Short", type=input.source)
  76.  
  77. LongSLTP = input(defval=true, title="Attiva SL e TP Long", type=input.bool)
  78. LongLimit = input(defval=8, title="Long TP in %", type=input.float, step=0.5)
  79. LongStop = input(defval=4, title="Long SL in %", type=input.float, step=0.5)
  80.  
  81. ShortSLTP = input(defval=true, title="Attiva SL e TP Short", type=input.bool)
  82. ShortLimit = input(defval=16, title="Short TP in %", type=input.float, step=0.5)
  83. ShortStop = input(defval=8, title="Short SL in %", type=input.float, step=0.5)
  84.  
  85.  
  86. trade_long_on_dow_1 = input(true)
  87. trade_long_on_dow_2 = input(true)
  88. trade_long_on_dow_3 = input(true)
  89. trade_long_on_dow_4 = input(true)
  90. trade_long_on_dow_5 = input(true)
  91. trade_long_on_dow_6 = input(true)
  92. trade_long_on_dow_7 = input(true)
  93.  
  94. trade_short_on_dow_1 = input(true)
  95. trade_short_on_dow_2 = input(true)
  96. trade_short_on_dow_3 = input(true)
  97. trade_short_on_dow_4 = input(true)
  98. trade_short_on_dow_5 = input(true)
  99. trade_short_on_dow_6 = input(true)
  100. trade_short_on_dow_7 = input(true)
  101.  
  102.  
  103. 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_5 and dayofweek == 5)or (trade_long_on_dow_6 and dayofweek == 6) or (trade_long_on_dow_7 and dayofweek == 7)
  104. 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_5 and dayofweek == 5)or (trade_short_on_dow_6 and dayofweek == 6) or (trade_short_on_dow_7 and dayofweek == 7)
  105.  
  106.  
  107. // Definizione giorni della settimana e ore di trading
  108. RangeTrading=time(resolution=timeframe.period, session=InputRangeTrading)
  109.  
  110. // Definizione degli inidicatori per gli ingressi
  111. MediaLentaLong=ema(SourceMedie, PeriodoMediaLentaLong)
  112. MediaVeloceLong=ema(SourceMedie, PeriodoMediaVeloceLong)
  113.  
  114. MediaLentaShort=ema(SourceMedie, PeriodoMediaLentaShort)
  115. MediaVeloceShort=ema(SourceMedie, PeriodoMediaVeloceShort)
  116.  
  117.  
  118. // Condizioni di ingresso e uscita long
  119. CondizioneEntrataLong= crossover(SorgenteEntrataLong, MediaLentaLong) and (MediaVeloceLong-MediaLentaLong)>DifferenzaMedie and RangeTrading and TradaLong and RangeBacktest() and trade_long_today
  120. CondizioneUscitaLong = crossunder(MediaVeloceLong, MediaLentaLong)// and (MediaVeloceLong-MediaLentaLong)<DifferenzaMedie
  121.  
  122.  
  123. // Condizioni di ingresso e uscita short
  124. CondizioneEntrataShort= crossunder(SorgenteEntrataShort, MediaLentaShort) and (MediaLentaShort-MediaVeloceShort)>DifferenzaMedie and RangeTrading and TradaShort and RangeBacktest() and trade_short_today
  125. CondizioneUscitaShort= crossover(SorgenteEntrataShort, MediaVeloceShort) // and (MediaVeloceLong-MediaLentaLong)<DifferenzaMedie
  126.  
  127. // Ingressi e uscite long
  128. strategy.entry(id="Entrata Long", long=true, when=CondizioneEntrataLong) // ingresso originale long
  129. strategy.close(id="Entrata Long", when=CondizioneUscitaLong, comment="Condizione Uscita Long") // uscita originale long
  130. strategy.close(id="Entrata Long", when=strategy.position_size > 0 and CondizioneEntrataShort, comment="Reverse della Posizione Long") // uscita long per reverse della posizione (si usa solo se si va solo long)
  131.  
  132. LongLimitPrice = strategy.position_avg_price+(strategy.position_avg_price/100*LongLimit)
  133. LongStopPrice = strategy.position_avg_price-(strategy.position_avg_price/100*LongStop)
  134. strategy.exit(id="SL e TP SHORT", from_entry="Entrata Long", limit=LongLimitPrice, stop=LongStopPrice, when=LongSLTP)
  135.  
  136.  
  137. // Ingressi e uscite short
  138. strategy.entry(id="Entrata Short", long=false, when=CondizioneEntrataShort) // ingresso originale short
  139. strategy.close(id="Entrata Short", when=CondizioneUscitaShort) // uscita originale short
  140. strategy.close(id="Entrata Short", when=strategy.position_size < 0 and CondizioneEntrataLong, comment="Reverse della Posizione Short") // uscita short per il rerverse della posizione  (si usa solo se si va solo short)
  141.  
  142. ShortLimitPrice = strategy.position_avg_price-(strategy.position_avg_price/100*ShortLimit)
  143. ShortLimitStop = strategy.position_avg_price+(strategy.position_avg_price/100*ShortStop)
  144. strategy.exit(id="SL e TP LONG", from_entry="Entrata Short", limit=ShortLimitPrice, stop=ShortLimitStop,when=ShortSLTP)
  145.  
  146.  
  147. // Stampa sul grafico le informazioni utili
  148.  
  149. // stampa le medie
  150. plot(series=MediaLentaLong, title="Media Lenta Long", color=color.green, linewidth=2)
  151. plot(series=MediaVeloceLong, title="Media Veloce Long", color=color.teal, linewidth=2)
  152.  
  153.  
  154. plot(series=MediaLentaShort, title="Media Lenta Short", color=color.red, linewidth=2)
  155. plot(series=MediaVeloceShort, title="Media Veloce Short", color=color.maroon, linewidth=2)
  156.  
  157. 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) // stampa l'entry price in rosso se short in blu se long
  158.  
  159. plot(strategy.position_size > 0 ?  LongLimitPrice: strategy.position_size < 0 ? ShortLimitPrice : na, color=color.green, style=plot.style_linebr)
  160. plot(strategy.position_size > 0 ?  LongStopPrice: strategy.position_size < 0 ? ShortLimitStop : na, color=color.red, style=plot.style_linebr)
  161.  
  162. 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
  163.  
  164.  
  165. // Tabella risultati mensili by QuantNomad
  166.  
  167. new_month = month(time) != month(time[1])
  168. new_year  = year(time)  != year(time[1])
  169.  
  170. eq = strategy.equity
  171.  
  172. bar_pnl = eq / eq[1] - 1
  173.  
  174. cur_month_pnl = 0.0
  175. cur_year_pnl  = 0.0
  176.  
  177. // Current Monthly P&L
  178. cur_month_pnl := new_month ? 0.0 :
  179.                  (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
  180.  
  181. // Current Yearly P&L
  182. cur_year_pnl := new_year ? 0.0 :
  183.                  (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1  
  184.  
  185. // Arrays to store Yearly and Monthly P&Ls
  186. var month_pnl  = array.new_float(0)
  187. var month_time = array.new_int(0)
  188.  
  189. var year_pnl  = array.new_float(0)
  190. var year_time = array.new_int(0)
  191.  
  192. last_computed = false
  193.  
  194. if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
  195.     if (last_computed[1])
  196.         array.pop(month_pnl)
  197.         array.pop(month_time)
  198.        
  199.     array.push(month_pnl , cur_month_pnl[1])
  200.     array.push(month_time, time[1])
  201.  
  202. if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
  203.     if (last_computed[1])
  204.         array.pop(year_pnl)
  205.         array.pop(year_time)
  206.        
  207.     array.push(year_pnl , cur_year_pnl[1])
  208.     array.push(year_time, time[1])
  209.  
  210. last_computed := barstate.islast ? true : nz(last_computed[1])
  211.  
  212. // Monthly P&L Table    
  213. var monthly_table = table(na)
  214. prec      = input(2, title = "Return Precision")
  215.  
  216. if (barstate.islast)
  217.     monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_width=1,border_color=#000000)
  218.  
  219.     table.cell(monthly_table, 0,  0, "",     text_color=#D3D3D3, bgcolor=#0F0F0F)
  220.     table.cell(monthly_table, 1,  0, "Jan",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  221.     table.cell(monthly_table, 2,  0, "Feb",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  222.     table.cell(monthly_table, 3,  0, "Mar",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  223.     table.cell(monthly_table, 4,  0, "Apr",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  224.     table.cell(monthly_table, 5,  0, "May",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  225.     table.cell(monthly_table, 6,  0, "Jun",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  226.     table.cell(monthly_table, 7,  0, "Jul",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  227.     table.cell(monthly_table, 8,  0, "Aug",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  228.     table.cell(monthly_table, 9,  0, "Sep",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  229.     table.cell(monthly_table, 10, 0, "Oct",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  230.     table.cell(monthly_table, 11, 0, "Nov",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  231.     table.cell(monthly_table, 12, 0, "Dec",  text_color=#D3D3D3, bgcolor=#0F0F0F)
  232.     table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
  233.  
  234.     for yi = 0 to array.size(year_pnl) - 1
  235.         table.cell(monthly_table, 0,  yi + 1, tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
  236.        
  237.         y_color = array.get(year_pnl, yi) > 0 ? color.lime :  array.get(year_pnl, yi) < 0 ? color.red : color.gray
  238.         table.cell(monthly_table, 13, yi + 1, tostring(round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
  239.        
  240.     for mi = 0 to array.size(month_time) - 1
  241.         m_row   = year(array.get(month_time, mi))  - year(array.get(year_time, 0)) + 1
  242.         m_col   = month(array.get(month_time, mi))
  243.         m_color = array.get(month_pnl, mi) > 0 ? color.lime : array.get(month_pnl, mi) < 0 ? color.red : color.gray
  244.        
  245.         table.cell(monthly_table, m_col, m_row, tostring(round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
  246.  
  247.  
  248.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement