I'm trying to backtest a simple opening range breakout strategy but I get no trade results when backtesting. I have included the code below. Any assistance would be appreciated
One thing that does confuse me is why the script renames my indicator from "HighLowByTimeRange" to "HighLowByTimeRange1". Why the additional "1"?
namespace NinjaTrader.NinjaScript.Strategies
{
public class OpeningRange : Strategy
{
private HighLowByTimeRange HighLowByTimeRange1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "OpeningRange";
Calculate = Calculate.OnEachTick;
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 = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
SetProfitTarget(@"LongMarket", CalculationMode.Ticks, 8);
SetStopLoss(@"LongMarket", CalculationMode.Ticks, 8, false);
}
else if (State == State.DataLoaded)
{
HighLowByTimeRange1 = HighLowByTimeRange(8, 30, 8, 45);
HighLowByTimeRange1.Plots[0].Brush = new SolidColorBrush(Colors.Green);
HighLowByTimeRange1.Plots[1].Brush = new SolidColorBrush(Colors.Red);
AddChartIndicator(HighLowByTimeRange1);
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] < 1
|| HighLowByTimeRange1.CurrentBar < 1)
return;
// Set
if ((Time[0].TimeOfDay > new TimeSpan(8, 45, 28))
&& (Time[0].TimeOfDay < new TimeSpan(10, 30, 8))
&& ((Close[0] + (2 * TickSize)) >= HighLowByTimeRange1.HighestHigh[0]))
{
EnterLong(Convert.ToInt32(DefaultQuantity), @"LongMarket");
}
}
}
}
Comment