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 - Trend-wide (Strategia Trend Following) - parte 1
- //@version=4
- // I migliori settaggi //
- //ETH/USD // TIMFRAME 4H LONG E SHORT FTX//
- // Se gli hub permettono di andare solo long o solo short cambiare le impostazioni onlyLong true e onlyshort false nei parametri di imput.
- // Se permette long e short insieme setterli tutti e due true.
- // Modificare anche il true o false dello strategy entry.
- // Collegamento a wunderbit: 1) Inserire la stringa di entry long di wunderbit a strategy.entry aggiungendo ,comment="stringa-wunderbit per entry long".
- // Stessa cosa per short con exti short. Se ordini in short anche per gli short entry ed exit.
- // 2) Aggiungere webhook di wunderbit sull'allert di tradingview
- // 3) Nelle note di tradingview cancellare tutto ed aggiungere solo: {{strategy.order.comment}}
- // 4) Inserire l'allert sempre alla fine dopo le impostazioni e tutto il resto
- // 5) Ordine minimo wunderbit 20$ altrimenti il bot non entra
- // 6) Se si ha un solo portafoglio con più strategie su un unico portafoglio i bot potrebbero avere size di ingresso diverse da qualle prestabilite
- // 7) Se non si vuole usare la leva, impostare leva 1 su wunderbit altrimenti userà la leva che in quel momento è impostata sull'exchange
- // 8) Avere un capitale adeguato per il margine di mantenimento e considerare che se il capitale scende la posizione è minore dei 20$ di wunderbit richiesti per entrare in posizione
- strategy(title="Strategia trend-wide Eth/USD Ftx 4H Long E Short", overlay=true,
- pyramiding=0, initial_capital=266,
- commission_type=strategy.commission.percent,
- commission_value=0.1, slippage=50,
- default_qty_type=strategy.percent_of_equity,
- default_qty_value=15)
- maInputSlowLong = input(title="MA SlowLong", type=input.integer, defval=74, minval=0, maxval=500, group="Medie")
- maInputSlowShort = input(title="MA SlowShort", type=input.integer, defval=49, minval=0, maxval=500, group="Medie")
- maInputFastLong = input(title="MA FastLong", type=input.integer, defval=15, minval=0, maxval=500, group="Medie")
- maInputFastShort = input(title="MA FastShort", type=input.integer, defval=15, minval=0, maxval=500, group="Medie")
- smaInputMediumShort = input(title="SMA MediumShort", type=input.integer, defval=50, minval=0, maxval=500, group="Medie")
- maInputExitLong = input(title="MA Exit Long", type=input.integer, defval=43, minval=0, maxval=500, group="Medie")
- smaInputExitShort = input(title="SMA Exit Short", type=input.integer, defval=11, minval=0, maxval=500, group="Medie")
- maMinDiffLong = input(title="Distanza min medie Long", type=input.float, defval=8.31, step=0.01, group="Filtri Medie")
- maMinDiffShort = input(title="Distanza min medie Short", type=input.float, defval=7.23, step=0.01, group="Filtri Medie")
- hourTrading = input(title="Sessione valida di trading", type=input.string, defval="24x7", group="Sessioni")
- 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")
- // STEP 1 DATARANGE:
- // Make input options that configure backtest date range
- startDate = input(title="Start Date", type=input.integer,
- defval=01, minval=1, maxval=31, group="Periodo")
- startMonth = input(title="Start Month", type=input.integer,
- defval=1, minval=1, maxval=12, group="Periodo")
- startYear = input(title="Start Year", type=input.integer,
- defval=2000, 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))
- // Include or Exclude Days of the Week
- // Trade_long_on_dow_1 is Sunday/Domenica
- // Trade_long_on_dow_2 is Monday/Lunedì Etc...
- // If a position is opened it will be closed as srategy without excluding the month
- trade_long_on_dow_1 = input(true, group="Include/Esclude Giorni Long")
- trade_long_on_dow_2 = input(true, group="Include/Esclude Giorni Long")
- trade_long_on_dow_3 = input(true, group="Include/Esclude Giorni Long")
- trade_long_on_dow_4 = input(true, group="Include/Esclude Giorni Long")
- trade_long_on_dow_5 = input(true, group="Include/Esclude Giorni Long")
- trade_long_on_dow_6 = input(true, group="Include/Esclude Giorni Long")
- trade_long_on_dow_7 = input(true, group="Include/Esclude Giorni Long")
- trade_short_on_dow_1 = input(true, group="Include/Esclude Giorni Short")
- trade_short_on_dow_2 = input(true, group="Include/Esclude Giorni Short")
- trade_short_on_dow_3 = input(true, group="Include/Esclude Giorni Short")
- trade_short_on_dow_4 = input(true, group="Include/Esclude Giorni Short")
- trade_short_on_dow_5 = input(true, group="Include/Esclude Giorni Short")
- trade_short_on_dow_6 = input(true, group="Include/Esclude Giorni Short")
- trade_short_on_dow_7 = input(true, group="Include/Esclude Giorni Short")
- 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_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_6 and dayofweek == 6) or (trade_short_on_dow_7 and dayofweek == 7)
- // Exit open market position when date range ends
- //if (not trade_long_today)
- // strategy.close_all()
- //if (not trade_short_today)
- // strategy.close_all()
- // Include or Exclude Moths of the Year
- // Trade_long_on_dow_1 is Sunday/Domenica
- // Trade_long_on_dow_2 is Monday/Lunedì Etc...
- // If a position is opened it will be closed as srategy without excluding the month
- trade_long_on_dow_month_1 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_2 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_3 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_4 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_5 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_6 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_7 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_8 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_9 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_10 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_11 = input(true, group="Include/Esclude Mesi Long")
- trade_long_on_dow_month_12 = input(true, group="Include/Esclude Mesi Long")
- trade_short_on_dow_month_1 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_2 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_3 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_4 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_5 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_6 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_7 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_8 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_9 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_10 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_11 = input(true, group="Include/Esclude Mesi Short")
- trade_short_on_dow_month_12 = input(true, group="Include/Esclude Mesi Short")
- trade_long_this_month = (trade_long_on_dow_month_1 and month == 1) or (trade_long_on_dow_month_2 and month == 2) or (trade_long_on_dow_month_3 and month == 3) or (trade_long_on_dow_month_4 and month == 4) or (trade_long_on_dow_month_5 and month == 5) or (trade_long_on_dow_month_5 and month == 5) or (trade_long_on_dow_month_6 and month == 6) or (trade_long_on_dow_month_7 and month == 7) or (trade_long_on_dow_month_8 and month == 8) or (trade_long_on_dow_month_9 and month == 9) or (trade_long_on_dow_month_10 and month == 10) or (trade_long_on_dow_month_11 and month == 11) or (trade_long_on_dow_month_12 and month == 12)
- trade_short_this_month = (trade_short_on_dow_month_1 and month == 1) or (trade_short_on_dow_month_2 and month == 2) or (trade_short_on_dow_month_3 and month == 3) or (trade_short_on_dow_month_4 and month == 4) or (trade_short_on_dow_month_5 and month == 5) or (trade_short_on_dow_month_5 and month == 5) or (trade_short_on_dow_month_6 and month == 6) or (trade_short_on_dow_month_7 and month == 7) or (trade_short_on_dow_month_8 and month == 8) or (trade_short_on_dow_month_9 and month == 9) or (trade_short_on_dow_month_10 and month == 10) or (trade_short_on_dow_month_11 and month == 11) or (trade_short_on_dow_month_12 and month == 12)
- // Exit open market position when date range ends
- //if (trade_long_this_month)
- // strategy.close_all()
- //if (trade_short_this_month)
- // strategy.close_all()
- maSlowLong = ema(close, maInputSlowLong)
- maSlowShort = ema(close, maInputSlowShort)
- maFastLong = ema(close, maInputFastLong)
- maFastShort = ema(close, maInputFastShort)
- smaMediumShort = sma(close, smaInputMediumShort)
- maExitLong = ema(close, maInputExitLong)
- smaExitShort = sma(close, smaInputExitShort)
- rangeTrading = time(timeframe.period, hourTrading)
- differenziale_attuale_long = maFastLong - maSlowLong
- differenziale_attuale_short = maSlowShort - maFastShort
- plot(maSlowLong, color=color.orange, title="maSlowLong")
- plot(maSlowShort, color=color.green, title="maSlowShort")
- plot(maFastLong, color=color.blue, title="maFastLong")
- plot(maFastShort, color=color.purple, title="maFastShort")
- plot(smaMediumShort, color=color.gray, title="smaMediumShort")
- plot(maExitLong, color=color.black, title="maExitLong")
- plot(smaExitShort, color=color.red, title="smaExitShort")
- plot(differenziale_attuale_long, title="diff_attuale_long")
- plot(differenziale_attuale_short, title="diff_attuale_short")
- // Filtro di emergenza chiusura posizione solo se aperta solo per lo short perchè se il cross di uscita posizione delle medie sono invertite non esco più
- if (close > maSlowShort)
- if (smaExitShort > maSlowShort)
- if (strategy.position_size < 0)
- strategy.close(id="short")
- //Entry/Exit Conditions
- //Condizione Entry Long: Chiusura candela sopra media lenta con differenziale medie e media veloce maggiore media ExitLong
- condEntryLong = close > maSlowLong and (maFastLong - maSlowLong) > maMinDiffLong and rangeTrading and inDateRange and trade_long_today and trade_long_this_month and maFastLong > maExitLong and not onlyShort
- //Condizione Exit Long: Crossunder di due medie
- condExitLong = crossunder(maFastLong, maExitLong)
- // Condizione Entry Short: Crossunder di due medie con il prezzo di chiusura che è comunque sotto una terza media con differenziale delle medie
- condEntryShort = crossunder(smaExitShort, maFastShort) and close < smaMediumShort and (maSlowShort - maFastShort) > maMinDiffShort and rangeTrading and inDateRange and trade_short_today and trade_short_this_month and not onlyLong
- // Condizione Exit Short: Crossover di medie con il prezzo di chiusura che è comunque sopra una terza media
- condExitShort = crossover(smaExitShort, smaMediumShort) and close > maSlowShort
- //Entry/Exit Orders
- strategy.entry("long", true, when = condEntryLong, comment="eac4917c_ENTER-LONG_FTX_ETH-PERP_eac4917c-FTX-ETH-PERP4H-LONG-SHORT_4H")
- strategy.close("long", when=condExitLong, comment="eac4917c_EXIT-LONG_FTX_ETH-PERP_eac4917c-FTX-ETH-PERP4H-LONG-SHORT_4H")
- strategy.entry("short", false, when = condEntryShort, comment="eac4917c_ENTER-SHORT_FTX_ETH-PERP_eac4917c-FTX-ETH-PERP4H-LONG-SHORT_4H")
- strategy.close("short", when=condExitShort, comment="eac4917c_EXIT-SHORT_FTX_ETH-PERP_eac4917c-FTX-ETH-PERP4H-LONG-SHORT_4H")
- // 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_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 : 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)
- //SETTAGGI FTX FUTURE SOLO LONG:
- //MaSlowLong: 66/116/114/100 le migliori sono 66 e 116 la 66 miglio equity line e non stalla, la 116 guadagna un pò di più ma stalla di più.
- //MaFastLong: 15 entra ed esce bene le altre più alte fanno meno operazioni ma entrano ed escono dopo.
- //Continuare ad usare i settaggi: MaSlowLong: 66.....MaFastLong: 15
- //SETTAGGI ETH/USD FTX FUTURE IDENTICI A BITFINEX COME INGRESSI E USCITE ECCEZIONE MINIMA DELLA CANDELA SPORADICA LONG E SHORT:
- // maSlowLong 74
- // maSlowShort 49
- // maFastLong 15
- // maFastShort 15
- // smaMediumShort 50
- // maExitLong 43
- // maExitShort 11
- // Distanza min Medie Long 8.31
- // Distanza min Medie Short 7.23
- // Quando valutare se stoppare il trading sytem, Valutare le percentuali perchè il portfoglio è incrementale:
- //1) Equity line che rompe la retta dei minimi
- //2) Report valori passato: Inizio 16/03/2017 Fine 26/11/2021
- // Sommario Performance: DD MAx - 31.01$ = 4.89% ----- Perdita Media - 5.41% = - 2.03 Del Portafoglio ------ Peggio Perdita 14.60$ = 13.77%
- // Perdita più grandi realizzate per operazione ordine crescente
- // -10.96$ = -13.77%
- // -7.17$ = -11.57%
- // -11.48$ = -11.11%
- // -14.31$ = -10.96%
- // -7.64$ = -10.93%
- // -7.43$ = -10.52%
- // -14.60 = -10.36%
- // Draw Down Max Non Realizzato In Operazione In Ordine Crescente:
- // -10.92$ = 27.04%
- // -18.53$ = 12.87%
- // -13.03$ = 18.26%
- // -10.34$ = 16.69%
- // -13.20$ = 16.58%
- // -18.16$ = 13.91%
- // - 9.39$ = 13.74%
- // -12.06$ = 13.72%
- // -14.16$ = 13.70%
- // -19.17$ = 13.61%
Add Comment
Please, Sign In to add comment