Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help...should be easy

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

    Need help...should be easy

    I am getting my feet wet with NT8 having been a longtime NT7 user...for some reason, I can't get this simple script to enable on a 1 minute chart:

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class test : Strategy
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "test";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 10;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.High;
                    OrderFillResolutionType                        = BarsPeriodType.Minute;
                    OrderFillResolutionValue                    = 1;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    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)
                {
                    SetStopLoss(@"", CalculationMode.Currency, (Low[1] - 0.25) , false);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 1)
                    return;
    
                EnterLongStopLimit(Convert.ToInt32(DefaultQuantity), (High[1] + 0.25) , (High[1] + 0.25) , @"test");
            }
        }
    }
    As you can see, this is a simple bit of code generated by NT8 itself using the wizard.

    Yet when I try and enable it, I keep getting this error in the logs:

    Strategy 'test': 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.
    I am tearing out what is left of my hair trying to see what is wrong...anybody have any ideas?

    I am putting it on a 1-min chart of ES with 5 days of data on it...
    Last edited by Mike0405; 03-14-2020, 04:52 PM.

    #2
    else if (State == State.Configure) //another state with parameters(apply, enable)



    AddDataSeries("ES 03-20", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);


    protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBar<0)return; if()... condition... { EnterLongStopLimit(Convert.ToInt32(DefaultQuantity ), (High[1] + 0.25) , (High[1] + 0.25) , test); } }
    Last edited by Emma1; 03-14-2020, 08:00 PM.

    Comment


      #3
      Hi Mike0405, thanks for your post.

      The strategy is accessing Low[1] in your SetStopLoss method. You can not access data during the OnStateChanged method like this. Set your top loss before you go into the position instead:

      Code:
      protected override void OnBarUpdate()
      {
          if (BarsInProgress != 0)
              return;
      
          if (CurrentBars[0] < 1)
              return;
      
          if (Position.MarketPosition == MarketPosition.Flat)
          {
              SetStopLoss(@"", CalculationMode.Currency, (Low[1] - 0.25) , false);
              EnterLongStopLimit(Convert.ToInt32(DefaultQuantity), (High[1] + 0.25) , (High[1] + 0.25) , @"test");
          }
      }
      Kind regards,

      -ChrisL
      Last edited by NinjaTrader_Jim; 03-16-2020, 09:21 AM.

      Comment


        #4

        #region Using declarations
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Gui;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Gui.SuperDom;
        using NinjaTrader.Gui.Tools;
        using NinjaTrader.Data;
        using NinjaTrader.NinjaScript;
        using NinjaTrader.Core.FloatingPoint;
        using NinjaTrader.NinjaScript.Indicators;
        using NinjaTrader.NinjaScript.DrawingTools;
        #endregion

        //This namespace holds Strategies in this folder and is required. Do not change it.
        namespace NinjaTrader.NinjaScript.Strategies
        {
        public class test : Strategy
        {
        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Enter the description for your new custom Strategy here.";
        Name = "test";
        Calculate = Calculate.OnBarClose;
        EntriesPerDirection = 1;
        EntryHandling = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy = true;
        ExitOnSessionCloseSeconds = 10;
        IsFillLimitOnTouch = false;
        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution = OrderFillResolution.High;
        OrderFillResolutionType = BarsPeriodType.Minute;
        OrderFillResolutionValue = 1;
        Slippage = 0;
        StartBehavior = StartBehavior.WaitUntilFlat;
        TimeInForce = TimeInForce.Gtc;
        TraceOrders = false;
        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
        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)
        {

        }
        }

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

        if (CurrentBars[0] < 1)
        return;
        SetStopLoss(@"", CalculationMode.Currency, (Low[1] - 0.25) , false);
        EnterLongStopLimit(Convert.ToInt32(DefaultQuantity ), (High[1] + 0.25) , (High[1] + 0.25) , test);
        }
        }
        }

        Comment


          #5
          Hello madams212121,

          Did you have a question involving this code?

          Please note that when calling SetStopLoss in OnBarUpdate, the stop loss should be reset to an initial level when the strategy is flat before the next entry. The example below demonstrates.

          SamplePriceModification - https://ninjatrader.com/support/help...of_stop_lo.htm

          Please also use "[CODE]" blocks when sharing code to ensure the code stays well formatted and readable.

          We look forward to assisting.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          59 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
          74 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