Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Try to understanding how to code NT8

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

    Try to understanding how to code NT8

    Good Day, I tried to understand how to code with the strategy builder. This is a simple strategy. It works but when I want to include a ProfitTarget, I have this error and I do not understand what I have to do to correct it. I have included the total scrip that the view code give me.
    Thanks for your help.

    (Strategy 'DemoTest2': Error on calling 'OnStateChange' method: 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.)

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class DemoTest2 : Strategy
    {
    private ZLEMA ZLEMA1;
    private EMA EMA1;
    private ATR ATR1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "DemoTest2";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    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.ByStrategyPosition;
    BarsRequiredToTrade = 55;
    // 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)
    {
    ZLEMA1 = ZLEMA(Close, 14);
    EMA1 = EMA(Close, 50);
    ATR1 = ATR(Close, 14);
    ZLEMA1.Plots[0].Brush = Brushes.Goldenrod;
    ATR1.Plots[0].Brush = Brushes.DarkCyan;
    AddChartIndicator(ZLEMA1);
    AddChartIndicator(ATR1);
    SetProfitTarget(@"GoLong", CalculationMode.Price, (Close[1] + ((ATR1[1] * 1.5) )) );
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 2)
    return;

    // Set 1
    if ((ZLEMA1[0] > ZLEMA1[1])
    && (ZLEMA1[1] < ZLEMA1[2])
    && (EMA1[0] >= EMA1[1]))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"GoLong");
    Draw.ArrowUp(this, @"DemoTest2 Arrow up_1", false, 0, (Low[0] + (-5 * TickSize)) , Brushes.Lime);
    }

    // Set 2
    if ((ZLEMA1[0] < ZLEMA1[1])
    && (ZLEMA1[1] > ZLEMA1[2])
    && (EMA1[0] <= EMA1[1]))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"GoShort");
    Draw.ArrowDown(this, @"DemoTest2 Arrow down_1", false, 0, (High[0] + (5 * TickSize)) , Brushes.Red);
    }

    }
    }
    }

    #2
    Hi Tmarois, thanks for posting.

    I answered your email on the same subject. The support team is active on the forum as well, so we will answer on the forum or email.

    It's coming from this part of OnStateChanged:

    SetProfitTarget(@"GoLong", CalculationMode.Price, (Close[1] + ((ATR1[1] * 1.5) )) );

    The data is not loaded yet in this state, so you cant use Close or Indicator values. If you need to set a stop loss based on dynamic data it must be set up in your conditions and actions menu using an exit method e.g. ExitLongStopMarket. I linked a post to an example that sets up stop losses dynamically in the strategy builder:

    https://ninjatrader.com/support/foru...der#post806596

    You can also call SetProfitTarget dynamically in OnBarUpdate, but you need to unlock the script and code by hand to do so.

    Best regards,
    -ChrisL

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    53 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X