Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error when enabling strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Error when enabling strategy

    I am just learning strategy builder and just trying to get my feet wet with a simple strategy but I am getting an error.

    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");
    }

    }
    }
    }
    Last edited by bengalvfan; 06-11-2019, 10:21 AM.

    #2
    Hello bengalvfan,

    Thanks for your post.

    The TrendLines indicator requires access to ChartControl and would need to be plotted on the chart in order to function. When creating a condition for this indicator, please select Plot On Chart so this indicator can be used in the Strategy Builder.

    Please also note that the strategy should be checking if a Price Series is crossing above an indicator plot. For example: Price > Close CrossAbove Trendlines. (Screenshot attached.)

    Publicly available resources for using the Strategy Builder can be found below.

    Strategy Builder 301 - https://www.youtube.com/watch?v=_KQF2Sv27oE

    Conditions exmaples - https://ninjatrader.com/support/help...on_builder.htm

    Actions examples - https://ninjatrader.com/support/help...us/actions.htm

    Please let me know if I can be of further assistance.
    Attached Files

    Comment


      #3
      This helped me out a lot thanks

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      77 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      40 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      63 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      63 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      53 views
      0 likes
      Last Post CarlTrading  
      Working...
      X