Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Il trading system completo - Swing-Trend-Trailing (Strategia Trend Following Con Swing Di Posizione) - parte 1
- // (Exchange= FTX) (Sottostante ETH-PERP) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= SI) (Esclusione Ore=NO) (Esclusione Giorni=NO') (Esclusione Mesi=NO)
- // (Take Profit Long/Short Market = Trailing) (Take Profit Limit Long/Short= NO) (Stop Loss Limit Long/Short= -10%) (Stop Loss Market Long/Short= SI) (Trailing Stop=SI) (Stop Emergenza= NO)
- // (Rischio Operazione 2% Perdita Media)
- // (In Sample Dal=17/08/2017 Al 17/10/2020) (Out Of Sample Dal=18/10/2020 Al 15/03/2021)
- // GLI IMPUT POTREBBERO ESSERE DIVERSI E VANNO PRESI DAL CODICE DI PINESCRIPT, INSERIRE ANCHE LE VARIABILI SHORT CHE POTREBBERO AVERE INPUT DIVERSI
- // Auentare il Max Bars Back Su Tradestation Altrimenti Non Funziona Il Conteggio Barssinceentry 500 E Cliccare Su Enable Strategy Altrimenti Non Parte
- input:
- InitialCapital(100000),
- percent_risk(25),
- lunghezza_ema(90),
- perc_apertura_minima(2),
- perc_apertura_massima(4),
- perc_chiusura_minima(0),
- input_trailing_stop_trigger(12),
- input_trailing_stop_close(6),
- input_stop_loss_percent(10), // Unico Long E Short
- //skipday(saturday),
- //skipmonth1(10),
- //skipmonth2(9),
- solo_long(false),
- solo_short(false);
- Vars:
- media_ema(0),
- apertura_minima(0),
- apertura_massima(0),
- chiusura_minima(0),
- stop_loss(0),
- media_ema_1(0),
- trailing_stop_trigger(0),
- trailing_stop_close(0),
- trailing_stop_trigger_long(0),
- trailing_stop_close_long(0),
- trailing_stop_trigger_short(0),
- trailing_stop_close_short(0),
- trailing_long(false),
- trailing_short(false),
- highesthigh(0),
- lowestlow(0),
- ATR(0),
- risk(0),
- nr_share(0);
- // Calcololo degli indicatori
- media_ema = XAverage(Close, lunghezza_ema);
- media_ema_1 = XAverage(Close[1], lunghezza_ema);
- // calcolo dei filtri in percentuale
- apertura_minima = (media_ema / 100) * perc_apertura_minima;
- apertura_massima = (media_ema / 100) * perc_apertura_massima;
- chiusura_minima = (media_ema / 100) * perc_chiusura_minima;
- // Calcolo Trailing Stop Long E Short
- trailing_stop_trigger_long = Entryprice + (entryprice * input_trailing_stop_trigger)/100;
- trailing_stop_close_long = entryprice + (entryprice * input_trailing_stop_close)/100;
- trailing_stop_trigger_short = Entryprice - (entryprice * input_trailing_stop_trigger)/100;
- trailing_stop_close_short = entryprice - (entryprice * input_trailing_stop_close)/100;
- // Originale
- {trailing_long = high >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[1] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[2] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[3] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long)
- or (MarketPosition =1 and high[4] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[5] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[6] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[7] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long)
- or (MarketPosition =1 and high[8] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[9] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[10] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[11] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long)
- or (MarketPosition =1 and high[12] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[13] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[14] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long) or (MarketPosition =1 and high[15] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long)
- or (MarketPosition =1 and high[16] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long);
- // Originale
- trailing_short = low <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[1] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[2] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[3] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short)
- or (MarketPosition =-1 and low[4] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[5] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[6] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[7] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short)
- or (MarketPosition =-1 and low[8] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[9] <=trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[10] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[11] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short)
- or (MarketPosition =-1 and low[12] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[13] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[14] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short) or (MarketPosition =-1 and low[15] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short)
- or (MarketPosition =-1 and low[16] <= trailing_stop_trigger_short and Close > trailing_stop_close_short and close[1] < trailing_stop_close_short);}
- {trailing_long = high >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[1] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[2] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[3] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long)
- or (MarketPosition =1 and high[4] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[5] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[6] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[7] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long)
- or (MarketPosition =1 and high[8] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[9] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[10] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[11] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long)
- or (MarketPosition =1 and high[12] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[13] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[14] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long) or (MarketPosition =1 and high[15] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long)
- or (MarketPosition =1 and high[16] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long);
- trailing_short = low <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[1] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[2] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[3] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short)
- or (MarketPosition =-1 and low[4] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[5] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[6] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[7] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short)
- or (MarketPosition =-1 and low[8] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[9] <=trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[10] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[11] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short)
- or (MarketPosition =-1 and low[12] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[13] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[14] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short) or (MarketPosition =-1 and low[15] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short)
- or (MarketPosition =-1 and low[16] <= trailing_stop_trigger_short and Close cross over trailing_stop_close_short)}
- highesthigh = highest(high, Barssinceentry);
- trailing_long = (highesthigh >= trailing_stop_trigger_long and close <= trailing_stop_close_long);
- lowestlow = lowest(low, Barssinceentry);
- trailing_short = (lowestlow <= trailing_stop_trigger_short and close >= trailing_stop_close_short);
- // Money menagment
- risk = percent_risk/100;
- nr_share = floor((InitialCapital + NetProfit) * risk) / close ;
- stop_loss=((((InitialCapital + NetProfit) * risk)/100) * input_stop_loss_percent);
- // Entrata Long
- { and (close[1] < ema1 + apertura_minima or low[1] < ema1 + apertura_minima Tradingview}
- //if close > (media_ema+apertura_minima) and close < (media_ema + apertura_massima) and close[2] < (media_ema+apertura_minima) and open > close and not solo_short {and Dayofweek(date) <> skipday and month(date) <> skipmonth1 and Dayofmonth(date) > 5} then
- if close > (media_ema + apertura_minima) and close < (media_ema + apertura_massima) and ((close[1] < (media_ema_1 + apertura_minima)) or (low[1] < (media_ema_1 + apertura_minima))) and close > open and not solo_short {and Dayofweek(date) <> skipday and month(date) <> skipmonth1 and Dayofmonth(date) > 5} then
- Buy("Long") nr_share contracts Next Bar at market;
- // Uscita Long
- if close < media_ema - chiusura_minima then
- Sell("Chiusura Long") from entry("Long") Next Bar at market;
- // Uscita Trailing Long
- If (MarketPosition =1 and trailing_long) then
- Sell("Trailing Long") from entry("Long") Next Bar at market;
- // Entrata Short
- {and (close[1] > ema1 - apertura_minima or low[1] > ema1 - apertura_minima)}
- if close < (media_ema - apertura_minima) and close > (media_ema - apertura_massima) and ((close[1] > (media_ema_1 - apertura_minima)) or (high[1] > (media_ema_1 - apertura_minima))) and close < open and not solo_long {and Dayofweek(date) <> skipday and month(date) <> skipmonth1 and Dayofmonth > 5} then
- Sellshort("Short") nr_share Contracts Next Bar at market;
- // Uscita Short
- if close > (media_ema + chiusura_minima) then
- Buytocover("Chiusura Short") from entry("Short") Next Bar at market;
- // Uscita Trailing Short
- If (MarketPosition =-1 and trailing_short)
- then
- Buytocover("Trailing Short") from entry("Short") Next Bar at market;
- Setstopposition;
- SetStopLoss(stop_loss);
Add Comment
Please, Sign In to add comment