Maurizio-Ciullo

Bot Futures @S Soia Breakout Long E Short 15 Min

Dec 15th, 2025 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.                                                         // ============================================================
  3.                                                         // *** BOT FUTURES @S SOIA BREAKOUT LONG E SHORT 15 MIN  *** //
  4.                                                         // ============================================================
  5.                                                        
  6.                                                                                                                                                        
  7. // ============================================================
  8. // TIPO STRATEGIA: BREAKOUT SESSIONE PRECEDENTE
  9. // STRUMENTO: FUTURES
  10. // TICKER: TRADESTATION @S
  11. // TIMEFRAME: 15 MIN
  12. // DATI: TRADESTATION DAL 04/01/2010 AL 27/10/2025
  13. // SESSIONI: EXCHANGE TIME
  14. // DOM-LUN 19:00 - 13:20 SESSION END YES
  15. // LUN-MAR 19:00 - 13:20 SESSION END YES
  16. // MAR-MER 19:00 - 13:20 SESSION END YES
  17. // MER-GIO 19:00 - 13:20 SESSION END YES
  18. // GIO-VEN 19:00 - 13:20 SESSION END YES
  19. // MINI PAUSA : 07:54 08:30
  20. // BARS BACK: 50
  21. // COMMISSIONI SVILUPPO: 0
  22. // COMMISSIONI DOPO SVILUPPO: 3$, 6 $ TOT, 1 TICK SLIPPAGE
  23. // POSITION SIZE SVILUOPPO 1 CONTRATTO
  24. // CORRELAZIONI STRUMENTI:  Corn (@C) / Wheat (@W)
  25. // ============================================================
  26.  
  27. // ==================LONG======================================
  28. // DESCRIZIONE INGRESSO: INGRESSO BREAKOUT MASSIMO SESSIONE PRECEDENTE / INGRESSO FASCE ORARIE / VERIFICA PATTERN DI PREZZO
  29. // DESCRIZIONE USCITA: NUMERO CANDELE DA ENTRY / STOP LOSS MONETARIO / TAKE PROFIT MONETARIO / TRAILING STOP MONETARIO
  30. // ESCLUSIONI: MESE SETTEMBRE
  31. // ============================================================
  32.  
  33. // ==================SHORT======================================
  34. // DESCRIZIONE INGRESSO: INGRESSO BREAKOUT MINIMO SESSIONE PRECEDENTE / INGRESSO FASCE ORARIE / VERIFICA PATTERN DI PREZZO
  35. // DESCRIZIONE USCITA: NUMERO CANDELE DA ENTRY MINIMO CON ORARIO PREDEFINITO / STOP LOSS MONETARIO / TAKE PROFIT MONETARIO / TRAILING STOP MONETARIO
  36. // ESCLUSIONI: MESE NESSUNO / SKIP MONTH VALUTABILE: https://www.barchart.com/futures/quotes/ZSF26/seasonality-chart
  37. // ============================================================
  38.  
  39.  
  40. // ============================================================
  41. // *** Inizio Parte 1 Study Funzione In-Sample Out-Sample QTA *** //
  42. // ============================================================
  43. {---------------- HEADER INIZIO: DA ELIMINARE ----------------}
  44. Inputs: isSampleOutOfSampleActive(0), displayMode(1);
  45. if isSampleOutOfSampleActive = 0 or (
  46. ((displayMode = 1 or displayMode = 3) and isSampleOutOfSampleActive = 1 and ((Date >= 1100103 and Date <= 1152954) or (Date >= 1175607 and Date <= 1228458) or marketposition <> 0)) or
  47. ((displayMode = 2 or displayMode = 3) and isSampleOutOfSampleActive = 1 and ((Date > 1152955 and Date <= 1175606) or (Date > 1228459 and Date <= 1251110) or marketposition <> 0))) then begin
  48. {---------------- HEADER FINE: DA ELIMINARE ----------------}
  49. // *** Fine Parte 1 Study Funzione In-Sample Out-Sample QTA *** //
  50.  
  51. //                                                          ============================================================
  52. //                                                                                     LONG
  53. //                                                          ============================================================
  54.  
  55. // ============================================================
  56. // INPUTS LONG
  57. // ============================================================
  58. Inputs:  
  59.     OnlyLong(false),
  60.     OnlyShort(false),                                                                                  
  61.     StopLossLong(1240),          
  62.     TakeProfitLong(2800),        // 1085 valutabile ma forse troppo piccolo              
  63.     EntryTimeLong(1100),      
  64.     EndEntryTimeLong(2030),      // Ora fine finestra ingresso anche 1330 va bene
  65.     //ExitTimeLong(200),
  66.     ExitAfterBarsLong(250),      // 250 (sono 3 sessioni) e considerando la fascia oraria di ingresso esco circa delle ore 200 o a qualche barra più in là.
  67.     ExcludeMonth1Long(9),        // Anche 5 va bene
  68.     BreakEvenPriceLong(2000),    // Non rilavantissimo, aiuta in qualche posizione senza dare fastidio
  69.     ProfitBufferLong(800),       // Non rilavantissimo, aiuta in qualche posizione senza dare fastidio
  70.     InizioInputShort("------------InizioInputShort------------"),
  71.     //MyPtnLY(14)                // Il PtnBaseSA(MyPtnLY) è inserito come: CloseSession(0,1) > CloseSession(0,2)
  72.  
  73. // ============================================================
  74. // INPUTS SHORT
  75. // ============================================================  ,
  76.     StopLossShort(1050),         // RANGE DA (950/1200) DA VALUTARE CON TUTTE LE OTTIMIZZAZIONI !!!
  77.     TakeProfitShort(2125),       // 2000  VA ANCHE BENE
  78.     EntryTimeShort(200),         // ora inizio finestra ingresso  200/300/400
  79.     EndEntryTimeShort(600),      // ora fine finestra ingresso  200/300/400             ATTENZIONE 200 300 STABILE MA SOLO 1 ORA POTREI OVERFITTARE, TROPPO POCO, ALTRIMENTI MI DEVO ALLUNGARE FINO A MX 600 !!! VALUTARE !!!
  80.     ExitTimeShort(1200),         // ora uscita  1100/1200
  81.     ExitAfterBarsShort(92),      // quante barre dopo l’ingresso esci
  82.     MyPtnSY(41), MyPtnSN(20),    // Il PTN 20 Toglie solo pochi trades ma è molto stabile "Opzionale Scelta Personale"
  83.     //ExcludeMonth1Short(10),    // Anche se va bene, ci sono 5 mesi che vanno molto bene per il Long allora non metto questo filtro, altra cosa short diventerebbe con troppi pochi trades !!!.
  84.     BreakEvenPriceShort(1800),   // Non rilavantissimo, aiuta in qualche posizione senza dare fastidio
  85.     ProfitBufferShort(800);      // Non rilavantissimo, aiuta in qualche posizione senza dare fastidio
  86.     //MyPtnSN(20)                // Il PtnBaseSA(MyPtnSN) è inserito come: not (HighSession(0,0) > HighSession(0,1))
  87.    
  88. // ============================================================
  89. // VARS LONG
  90. // ============================================================
  91. Vars:
  92.     IntraBarPersist InEntryWindow(false),
  93.     IntraBarPersist MaxPriceDuringTradeLong(0),  // Massimo prezzo intratrade
  94.     IntraBarPersist BarCounterLong(0),
  95.     BufferPointsLong(0),                         // Uscita se il prezzo ritraccia
  96.    
  97. // ============================================================
  98. // VARS SHORT
  99. // ============================================================
  100.     IntraBarPersist InEntryWindowShort(false),  
  101.     IntraBarPersist MinPriceDuringTradeShort(0), // Minimo prezzo intratrade
  102.     IntraBarPersist BarCounterShort(0),
  103.     BufferPointsShort(0);                        // Uscita se il prezzo ritraccia
  104.    
  105. // ============================================================
  106. // LOGICA UNIVERSALE PER FINESTRA ORARIA LONG
  107. // ============================================================    
  108.    
  109. // Se la finestra NON attraversa la mezzanotte → usa AND
  110. If EntryTimeLong < EndEntryTimeLong then
  111.     InEntryWindow = (Time >= EntryTimeLong) and (Time <= EndEntryTimeLong)      
  112. else
  113.     // Se attraversa la mezzanotte (es. 2200→1000) → usa OR
  114.     InEntryWindow = (Time >= EntryTimeLong) or (Time <= EndEntryTimeLong);
  115.  
  116. // ============================================================
  117. // ENTRY LONG
  118. // ============================================================    
  119.  
  120. If InEntryWindow and MarketPosition = 0 and month(date) <> ExcludeMonth1Long and not OnlyShort then begin      
  121.     If CloseSession(0,1) > CloseSession(0,2) then                                                             {PtnBaseSA(MyPtnLY) 14 }
  122.         Buy ("BreakPrevHigh") next bar at HighSession(1,1) stop;
  123.  end;
  124.  
  125. // ============================================================
  126. // USCITA LONG DOPO N BARRE
  127. // ============================================================
  128. If MarketPosition = 1 then begin
  129.     BarCounterLong = BarCounterLong + 1;
  130.  
  131.     If BarCounterLong >= ExitAfterBarsLong {and time >= ExitTimeLong} then begin
  132.         Sell ("ExitAfterBarsLong") next bar at market;
  133.         BarCounterLong = 0;
  134.     end;
  135. end else
  136.     BarCounterLong = 0;
  137.    
  138. // ============================================================
  139. // STOP LOSS / TAKE PROFIT LONG                                                
  140. // ============================================================
  141.  
  142. If MarketPosition = 1 then begin          
  143.     SetStopLoss(StopLossLong);
  144.     SetProfitTarget(TakeProfitLong);
  145. end;
  146.  
  147. // ============================================================
  148. // BREAKEVEN + BUFFER LONG //
  149. // ============================================================
  150. // Conversione $ -> punti (1 punto @S = $50)
  151. BufferPointsLong = ProfitBufferLong / 50;
  152.  
  153. // ============================
  154. // EXIT BREAKEVEN LONG TRADE
  155. // ============================
  156. If MarketPosition = 1 Then Begin
  157.     // Inizio trade: registra massimo intratrade
  158.     If MaxPriceDuringTradeLong = 0 Then
  159.         MaxPriceDuringTradeLong = EntryPrice;
  160.     // Aggiorna massimo intratrade
  161.     If High > MaxPriceDuringTradeLong Then
  162.         MaxPriceDuringTradeLong = High;
  163.     // Attiva breakeven + buffer se profitto raggiunto
  164.     If (MaxPriceDuringTradeLong - EntryPrice) * 50 >= BreakEvenPriceLong Then
  165.         Sell ("BEPlusStopLong") next bar at EntryPrice + BufferPointsLong stop;
  166. End;
  167. // ============================
  168. // RESET variabili alla chiusura del trade per trailing
  169. // ============================
  170. If MarketPosition = 0 Then Begin
  171.     MaxPriceDuringTradeLong = 0;
  172. End;                                                                                              
  173.                
  174. //                                                          ============================================================
  175. //                                                                                     SHORT
  176. //                                                          ============================================================              
  177.                          
  178. // ============================================================
  179. // LOGICA UNIVERSALE PER FINESTRA ORARIA SHORT
  180. // ============================================================
  181.  
  182. // Se la finestra NON attraversa la mezzanotte (es. 02:00 → 06:00) → usa AND
  183. If EntryTimeShort < EndEntryTimeShort then
  184.     InEntryWindowShort = (Time >= EntryTimeShort) and (Time <= EndEntryTimeShort)
  185. // Se la finestra attraversa la mezzanotte (es. 22:00 → 03:00) → usa OR  
  186. else
  187.     InEntryWindowShort = (Time >= EntryTimeShort) or (Time <= EndEntryTimeShort);
  188.  
  189. // ============================================================
  190. // TEST ENTRATA SHORT ORARIO CON BREAKOUT
  191. // ============================================================
  192.  
  193. If InEntryWindowShort and MarketPosition = 0 {and month(date) <> ExcludeMonth1Short} and not OnlyLong then begin
  194.     If  not (HighSession(0,0) > HighSession(0,1)) then                                                        {PtnBaseSA(MyPtnSN) 20}  
  195.         SellShort("BreakPrevLow") next bar at LowSession(1,1) stop;    
  196. end;
  197.  
  198. // ============================================================
  199. // USCITA DOPO N BARRE
  200. // ============================================================
  201. If MarketPosition = -1 then begin
  202.     BarCounterShort = BarCounterShort + 1;
  203.  
  204.     If BarCounterShort >= ExitAfterBarsShort and time >= ExitTimeShort then begin    // * ATTENZIONE A DIFFERENZA DEL LONG ABBIAMO USCITA BARRE > DI INPUT CON ORARIO FORZATO A INPUT 1200 * //
  205.         Buytocover ("ExitAfterBarsShort") next bar at market;
  206.         BarCounterShort = 0;
  207.     end;
  208. end else
  209.     BarCounterShort = 0;
  210.  
  211. // ============================================================
  212. // STOP LOSS SHORT / TAKE PROFIT SHORT                                             
  213. // ============================================================
  214.  
  215. If MarketPosition = -1 then begin           // Condizione Tk e Sl Solo Per Long, ATTENZIONE GLI ORDINI ENTRANO DOPO LA PRIMA BARRA !!!
  216.     SetStopLoss(StopLossShort);
  217.     SetProfitTarget(TakeProfitShort);
  218. end;
  219.  
  220. // ============================================================
  221. // BREAKEVEN + BUFFER SHORT
  222. // ============================================================
  223. // Conversione $ -> punti (1 punto @S = $50)
  224. BufferPointsShort = ProfitBufferShort / 50;
  225.  
  226. // ============================
  227. // EXIT BREAKEVEN SHORT TRADE
  228. // ============================
  229. If MarketPosition = -1 Then Begin
  230.     // Inizio trade: registra minimo intratrade
  231.     If MinPriceDuringTradeShort = 0 Then
  232.         MinPriceDuringTradeShort = EntryPrice;
  233.  
  234.     // Aggiorna minimo intratrade
  235.     If Low < MinPriceDuringTradeShort Then
  236.         MinPriceDuringTradeShort = Low;
  237.  
  238.     // Attiva breakeven + buffer se profitto raggiunto
  239.     If (EntryPrice - MinPriceDuringTradeShort) * 50 >= BreakEvenPriceShort Then
  240.         BuyToCover ("BEPlusStopShort") next bar at EntryPrice - BufferPointsShort stop;
  241. End;
  242.  
  243. // ============================
  244. // RESET variabili alla chiusura del trade
  245. // ============================
  246. If MarketPosition = 0 Then Begin
  247.     MinPriceDuringTradeShort = 0;
  248. End;
  249.  
  250. // ============================================================
  251. // Inizio Parte 2 Study Funzione In-Sample Out-Sample QTA *** //
  252. // ============================================================
  253.  
  254. {---------------- FOOTER INIZIO: DA ELIMINARE ----------------}
  255. end;
  256. {---------------- FOOTER FINE: DA ELIMINARE ----------------}
  257.  
  258. // ============================================================
  259. // *** Fine Parte 2 Study Funzione In-Sample Out-Sample QTA *** //
  260. // ============================================================
  261.  
Advertisement
Add Comment
Please, Sign In to add comment