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++;
}
}
}
}
Comment