Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ============================================================
- // *** BOT FUTURES @NG NATURAL GAS BREAKOUT LONG E SHORT 15 MIN *** //
- // ============================================================
- // ============================================================
- // TIPO STRATEGIA: BREAKOUT CLOSE 23:00 + BUFFER
- // STRUMENTO: FUTURES
- // TICKER: TRADESTATION @NG
- // TIMEFRAME: 15 MIN
- // DATI: TRADESTATION DAL 01/01/2010 AL 01/12/2025
- // SESSIONI: EXCHANGE TIME
- // DOM-LUN 18:00 - 17:00 SESSION END YES
- // LUN-MAR 18:00 - 17:00 SESSION END YES
- // MAR-MER 18:00 - 17:00 SESSION END YES
- // MER-GIO 18:00 - 17:00 SESSION END YES
- // GIO-VEN 18:00 - 17:00 SESSION END YES
- // MINI PAUSA : NO
- // BARS BACK: 50
- // COMMISSIONI SVILUPPO: 0
- // COMMISSIONI DOPO SVILUPPO: 2.5 $, 5 $ TOT, 4 TICK SLIPPAGE
- // POSITION SIZE SVILUOPPO 1 CONTRATTO
- // CORRELAZIONI STRUMENTI: ?
- // ============================================================
- // ==================LONG======================================
- // DESCRIZIONE INGRESSO 1: INGRESSO BREAKOUT CLOSE 23:00 + BUFFER / INGRESSO FASCE ORARIE
- // DESCRIZIONE INGRESSO 2: INGRESSO CLOSE > DELLE 3 CHIUSURE PRECEDENTI / INGRESSO FASCE ORARIE
- // DESCRIZIONE USCITA 1 E 2: ORARIA / STOP LOSS MONETARIO / TAKE PROFIT MONETARIO / TRAILING STOP MONETARIO
- // ESCLUSIONI: GIORNO: GIOVEDI' / MESE DICEMBRE // SKIP MONTH VALUTABILE: https://www.barchart.com/futures/quotes/NGF26/seasonality-chart
- // ============================================================
- // ==================SHORT======================================
- // DESCRIZIONE INGRESSO: INGRESSO BREAKOUT CLOSE 23:00 - BUFFER / INGRESSO FASCE ORARIE
- // DESCRIZIONE USCITA: NUMERO CANDELE DA ENTRY MINIMO CON ORARIO PREDEFINITO / STOP LOSS MONETARIO / TRAILING STOP MONETARIO
- // ESCLUSIONI: MESE APRILE / SKIP MONTH VALUTABILE: https://www.barchart.com/futures/quotes/NGF26/seasonality-chart
- // ============================================================
- // ============================================================
- // *** Inizio Parte 1 Study Funzione In-Sample Out-Sample QTA *** //
- // ============================================================
- {---------------- HEADER INIZIO: DA ELIMINARE ----------------}
- Inputs: isSampleOutOfSampleActive(0), displayMode(1);
- if isSampleOutOfSampleActive = 0 or (
- ((displayMode = 1 or displayMode = 3) and isSampleOutOfSampleActive = 1 and ((Date >= 1100103 and Date <= 1152987) or (Date >= 1175654 and Date <= 1228538) or marketposition <> 0)) or
- ((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
- {---------------- HEADER FINE: DA ELIMINARE ----------------}
- // *** Fine Parte 1 Study Funzione In-Sample Out-Sample QTA *** //
- // ============================================================
- // LONG
- // ============================================================
- // ============================================================
- // INPUTS LONG
- // ============================================================
- Inputs:
- OnlyLong(false),
- OnlyShort(false),
- EnableSecondEntryLong(true), // true = abilita secondo ingresso, false = solo primo ingresso
- EntryTimeLong(1000),
- StopEntryTimeLong(1430),
- ExitTimeLong(800),
- RangePtnLong(0.05),
- StopLossLong(980),
- TakeProfitLong(4500),
- BreakEvenPriceLong(1460), // anche 1200 va bene, più protettivo abbianto a ProfitBufferLong 800
- ProfitBufferLong(900),
- MyPtnLY(41),
- MyPtnLN(7),
- InizioInputShort("------------InizioInputShort------------"),
- // ============================================================
- // INPUTS SHORT
- // ============================================================
- EntryTimeShort(800),
- StopEntryTimeShort(900),
- ExitTimeShort(800), // anche 700 va bene
- MinBarsInTrade(25), // numero barre minime in trade
- RangePtnShort(0.05),
- StopLossShort(1350),
- //TakeProfitShort(100000),
- BreakEvenPriceShort(1200),
- ProfitBufferShort(600),
- MyPtnSY(41),
- MyPtnSN(7);
- // ============================================================
- // VARS LONG
- // ============================================================
- Vars:
- closingPriceLong(0),
- ptnLong(false),
- tradesTodayLong(0),
- maxPrev3(0),
- IntraBarPersist MaxPriceDuringTradeLong(0),
- BufferPointsLong(0),
- // ============================================================
- // VARS SHORT
- // ============================================================
- InEntryTimeWindowShort(false),
- closingPriceShort(0),
- ptnShort(false),
- myCounter(0), // contatore candele intratrade
- IntraBarPersist MinPriceDuringTradeShort(0),
- BufferPointsShort(0);
- // ============================================================
- // PTN BREAKOUT CLOSE 23:00 + BUFFER LONG
- // ============================================================
- // ===== SALVA CLOSE DELLE 23:00 =====
- if time = 2300 then
- closingPriceLong = Close;
- ptnLong = (closingPriceLong <> 0) and (Close > closingPriceLong + RangePtnLong);
- // ============================================================
- // RESET CONTATORE SESSIONE DEL CONTEGGIO NUMERO DI TRADES NELLA STESSA SESSIONE
- // ============================================================
- if Date <> Date[1] or Time < Time[1] then
- tradesTodayLong = 0;
- // ============================================================
- // CALCOLO MASSIMO DELLE 3 CHIUSURE PRECEDENTI
- // ============================================================
- maxPrev3 = Highest(Close[1], 3); // <--- QUI, ogni barra
- // ============================================================
- // ENTRY LONG
- // ============================================================
- if time >= EntryTimeLong and time <= StopEntryTimeLong
- and ptnLong and not OnlyShort
- and DayOfWeek(Date) <> 4 // giovedi': Gas Storage Report dell’EIA (Energy Information Administration).
- and Month(Date) <> 12 then
- begin
- if tradesTodayLong = 0 and MarketPosition = 0 then
- begin
- // Primo ingresso → regole standard
- buy ("Long") next bar at market;
- tradesTodayLong = tradesTodayLong + 1;
- //Print("Primo ingresso Long eseguito, tradesToday=", tradesToday:1:0);
- end
- else if tradesTodayLong = 1 and EnableSecondEntryLong and MarketPosition = 0 then
- begin
- // Secondo ingresso → filtro aggiuntivo: Close > max delle ultime 3 chiusure
- if Close > maxPrev3 then
- begin
- buy ("Long2") next bar at market;
- tradesTodayLong = tradesTodayLong + 1;
- //Print(
- // "Secondo ingresso Long eseguito | ",
- // "tradesTodayLong=", tradesTodayLong,
- // " | Data=", Date,
- // " | Ora=", Time:4:0);
- //Print("Secondo ingresso Long eseguito, tradesTodayLong=", tradesTodayLong:1:0);
- end;
- end;
- end;
- // ============================================================
- // USCITA LONG 1 E 2 ORARIA
- // ============================================================
- // Uscita Long 1
- if time = ExitTimeLong then
- sell ("ExitLong") from entry("Long") next bar at market;
- // Uscita Long 2
- if time = ExitTimeLong then
- sell ("ExitLong2") from entry("Long2") next bar at market;
- // ============================================================
- // STOP LOSS E TAKE PROFIT LONG 1 E 2
- // ============================================================
- if MarketPosition = 1 then begin
- SetStopLoss(StopLossLong);
- SetProfitTarget(TakeProfitLong);
- end;
- // ============================================================
- // BREAKEVEN + BUFFER LONG 1 E 2
- // ============================================================
- // Conversione $ -> punti (1 punto @NG = $10000)
- BufferPointsLong = ProfitBufferLong / bigpointvalue; // 10000
- // ============================================================
- // EXIT BREAKEVEN LONG TRADE 1 E 2
- // ============================================================
- If MarketPosition = 1 Then Begin
- // Inizio trade: registra massimo intratrade
- If MaxPriceDuringTradeLong = 0 Then
- MaxPriceDuringTradeLong = EntryPrice;
- // Aggiorna massimo intratrade
- If High > MaxPriceDuringTradeLong Then
- MaxPriceDuringTradeLong = High;
- // Attiva breakeven + buffer se profitto raggiunto
- If (MaxPriceDuringTradeLong - EntryPrice) * bigpointvalue >= BreakEvenPriceLong Then // 10000 BPV
- Sell ("BEPlusStopLong") next bar at EntryPrice + BufferPointsLong stop;
- End;
- // ============================================================
- // RESET VARIABILI ALLA CHIUSURA DEL TRADE PER IL TRAILING
- // ============================================================
- If MarketPosition = 0 Then Begin
- MaxPriceDuringTradeLong = 0;
- End;
- // ============================================================
- // SHORT
- // ============================================================
- // ============================================================
- // PTN BREAKOUT CLOSE 23:00 - BUFFER SHORT
- // ============================================================
- // ===== SALVA CLOSE DELLE 23:00 =====
- if time = 2300 then
- closingPriceShort = Close;
- ptnShort = (closingPriceShort <> 0) and (Close < closingPriceShort - RangePtnShort);
- // ============================================================
- // ENTRY SHORT
- // ============================================================
- if time >= EntryTimeShort and time <= StopEntryTimeShort
- and ptnShort
- and not OnlyLong
- and MarketPosition = 0
- 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
- Sellshort ("Short") next bar at market;
- myCounter = 0; // reset contatore all’ingresso per contatore candele intratrade
- end;
- // ============================================================
- // AGGIORNA CONTATORE SE SEI IN TRADE
- // ============================================================
- if MarketPosition = -1 then
- myCounter = myCounter + 1;
- // ============================================================
- // USCITA SHORT ORARIA SESSIONE SUCCESSIVA
- // ============================================================
- if MarketPosition = -1 then
- begin
- // uscita per numero minimo candele
- if myCounter >= MinBarsInTrade and time = ExitTimeShort then // ** PER OTTIMIZZAZIONE PRODUZIONE **
- //if myCounter >= MinBarsInTrade and time >= ExitTimeShort and time <= 845 then // ** PER TRADING LIVE REALE ** ** SOLUZIONE CHE GARANTISCE USCITA SE MANCA CANDELA ORA SPECIFICA **
- begin
- BuyToCover("ExitShort_MinBars") from entry("Short") next bar at market;
- myCounter = 0; // reset contatore all’uscita
- end;
- end;
- // ============================================================
- // STOP LOSS E TAKE PROFIT SHORT
- // ============================================================
- if MarketPosition = -1 then
- begin
- SetStopLoss(StopLossShort);
- //SetProfitTarget(TakeProfitShort);
- end;
- // ============================================================
- // BREAKEVEN - BUFFER SHORT //
- // ============================================================
- // Conversione $ -> punti (1 punto @NG = $10000)
- BufferPointsShort = ProfitBufferShort / bigpointvalue; // 10000
- // ============================================================
- // EXIT BREAKEVEN SHORT TRADE
- // ============================================================
- If MarketPosition = -1 Then Begin
- // Inizio trade: registra minimo intratrade
- If MinPriceDuringTradeShort = 0 Then
- MinPriceDuringTradeShort = EntryPrice;
- // Aggiorna minimo intratrade
- If Low < MinPriceDuringTradeShort Then
- MinPriceDuringTradeShort = Low;
- // Attiva breakeven + buffer se profitto raggiunto
- If (EntryPrice - MinPriceDuringTradeShort) * bigpointvalue >= BreakEvenPriceShort Then // 10000 BPV
- BuyToCover ("BEPlusStopShort") next bar at EntryPrice - BufferPointsShort stop;
- End;
- // ============================================================
- // RESET VARIABILI ALLA CHIUSURA DEL TRADE PER IL TRAILING
- // ============================================================
- If MarketPosition = 0 Then Begin
- MinPriceDuringTradeShort = 0;
- End;
- {---------------- FOOTER INIZIO: DA ELIMINARE ----------------}
- end;
- {---------------- FOOTER FINE: DA ELIMINARE ----------------}
Advertisement