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 NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      61 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X