New to Ninja Trader, trying to do something I thought would be pretty simple but it's not going quite as smoothly as I was hoping.
Any help would be appreciated :-)
I'm trying to create a pretty simple multi time frame strategy.
As you can see in the code below I'm adding 3 Data Series: 1 Second, 30 Minutes and 1 Day
When I run the strategy it seems to ignore the 1 Second (1 Second isn't written to output), I've also tried 1 Tick (which is actually what I want) but that doesn't work either.
When I change that 1 second to something like 1 Minute it works as expected.
What am I missing here?
Another oddity (or just lack of understanding on my part), the code below only works when I set the primary Data Series (via the UI) to 1 Minute or above, if I set it to something like 30 seconds nothing is printed to the output window at all and I don't quite understand why.
Also a couple questions about the SMA in the code.
1. It looks like the Time Frame I choose in the UI must be 200 Days or above, even though I don't really need that level of detail for the primary Data Series (I actually ignore it), I just need it to preload for the 1 Day Data Series so I can calculate the SMA.
Am I approaching this correctly?
2. I noticed the SMA is only updated on bar close even though in the State.SetDefaults I've set Calculate to OnEachTick. (I've also enabled "Show Tick Replay" in options)
Again, what am I not understanding here?
public class MyMultiTimeFrame : Strategy
{
private int _dailyBarsRequired = 201;
private SMA _daily200SMA;
public bool DebugMode { get; set; }
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MyMultiTimeFrame";
Calculate = Calculate.OnEachTick;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
TimeInForce = TimeInForce.Gtc;
//TraceOrders = true;
//RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Second, 1);
AddDataSeries(Data.BarsPeriodType.Minute, 30);
AddDataSeries(Data.BarsPeriodType.Day, 1);
}
else if (State == State.DataLoaded)
{
_daily200SMA = SMA(Closes[2], 200);
}
}
protected override void OnBarUpdate()
{
// Return if not enough daily bars or current bar is the primary series
if (CurrentBars[3] < _dailyBarsRequired || BarsInProgress == 0)
return;
var sb = new StringBuilder();
sb.AppendLine(string.Format("BarsInProgeess: {0}", BarsInProgress));
// Enter on 1 Second Bar
if (BarsInProgress == 1)
{
sb.AppendLine(string.Format(" 1 Second Bar"));
sb.AppendLine(string.Format(" Time: {0}", Time[0]));
sb.AppendLine(string.Format(" Close: {0}", Close[0]));
sb.AppendLine(string.Format(" Day 200 SMA: {0}", _daily200SMA[0]));
}
// Enter on 60 Minute Bar
if(BarsInProgress == 2)
{
sb.AppendLine(string.Format(" 30 Minute Bar"));
sb.AppendLine(string.Format(" Time: {0}", Time[0]));
sb.AppendLine(string.Format(" Close: {0}", Close[0]));
sb.AppendLine(string.Format(" Day 200 SMA: {0}", _daily200SMA[0]));
}
// Enter on Daily Bar
if (BarsInProgress == 3)
{
sb.AppendLine(string.Format(" Daily Bar"));
sb.AppendLine(string.Format(" Time: {0}", Time[0]));
sb.AppendLine(string.Format(" Close: {0}", Close[0]));
sb.AppendLine(string.Format(" Day 200 SMA: {0}", _daily200SMA[0]));
}
DebugOutput(sb.ToString());
}
private void DebugOutput(object obj)
{
if (!DebugMode)
return;
Print(obj);
Print(string.Empty);
}
}

Comment