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

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
    Chris L.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Barry Milan, Yesterday, 10:35 PM
    5 responses
    16 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by DanielSanMartin, Yesterday, 02:37 PM
    2 responses
    13 views
    0 likes
    Last Post DanielSanMartin  
    Started by DJ888, 04-16-2024, 06:09 PM
    4 responses
    13 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by terofs, Today, 04:18 PM
    0 responses
    12 views
    0 likes
    Last Post terofs
    by terofs
     
    Started by nandhumca, Today, 03:41 PM
    0 responses
    8 views
    0 likes
    Last Post nandhumca  
    Working...
    X