Maurizio-Ciullo

Bot Futures @NG Natural Gas Breakout Long E Short 15 min

Jan 22nd, 2026 (edited)
94
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                                                         // ============================================================
  2.                                                         // *** BOT FUTURES @NG NATURAL GAS BREAKOUT LONG E SHORT 15 MIN  *** //
  3.                                                         // ============================================================
  4.                                                        
  5.                                                                                                                                                        
  6. // ============================================================
  7. // TIPO STRATEGIA: BREAKOUT CLOSE 23:00 + BUFFER
  8. // STRUMENTO: FUTURES
  9. // TICKER: TRADESTATION @NG
  10. // TIMEFRAME: 15 MIN
  11. // DATI: TRADESTATION DAL 01/01/2010 AL 01/12/2025
  12. // SESSIONI: EXCHANGE TIME
  13. // DOM-LUN 18:00 - 17:00 SESSION END YES
  14. // LUN-MAR 18:00 - 17:00 SESSION END YES
  15. // MAR-MER 18:00 - 17:00 SESSION END YES
  16. // MER-GIO 18:00 - 17:00 SESSION END YES
  17. // GIO-VEN 18:00 - 17:00 SESSION END YES
  18. // MINI PAUSA : NO
  19. // BARS BACK: 50
  20. // COMMISSIONI SVILUPPO: 0
  21. // COMMISSIONI DOPO SVILUPPO: 2.5 $, 5 $ TOT, 4 TICK SLIPPAGE
  22. // POSITION SIZE SVILUOPPO 1 CONTRATTO
  23. // CORRELAZIONI STRUMENTI:  ?
  24. // ============================================================
  25.  
  26. // ==================LONG======================================
  27. // DESCRIZIONE INGRESSO 1: INGRESSO BREAKOUT CLOSE 23:00 + BUFFER / INGRESSO FASCE ORARIE
  28. // DESCRIZIONE INGRESSO 2: INGRESSO CLOSE > DELLE 3 CHIUSURE PRECEDENTI / INGRESSO FASCE ORARIE
  29. // DESCRIZIONE USCITA 1 E 2: ORARIA / STOP LOSS MONETARIO / TAKE PROFIT MONETARIO / TRAILING STOP MONETARIO
  30. // ESCLUSIONI: GIORNO: GIOVEDI' / MESE DICEMBRE // SKIP MONTH VALUTABILE: https://www.barchart.com/futures/quotes/NGF26/seasonality-chart
  31. // ============================================================
  32.  
  33. // ==================SHORT======================================
  34. // DESCRIZIONE INGRESSO: INGRESSO BREAKOUT CLOSE 23:00 - BUFFER / INGRESSO FASCE ORARIE
  35. // DESCRIZIONE USCITA: NUMERO CANDELE DA ENTRY MINIMO CON ORARIO PREDEFINITO / STOP LOSS MONETARIO / TRAILING STOP MONETARIO
  36. // ESCLUSIONI: MESE APRILE / SKIP MONTH VALUTABILE: https://www.barchart.com/futures/quotes/NGF26/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 <= 1152987) or (Date >= 1175654 and Date <= 1228538) or marketposition <> 0)) or
  47. ((displayMode = 2 or displayMode = 3) and isSampleOutOfSampleActive = 1 and ((Date > 1152988 and Date <= 1175653) or (Date > 1228539 and Date <= 1251204) 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.     EnableSecondEntryLong(true),  // true = abilita secondo ingresso, false = solo primo ingresso
  62.     EntryTimeLong(1000),
  63.     StopEntryTimeLong(1430),
  64.     ExitTimeLong(800),
  65.     RangePtnLong(0.05),
  66.     StopLossLong(980),
  67.     TakeProfitLong(4500),
  68.     BreakEvenPriceLong(1460),     // anche 1200 va bene, più protettivo abbianto a ProfitBufferLong 800
  69.     ProfitBufferLong(900),
  70.     MyPtnLY(41),
  71.     MyPtnLN(7),
  72.     InizioInputShort("------------InizioInputShort------------"),
  73.    
  74. // ============================================================
  75. // INPUTS SHORT
  76. // ============================================================
  77.     EntryTimeShort(800),
  78.     StopEntryTimeShort(900),
  79.     ExitTimeShort(800),          // anche 700 va bene
  80.     MinBarsInTrade(25),        // numero barre minime in trade
  81.     RangePtnShort(0.05),
  82.     StopLossShort(1350),        
  83.     //TakeProfitShort(100000),    
  84.     BreakEvenPriceShort(1200),
  85.     ProfitBufferShort(600),
  86.     MyPtnSY(41),
  87.     MyPtnSN(7);
  88.  
  89. // ============================================================
  90. // VARS LONG
  91. // ============================================================    
  92. Vars:
  93.     closingPriceLong(0),
  94.     ptnLong(false),
  95.     tradesTodayLong(0),
  96.     maxPrev3(0),
  97.     IntraBarPersist MaxPriceDuringTradeLong(0),
  98.     BufferPointsLong(0),
  99.  
  100. // ============================================================
  101. // VARS SHORT
  102. // ============================================================      
  103.     InEntryTimeWindowShort(false),
  104.     closingPriceShort(0),
  105.     ptnShort(false),
  106.     myCounter(0),            // contatore candele intratrade
  107.     IntraBarPersist MinPriceDuringTradeShort(0),
  108.     BufferPointsShort(0);  
  109.  
  110. // ============================================================
  111. // PTN BREAKOUT CLOSE 23:00 + BUFFER LONG
  112. // ============================================================  
  113. // ===== SALVA CLOSE DELLE 23:00 =====
  114. if time = 2300 then
  115.     closingPriceLong = Close;
  116.    
  117. ptnLong = (closingPriceLong <> 0) and (Close > closingPriceLong + RangePtnLong);
  118.  
  119. // ============================================================
  120. // RESET CONTATORE SESSIONE DEL CONTEGGIO NUMERO DI TRADES NELLA STESSA SESSIONE
  121. // ============================================================
  122. if Date <> Date[1] or Time < Time[1] then
  123.     tradesTodayLong = 0;
  124.  
  125. // ============================================================
  126. // CALCOLO MASSIMO DELLE 3 CHIUSURE PRECEDENTI
  127. // ============================================================    
  128. maxPrev3 = Highest(Close[1], 3);  // <--- QUI, ogni barra
  129.  
  130. // ============================================================
  131. // ENTRY LONG
  132. // ============================================================  
  133. if time >= EntryTimeLong and time <= StopEntryTimeLong
  134.    and ptnLong and not OnlyShort
  135.    and DayOfWeek(Date) <> 4                      // giovedi': Gas Storage Report dell’EIA (Energy Information Administration).
  136.    and Month(Date) <> 12 then
  137.    
  138. begin
  139.     if tradesTodayLong = 0 and MarketPosition = 0 then
  140.     begin
  141.         // Primo ingresso → regole standard
  142.         buy ("Long") next bar at market;
  143.         tradesTodayLong = tradesTodayLong + 1;
  144.         //Print("Primo ingresso Long eseguito, tradesToday=", tradesToday:1:0);
  145.     end
  146.     else if tradesTodayLong = 1 and EnableSecondEntryLong and MarketPosition = 0 then
  147.     begin
  148.         // Secondo ingresso → filtro aggiuntivo: Close > max delle ultime 3 chiusure
  149.         if Close > maxPrev3 then
  150.         begin
  151.             buy ("Long2") next bar at market;
  152.             tradesTodayLong = tradesTodayLong + 1;
  153.             //Print(
  154.             //  "Secondo ingresso Long eseguito | ",
  155.             //  "tradesTodayLong=", tradesTodayLong,
  156.             //  " | Data=", Date,
  157.             //  " | Ora=", Time:4:0);
  158.        
  159.             //Print("Secondo ingresso Long eseguito, tradesTodayLong=", tradesTodayLong:1:0);
  160.         end;
  161.     end;
  162. end;
  163.  
  164. // ============================================================
  165. // USCITA LONG 1 E 2 ORARIA
  166. // ============================================================
  167. // Uscita Long 1
  168. if time = ExitTimeLong then
  169.     sell ("ExitLong") from entry("Long") next bar at market;
  170.  
  171. // Uscita Long 2
  172. if time = ExitTimeLong then
  173.     sell ("ExitLong2") from entry("Long2") next bar at market;
  174.    
  175. // ============================================================
  176. // STOP LOSS E TAKE PROFIT LONG 1 E 2
  177. // ============================================================
  178. if MarketPosition = 1 then begin
  179.     SetStopLoss(StopLossLong);
  180.     SetProfitTarget(TakeProfitLong);
  181. end;                          
  182.  
  183. // ============================================================
  184. // BREAKEVEN + BUFFER LONG 1 E 2
  185. // ============================================================    
  186. // Conversione $ -> punti (1 punto @NG = $10000)
  187. BufferPointsLong = ProfitBufferLong / bigpointvalue;  // 10000  
  188.  
  189. // ============================================================  
  190. // EXIT BREAKEVEN LONG TRADE 1 E 2
  191. // ============================================================  
  192. If MarketPosition = 1 Then Begin
  193.     // Inizio trade: registra massimo intratrade
  194.     If MaxPriceDuringTradeLong = 0 Then
  195.         MaxPriceDuringTradeLong = EntryPrice;
  196.     // Aggiorna massimo intratrade
  197.     If High > MaxPriceDuringTradeLong Then
  198.         MaxPriceDuringTradeLong = High;
  199.     // Attiva breakeven + buffer se profitto raggiunto
  200.     If (MaxPriceDuringTradeLong - EntryPrice) * bigpointvalue >= BreakEvenPriceLong Then  // 10000 BPV
  201.         Sell ("BEPlusStopLong") next bar at EntryPrice + BufferPointsLong stop;
  202. End;
  203. // ============================================================  
  204. // RESET VARIABILI ALLA CHIUSURA DEL TRADE PER IL TRAILING
  205. // ============================================================  
  206. If MarketPosition = 0 Then Begin
  207.     MaxPriceDuringTradeLong = 0;
  208. End;  
  209.                                                                                                                                                                                                                                                    
  210. //                                                          ============================================================
  211. //                                                                                     SHORT
  212. //                                                          ============================================================
  213.  
  214. // ============================================================
  215. // PTN BREAKOUT CLOSE 23:00 - BUFFER SHORT
  216. // ============================================================
  217. // ===== SALVA CLOSE DELLE 23:00 =====
  218. if time = 2300 then
  219.     closingPriceShort = Close;
  220.  
  221. ptnShort = (closingPriceShort <> 0) and (Close < closingPriceShort - RangePtnShort);
  222.  
  223. // ============================================================
  224. // ENTRY SHORT
  225. // ============================================================
  226. if time >= EntryTimeShort and time <= StopEntryTimeShort                                              
  227.    and ptnShort
  228.    and not OnlyLong
  229.    and MarketPosition = 0
  230.    and Month(Date) <> 4 then begin                          // filtro poco rilevante ma solo perché nei periodi rialzisti non si sono verificati breakout ribassisti e non siamo entrati, ma filtro persistente e duraturo con alta % mensile rialzista
  231.    Sellshort ("Short") next bar at market;
  232.    myCounter = 0;                                           // reset contatore all’ingresso per contatore candele intratrade                  
  233. end;
  234.  
  235. // ============================================================
  236. // AGGIORNA CONTATORE SE SEI IN TRADE
  237. // ============================================================
  238. if MarketPosition = -1 then
  239.     myCounter = myCounter + 1;
  240.    
  241. // ============================================================  
  242. // USCITA SHORT ORARIA SESSIONE SUCCESSIVA
  243. // ============================================================                                                                              
  244. if MarketPosition = -1 then
  245. begin
  246.     // uscita per numero minimo candele
  247.     if myCounter >= MinBarsInTrade and time = ExitTimeShort then     // ** PER OTTIMIZZAZIONE PRODUZIONE **
  248.     //if myCounter >= MinBarsInTrade and time >= ExitTimeShort and time <= 845 then   // ** PER TRADING LIVE REALE ** ** SOLUZIONE CHE GARANTISCE USCITA SE MANCA CANDELA ORA SPECIFICA **
  249.     begin
  250.         BuyToCover("ExitShort_MinBars") from entry("Short") next bar at market;
  251.         myCounter = 0;  // reset contatore all’uscita
  252.     end;
  253. end;
  254.  
  255. // ============================================================
  256. // STOP LOSS E TAKE PROFIT SHORT
  257. // ============================================================
  258.    
  259. if MarketPosition = -1 then
  260. begin
  261.     SetStopLoss(StopLossShort);
  262.     //SetProfitTarget(TakeProfitShort);
  263. end;
  264.  
  265. // ============================================================
  266. // BREAKEVEN - BUFFER SHORT //
  267. // ============================================================  
  268. // Conversione $ -> punti (1 punto @NG = $10000)
  269. BufferPointsShort = ProfitBufferShort / bigpointvalue;  // 10000
  270.  
  271. // ============================================================  
  272. // EXIT BREAKEVEN SHORT TRADE
  273. // ============================================================  
  274. If MarketPosition = -1 Then Begin
  275.     // Inizio trade: registra minimo intratrade
  276.     If MinPriceDuringTradeShort = 0 Then
  277.         MinPriceDuringTradeShort = EntryPrice;
  278.  
  279.     // Aggiorna minimo intratrade
  280.     If Low < MinPriceDuringTradeShort Then
  281.         MinPriceDuringTradeShort = Low;
  282.  
  283.     // Attiva breakeven + buffer se profitto raggiunto
  284.     If (EntryPrice - MinPriceDuringTradeShort) * bigpointvalue >= BreakEvenPriceShort Then  // 10000 BPV
  285.         BuyToCover ("BEPlusStopShort") next bar at EntryPrice - BufferPointsShort stop;
  286. End;
  287.  
  288. // ============================================================  
  289. // RESET VARIABILI ALLA CHIUSURA DEL TRADE PER IL TRAILING
  290. // ============================================================  
  291. If MarketPosition = 0 Then Begin
  292.     MinPriceDuringTradeShort = 0;
  293. End;                                                                                                                                                                                                                                                                                                                                                                                                                         
  294.  
  295. {---------------- FOOTER INIZIO: DA ELIMINARE ----------------}
  296. end;
  297. {---------------- FOOTER FINE: DA ELIMINARE ----------------}  
  298.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment