Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Tracking gaps on ES Futures

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Tracking gaps on ES Futures

    Hi all,

    I am trying to create a simple script that checks for gap ups or gap downs on ES futures. The logic is quite simple so I'm not sure where I'm screwing up (output results are incorrect)

    All I want to do is the following

    1) Compare the first 5 minute bar data with the last 5 minute bar of the previous day
    2) Compute whether there was a gap up or gap down
    3) Print the gap results at the end of the script

    For some reason when I run the script against 2019, the logic is running on the 8:35am bar instead of the 9:35am bar (close of the first bar, BarsRequiredToTrade being 1). I am also not getting any gap prints once the script is terminated. Hoping an expert can chime in here and point me in the right direction.

    I am testing the logic in the Strategy Analyzer, and I have the template set specifically to US Equities RTH because I want 9:30-4pm EST data to be included only.


    namespace NinjaTrader.NinjaScript.Strategies.JBAlgos

    {



    public class TradeOrFade : Strategy

    {



    private int gapCount;

    private int gapUps;

    private int gapDowns;



    protected override void OnStateChange()

    {

    if (State == State.SetDefaults)

    {

    Description = @"Enter the description for your new custom Strategy here.";

    Name = "TradeOrFade";

    Calculate = Calculate.OnBarClose;

    EntriesPerDirection = 1;

    EntryHandling = EntryHandling.AllEntries;

    IsExitOnSessionCloseStrategy = true;

    ExitOnSessionCloseSeconds = 30;

    IsFillLimitOnTouch = false;

    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;

    OrderFillResolution = OrderFillResolution.Standard;

    Slippage = 0;

    StartBehavior = StartBehavior.WaitUntilFlat;

    TimeInForce = TimeInForce.Gtc;

    TraceOrders = false;

    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;

    StopTargetHandling = StopTargetHandling.PerEntryExecution;

    BarsRequiredToTrade = 1;

    // Disable this property for performance gains in Strategy Analyzer optimizations

    // See the Help Guide for additional information

    IsInstantiatedOnEachOptimizationIteration = true;

    }

    else if (State == State.Configure)

    {

    AddDataSeries(BarsPeriodType.Minute, 5);

    }

    else if(State == State.Terminated){

    Print(string.Format("Found {0} gaps in total, {1} were down gaps, {2} were up gaps:", gapCount, gapDowns, gapUps));

    }

    }




    protected override void OnBarUpdate()

    {

    // Check if there are enough bars loaded for the primary data series

    if (CurrentBar < BarsRequiredToTrade) return;



    Print(string.Format("Close[1], {0}, @ {1}", Close[1], Time[1]));

    Print(string.Format("Open[0], {0}, @ {1}", Open[0], Time[0]));



    if(Close[1] - Open[0] > 1){

    Print("Found down gap " + Time[0]);

    gapCount++;

    gapDowns++;

    }



    if(Open[0] - Close[1] > 1){

    Print("Found up gap " + Time[0]);

    gapCount++;

    gapUps++;

    }



    }

    }

    }
    jaybedreamin
    NinjaTrader Ecosystem Vendor - Zion Trading Algos

    #2
    Hello jaybedreamin,

    Thank you for your post.

    You may be able to utilize IsFirstBarOfSession and IsLastBarOfSession in order to get the desired results in your script. For more information, please see the following help guide pages:For the print when the State is State.Terminated, do you receive any error messages on the Log tab of the Control Center? More information about the NinjaScript Lifecycle and State.Terminated may be found at the following links:Please let me know if I may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    53 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X