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

5 day period after CrossAbove

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

    5 day period after CrossAbove

    Hallo,

    I would like to develop an entry strategy based on two crossing SMAs and the second condition that the following 5 days (after the crossing) are above the slow SMA line. My first idea was to check for 5 closings above the slow SMA first (using a "for loop"). If 5 following closings were above the slow SMA I added a CrossAbove (SMA1 SMA2, 5) function as a second condition.

    The problem is, that the CrossAbove function provides a True if there was a crossing above WITHIN the lookbackperiod. That means, my idea does not work as the second condition provides always a True for the last 5 days.

    Can anyone give me a hint how to solve that problem? I could not find a similar question in the forum.

    Thanks

    #2
    Phadreus,

    I am having a hard time following your setup here.

    Could you provide a sample of the code in what you are trying to accomplish?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Cal, thanks for your effort. My coding does not work. Therefore here the strategy I would like to implement:

      I would like to generate a buying signal based on two conditions
      Condition 1: a fast SMA (e.g. 10) crosses a slow SMA (e.g. 65)
      Condition 2: the closing prices of the following 5 days are above the slow SMA

      I don't know how to remember the date of the SMA crossing for the following 5 day loop.

      Hope my explanation is better this time.Could you give me a hint, which method I can use for Condition 2.

      Thanks
      Last edited by phadreus; 08-18-2014, 07:19 AM.

      Comment


        #4
        Hello phadreus,

        Thank you for your response.

        So you want to track what bar the Fast crosses above the Slow and then compare the last five days closes against that value of the Slow? Is this correct?

        Comment


          #5
          Thanks Patrick, nearly correct. I would like to buy at the end of the NEXT five consecutive days that close above the value of the slow SMA. Sorry my explanations are obviously not very clear. Therefore here an example with a 3 day rule:

          Day 1: fast SMA crosses above the slow SMA
          Day 2: Close is above slow SMA (counter = 1)
          Day 3: Close is above slow SMA (counter = 2)
          Day 3: Close is below slow SMA (counter still = 2) > condition not satisfied > counter set = 0
          Day 4: Close is above slow SMA (counter = 1)
          Day 5: Close is above slow SMA (counter = 2)
          Day 6: Close is above slow SMA (counter = 3) > condition satisfied > BUY

          I hope that makes my intentions clear. I would like to integrate an additional filter that checks if there is really a trend and avoids whipsaw trades.

          Thanks for your help
          Last edited by phadreus; 08-18-2014, 01:53 PM.

          Comment


            #6
            Hello phadreus,

            Thank you for your response.

            You can find the code below:
            Code:
            			if(CrossAbove(SMA(fast), SMA(slow), 1))
            			{
            				smaSlowAtCross = SMA(slow)[0];
            			}
            			if(Close[0] > smaSlowAtCross)
            				counter++;
            			
            			if(counter >= 3)
            				EnterLong();

            Comment


              #7
              Patrick, thanks for your help, but it still does not work. I implemented your proposal as follows:

              double smaSlowAtCross = 0;
              int count = 0;

              if (CrossAbove( SMA(smaFast), SMA(smaSlow), 1))

              {
              smaSlowAtCross = SMA(smaSlow)[0];
              PrintWithTimeStamp(smaSlowAtCross.ToString());

              }

              if ( Close[0] > smaSlowAtCross)

              {
              count++;
              PrintWithTimeStamp(count.ToString());
              }

              if (count >= 3)

              {
              EnterLong();
              }

              The problem is, that it counts only one close. What ist my mistake?

              Thanks

              Comment


                #8
                Hello phadreus,

                Thank you for your patience.

                It is not what you missed. I did not mention that the condition that sets the smaSlowAtCross will check on every bar. So you need a switch that makes it so it does not reset the value. However, what would determine when to not reset the smaSlowAtCross and when to reset the smaSlowAtCross? That part you would need to figure out, because currently whenever a cross occurs the smaSlowAtCross resets to the new value and the Closes are now checking this value.

                Comment


                  #9
                  Patrick, many thanks that was much more support than I expected. It works pretty well. Thanks

                  Comment


                    #10
                    Hello,

                    Thanks phadreus and Patrick for inspiration in this thread.

                    I am trying to count days after cross above EMA288. I am not able do create a ""switch"" that Patrick wrote about in #8. So I have the problem that it counts only 1 Close as described in #7.

                    My idea is simple - I want to count days above EMA 288 all the time until price crosses below EMA288. This is the switch I assume. I will welcome any hints on how to code this simple idea.

                    My code so far:

                    protected override void OnBarUpdate()
                    {
                    double nadema288 = 0;
                    double podema288 = 0;
                    int countnad = 0;
                    int countpod = 0;

                    if (Close[0] > EMA(Close, 288)[0] && Close[1] < EMA(Close, 288)[1])
                    {
                    nadema288 = EMA(Close, 288)[0];
                    Print("NAD" + nadema288);
                    }

                    if (Close[0] > nadema288)
                    {
                    countnad++;
                    Print(countnad);
                    }

                    Comment


                      #11
                      Hello kadan,

                      You need to declare the variables in the scope of the class (outside of OnBarUpdate) so that these are not created as new objects and set to 0 on each run of OnBarUpdate().
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello Chelsea,

                        Thank you for your suggestion.

                        I did not create a class but only private int outside of OnBarUpdate and I got the indicator working as I excpected after few hours of work.

                        Comment


                          #13
                          Hello kadan,

                          The Strategy or Indicator is the class. The variables need to be declared within the scope of this.

                          Below is a public link to the NinjaScript Editor 401 video that details.


                          Also, below is a link to a forum post with helpful information about getting started with NinjaScript.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Chelsea B.,

                            Thank you for clarification. The video is very good :-)

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by jaybedreamin, Today, 05:56 PM
                            0 responses
                            3 views
                            0 likes
                            Last Post jaybedreamin  
                            Started by DJ888, 04-16-2024, 06:09 PM
                            6 responses
                            18 views
                            0 likes
                            Last Post DJ888
                            by DJ888
                             
                            Started by Jon17, Today, 04:33 PM
                            0 responses
                            1 view
                            0 likes
                            Last Post Jon17
                            by Jon17
                             
                            Started by Javierw.ok, Today, 04:12 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post Javierw.ok  
                            Started by timmbbo, Today, 08:59 AM
                            2 responses
                            10 views
                            0 likes
                            Last Post bltdavid  
                            Working...
                            X