Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Maurizio-Ciullo
- //Il trading system completo - Swing-Trend (Strategia Trend Following Con Swing Di Posizione) - parte 1
- // (Exchange= FTX) (Sottostante ETH-PERP) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= SI) (Esclusione Giorni=Sabato) (Esclusione Mesi=Ottobre)
- // (Take Profit Long/Short= Market Close Sopra/Sotto Medie) (Stop Loss Limit= -2,5% Tradingview-Hub) (Stop Loss Emergenza Long/Short = Market Close Sopra/Sotto Medie)
- //@version=4
- strategy(title="Bot Swing-Trend ETH/PERP FTX 4H LONG E SHORT", overlay=true,
- pyramiding=0, initial_capital=150,
- commission_type=strategy.commission.percent,
- commission_value=0.1, slippage=3,
- default_qty_type=strategy.percent_of_equity,
- default_qty_value=27.5)
- input_ema_long_apertura = input(title="Media Long Apertura", type=input.integer, defval=45, minval=0, maxval=500, group="Medie")
- input_ema_long_chiusura = input(title="Media Long Chiusura", type=input.integer, defval=35, minval=0, maxval=500, group="Medie")
- input_ema_short_apertura = input(title="Media Short Apertura", type=input.integer, defval=40, minval=0, maxval=500, group="Medie")
- input_ema_short_chiusura = input(title="Media Short Chiusura", type=input.integer, defval=30, minval=0, maxval=500, group="Medie")
- perc_apertura_minima_long = input(title="perc_apertura_minima_long", type=input.float, defval=0, step=0.1, group="Filtri Posizione")
- perc_apertura_massima_long = input(title="perc_apertura_massima_long", type=input.float, defval=2, step=0.1, group="Filtri Posizione ")
- perc_chiusura_minima_long = input(title="perc_chiusura_minima_long", type=input.float,defval=1.5, step=0.1, group="Filtri Posizione")
- perc_apertura_minima_short = input(title="perc_apertura_minima_short", type=input.float, defval=0.75, step=0.01, group="Filtri Posizione")
- perc_apertura_massima_short = input(title="perc_apertura_massima_short", type=input.float, defval=2, step=0.1, group="Filtri Posizione")
- perc_chiusura_minima_short = input(title="perc_chiusura_minima_short", type=input.float, defval=1, step=0.1, group="Filtri Posizione")
- onlyLong = input(title="Solo long trade", type=input.bool, defval=false, inline="1", group="Direzione")
- onlyShort = input(title="Solo short trade", type=input.bool, defval=false, inline="1", group="Direzione")
- input_stop_loss = input(title="stop_loss_long", type=input.float, defval=2.5, minval=0, maxval=100, step=0.1, group="Stop")
- input_risk = input(title="Percentuale rischio", type=input.float, defval=27.5, minval=0, maxval=100, step = 0.01, group="Stop")
- //dmi_min_diff_input_long = input(title="Distanza min dim", type=input.float, minval=-100, maxval=1000000, defval=19, step=0.1)
- //dmi_min_diff_input_short = input(title="Distanza min dim", type=input.float, minval=-100, maxval=1000000, defval=7, step=0.1)
- // STEP 1 DATARANGE:
- // Make input options that configure backtest date range
- startDate = input(title="Start Date", type=input.integer,
- defval=18, minval=1, maxval=31, group="Periodo")
- startMonth = input(title="Start Month", type=input.integer,
- defval=08, minval=1, maxval=12, group="Periodo")
- startYear = input(title="Start Year", type=input.integer,
- defval=2017, minval=1800, maxval=2100, group="Periodo")
- endDate = input(title="End Date", type=input.integer,
- defval=01, minval=1, maxval=31, group="Periodo")
- endMonth = input(title="End Month", type=input.integer,
- defval=01, minval=1, maxval=12, group="Periodo")
- endYear = input(title="End Year", type=input.integer,
- defval=2121, minval=1800, maxval=2100, group="Periodo")
- // STEP 2 DATARANGE:
- // Look if the close time of the current bar
- // falls inside the date range
- inDateRange = (time >= timestamp(syminfo.timezone, startYear,
- startMonth, startDate, 0, 0)) and
- (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
- ema_long_apertura = ema(close, input_ema_long_apertura)
- ema_long_chiusura = ema(close, input_ema_long_chiusura)
- ema_short_apertura = ema(close,input_ema_short_apertura)
- ema_short_chiusura = ema(close,input_ema_short_chiusura)
- //Calcolo filtri e stop loss
- apertura_minima_long = (ema_long_apertura / 100) * perc_apertura_minima_long
- apertura_massima_long = (ema_long_apertura / 100) * perc_apertura_massima_long
- chiusura_minima_long = (ema_long_chiusura / 100) * perc_chiusura_minima_long
- apertura_minima_short = (ema_short_apertura / 100) * perc_apertura_minima_short
- apertura_massima_short = (ema_short_apertura / 100) * perc_apertura_massima_short
- chiusura_minima_short = (ema_short_chiusura / 100) * perc_chiusura_minima_short
- size = ((strategy.equity * input_risk) / 100)
- nr_contratti = (size) / close
- //stop_loss =round(((size /syminfo.mintick) / 100) * input_stop_loss)/nr_contratti
- plot(ema_long_apertura, color=color.blue, title="Ema Long Apertura", linewidth = 2)
- plot(ema_long_chiusura, color=color.blue, title="Ema Long Chiusura", linewidth = 2)
- plot(ema_short_apertura, color=color.red, title="Ema Short Apertura", linewidth = 2)
- plot(ema_short_chiusura, color=color.red, title="Ema Short Chiusura", linewidth = 2)
- filtro_long_basso = (ema_long_apertura + apertura_minima_long)
- filtro_long_alto = (ema_long_apertura + apertura_massima_long)
- plot_filtro_long_basso = plot(filtro_long_basso, color=color.green, title="filtro_long_basso")
- plot_filtro_long_alto = plot(filtro_long_alto, color=color.green, title="filtro_long_alto")
- fill(plot_filtro_long_basso, plot_filtro_long_alto, color=color.green)
- filtro_short_basso = (ema_short_apertura - apertura_minima_short)
- filtro_short_alto = (ema_short_apertura - apertura_massima_short)
- plot_filtro_short_basso = plot(filtro_short_basso, color=color.yellow, title="filtro_short_basso")
- plot_filtro_short_alto = plot(filtro_short_alto, color=color.yellow, title="filtro_short_alto")
- fill(plot_filtro_short_basso, plot_filtro_short_alto, color=color.red)
- plot(ema_long_chiusura - chiusura_minima_long, color=color.green, style=plot.style_circles, linewidth=3, title="Emergency_Stop_Long")
- plot(ema_short_chiusura + chiusura_minima_short, color=color.red, style=plot.style_circles, linewidth=3, title="Emergency_Stop_Short")
- stop_loss =round(((size /syminfo.mintick) / 100) * input_stop_loss)/nr_contratti
- 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
- plot(strategy.position_size > 0 ? stop_loss: strategy.position_size < 0 ? stop_loss : na, color=color.green, style=plot.style_linebr, title="stop_loss_limit")
- plot(strategy.position_size > 0 ? stop_loss: strategy.position_size < 0 ? stop_loss : na, color=color.red, style=plot.style_linebr, title="stop_loss_limit")
- 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
- // Filtro voglio che sia true se il +DI è sopra sia il -DI ed anche dell'ADX //21,3587
- //[di_pos, di_neg, adx] = dmi(14, 14)
- //dmi_total_green_cross = di_pos > di_neg and di_pos > adx? true : false
- //dmi_total_green_cross = (di_pos - di_neg) > dma_min_diff_input ? true : false
- //dmi_total_green_cross = (di_neg - di_pos) > dmi_min_diff_input ? true : false
- //dmi_total_green_cross_short = (di_neg > adx + dmi_min_diff_input_short) ? true : false
- //dmi_total_green_cross_long = (di_pos > adx + dmi_min_diff_input_long) ? true : false
- //dmi_total_green_cross = (di_pos - di_neg) < dmi_min_diff_input ? true : false
- //plot(dmi_total_green_cross ? 1 : 0, title="dmi_total_green_cross")
- //Entry/Exit Conditions
- //Condizione Entry Long: Chiusura candela sopra media lenta con differenziale medie e media veloce maggiore media ExitLong
- condEntryLong = close > (ema_long_apertura + apertura_minima_long) and close < (ema_long_apertura + apertura_massima_long) and close > open and not onlyShort and dayofweek != 7 and month !=10 and inDateRange
- //Condizione Exit Long: Crossunder di due medie
- condExitLong = close < (ema_long_chiusura - chiusura_minima_long)
- // Condizione Entry Short: Crossunder di due medie con il prezzo di chiusura che è comunque sotto una terza media con differenziale delle medie
- condEntryShort = close < (ema_short_apertura - apertura_minima_short) and close > (ema_short_apertura - apertura_massima_short) and close < open and not onlyLong and dayofweek != 7 and month !=10 and inDateRange
- // Condizione Exit Short: Crossover di medie con il prezzo di chiusura che è comunque sopra una terza media
- condExitShort = close > (ema_short_chiusura + chiusura_minima_short)
- plot(strategy.position_avg_price - stop_loss * 0.01 * 10, title="stop_loss_limit_long", color=color.silver, transp=3, linewidth=4)
- plot(strategy.position_avg_price + stop_loss * 0.01 * 10, title="stop_loss_limit_short", color=color.purple, transp=3, linewidth=4)
- //Entry/Exit Orders
- //strategy.entry("long", true, when = condEntryLong)
- //strategy.close("long", when=condExitLong)
- //strategy.entry("short", false, when = condEntryShort)
- //strategy.close("short", when=condExitShort)
- if (condEntryLong)
- strategy.entry("long", true)
- strategy.exit("stop loss", from_entry = "long", loss = stop_loss)
- if (condExitLong)
- strategy.close(id="long")
- if (condEntryShort)
- strategy.entry("short", false)
- strategy.exit("stop loss", from_entry = "short", loss = stop_loss)
- if (condExitShort)
- strategy.close(id="short")
- // Tabella risultati mensili by QuantNomad (TSC Boilerplate) Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick
- 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_color=#000000, border_width=1) //frame_width=1 tabella più piccola sostituisce border_width=1
- 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 : color.red
- 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 : color.red
- table.cell(monthly_table, m_col, m_row, tostring(round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
Add Comment
Please, Sign In to add comment