Ok, I have a second issue: the method works great except that it updates the current bar on the :01 and :06 minutes and not the :00 and :05 minutes. I actually need this to shift to the left by one minute in order to align perfectly with the :00 and :05 changes for the normal historical bars. The update at :01 creates a problem because the :00 and :01 values are different, which leads to the potential for :59, :00 and :01 having three different values rather than :59 and :00 having two different values when I am doing a calculation that involves the historical bars and the current bar.
Please find below a simple code with the current bar updating at :01 and :06. I also attach a picture. Could you please let me know if there's a solution to this problem?
I truly appreciate your help!!!
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class A1TEST3 : Strategy
{
private Series<double> test;
private DateTime cacheTime;
private int cacheBar;
private double barOpen;
private double barHigh;
private double barLow;
private double barClose;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "A1TEST3";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.AdoptAccountPosition;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
AddPlot(Brushes.WhiteSmoke, "StrategyPlot");
// 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(Data.BarsPeriodType.Minute, 5);
}
else if (State == State.DataLoaded)
{
test = new Series<double>(this, MaximumBarsLookBack.Infinite);
//int cacheHigh = 0;
//int cacheLow = 0;
//barHigh = new Series<double>(this, MaximumBarsLookBack.Infinite);
//barLow = new Series<double>(this, MaximumBarsLookBack.Infinite);
Plots[0].Width = 4;
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 5
|| CurrentBars[1] < 5)
return;
if (cacheTime != Times[0][0])
{
cacheTime = Times[0][0];
cacheBar = BarsArray[1].GetBar(Times[0][0]);
}
barOpen = BarsArray[1].GetOpen(cacheBar);
barHigh = BarsArray[1].GetHigh(cacheBar);
barLow = BarsArray[1].GetLow(cacheBar);
barClose = BarsArray[1].GetClose(cacheBar);
StrategyPlot[0] = (barOpen + barHigh + barLow + barClose) / 4;
// Set 1
if (Close[0] > Close[1])
{
EnterLongLimit(Convert.ToInt32(DefaultQuantity), (Close[0] + (-2 * TickSize)) , "");
}
// Set 2
if (Close[0] < Close[1])
{
ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
}
// Set 3
}
#region Properties
public Series<double> StrategyPlot
{
get { return Values[0]; }
}
#endregion
}
}
Comment