Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code for price closing below 2 SMA

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

    Code for price closing below 2 SMA

    Hi,

    I'm trying to build a strategy and backtested where price has to close 2 consecutive times above the SMA 10 and SMA 12 then enter long and then exit on 2 closes below.
    NinjaSupport gave me the example below but it does not work.

    Can anyone shed some light?

    // Condition set 1
    if (CrossAbove(SMA(10), SMA(12), 1)
    && Variable0 > 0)
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossAbove(SMA(10), SMA(12), 1))
    {
    Variable0 = 1;
    }

    #2
    Hello john_robertson00,

    That code will only trigger once the SMA(10) crosses the SMA(12) the second time. Meaning that it the SMA(10) goes above the SMA(12) drops below and then crosses above once again. You may try the following to see if works better for you.

    Code:
    //Check current bar and 1 bar ago to see if the SMA(10) is greater than the SMA(12) on consecutive bars
    if (SMA(10)[1] > SMA(12)[1]
                    && SMA(10)[0] > SMA(12)[0])
                {
                    EnterLong(DefaultQuantity, "");
                }
    Let us know if we can be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      Hi JC, I have tried this but it did not work. It uses SMA being greater than the other so the entries are no good. It has to enter when the price closes above the SMA. Maybe we can start with price closing above only 1 SMA. Any suggestions? Thx.

      Comment


        #4
        I have tried the following code, but it is entering everytime that 2 Highs are greater then SMA 10 instead of price closing above SMA. Any hints why or is this a bug?

        if (Close[1] > SMA(10)[0])
        {
        EnterLong(DefaultQuantity, "");
        }

        Comment


          #5
          Originally posted by john_robertson00 View Post
          Hi,

          I'm trying to build a strategy and backtested where price has to close 2 consecutive times above the SMA 10 and SMA 12 then enter long and then exit on 2 closes below.
          NinjaSupport gave me the example below but it does not work.

          Can anyone shed some light?

          // Condition set 1
          if (CrossAbove(SMA(10), SMA(12), 1)
          && Variable0 > 0)
          {
          EnterLong(DefaultQuantity, "");
          }

          // Condition set 2
          if (CrossAbove(SMA(10), SMA(12), 1))
          {
          Variable0 = 1;
          }
          The code to do what you describe is shown below.
          Code:
          bool Condition1 = Close[0] > SMA(10)[0] && Close[1] > SMA(10)[1];
          bool Condition2 = Close[0] > SMA(12)[0] && Close[1] > SMA(12)[1];
          bool Condition3 = Close[0] < SMA(10)[0] && Close[1] < SMA(10)[1];
          bool Condition4 = Close[0] < SMA(12)[0] && Close[1] < SMA(12)[1];
          
          if (Condition1 && Condition2) EnterLong(DefaultQuantity, "");
          if (Condition3 && Condition4) ExitLong();
          You need to be aware that there is considerably more code that you are going to have to write to have a working strategy. The code that I have given addresses only the exact question that you asked.
          Last edited by koganam; 12-27-2012, 04:56 AM. Reason: Corrected code listing.

          Comment


            #6
            Hi koganam, thanks for the code. this is what I was looking for. this does what I was trying to accomplish. I can build from here. thanks.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Kaledus, Today, 01:29 PM
            5 responses
            12 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by Waxavi, Today, 02:00 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by alifarahani, Today, 09:40 AM
            5 responses
            23 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by gentlebenthebear, Today, 01:30 AM
            3 responses
            16 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by PhillT, Today, 02:16 PM
            2 responses
            7 views
            0 likes
            Last Post PhillT
            by PhillT
             
            Working...
            X