Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: SniperTrend
- //Category: Trend Follower.
- //Operating mode: futures position long and short
- //Trades duration: less than a day
- //Exchange: Binance Future
- //Pair: BTC/Therer US
- //Strumento: Future Perpetual
- //Timeframe: 30M/15M chart to find the best entry price.
- //Suggested usage: in trend.
- // Appuntarsi le configurazioni
- // 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
- // 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
- // BINANCE:BTCUSDT 30M 100 75 open 100 true false true 8 4.5 | 2 3 5 6
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Maurizio-Ciullo
- // 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/
- //@version=4
- // Definizione della strategia
- //ATTENZIONE: PER VISUALIZZARE CORRETTAMENTE LA TABELLA E' NECESSARIO SPUNTARE LA VOCE "On Every Tick" DAL SETTING DELLA STRATEGIA
- strategy(title="SniperTrend v1.0.2",
- shorttitle="ST",
- overlay=true,
- initial_capital=100,
- pyramiding=0,
- default_qty_type=strategy.percent_of_equity,
- default_qty_value=100,
- commission_type=strategy.commission.percent,
- commission_value=0.025,
- slippage=3)
- // Date per il backtesting by khalidsalomao
- // Date per il backtesting
- fromYear = input(defval = 2019, title = "Backtesting - Start Year")
- fromMonth = input(defval = 1, title = "Backtesting - Star Month")
- fromDay = input(defval = 1, title = "Backtesting - Star Day")
- thruYear = input(defval = 2112, title = "Backtesting - End Year")
- thruMonth = input(defval = 1, title = "Backtesting - End Month")
- thruDay = input(defval = 1, title = "Backtesting - End Day")
- Start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // inizio backtesting
- Finish = timestamp(thruYear, thruMonth, thruDay, 23, 59) // fine backtesting
- RangeBacktest() => time >= Start and time <= Finish ? true : false // funzione che restituisce vero/falso se siamo in range
- // Variabili e funzioni utili
- PeriodoMediaLentaShort=input(defval=200, title="Periodo Media Lenta Short", type=input.integer)
- PeriodoMediaVeloceShort=input(defval=75, title="Periodo Media Veloce Short", type=input.integer)
- PeriodoMediaLentaLong=input(defval=200, title="Periodo Media Lenta Long", type=input.integer)
- PeriodoMediaVeloceLong=input(defval=75, title="Periodo Media Veloce Long", type=input.integer)
- SourceMedie=input(defval=open, title="Sorgente Medie", type=input.source)
- DifferenzaMedie=input(defval=70, title="Differenza tra le medie", type=input.integer)
- TradaLong=input(defval=true, title="Attiva i Long", type=input.bool)
- TradaShort=input(defval=true, title="Attiva gli Short", type=input.bool)
- InputRangeTrading=input(defval="24x7", title="Sessione di Trading", type=input.string)
- SorgenteEntrataLong=input(defval=close, title="Sorgente Entrata Long", type=input.source)
- SorgenteEntrataShort=input(defval=close, title="Sorgente Entrata Short", type=input.source)
- LongSLTP = input(defval=true, title="Attiva SL e TP Long", type=input.bool)
- LongLimit = input(defval=8, title="Long TP in %", type=input.float, step=0.5)
- LongStop = input(defval=4, title="Long SL in %", type=input.float, step=0.5)
- ShortSLTP = input(defval=true, title="Attiva SL e TP Short", type=input.bool)
- ShortLimit = input(defval=16, title="Short TP in %", type=input.float, step=0.5)
- ShortStop = input(defval=8, title="Short SL in %", type=input.float, step=0.5)
- trade_long_on_dow_1 = input(true)
- trade_long_on_dow_2 = input(true)
- trade_long_on_dow_3 = input(true)
- trade_long_on_dow_4 = input(true)
- trade_long_on_dow_5 = input(true)
- trade_long_on_dow_6 = input(true)
- trade_long_on_dow_7 = input(true)
- trade_short_on_dow_1 = input(true)
- trade_short_on_dow_2 = input(true)
- trade_short_on_dow_3 = input(true)
- trade_short_on_dow_4 = input(true)
- trade_short_on_dow_5 = input(true)
- trade_short_on_dow_6 = input(true)
- trade_short_on_dow_7 = input(true)
- 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)
- 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)
- // Definizione giorni della settimana e ore di trading
- RangeTrading=time(resolution=timeframe.period, session=InputRangeTrading)
- // Definizione degli inidicatori per gli ingressi
- MediaLentaLong=ema(SourceMedie, PeriodoMediaLentaLong)
- MediaVeloceLong=ema(SourceMedie, PeriodoMediaVeloceLong)
- MediaLentaShort=ema(SourceMedie, PeriodoMediaLentaShort)
- MediaVeloceShort=ema(SourceMedie, PeriodoMediaVeloceShort)
- // Condizioni di ingresso e uscita long
- CondizioneEntrataLong= crossover(SorgenteEntrataLong, MediaLentaLong) and (MediaVeloceLong-MediaLentaLong)>DifferenzaMedie and RangeTrading and TradaLong and RangeBacktest() and trade_long_today
- CondizioneUscitaLong = crossunder(MediaVeloceLong, MediaLentaLong)// and (MediaVeloceLong-MediaLentaLong)<DifferenzaMedie
- // Condizioni di ingresso e uscita short
- CondizioneEntrataShort= crossunder(SorgenteEntrataShort, MediaLentaShort) and (MediaLentaShort-MediaVeloceShort)>DifferenzaMedie and RangeTrading and TradaShort and RangeBacktest() and trade_short_today
- CondizioneUscitaShort= crossover(SorgenteEntrataShort, MediaVeloceShort) // and (MediaVeloceLong-MediaLentaLong)<DifferenzaMedie
- // Ingressi e uscite long
- strategy.entry(id="Entrata Long", long=true, when=CondizioneEntrataLong) // ingresso originale long
- strategy.close(id="Entrata Long", when=CondizioneUscitaLong, comment="Condizione Uscita Long") // uscita originale long
- 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)
- LongLimitPrice = strategy.position_avg_price+(strategy.position_avg_price/100*LongLimit)
- LongStopPrice = strategy.position_avg_price-(strategy.position_avg_price/100*LongStop)
- strategy.exit(id="SL e TP SHORT", from_entry="Entrata Long", limit=LongLimitPrice, stop=LongStopPrice, when=LongSLTP)
- // Ingressi e uscite short
- strategy.entry(id="Entrata Short", long=false, when=CondizioneEntrataShort) // ingresso originale short
- strategy.close(id="Entrata Short", when=CondizioneUscitaShort) // uscita originale short
- 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)
- ShortLimitPrice = strategy.position_avg_price-(strategy.position_avg_price/100*ShortLimit)
- ShortLimitStop = strategy.position_avg_price+(strategy.position_avg_price/100*ShortStop)
- strategy.exit(id="SL e TP LONG", from_entry="Entrata Short", limit=ShortLimitPrice, stop=ShortLimitStop,when=ShortSLTP)
- // Stampa sul grafico le informazioni utili
- // stampa le medie
- plot(series=MediaLentaLong, title="Media Lenta Long", color=color.green, linewidth=2)
- plot(series=MediaVeloceLong, title="Media Veloce Long", color=color.teal, linewidth=2)
- plot(series=MediaLentaShort, title="Media Lenta Short", color=color.red, linewidth=2)
- plot(series=MediaVeloceShort, title="Media Veloce Short", color=color.maroon, linewidth=2)
- 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
- plot(strategy.position_size > 0 ? LongLimitPrice: strategy.position_size < 0 ? ShortLimitPrice : na, color=color.green, style=plot.style_linebr)
- plot(strategy.position_size > 0 ? LongStopPrice: strategy.position_size < 0 ? ShortLimitStop : na, color=color.red, style=plot.style_linebr)
- 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
- // Tabella risultati mensili by QuantNomad
- new_month = month(time) != month(time[1])
- new_year = year(time) != year(time[1])
- eq = strategy.equity
- bar_pnl = eq / eq[1] - 1
- cur_month_pnl = 0.0
- cur_year_pnl = 0.0
- // Current Monthly P&L
- cur_month_pnl := new_month ? 0.0 :
- (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
- // Current Yearly P&L
- cur_year_pnl := new_year ? 0.0 :
- (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1
- // Arrays to store Yearly and Monthly P&Ls
- var month_pnl = array.new_float(0)
- var month_time = array.new_int(0)
- var year_pnl = array.new_float(0)
- var year_time = array.new_int(0)
- last_computed = false
- if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
- if (last_computed[1])
- array.pop(month_pnl)
- array.pop(month_time)
- array.push(month_pnl , cur_month_pnl[1])
- array.push(month_time, time[1])
- if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
- if (last_computed[1])
- array.pop(year_pnl)
- array.pop(year_time)
- array.push(year_pnl , cur_year_pnl[1])
- array.push(year_time, time[1])
- last_computed := barstate.islast ? true : nz(last_computed[1])
- // Monthly P&L Table
- var monthly_table = table(na)
- prec = input(2, title = "Return Precision")
- if (barstate.islast)
- monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_width=1,border_color=#000000)
- table.cell(monthly_table, 0, 0, "", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 1, 0, "Jan", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 2, 0, "Feb", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 3, 0, "Mar", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 4, 0, "Apr", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 5, 0, "May", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 6, 0, "Jun", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 7, 0, "Jul", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 8, 0, "Aug", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 9, 0, "Sep", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 10, 0, "Oct", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 11, 0, "Nov", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 12, 0, "Dec", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
- for yi = 0 to array.size(year_pnl) - 1
- table.cell(monthly_table, 0, yi + 1, tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
- y_color = array.get(year_pnl, yi) > 0 ? color.lime : array.get(year_pnl, yi) < 0 ? color.red : color.gray
- table.cell(monthly_table, 13, yi + 1, tostring(round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
- for mi = 0 to array.size(month_time) - 1
- m_row = year(array.get(month_time, mi)) - year(array.get(year_time, 0)) + 1
- m_col = month(array.get(month_time, mi))
- m_color = array.get(month_pnl, mi) > 0 ? color.lime : array.get(month_pnl, mi) < 0 ? color.red : color.gray
- table.cell(monthly_table, m_col, m_row, tostring(round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement