Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    JimNinjaTrader Customer Service

    Comment


      #3
      This helped me out a lot thanks

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by AveryFlynn, Today, 04:57 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by Max238, Today, 01:28 AM
      4 responses
      37 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by r68cervera, Today, 05:29 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by geddyisodin, Today, 05:20 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by timko, Today, 06:45 AM
      2 responses
      14 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Working...
      X