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

highest high historic

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

    highest high historic

    Hi,

    I have this code to calculate the highest high of the last 20 bars


    double recent_highest_value = MAX(High, 20)[0];


    However, I really want the highest high of the last 19 historic bars, not including the current bar. In other words, I need a concise code for calculating the highest value of bars[1...20], where bar[0], is the current bar. How can I do that?

    Thanks,
    Matt

    #2
    Hi Matt,

    You can apply indexing 1 to that MAX expression, to start evaluating 1 bar prior:

    double recent_highest_value = MAX(High, 20)[1];

    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      so that will evaluate 20 bars starting from 1 bar ago?

      Thanks,
      Matt

      Comment


        #4
        Hi, I tried that the following

        double highest_value = MAX(High, 12)[1]; //;highest high value over the last 20 periods
        double lowest_value = MIN(Low, 12)[1];
        Print("highest_value[1]: " + highest_value);
        Print("lowest_value[1]: " + lowest_value);

        , but I get the following message. It is out of bounds for some reason. I have more than 12 bars on the chart i am testing.


        **NT** Enabling NinjaScript strategy 'test/951ccc5f3d3a487da338dc086fcded9f' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True MaxRestarts=4 in 5 minutes
        **NT** Error on calling 'OnBarUpdate' method for strategy 'test/951ccc5f3d3a487da338dc086fcded9f': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.


        Here is all the code



        #region Using declarations
        using System;
        using System.ComponentModel;
        using System.Diagnostics;
        using System.Drawing;
        using System.Drawing.Drawing2D;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Data;
        using NinjaTrader.Indicator;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Strategy;
        #endregion

        // This namespace holds all strategies and is required. Do not change it.
        namespace NinjaTrader.Strategy
        {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]

        public class test : Strategy
        {
        #region Variables
        // Wizard generated variables
        private int myInput0 = 1; // Default setting for MyInput0
        // User defined variables (add any user defined variables below)
        #endregion
        //DateTime startTime;
        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
        CalculateOnBarClose = false;

        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {

        double highest_value = MAX(High, 12)[1]; //;highest high value over the last 20 periods
        double lowest_value = MIN(Low, 12)[1];
        Print("highest_value[1]: " + highest_value);
        Print("lowest_value[1]: " + lowest_value);

        }

        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public int MyInput0
        {
        get { return myInput0; }
        set { myInput0 = Math.Max(1, value); }
        }
        #endregion
        }
        }

        Comment


          #5
          Yes, exactly. Will evalute lookback of 20 starting 1 bar ago.

          You're running into this issue with the exception:


          You can resolve by adding:
          if (CurrentBar < 1) return;
          Ryan M.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by andrewtrades, Today, 04:57 PM
          0 responses
          0 views
          0 likes
          Last Post andrewtrades  
          Started by chbruno, Today, 04:10 PM
          0 responses
          3 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          436 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          6 views
          0 likes
          Last Post FAQtrader  
          Started by rocketman7, Today, 09:41 AM
          5 responses
          19 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X