Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

bars since cross

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

    bars since cross

    I would like to set a condition for a strategy, an action to take place within the first 15 bars of a MACD cross, how would I do this?

    #2
    Hello jackegtrader, and thank you for your question.

    I have prepared a series of images showing how to generate a template for the kind of condition you would like through the New Strategy wizard. Step by step,

    1. Control Center -> Tools -> New NinjaScript -> Strategy
    2. Next -> Give your script a name -> Next
    3. Let's skip the parameter screen. Next
    4. On the Condition Builder screen, click "Add"
    5. Select Indicator -> MACD and set the Bars ago to 15 on the left. Select == as the operator. Select Misc -> true on the right hand side. Select OK
    6 (Not pictured) Select "Unlock Code"

    Please let us know if there is anything else we can do to help.
    Attached Files
    Last edited by NinjaTrader_JessicaP; 05-02-2016, 10:00 AM.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      1. Control Center -> Tools -> New NinjaScript -> Indicator
      Surely, you mean Strategy, right?

      Comment


        #4
        Correct, I meant Strategy, thank you for the catch. My original post has been edited to reflect this change.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Jessica, how does this help with bars 1-14? And it only refers to the MACD, not the MACD cross. And where does one go once the code is unlocked?

          Comment


            #6
            Hello birdjaguar,

            I am attaching an image with a more complete example. Note that I set the 2 MACD data series in the Plot section. Note also that I set 15 for the look back period. Finally note that I set crossabove as the comparator.

            I am not sure I understand what is meant with your second question, but all the code generated here will go into the OnBarUpdate section. If that does not quite answer your second question, could you elaborate?
            Attached Files
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Thank you. Your attachment is very helpful. My question now is how can a lookback period be applied to an operator? In the wizard the lookback period is only available for crossabove or crossbelow. How can the lookback period be used with > or < or >= etc.?

              Comment


                #8
                Thank you for the clarification. You are correct that you would have to unlock code for this. Applying a lookback period with an operator can be accomplished with a for loop. For instance, this for loop over a 15 bar lookback period will alert you if two MACD lines were equal at any point in the last 15 bars.

                Code:
                [FONT=Courier New]int lookback = 15, fast = 3, slow = 15, smooth = 7;
                bool conditionOccurred = false;
                MACD macd = new MACD(fast, slow, smooth);
                for(int i = 0; i < lookback && ! conditionOccurred; i++)
                {
                    if (macd.Avg[i] == macd.Diff[i])
                    {
                        conditionOccurred = true;
                    }
                }[/FONT]
                Once that loop completes, conditionOccurred will let you know whether the comparator was valid.

                The above style of for loop tests to see if the comparator was valid at any point in the lookback period. If you would prefer to see if the comparator was valid at every point in the lookback period, we can just invert our boolean, like so :

                Code:
                [FONT=Courier New]int lookback = 15, fast = 3, slow = 15, smooth = 7;
                bool conditionOccurred = [B]true[/B];
                MACD macd = new MACD(fast, slow, smooth);
                for(int i = 0; i < lookback && ! conditionOccurred; i++)
                {
                    if (macd.Avg[i] [B]!=[/B] macd.Diff[i])
                    {
                        conditionOccurred = [B]false[/B];
                    }
                }[/FONT]
                Remember when inverting other comparators, < is the opposite of >= and > is the opposite of <= .
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you. That is thorough and very useful. I actually did go down that path, but was not savvy enough in C# programming to be able to pull it off. I was trying to combine signals between a MACD and Heiken Ashi within 5 bars. The MACD became too complicated for me at this point; so I used the "lookback"concept from your previous post to solve the problem on the Heiken Ahsi end. Given a MACD signal, I used the following:
                  CrossAbove(HeikenAshi().HAClose, HeikenAshi().HAOpen, 5) for the long side and
                  CrossBelow(HeikenAshi().HAClose, HeikenAshi().HAOpen, 5) for the short side.
                  Thanks again. I am good to go.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by algospoke, Yesterday, 06:40 PM
                  2 responses
                  20 views
                  0 likes
                  Last Post algospoke  
                  Started by ghoul, Today, 06:02 PM
                  3 responses
                  14 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by jeronymite, 04-12-2024, 04:26 PM
                  3 responses
                  45 views
                  0 likes
                  Last Post jeronymite  
                  Started by Barry Milan, Yesterday, 10:35 PM
                  7 responses
                  21 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by AttiM, 02-14-2024, 05:20 PM
                  10 responses
                  181 views
                  0 likes
                  Last Post jeronymite  
                  Working...
                  X