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

Range Logic

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

    Range Logic

    I have a strategy that uses code that says
    if (macd(Fast, Slow, Smooth)[0] > 50
    || (macd(Fast, Slow, Smooth)[1] > 50
    || (macd(Fast, Slow, Smooth)[2] > 50
    || (macd(Fast, Slow, Smooth)[3] > 50
    || (macd(Fast, Slow, Smooth)[4] > 50
    || (macd(Fast, Slow, Smooth)[5] > 50
    && Sell signal [0] goes here...)

    Can you tell me how to do this as a range from current bar to 5 bars ago in one line of code instead of 6?

    Thanks

    #2
    Hello Kenb2004,

    One option here is using CountIf()


    if (CountIf(delegate {return MACD(Fast, Slow, Smooth)[0] > 50;}, 6) >= 1)
    //Do Something

    Says:
    Look for this condition over the last 6 bars. If occurred more than once do something.
    Last edited by NinjaTrader_RyanM1; 12-06-2010, 04:17 PM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      So you could use this for "at least" once?

      if (CountIf(delegate {return MACD(Fast, Slow, Smooth3)[0] > 50;}, 6) >= 1)
      //Do Something
      Last edited by kenb2004; 12-06-2010, 04:19 PM.

      Comment


        #4
        Correct: >= 1 is more appropriate for your code snippet.

        3 was placed there inadvertently and has been fixed. Thanks for letting me know.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Thanks, one other thing unrelated. How do I make this 50 line 'dashed' ?
          Add(new Line(new Pen(Color.Blue, 2), 50, "50 line"));
          thanks

          Comment


            #6
            You could do something like:
            Lines[0].Pen.DashStyle = DashStyle.Dash;
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks, that works great.

              Comment


                #8
                If you wanted a condition to be true when it was true consecutively for 10 or more bars would this be the correct way to do this or is there a better way?

                if (CountIf(delegate {return MACD(Fast, Slow, Smooth)[0] > 50;}, 10) >= 10)
                //Do Something

                Comment


                  #9
                  Hi Ken,

                  Yes, using CountIf() like this will work as a consecutive function. The alternative to this is custom coding your own method.

                  There are built in functions for consecutive up and down closes:

                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    I tried this and only got a yellow background color. Can you tell me how to get all 3 colors working? Logic I am attempting is when the EMA7 has been over the SMA20 for at least 10 consecutive bars, make background color green. When the EMA7 has been under the SMA20 for at least 10 consecutive bars, make background color red. If Not either of these make background color Yellow.

                    if (CountIf(delegate {return EMA(7)[0] > SMA(20)[0];}, 10) >= 10)
                    {
                    BackColor = Color.FromArgb(100,130,255,130); // Green Background Color, UpTrend
                    }
                    if (CountIf(delegate {return EMA(7)[0] < SMA(20)[0];}, 10) >= 10)
                    {
                    BackColor = Color.FromArgb(100,255,165,165); // Red Background Color, DownTrend
                    }
                    if (CountIf(delegate {return EMA(7)[0] > SMA(20)[0];}, 10) < 10
                    || CountIf(delegate {return EMA(7)[0] < SMA(20)[0];}, 10) < 10)
                    {
                    BackColor = Color.FromArgb(100,255,255,150); // Yellow Background Color, Neutral
                    }
                    Last edited by kenb2004; 12-08-2010, 04:46 PM.

                    Comment


                      #11
                      Ken,

                      To debug this, you'll want to focus on one statement at a time and use Print statements to keep track of the countif value.

                      Print(CountIf(delegate {return EMA(7)[0] > SMA(20)[0];}, 10));
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12

                        In the code below I would like to enter a long position at 10 ticks below the closing price of the "crossover" bar. What should I replace "[0]" with? Since the crossover bar could be 1 of 5 past bars. Thanks
                        if (CountIf(delegate {return CrossAbove(WMA(Fast), WMA(Slow), 1);}, 5) >= 1)

                        EnterLongLimit(DefaultQuantity, Close[
                        0] + -10 * TickSize, "Crossover Buy");

                        Comment


                          #13
                          Hello Kenb2004,

                          You can work with Most Recent Occurrence. It returns the number of bars ago that the test condition evaluated to true within the specified look back period expressed in bars.
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            This appears to be what I need but I'm not sure how to apply it. Do you have any samples or examples of how to apply this method. In my strategy would I use it with CountIf() or instead of?

                            Comment


                              #15
                              Here is an example that returns the number of bars ago for the most recent occurrence of Close[0] > Open[0]. The lookback period is 10 bars.

                              This is used instead of CountIf to return a different type of value.

                              int barsAgo = MRO(delegate {return Close[0] > Open[0];}, 1, 10);
                              if (barsAgo > -1)
                              Print("The bar high was " + High[barsAgo]);
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rajendrasubedi2023, Today, 09:50 AM
                              2 responses
                              14 views
                              0 likes
                              Last Post rajendrasubedi2023  
                              Started by geddyisodin, Today, 05:20 AM
                              4 responses
                              29 views
                              0 likes
                              Last Post geddyisodin  
                              Started by geotrades1, Today, 10:02 AM
                              2 responses
                              8 views
                              0 likes
                              Last Post geotrades1  
                              Started by ender_wiggum, Today, 09:50 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by bmartz, Today, 09:30 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Working...
                              X