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

BarSince converted to MRO

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

    BarSince converted to MRO

    Hi,

    I used to use Barsince & Valuewhen function from my previous trading platform - Meta Stock.

    My indicator has 2 conditions
    1) Close[0] > Open[0]
    2) Close[0] > Close[-1]


    I have 2 question here:

    a) How can I write it on MRO()? I got error and don't know where is going wrong. #CS1502. I need the result as most recent when both conditions are matched. (Barsince)

    private bool Con1;
    private bool Con2;

    private string Result;

    Result = MRO (Con1 & Con2,1,CurrentBar);
    Got Errors here.


    b) Similar case as above but need to return as value. (Valuewhen)
    Same condition as above and to return the Lowest low of recent 5 candles if condition met.

    private double LLV;

    LLV = MIN(Low,5)[0];

    How I can do it by using MRO ? and similar formula on NT8?

    Thank you very much

    #2
    Hello wtfalfred,

    Thank you for writing in today.

    Code:
      // Prints the high price of the most recent up bar over the last 10 bars (current bar + look back period's 9 bars before that)
      int barsAgo = MRO(() => Close[0] > Open[0], 1, 9);
      if (barsAgo > -1)
          Print("The bar high was " + High[barsAgo]);


    The errors you are receiving are for using a boolean type as your condition passed instead of a lambda expression, and for assigning the returned integer type from MRO() to a string. I would suggest assigning the return value to an integer variable, like above, and assigning it to your Result string so you may use the value later without recalculating.

    Here is the MSDN documentation on Lambda expressions: https://msdn.microsoft.com/en-us/library/bb397687.aspx

    As an example, your 2 condition lambda expression can be written as:
    Code:
    (() => (Close[0] > Open[0])&&(Close[0] > Close[-1]))
    To return the lowest price from the last five bars, the syntax will be the following:
    Code:
    int barsAgo = MRO(() => Close[0] < Open[0], 1, 4);
    If you have any other questions, please don't hesitate to ask.
    JimNinjaTrader Customer Service

    Comment


      #3
      Jim,

      Still testing on my custom plot.
      My below codes got stuck under onbarupdates and get errors.

      if (Close[0] > EMA(Close, 4)[0])

      {
      LLV = MIN(Low, 5)[0];
      }
      else
      {
      LLV = MIN(Low, 5)[1];
      }


      My logic is if "that day" (each day as individual) close price > "that day "EMA,Close,4

      then get lowest low value of previous 5 days from today onward
      else get then get lowest low value of previous 5 days from "previous 1day" onward

      Do you have any ideas?

      Alfred
      Last edited by wtfalfred; 12-21-2016, 09:38 AM.

      Comment


        #4
        Hello wtfalfred,

        The condition you have written is valid and compares the current bar's close value to the value returned by an EMA with a period of 4 bars.

        Your syntax is correct as well for how to run MIN() against previous bars. However, Ninjatrader will throw an error if it tries to calculate a MIN() going any number of bars back, when there has not been enough bars to look that far back. In this case, we must ensure that 5 bars have passed.

        Please consider the following:
        Code:
        if (CurrentBar <= 5) return; 
        if (Close[0] > EMA(4)[0])
        { 
        	LLV = MIN(Low,5)[0];
        }
        else
        { 
        	LLV = MIN(Low,5)[1];
        }
        Please let me know if you have any further questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jim View Post
          Hello wtfalfred,

          The condition you have written is valid and compares the current bar's close value to the value returned by an EMA with a period of 4 bars.

          Your syntax is correct as well for how to run MIN() against previous bars. However, Ninjatrader will throw an error if it tries to calculate a MIN() going any number of bars back, when there has not been enough bars to look that far back. In this case, we must ensure that 5 bars have passed.

          Please consider the following:
          Code:
          if (CurrentBar <= 5) return; 
          if (Close[0] > EMA(4)[0])
          { 
              LLV = MIN(Low,5)[0];
          }
          else
          { 
              LLV = MIN(Low,5)[1];
          }
          Please let me know if you have any further questions.
          Hi Jim,

          Thank you very much for your reply. THAT WORKS.!

          Something I don't understand here, my plot is applying to 1 minute chart and there are more than 100 bars there for sure.
          Since I tried if I remove the "else" part, both conditions work like a charm.
          So I don't understand the logic that "not enough bars to look" for MIN() to work.
          Could you explain how it works here?


          Codes as below. No errors here without the "else".

          if (Close[0] > EMA(4)[0])
          { LLV = MIN(Low,5)[0];}

          or

          if (Close[0] > EMA(4)[0])
          { LLV = MIN(Low,5)[1];}


          Appreciated your help. Thank you very much.
          Alfred
          Last edited by wtfalfred; 12-22-2016, 08:08 AM.

          Comment


            #6
            Hello wtfalfred,

            I am glad to hear that you have gotten your indicator working.

            It is important to add if (CurrentBar <= 5) return; at the beginning of OnBarUpdate() because if your if(Close[0] > EMA(4)[0]) condition is met on the first bar, LLV = MIN(Low,5)[1]; will be called to calculate a MIN() when the indicator does not have any barsAgo to index.

            You can reproduce this scenario by removing the if statement for LLV = MIN(Low,5)[1]; You will be met with an error in the Log Tab of the Control Center upon loading the indicator:
            Error on calling 'OnBarUpdate' method on bar 0: 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.
            JimNinjaTrader Customer Service

            Comment


              #7
              Jim,

              Great, Thanks a lot.

              Comment


                #8
                Hello,
                I am also having some problems with the MRO method. My indicator compiles, but it does not draw my lines in the correct place. Can I use the MRO with a bool for the condition as below?
                Code:
                //For the upper range line - resistance
                			if (arrowdown1[5] == (true) || arrowdown2[5] == (true) || arrowdown3[5] == (true) || arrowdown4[5] == (true)
                			|| arrowdown1[6] == (true) || arrowdown2[6] == (true) || arrowdown3[6] == (true) || arrowdown4[6] == (true)
                			|| arrowdown1[7] == (true) || arrowdown2[7] == (true) || arrowdown3[7] == (true) || arrowdown4[7] == (true)
                			|| arrowdown1[8] == (true) || arrowdown2[8] == (true) || arrowdown3[8] == (true) || arrowdown4[8] == (true))
                			arrowdownbars5to8 = true;
                			
                			int barsAgo = MRO(delegate {return arrowdownbars5to8 = true;}, 2, 8); //2nd occurence from bars 0-8 = 9 bars
                			if (barsAgo > -1) //that is at least one of the arrowdown == true for barsAgo 5 to 8.
                			{
                			Print(Time.ToString()+" " + "High of Most recent occurrence of another arrowdown from bars 5-8" + " " + High[barsAgo]);
                			if (High[barsAgo] > high3)
                			DrawHorizontalLine("tagResistance", false, High[barsAgo], Color.DarkBlue, DashStyle.DashDotDot, 2);
                			else if (High[barsAgo] <= high3)
                			DrawHorizontalLine("tagResistance", false, high3, Color.DarkBlue, DashStyle.DashDotDot, 2);
                }
                The reason I use a bool is because my indicator signals a divergence on certain bars with an arrow. I use BoolSeries in the indicator to show when a bar is marked by an arrow, like so: arrowdown1.Set(true);
                When a bar marked by an arrowdown is followed by several consecutive bear bars (initial arrowdown), I then need to pick up the previous arrowdown bar, if it is within a certain number of bars, so that my line can be drawn at the high of the two arrowdown bars, which is why I am using MRO. Currently, the line is drawing at the high of the initial arrowdown, even when there is a higher arrowdown bar in the MRO range. Also, the MRO barsAgo print statement is picking up all bars, even those without an arrowdown.

                Thank you.

                Comment


                  #9
                  Hello GeorgeW,

                  Thanks for writing into this thread!

                  I have received your note and I will respond with a complete answer of my findings.
                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    Hello Jim,

                    Thanks for responding.
                    I have tried this, which seems to work for the upper line. I have checked a few results which are correct.
                    Code:
                    //For the upper range line - resistance
                    			int barsAgo = MRO(delegate {return arrowdown1[0] == (true) || arrowdown2[0] == (true) || arrowdown3[0] == (true) || arrowdown4[0] == (true);}, 2, 8); //2nd occurence from bars 0-8 = 9 bars
                    			if (barsAgo > -1) //that is at least one of the arrowdown == true for barsAgo 5 to 8.
                    			{
                    			Print(Time.ToString()+" " + "High of Most recent occurrence of another arrowdown from bars 5-8" + " " + High[barsAgo]);
                    			if (High[barsAgo] > high3)
                    			DrawHorizontalLine("tagResistance", false, High[barsAgo], Color.DarkBlue, DashStyle.DashDotDot, 2);
                    			else if (High[barsAgo] <= high3)
                    			DrawHorizontalLine("tagResistance", false, high3, Color.DarkBlue, DashStyle.DashDotDot, 2);
                    			
                    			//The code below is filtered for NHG @ least 0.5pts 2* TickSize above previous 6 bars. High[4]-TickSize > MAX(High,8)[5]
                    			else if (barsAgo == -1 && High[4] > high3 && High[4]-TickSize > MAX(High,8)[5]) //all of barsAgo arrowdown == false for barsAgo 5 to 8.
                    			DrawHorizontalLine("tagResistance", false, High[4], Color.DarkBlue, DashStyle.DashDotDot, 2);
                    			else if (barsAgo == -1 && High[4] <= high3 && High[3]-TickSize > MAX(High,8)[5]) //all of barsAgo arrowdown == false for barsAgo 5 to 8.
                    			DrawHorizontalLine("tagResistance", false, high3, Color.DarkBlue, DashStyle.DashDotDot, 2);					
                    			}
                    For some reason it did work when I used the same format for the lower line, so I had to adjust the first part of the code as follows:
                    Code:
                    //For the lower range line - support; MRO - looking for Most Recent Occurance of another arrowup.
                    			if (arrowup1[0] == (true) || arrowup2[0] == (true) || arrowup3[0] == (true) || arrowup4[0] == (true))
                    			arrowupbars5to8 = true;
                    int barsAgo = MRO(delegate {return arrowupbars5to8 = true;}, 2, 8);
                    On checking the print results for the red line which is defined by the 2nd code above, I am not picking up the 2nd previous Arrow up or down bar, all I am picking up is the 2nd bar back. So that part of the code is still not correct.
                    Last edited by GeorgeW; 12-29-2016, 05:24 AM.

                    Comment


                      #11
                      Hello,
                      I have solved the issue by changing the barsAgo for the line that did not work correctly to barAgoAU. It seemed having 2 lines using barsAgo was causing the problem. So that 2nd lines code now starts:
                      Code:
                      int barsAgoAU = MRO(delegate {return arrowup1[0] == (true) || arrowup2[0] == (true) || arrowup3[0] == (true) || arrowup4[0] == (true);}, 2, 8);			
                      if (barsAgoAU > -1)

                      Comment


                        #12
                        Hello GeorgeW,

                        Thanks for writing back and sharing you have gotten your indicator working.

                        Code:
                        int barsAgo = MRO(delegate {return arrowdownbars5to8 = true;}, 2, 8); //2nd occurence from bars 0-8 = 9 bars
                        I was curious about your use of the delegated expression return arrowdownbars5to8 = true; This line of code would set arrowdownbars5to8 to true and returned that value to the first argument in MRO(). This would explain the behavior you described in your post. Generally, = will set values while == will check equality and evaluate to a 1 or 0 if the expression is true or false.

                        If you have any other questions, please don't hesitate to ask.
                        JimNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by ScottWalsh, Today, 06:52 PM
                        3 responses
                        19 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by trilliantrader, Today, 03:01 PM
                        2 responses
                        19 views
                        0 likes
                        Last Post helpwanted  
                        Started by cre8able, Today, 07:24 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post cre8able  
                        Started by Haiasi, Today, 06:53 PM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by ScottW, Today, 06:09 PM
                        1 response
                        7 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Working...
                        X