I am trying to create a simple strategy that goes long when the current bar crosses above the trendline indicator.
When try and enable the strat I get the following errors:
Strategy 'BuyOnTrendBreak': Error on calling 'OnBarUpdate' method on bar 10: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart. |
Indicator 'Trend lines': Error on calling 'OnBarUpdate' method on bar 10: Object reference not set to an instance of an object. |
My code after using the strategy builder looks like this:
namespace NinjaTrader.NinjaScript.Strategies
{
public class BuyOnTrendBreak : Strategy
{
private TrendLines TrendLines1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "BuyOnTrendBreak";
Calculate = Calculate.OnPriceChange;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 10;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
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)
{
}
else if (State == State.DataLoaded)
{
TrendLines1 = TrendLines(Close, 1, 8, 25, false);
SetProfitTarget(CalculationMode.Currency, 155);
SetStopLoss(CalculationMode.Currency, 55);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if (CrossAbove(CurrentBars[0], TrendLines1, 1))
{
EnterLong(Convert.ToInt32(250), @"LongTrendBreak");
}
}
}
}
Comment