Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Pullback to 20 EMA Strategy

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

    Pullback to 20 EMA Strategy

    I'm trying to create pullback strategy in the Strategy Builder, but I cannot set the right conditions to make it happen.
    1. Price is pulling back down to the 20 EMA
    2. Price crosses below the 20 EMA, but CLOSES above the EMA
    3. Place Buy Order when price breaks the high of the previous candle (# 2 above). See attached screenshot

    Please advise. Thanks!

    #2
    Hello rjsjr64,

    What do you specifically consider "Price is pulling back down to the 20 EMA"?

    Does this mean that the Close[0] is more than a certain amount of ticks above the EMA(20)[0] on the previous bar and is less than an amount of ticks above the EMA(20)[0] on the current bar?
    If so, how many ticks above the EMA must the close be on the previous bar?
    How many ticks above the EMA must the close be on the current bar?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I'm not married to the # of ticks above the EMA.. What I'm looking for is a breakout to the upside (no matter how big or small) that is above the 20 EMA, and then the candle pulls back to AND CROSSES BELOW the 20 EMA but CLOSES ABOVE to 20 EMA. For the sake of argument, lets just say the last 3 candles put in lower highs and lower lows... ultimately crossing below the 20 EMA. but closing above it. Then, if in the next bar the price breaks the high of the previous candle (the one that crossed below but closed above), a long trade would be triggered.

      Comment


        #4
        Hello rjsjr64,

        Lets try to be specific.

        If the price is above the EMA(20) and the price of the current bar is less than the price of the previous bar? Would you consider that a pull-back?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Sure, we can go with that.. However, the main criteria is that the price crosses below the EMA, but closes above the EMA in the same candlestick. See the attached image in my initial post for the visual.

          Comment


            #6
            Option 2: price crosses below the EMA, and then the next candle it closes above the EMA. Go long once the price breaks above the high of the candle that closed above the EMA

            Comment


              #7
              Hello rjsjr64,

              I will not be deciding the logic for you. I am only attempting to help you understand how the logic you have chosen would look in C#.

              Going one step at a time and starting with "pullback", there is no method called pullback. You will have to decide what a pullback is the way you see it.

              So to you a pullback is the price crossing above the EMA and closing above the EMA?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Let’s try this:
                1. Price is above the EMA
                2. 2 consecutive candles closed lower than the previous candle (descending in toward the EMA)
                3. Candle crosses the EMA, but closes above it.
                4. Place buy order when price of next candle clears the high of the candle that crossed the EMA, but closed above it

                Comment


                  #9
                  Hello rjsjr64,

                  1, "Price is above the EMA"
                  Close[0] > EMA(20)[0]

                  2. "2 consecutive candles closed lower than the previous candle"
                  Close[3] < Close[1] && Close[2] > Close[1]

                  3. "Candle crosses the EMA, but closes above it"
                  (this makes condition 1 redundant. If there is a cross above, then series 1 is greater than series 2. Your first condition isn't necessary)
                  CrossAbove(Close, EMA(20), 1)

                  4. "Place buy order when price of next candle clears the high of the candle that crossed the EMA, but closed above it"
                  Save the bar number of where the cross occurred. Check that the current price is greater than the high of the bar where the cross occurred.
                  private int crossBarNum;

                  if (CrossAbove(Close, EMA(20), 1))
                  {
                  crossBarNum = CurrentBar;
                  }

                  if (Close[0] > High[CurrentBar - crossBarNum])
                  {
                  EnterLong();
                  }
                  Last edited by NinjaTrader_ChelseaB; 07-28-2020, 07:18 AM.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    THANK YOU!

                    I don't have much experience with coding. Is this how the entire sequence would be formatted?

                    {
                    Close[0] > EMA(20)[0]
                    }
                    {
                    Close[3] < Close[1] && Close[2] > Close[1]
                    }
                    {
                    CrossAbove(Close, EMA(20), 1)
                    }
                    {
                    if (CrossAbove(Close, EMA(20), 1))
                    }
                    {
                    crossBarNum = CurrentBar;
                    }
                    {
                    if (Close[0] > High[CurrentBar - crossBarNum])
                    }
                    {
                    EnterLong();
                    }

                    Comment


                      #11
                      Hello rjsjr64,

                      This would depend on when you want each action checked.

                      The conditions would appear similar to:

                      if (CrossAbove(Close, EMA(20), 1))
                      {
                      crossBarNum = CurrentBar;
                      }

                      if (Close[0] > EMA(20)[0] && Close[3] < Close[1] && Close[2] > Close[1] & Close[0] > High[CurrentBar - crossBarNum])
                      {
                      EnterLong();
                      }

                      As a tip, use prints every step of the way to ensure you are understanding the behavior and why the condition is evaluating as true or false.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        What would be the exact code in the scenario I spelled out?

                        1, "Price is above the EMA"
                        Close[0] > EMA(20)[0]

                        2. "2 consecutive candles closed lower than the previous candle"
                        Close[3] < Close[1] && Close[2] > Close[1]

                        3. "Candle crosses the EMA, but closes above it"
                        (this makes condition 1 redundant. If there is a cross above, then series 1 is greater than series 2. Your first condition isn't necessary)
                        CrossAbove(Close, EMA(20), 1)

                        4. "Place buy order when price of next candle clears the high of the candle that crossed the EMA, but closed above it"
                        Save the bar number of where the cross occurred. Check that the current price is greater than the high of the bar where the cross occurred.
                        private int crossBarNum;

                        if (CrossAbove(Close, EMA(20), 1))
                        {
                        crossBarNum = CurrentBar;
                        }

                        if (Close[0] > High[CurrentBar - crossBarNum])
                        {
                        EnterLong();
                        }
                        Last edited by rjsjr64; 07-29-2020, 04:31 AM.

                        Comment


                          #13
                          Hello rjsjr64,

                          Is the code I suggested in post #11 not what you are looking for?

                          I wasn't certain if all of the conditions were meant to be happening at once, but that would be each condition put together in this condition set.

                          The cross would of course have happened on a previous bar so this is in a separate condition.


                          Also, I wanted to provide a link to a forum post with helpful information about getting started with NinjaScript. Be sure to watch the Strategy Builder 301 and NinjaScript Editor 401 videos.


                          You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            It is what I’m looking for, but I don’t know where to add the { or } to the code, or when not to add it.

                            Comment


                              #15
                              Hello rjsjr64,

                              Curly braces are action blocks (this is discussed in the NinjaScript Editor 401 video). This is like the walls of a container. Anything between a set of curly braces is 'inside' the scope of that.

                              These action blocks are for if statements (branching commands), methods, and classes. The code within is evaluated when the if statement is true, or when a method is called, or will contain all of the methods and variables within a class.

                              if (/* condition here *)
                              { //<--- opening curly brace
                              // condition was true, code here will be executed
                              } //<-- closing curly brace
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              59 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              38 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              42 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              78 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X