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

simple high[i] question

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

    simple high[i] question

    Hi.

    Trying to make an indicator, for minute charts, that checks the previous day's prices for a high, then draws a line. Simple.

    The logic is here:

    for(int i = NumberofBars; i >= 1 ; i = i - 1)
    {
    if(High[i]> HighPrice)
    {
    HighPrice = High[i];
    }
    }

    If the High, i bars ago is greater than HighPrice, then set HighPrice equal to the high i bars ago. Then move on, checking a 'newer' bar in the timeline. After all this is done, simply plot the HighPrice.

    It is set to check every time at the 4:00PM bar, and I have it set so it doesn't bother trying if there aren't enough bars initially.

    In order to set things in motion and to have a price to compare things to, HighPrice is set to the high of the leftmost bar.

    The result is a line that never changes. I check the output, of High[i], and the result is one single price.

    I inserted a simple print statement, and all my High[i]'s, from High[390] to High[1] are all returning the same price, the price I intially set HighPrice too.

    I don't know what is going on here. For the life of me, I cannot understand how I can fail to program an indicator that draws a simple line.

    Regards,

    Dane.

    #2
    Hi Danedo, please post your full code for us to check and advise, maybe your time condition is too limiting? If you are looking for the session highs and lows you can also use CurrentDayOHL and PriorDayOHLC -

    1. http://www.ninjatrader-support.com/H...entDayOHL.html

    2. http://www.ninjatrader-support.com/H...orDayOHLC.html
    BertrandNinjaTrader Customer Service

    Comment


      #3
      #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.Gui.Chart;
      #endregion

      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      [Description("Enter the description of your new custom indicator here")]
      public class OHLC2 : Indicator
      {


      private int daysBack = 1;
      double HighPrice;
      double LowPrice;
      double OpenPrice;
      double ClosePrice;
      int NumberofBars;


      /// <summary>
      /// This method is used to configure the indicator and is called once before any bar data is loaded.
      /// </summary>
      protected override void Initialize()
      {
      Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "High"));
      Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Open"));
      Add(new Plot(Color.FromKnownColor(KnownColor.DarkViolet), PlotStyle.Line, "Low"));
      Add(new Plot(Color.FromKnownColor(KnownColor.Firebrick), PlotStyle.Line, "Close"));
      CalculateOnBarClose = false;
      Overlay = true;
      PriceTypeSupported = false;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      if(CurrentBar < 391)
      {
      return;
      }
      NumberofBars = 389;
      if(ToTime(Time[0]) == ToTime(16, 00, 00))
      {
      HighPrice = High[389];
      for(int i = 1; i < 389; i++)
      {
      if(High[i] > HighPrice)
      {
      HighPrice = High[i];
      }
      Print("High[i]: ");
      Print(High[i]);
      }

      LowPrice = Low[NumberofBars];
      for(int i = 1; i < NumberofBars; i++)
      {
      if(Low[i] < LowPrice)
      {
      LowPrice = Low[i];
      }
      Print("Low[i]: ");
      Print(Low[i]);
      }
      ClosePrice = Close[NumberofBars];
      OpenPrice = Open[NumberofBars];



      }







      // Use this method for calculating your indicator values. Assign a value to each
      // plot below by replacing 'Close[0]' with your own formula.
      High.Set(HighPrice);
      Low.Set(LowPrice);
      Open.Set(OpenPrice);
      Close.Set(ClosePrice);
      }

      #region Properties
      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries High
      {
      get { return Values[0]; }
      }

      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries Open
      {
      get { return Values[1]; }
      }

      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries Low
      {
      get { return Values[2]; }
      }

      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries Close
      {
      get { return Values[3]; }
      }

      [Description("")]
      [Category("Parameters")]
      public int DaysBack
      {
      get { return daysBack; }
      set { daysBack = Math.Max(1, value); }
      }
      #endregion
      }
      }

      #region NinjaScript generated code. Neither change nor remove.
      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      public partial class Indicator : IndicatorBase
      {
      private OHLC2[] cacheOHLC2 = null;

      private static OHLC2 checkOHLC2 = new OHLC2();

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public OHLC2 OHLC2(int daysBack)
      {
      return OHLC2(Input, daysBack);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public OHLC2 OHLC2(Data.IDataSeries input, int daysBack)
      {
      checkOHLC2.DaysBack = daysBack;
      daysBack = checkOHLC2.DaysBack;

      if (cacheOHLC2 != null)
      for (int idx = 0; idx < cacheOHLC2.Length; idx++)
      if (cacheOHLC2[idx].DaysBack == daysBack && cacheOHLC2[idx].EqualsInput(input))
      return cacheOHLC2[idx];

      OHLC2 indicator = new OHLC2();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      indicator.Input = input;
      indicator.DaysBack = daysBack;
      indicator.SetUp();

      OHLC2[] tmp = new OHLC2[cacheOHLC2 == null ? 1 : cacheOHLC2.Length + 1];
      if (cacheOHLC2 != null)
      cacheOHLC2.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cacheOHLC2 = tmp;
      Indicators.Add(indicator);

      return indicator;
      }

      }
      }

      // This namespace holds all market analyzer column definitions and is required. Do not change it.
      namespace NinjaTrader.MarketAnalyzer
      {
      public partial class Column : ColumnBase
      {
      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.OHLC2 OHLC2(int daysBack)
      {
      return _indicator.OHLC2(Input, daysBack);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public Indicator.OHLC2 OHLC2(Data.IDataSeries input, int daysBack)
      {
      return _indicator.OHLC2(input, daysBack);
      }

      }
      }

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      public partial class Strategy : StrategyBase
      {
      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.OHLC2 OHLC2(int daysBack)
      {
      return _indicator.OHLC2(Input, daysBack);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public Indicator.OHLC2 OHLC2(Data.IDataSeries input, int daysBack)
      {
      if (InInitialize && input == null)
      throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

      return _indicator.OHLC2(input, daysBack);
      }

      }
      }
      #endregion

      Comment


        #4
        Hi Danedo,

        I would substitute your print statement:

        Print("High[i]: ");
        Print(High[i]);

        with:

        Print("i: " + i + " High[i]: " + High[i] + " HighPrice: " + HighPrice);

        This way you could judge the current High-value with the current HighPrice in relation.

        Regards
        Ralph

        Comment


          #5
          Thanks for your input Ralph, to simply track the developing high (without reset at some point) you could also use this simpler snippet danedo -
          Code:
          [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] (CurrentBar < [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]) [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2];
          
          [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] (High[[/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]] > myHighTemp[[/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]])
          myHighTemp.Set(High[[/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]]);
          [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else
          [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]myHighTemp.Set(myHighTemp[[/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]]);
          [/SIZE][/SIZE]
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Thanks for your replies guys.

            After much struggle and intense concentration and focus, I found out the problem...

            I named my plots "High", "Low" etc.

            SIGH

            Comment


              #7
              Glad you got it resolved Danedo.
              Josh P.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by wzgy0920, 04-20-2024, 06:09 PM
              2 responses
              26 views
              0 likes
              Last Post wzgy0920  
              Started by wzgy0920, 02-22-2024, 01:11 AM
              5 responses
              32 views
              0 likes
              Last Post wzgy0920  
              Started by wzgy0920, Yesterday, 09:53 PM
              2 responses
              49 views
              0 likes
              Last Post wzgy0920  
              Started by Kensonprib, 04-28-2021, 10:11 AM
              5 responses
              192 views
              0 likes
              Last Post Hasadafa  
              Started by GussJ, 03-04-2020, 03:11 PM
              11 responses
              3,234 views
              0 likes
              Last Post xiinteractive  
              Working...
              X