Maurizio-Ciullo

Bot Swing-Trend-Trailing ETH/PERP FTX 4H LONG E SHORT

May 7th, 2022 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Il trading system completo - Swing-Trend-Trailing (Strategia Trend Following Con Swing Di Posizione) - parte 1
  2. // (Exchange= FTX) (Sottostante ETH-PERP) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= SI) (Esclusione Ore=NO) (Esclusione Giorni=NO') (Esclusione Mesi=NO)
  3. // (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)
  4. // (Rischio Operazione 2% Perdita Media)
  5. // (In Sample Dal=17/08/2017 Al 17/10/2020) (Out Of Sample Dal=18/10/2020 Al 15/03/2021)
  6.  
  7. // GLI IMPUT POTREBBERO ESSERE DIVERSI E VANNO PRESI DAL CODICE DI PINESCRIPT, INSERIRE ANCHE LE VARIABILI SHORT CHE POTREBBERO AVERE INPUT DIVERSI
  8.  
  9. // Auentare il Max Bars Back Su Tradestation Altrimenti Non Funziona Il Conteggio Barssinceentry 500 E Cliccare Su Enable Strategy Altrimenti Non Parte
  10.  
  11. input:
  12.   InitialCapital(100000),
  13.   percent_risk(25),
  14.   lunghezza_ema(90),
  15.   perc_apertura_minima(2),
  16.   perc_apertura_massima(4),
  17.   perc_chiusura_minima(0),
  18.   input_trailing_stop_trigger(12),
  19.   input_trailing_stop_close(6),
  20.   input_stop_loss_percent(10),  // Unico Long E Short
  21.   //skipday(saturday),
  22.   //skipmonth1(10),
  23.   //skipmonth2(9),
  24.   solo_long(false),
  25.   solo_short(false);
  26.  
  27. Vars:
  28.  
  29.    media_ema(0),
  30.    apertura_minima(0),
  31.    apertura_massima(0),
  32.    chiusura_minima(0),
  33.    stop_loss(0),
  34.    media_ema_1(0),
  35.    trailing_stop_trigger(0),
  36.    trailing_stop_close(0),
  37.    trailing_stop_trigger_long(0),
  38.    trailing_stop_close_long(0),
  39.    trailing_stop_trigger_short(0),
  40.    trailing_stop_close_short(0),
  41.    trailing_long(false),
  42.    trailing_short(false),
  43.    highesthigh(0),
  44.    lowestlow(0),
  45.    ATR(0),
  46.    risk(0),
  47.    nr_share(0);
  48.    
  49.      
  50.      
  51.  
  52. // Calcololo degli indicatori
  53.      
  54.       media_ema = XAverage(Close, lunghezza_ema);
  55.       media_ema_1 = XAverage(Close[1], lunghezza_ema);
  56.    
  57.    
  58.    
  59.  
  60. // calcolo dei filtri in percentuale
  61.  
  62.   apertura_minima   = (media_ema / 100) * perc_apertura_minima;
  63.   apertura_massima  = (media_ema / 100) * perc_apertura_massima;
  64.   chiusura_minima   = (media_ema / 100) * perc_chiusura_minima;
  65.  
  66.   // Calcolo Trailing Stop Long E Short
  67.  
  68.   trailing_stop_trigger_long = Entryprice + (entryprice * input_trailing_stop_trigger)/100;
  69.   trailing_stop_close_long = entryprice + (entryprice * input_trailing_stop_close)/100;
  70.   trailing_stop_trigger_short = Entryprice - (entryprice * input_trailing_stop_trigger)/100;
  71.   trailing_stop_close_short = entryprice - (entryprice * input_trailing_stop_close)/100;
  72.  
  73.   // Originale
  74.   {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)
  75.      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)
  76.      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)
  77.      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)
  78.      or (MarketPosition =1 and high[16] >= trailing_stop_trigger_long and Close < trailing_stop_close_long and close[1] > trailing_stop_close_long);
  79.  
  80.   // Originale
  81.   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)
  82.      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)
  83.      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)
  84.      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)
  85.      or (MarketPosition =-1 and low[16] <= trailing_stop_trigger_short and  Close > trailing_stop_close_short and close[1] < trailing_stop_close_short);}
  86.  
  87.  {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)
  88.      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)
  89.      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)
  90.      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)
  91.      or (MarketPosition =1 and high[16] >= trailing_stop_trigger_long and Close cross under trailing_stop_close_long);
  92.  
  93.   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)
  94.      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)
  95.      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)
  96.      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)
  97.      or (MarketPosition =-1 and low[16] <= trailing_stop_trigger_short and  Close cross over trailing_stop_close_short)}
  98.  
  99.   highesthigh = highest(high, Barssinceentry);
  100.   trailing_long = (highesthigh >= trailing_stop_trigger_long and close <= trailing_stop_close_long);
  101.  
  102.   lowestlow = lowest(low, Barssinceentry); 
  103.   trailing_short = (lowestlow <= trailing_stop_trigger_short and close >= trailing_stop_close_short);
  104.  
  105.  
  106. // Money menagment
  107.  
  108.   risk = percent_risk/100;
  109.   nr_share = floor((InitialCapital + NetProfit) * risk) / close ;
  110.   stop_loss=((((InitialCapital + NetProfit) * risk)/100) * input_stop_loss_percent);
  111.  
  112.  
  113. // Entrata Long
  114.                                                                                         { and (close[1] < ema1 + apertura_minima or low[1] < ema1 + apertura_minima Tradingview}
  115.     //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
  116.       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
  117.       Buy("Long") nr_share contracts Next Bar at market;
  118.        
  119.        
  120. // Uscita Long
  121.      
  122.     if close < media_ema - chiusura_minima then
  123.        Sell("Chiusura Long") from entry("Long") Next Bar at market;
  124.      
  125.  // Uscita Trailing Long
  126.      
  127.     If (MarketPosition =1 and trailing_long) then
  128.    
  129.      Sell("Trailing Long") from entry("Long") Next Bar at market;
  130.  
  131.      
  132.      
  133.      
  134.      
  135. // Entrata Short
  136.                                                                                            {and (close[1] > ema1 - apertura_minima or low[1] > ema1 - apertura_minima)}
  137.       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
  138.       Sellshort("Short") nr_share Contracts Next Bar at market;
  139.            
  140.    
  141. // Uscita Short
  142.  
  143.     if close > (media_ema + chiusura_minima) then
  144.         Buytocover("Chiusura Short") from entry("Short") Next Bar at market;
  145.  
  146.   // Uscita Trailing Short
  147.        
  148.      If (MarketPosition =-1 and trailing_short)
  149.      
  150.   then
  151.      
  152.         Buytocover("Trailing Short") from entry("Short") Next Bar at market;
  153.      
  154. Setstopposition;
  155. SetStopLoss(stop_loss);
Add Comment
Please, Sign In to add comment