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
- // 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
- // I migliori settaggi //
- //ETH/USD // TIMFRAME 4H LONG E SHORT BITFINEX//
- study(title="Study Trendwide ETH-PERP Ftx 4H Long E Short", overlay=false)
- 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)
- //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")
- // 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")
- //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)
- //condExitLong = 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 or (smaExitShort > maSlowShort) and close > maSlowShort
- //definizione variabili posizioni aperte
- IsLongOpen = false
- IsLongOpen := condEntryLong[1] ? true : condExitLong[1] ? false : IsLongOpen[1]
- IsShortOpen = false
- IsShortOpen := condEntryShort[1] ? true : condExitShort[1] ? false : IsShortOpen[1]
- IsFlat = true
- IsFlat := not IsLongOpen and not IsShortOpen
- //conversione bool -> float, per debug
- IsLongOpenFloat = if IsLongOpen == true
- 1
- else
- 0
- IsShortOpenFloat = if IsShortOpen == true
- 1
- else
- 0
- IsFlatFloat = if IsFlat == true
- 1
- else
- 0
- //plot posizioni aperte, per debug
- //plot (IsLongOpenFloat)
- //plot (IsShortOpenFloat,color=color.yellow)
- //plot (IsFlatFloat,color=color.red)
- //variabili apertura e chiusura posizione
- OpenLong = condEntryLong and not IsLongOpen
- CloseLong = condExitLong and IsLongOpen
- OpenShort = condEntryShort and not IsShortOpen
- CloseShort = condExitShort and IsShortOpen
- //conversione bool -> float, per debug
- OpenShortFloat = if OpenShort == true
- 1
- else
- 0
- CloseShortFloat = if CloseShort == true
- 1
- else
- 0
- OpenLongFloat = if OpenLong == true
- 1
- else
- 0
- CloseLongFloat = if CloseLong == true
- 1
- else
- 0
- //plot aperture ordini, per debug
- plot (OpenShortFloat,color=color.red)
- plot (CloseShortFloat,color=color.yellow)
- plot (OpenLongFloat,color=color.blue)
- plot (CloseLongFloat,color=color.black)
- //alert
- alertcondition(OpenLong,title="Open Long")
- alertcondition(CloseLong,title="Close Long")
- alertcondition(OpenShort,title="Open Short")
- alertcondition(CloseShort,title="Close Short")
- //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)
- //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
Add Comment
Please, Sign In to add comment