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

comparing the value of an indicator on different bars

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

    comparing the value of an indicator on different bars

    Can somebody tell me the easiest way to compare the value of an indicator on the current bar or 1 bar back to the value of an indicator x bars in the past. This is the type of code you would use to make divergence indicators like a macd or stochastic divergence, etc. I've seen this done using for loops but I'm not sure if that's the only way or even the most efficient way to accomplish this. I'm not going to be looking for a specific bar but scanning bars in the past to find the last bar that closed on a specific indicator level or within a specific zone.

    For example, let's say when the stochastic is above 80 I want it to check for the last time the stochastic was above 80 from the past bars and then if the price on the current bar is lower than the price was on that bar in the past an alert would go out. Please assist.

    #2
    Hello gordongekko,

    Thanks for your post.

    One way to accomplish your goal would be to save the bar number in an int variable when the stochastic value is > 80. For example:

    if (Stochastics(7, 14, 3).D[0] >= 80)
    {
    if (High[0] < High[CurrentBar - prev80bar]) // check against previous high
    {
    Alert(...)
    }
    prev80bar = CurrentBar;
    }
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,
      Thanks for the reply. Can you explain how to do this using 2 different price data series. For example, how do I make an alert for when the current 1m bar closes above the high of the previous daily bar. I'm assuming I would make a double for the previous days high but how do I reference the daily data series because if i use current bar etc it will think I'm taking about the main data series.

      Comment


        #4
        Hello gordongekko,

        Thanks for your reply.

        There would be a couple of ways to do this.

        In Ninjascript (or strategy builder) You can add a daily bar series and then reference the Highs[1][1] which points to the added daily bar high of yesterday (depending on Calculate setting). This would be creating a multi time frame script which causes references to change and you would want to fully review this section of the helpguide: https://ninjatrader.com/support/help...nstruments.htm

        Alternatively, you can add the indicator PriorDayOHLC and access the previous day High value. Prior day uses the intraday data. You can add it to a chart to see what that results in. Please see: https://ninjatrader.com/support/help...r_day_ohlc.htm

        Alternatively, if all you need is an alert, you can create a chart alert using Current price and the indicator PriorDayOHLC's High line. Reference: https://ninjatrader.com/support/help...us/?alerts.htm
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          I followed the instructions in the support guide but I'm getting a "Method name expected" error on this line when trying to compile the code:

          if(Close[0] > High[1](BarsArray[1]) || Close[0] > Low[1](BarsArray[1]))

          Comment


            #6
            Hello gordongekko,

            Thanks for your reply.

            If you have added a data series then you would need to use Highs[1][0] and Lows[1][0] to refer to the current bar High and low of the added data series.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              I'm assuming you mean High[1][1] referring to data series 1 the array and the previous bar in that data series. Is this correct?

              I added this data series to the state configure

              AddDataSeries(BarsPeriodType.Day, 1);

              i just need to see the correct syntax for this line comparing the main data series to the 1 data series in the barsarray.

              This is obviously incorrect because it wont compile:

              if(Close[0] > High[1](BarsArray) || Close[0] > Low[1](BarsArray[1]))

              What I want to say is "if the close of the main data series (e.g.1m) closes above the high of the previous days bar or if the main data series closes above the low of the previous days bar" then run the method
              Last edited by gordongekko; 12-05-2017, 03:50 PM.

              Comment


                #8
                Hello gordongekko,

                Thanks for your reply.

                If you are running the code with Calculate.OnBarClose then you would use:

                if (Closes[0][0] > Highs[1][0] || Closes[0][0] < Lows[1][0])

                Please note that with reference to the helpguide: https://ninjatrader.com/support/help...nstruments.htm you will want to isolate to BarsInProgress == 0 and you will need to ensure at least 1 day bar has been loaded before trying to access that statement (review CurrentBars[] check in https://ninjatrader.com/support/help...urrentbars.htm)
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Barry Milan, Today, 10:35 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by WeyldFalcon, 12-10-2020, 06:48 PM
                14 responses
                1,427 views
                0 likes
                Last Post Handclap0241  
                Started by DJ888, Yesterday, 06:09 PM
                2 responses
                9 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by jeronymite, 04-12-2024, 04:26 PM
                3 responses
                40 views
                0 likes
                Last Post jeronymite  
                Started by bill2023, Today, 08:51 AM
                2 responses
                16 views
                0 likes
                Last Post bill2023  
                Working...
                X