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

SMA crossover

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

    SMA crossover

    Hi, I have using SMAs as part of my strategy to initiate and exit long positions. If you look at the attached chart, the long position was entered correctly at 9:37am but the exit of that position should have been at 11:57am when the crossover occured instead of exit on close. BTW I've been using the strategy wizard to create this script.

    Another question I have is if I don't want to exit a position at a predetermined time like "exit on close" but rather let the indicator determine the time of exit, how do i go about doing that?

    I've attached the ninjascript for this strategy too.
    Attached Files

    #2
    If you feel your trade is not doing what it should you should debug your strategy with TraceOrders = true in Initialize(). Please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=3627

    If you do not want to use Exit on Close, set it to false when you start up the strategy.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh, I used the strategy wizard to create this strategy. There isn't or I can't see the line with ExitOnClose in it.
      I see why the strategy doesn't work now because the strategy only works when SMA(Fast)crosses above SMA(Medium) and SMA(Fast) crosses above SMA(Slow) on the same bar.
      How would I write a script that upon the crossing of SMA(Fast) above SMA(Medium) and to initiate a position only subsequent to the crossing of SMA(Fast) above SMA(Slow)? The crossing above does not necessarily have to be on the same bar.

      Comment


        #4
        Not quite sure I follow. Fast going above medium implies it already went above the slow.

        ExitOnClose is an option as you run your strategy. When you are parameterizing it on running of it, you have the option in that dialogue near the middle half.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Let's say Fast=5, Medium=20 and Slow=55. Fast will cross above Medium before it passes Slow. Fast may not even pass Slow or it may not pass Slow on the same bar.
          Please refer to the chart in my first post.

          Comment


            #6
            kaywai, have you tried just setting Exit On Close to false? That way, the strategy will wait until the conditions are right to close out a position instead of a time based exit.
            Attached Files
            AustinNinjaTrader Customer Service

            Comment


              #7
              Kaiwai,
              To do what you want you only need to think logically. You can ask questions and can answer them, but if I think that instead of trying to think you relay on others then you won't be a good trader.
              To your question:
              if (crossabove(fast,medium) && fast > slow[0] || crossabove(fast, slow) && fast > medium[0])

              Correct the syntax.

              Baruch

              Comment


                #8
                Austin, the ExitOnClose = False worked perfectly!
                Still need to sort out the crossover problem as it only triggers a trade if SMA(Fast) crosses above SMA(Medium) and SMA(Slow) on the same bar. I'm trying to use the strategy wizard to initiate a trade when SMA(Fast) crosses above SMA(Medium) and SMA(Slow) and the cross above does not necessarily have to be on the same bar. Could you please assist?

                Comment


                  #9
                  Baruch,

                  Having close to zero experience in programming is why I decided to resort to the strategy wizard. looking at some of the script that has been written just blows me away! so i am trying to keep in very simple at the moment and asking questions that I am unsure of as I go along. Hope that is ok with you.



                  Comment


                    #10
                    kaywai, I took a look at your code and it is written to look for two crossovers on the same bar:
                    Code:
                    if (CrossAbove(SMA(Fast), SMA(Medium), 1)
                                    && CrossAbove(SMA(Fast), SMA(Slow), 1))
                                {
                                    EnterLong(DefaultQuantity, "");
                                }
                    The && operator means AND, so the statement will only be true if both the conditions are true. Baruch's code included the || operator (OR), so the statement will return true if either of the conditions are true.

                    The code he posted should be a great starting point for your strategy.
                    Code:
                    // if (crossabove(fast,medium) && fast > slow[0] || crossabove(fast, slow) && fast > medium[0])
                    // translates to:
                    if ((CrossAbove(SMA(Fast), SMA(Medium), 1) && SMA(Fast)[0] > SMA(Slow)[0]) || CrossAbove(SMA(Fast), SMA(Slow), 1) && SMA(Fast)[0] > SMA(Medium)[0]))
                    AustinNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks for your help Baruch and Austin!

                      Comment


                        #12
                        Guys, One last question. With Baruch's code, I know need to "unlock code" in strategy wizard, correct?

                        Comment


                          #13
                          kaywai, that is correct. Also, keep in mind that you must compile the code after every change to see the difference. You can compile by right-clicking in the editor window and selecting compile, by hitting the F5 key, or by clicking the button to the right of the binoculars in the toolbar.
                          AustinNinjaTrader Customer Service

                          Comment


                            #14
                            Thanks Austin!

                            Comment


                              #15
                              old post, but it was worth reviving

                              a bit surprised no one actually answered original posters question. It is relevant to me, as I'm just picking this up, and lo and behold I have the same issue.

                              OP did not ask for OR, II operator, as I have figured that out myself too. What he was asking, if there is a way to have ONE cross above the TWO, WHILE it is already above THREE.

                              In the answer given, he will get a false signal, because the fastest moving average may already be above the slowest moving average, and that will trigger an action. What he is looking instead is that the fastest moving average is ALSO above the middle moving average. However, he was asking how to get that done, so that it doesn't work in that single magical place, when all THREE cross at the same time.

                              And yes, I'm having the same issue; for a programmer I'm sure it's simple, but I'm looking up references, and I can't just do "crossabove (fast, medium, 1) && (fast > slow) (not correct syntax, but you get the point)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by FrazMann, Today, 11:21 AM
                              2 responses
                              6 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Started by rjbtrade1, 11-30-2023, 04:38 PM
                              2 responses
                              80 views
                              0 likes
                              Last Post DavidHP
                              by DavidHP
                               
                              Started by Spiderbird, Today, 12:15 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Started by lorem, Yesterday, 09:18 AM
                              5 responses
                              18 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              12 responses
                              42 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X