Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with basic SMA Crossover strategy

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

    Help with basic SMA Crossover strategy

    I am trying to modify the Simple Moving Crossover strategy. I need to do the following to the basic strategy.
    1. Add a limit entry (long or short) that triggers at crossover that has the ability to enter several ticks above or below the crossover entry point.
    2. Add a automatic market exit (long or short) that triggers at next crossover.
    3. If the limit entry was not taken (not reached) then the entry/exit cycle would be cancled and renewed at the next crossover.
    I would like to use this in backtesting.
    Thanks, I have tried but failed to do this.

    #2
    Hello,

    Thank you for your post and welcome to our forums!

    Would you be able to post a snippet of the code you have tried that did not work? This should help us identify what was not working and help you get the results you are expecting.

    I look forward to assisting you further.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Kemosabe

      I just used the SMA crossover stratgey supplied in Ninjatrader and unsucessfully tried to modify it. Then I gave up and returned it to normal and asked for help and started studying the help guides for strategies.

      Comment


        #4
        Here is a basic example of the SampleMA modified to help get you started.

        For your first requirement, we specify a limit order by using EnterLongLimit() or EnterShortLimit().

        I have specified a price of 5 ticks from the current value of the slow SMA. You can change this to meet your needs.
        Code:
        if (CrossAbove(SMA(Fast), SMA(Slow), 1))
                        EnterLongLimit(SMA(Slow)[0] - 5 * TickSize);
                    else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
                        EnterShortLimit(SMA(Slow)[0] + 5 * TickSize);
        Next, we are using Position.MarketPosition to check if the strategy is in a long position AND checking for the opposite cross over to exit the long or short position.

        Code:
                    if (Position.MarketPosition == MarketPosition.Long && CrossBelow(SMA(Fast), SMA(Slow), 1))
                    {
                        ExitLong();
                    }
                    
                    else if (Position.MarketPosition == MarketPosition.Short && CrossAbove(SMA(Fast), SMA(Slow), 1))
                    {
                        ExitShort();
                    }
        The default behavior for NinjaTrader is to cancel limit orders if the trigger conditions are no longer true. However this can be changed if you need. Please take a look at the following reference sample:

        MatthewNinjaTrader Product Management

        Comment


          #5
          Kemosabe

          Thanks, let me give it a spin.
          Do I have to insert this into the existing SMA strategy (if so, where) or does this serve as a stand alone?
          Thanks so much.

          Comment


            #6
            You can make a copy of the existing SampleMACrossovers by going to Tools--> Edit NinjaScript--> Indicators--> open the SampleMACrossovers

            From the code, please right click on this window and select "Save As" and give it a new name.

            Then you can freely edit and modify this script.
            MatthewNinjaTrader Product Management

            Comment


              #7
              Kemosabe

              Thanks....

              Comment


                #8
                If this gets too complicated, I would recommend starting with the Strategy Wizard.
                Please see our Help Guide articles on How to use the Strategy Wizard as well as NinjaScript Strategies. More information on these subjects can be found below:


                There is also a 2 hour long Strategy Development Webinar conducted by our Product Manager Josh Peng hosted on the popular online trading community Big Mike Trading.com:

                The best futures trading community on the planet: futures trading, market news, trading charts, trading platforms, trading strategies
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  kemosabe

                  I figured where to replace the first part of the code but I am having some trouble inserting the second section in the right place.
                  We are allmost there.

                  Comment


                    #10
                    This would just go anywhere in the OnBarUpdate() method between the opening { and closing } (see screen shot)
                    Attached Files
                    MatthewNinjaTrader Product Management

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by yertle, Yesterday, 08:38 AM
                    7 responses
                    28 views
                    0 likes
                    Last Post yertle
                    by yertle
                     
                    Started by bmartz, 03-12-2024, 06:12 AM
                    2 responses
                    20 views
                    0 likes
                    Last Post bmartz
                    by bmartz
                     
                    Started by funk10101, Today, 12:02 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post funk10101  
                    Started by gravdigaz6, Yesterday, 11:40 PM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by MarianApalaghiei, Yesterday, 10:49 PM
                    3 responses
                    10 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Working...
                    X