Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple SMA Strategy Help

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

    Simple SMA Strategy Help

    I am new to strategy development in NT. I have a very simple strategy that I cannot quite get the logic right on in the strategy builder. I want to create a strategy that goes long if the daily close is above the 5-day simple moving average for 4 days straight. I then want the strategy to go short if the daily close is below the 5 day SMA for 4 days. I can get crossover strategies to work right, but I am not sure how to make sure the close is above or below for 4 daily bars before taking a long or short trade. Any help would be appreciated.

    #2
    Hi thebluescout,

    Thanks for your post.

    Are you checking that the Close[0] is greater than the PriorDayOHLC.PriorClose[0] price?

    May I have a screenshot of your condition in the condition builder?
    To send a screenshot press Alt + PRINT SCREEN to take a screen shot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.

    For detailed instructions please visit the following link
    http://take-a-screenshot.org/
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Here is the screenshot. I am not sure how to make the condition to ensure that the close is above the 5 day SMA for 4 consecutive days before taking a long trade, and vice versa for a short trade. Thanks.
      Attached Files

      Comment


        #4
        Originally posted by thebluescout View Post
        Here is the screenshot. I am not sure how to make the condition to ensure that the close is above the 5 day SMA for 4 consecutive days before taking a long trade, and vice versa for a short trade. Thanks.
        The problem with the wizard is it limitations. In your particular case, the lack of alternative branching means that you need to evaluate every possibility and set a variable to query. Translate this sequence into the wizard:
        1. If Close[0] is greater than SMA(5)[0], increase Variable0 by 1.
        2. If Close[0] is not greater than SMA(5)[0], make Variable0 equal to zero.
        3. If Variable0 is 4 or more, then enter trade.

        In the future, it might help you code better if you will breakdown, in writing, what your high level statement means in processing terms. I started with the statements:
        1. Accumulate or reset a count
        2. If the count is sufficient, take a trade.

        which is essentially saying, "If something happens this number of times consecutively ...".

        After that, translation to what I wrote here, was a simple breakdown to how to accumulate or reset the count, given the condition that you wanted met.
        Last edited by koganam; 08-15-2014, 10:31 AM.

        Comment


          #5
          Hi thebluescout,

          "5-day simple moving average for 4 days straight"

          Does this mean you want to use a daily data series for the SMA calculation?
          Also, the current close price would need to be above this calculation for 4 days in a row?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Yes, I do want a daily data series for the SMA. And the current daily close would have to be above the SMA for 4 days straight. Thanks.

            Comment


              #7
              I think I am close to getting the hang of what I need to do. My question is how do I make Variable0 increase. I see in the condition builder where I can make it equal to one iIf Close[0] is greater than SMA(5)[0]. Not sure if I am making it increase though.

              Comment


                #8
                Hi thebluescout,

                Does this setup work for what you are trying to do?

                (File attached)
                Attached Files
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I definitely appreciate the help. This is pretty close. It appears that the "count" is not on consecutive bars. Thus, if there are 4 bar closes below the SMA, even if non-consecutive, then it triggers a sell. Conversely, if there are 4 closes above the SMA, even if non-consecutive, it triggers a buy. I would like it to be 4 consecutive bars in order to trigger an order. Not sure if there is an easy way to do this.

                  Comment


                    #10
                    Hi thebluescout,

                    I may have misunderstood what you are trying to do.

                    The code looks that the current price is greater than the SMA of the daily period today and the current price is greater than the SMA of the daily period yesterday, and the current price is greater than the SMA of the daily period the day before yesterday, etc..

                    Is this not what you are looking for?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hi thebluescout,

                      I may have misunderstood what you are trying to do.

                      The code looks that the current price is greater than the SMA of the daily period today and the current price is greater than the SMA of the daily period yesterday, and the current price is greater than the SMA of the daily period the day before yesterday, etc..

                      Is this not what you are looking for?
                      Probably a problem with the last of your conditional lines:
                      Code:
                      && Close[0] > SMA(PriorDayOHLC().PriorOpen, 5)[3])
                      You probably want to change that to use the Close instead:
                      Code:
                      && Close[0] > SMA(PriorDayOHLC().PriorClose, 5)[3])
                      Last edited by koganam; 08-15-2014, 04:02 PM.

                      Comment


                        #12
                        koganam,

                        Nice job! Didn't see that.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Once again, I definitely appreciate all the help. I'm still having issues though, even with that revised code. For example in entering a long trade, it appears that there are 4 total closes above the SMA to trigger a long position. This "count" does not reset, even if there was an intervening close below the SMA. I would like the 4 closes above or below to be consecutive. I have attached a screenshot of the output I am getting. Please let me know if I need to explain something in greater detail.
                          Attached Files

                          Comment


                            #14
                            Hi thebluescout,

                            Have you corrected the mistake that kognam found?

                            Attached is a copy of the indicator with the correction.

                            This indicator does not use a count that is reset. The code looks that the current price is greater than the SMA of the daily period today and the current price is greater than the SMA of the daily period yesterday, and the current price is greater than the SMA of the daily period the day before yesterday, etc..

                            Please take a look at the code in the script and let me know if you have questions with this.
                            Attached Files
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by thebluescout View Post
                              Once again, I definitely appreciate all the help. I'm still having issues though, even with that revised code. For example in entering a long trade, it appears that there are 4 total closes above the SMA to trigger a long position. This "count" does not reset, even if there was an intervening close below the SMA. I would like the 4 closes above or below to be consecutive. I have attached a screenshot of the output I am getting. Please let me know if I need to explain something in greater detail.
                              Your picture shows that you are using Daily bars. In that case, you can just use the static approach for 4 days.

                              The first picture shows what the Strategy Panel looks like; the second shows what values you need to look at in the conditions pane.
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              646 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              367 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              569 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X