Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MACD Crossover

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

    MACD Crossover

    I have created a strategy, using the wizard, that places a trade when the MACD crosses the zero line. The intention is that only one trade is taken until that trade has concluded - SL or TP hit. Only one trade should be taken in any one direction. The next trade should not be taken until the MACD has retraced across the zero line.

    My problem is that more than one trade is being taken. The sequence occuring is that a trade is taken and concluded. If the trend continues, another trade is opened immediately. I have set the strategy setting "Entries per direction = 1" so it should not happen. Is there any code I should add to prevent this from occuring?

    I am using NT7 and testing live on demo account. Other aspects of the strategy seem to be working as intended.

    Thanks
    Karl

    #2
    Hello,

    Thanks for your note.

    This is occuring due to this setting. What your strategy is continuing to call EnterLong becouse your strategy conditions are still true in an uptrend. EntriesPerDirection set to one only allows 1 of these EnterLongs called to actually go long. However once the trade is exited and the condition is still try EnterLong is being called again and Since EntriesPerDirection is set to 1 and you do not have a position it takes the EnterLong and goes long again.

    What you need to do to not have this occur is to add in some sort of trigger or bool flag that you can reset once you go back below the 0 line to then allow long trades again. Then in the first long trade taken set this flag to false and in the conditions before going long you want to check to see if this flag is set to true before entering the EnterLong trade.

    Let me know if I can be of further assistance.

    Comment


      #3
      MACD Crossover

      Thanks Brett. The trigger for the first trade is the MACD crossing the zero line. That event only occurs for the first trade. The second trade being taken is well after the crossover. If it was asked to check if the MACD was above/below the zero line then your explanation would be correct. I am not experienced in writing code and rely on the wiz but will see if I can find a line of code that will check if a trade has already been taken following the crossover. If it has then it will return false and not take a further trade. The logic is OK but the code to do it....??
      Thanks again for your help
      Karl

      Comment


        #4
        Hello,

        If your just using the strategy wizard. A feature used to combat this is the CrossAbove and CrossBelow feature in the strategy wizard. Make sure that in the center of the conditions that you used these instead of the Creater Then or Less Then symbols. This is where you set the conditions in the condition builder.

        Let me know if I can be of further assistance.

        Comment


          #5
          MACD Crossover

          Brett - not sure what you mean. I used the < and > to determine the direction of the crossover - the code I get is:


          protected override void OnBarUpdate()
          {
          // Condition set 1
          if (CrossAbove(MACD(13, 26, 9), 0, 1)

          {
          EnterLong(DefaultQuantity, "");
          PlaySound(@"C:\Program Files\NinjaTrader 7\sounds\MACD crossed zero line.wav");
          }

          // Condition set 2
          if (CrossBelow(MACD(13, 26, 9), 0, 1)

          {
          EnterShort(DefaultQuantity, "");
          PlaySound(@"C:\Program Files\NinjaTrader 7\sounds\MACD crossed zero line.wav");
          }
          }

          What I need is code to check if a trade has already been taken following the last crossover and if it is true, then no further trade will be taken until after the next crossover.

          Comment


            #6
            Scartree,

            What you can do here is add market position checks to your entries, so that you only take when you are in a flat state. This can be created in the wizard as well as in code. Just add an extra condition to your entries expand Strategy > Current Market Position == Strategy > Flat

            if (CrossAbove(MACD(13, 26, 9), 0, 1) && Position.MarketPosition == MarketPosition.Flat)
            {
            EnterLong(DefaultQuantity, "");
            PlaySound(@"C:\Program Files\NinjaTrader 7\sounds\MACD crossed zero line.wav");
            }

            // Condition set 2
            if (CrossBelow(MACD(13, 26, 9), 0, 1) && Position.MarketPosition == MarketPosition.Flat)
            {
            EnterShort(DefaultQuantity, "");
            PlaySound(@"C:\Program Files\NinjaTrader 7\sounds\MACD crossed zero line.wav");
            }
            Attached Files
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              MACD Crossover

              I am in awe of you guys and the service from NT is incredible. Thanks.

              Ryan, I am not sure that your suggestion solves the problem. Checking to see if flat would see the first trade closed but still see the conditions that would allow a second trade to occur in the same direction?

              The counter/flag suggestion seems to make sense and a bool to check if the counter is >0. The counter would be reset to 0 when the MACD retraces across the zero line but while in the same direction, the counter would be >0 and prevent further trades in that direction. Maybe I need a counter long and a countershort to cover both situations. I am looking at examples of code to see if I can find code that would do the job. Any suggestions would be most welcome.

              I did some Basic and Qbasic programming many years ago so coding logic is familiar but the syntax of Ninjascript is still foreign. I plan to persevere with it - it is certainly challenging!

              Thanks again
              Karl

              Comment


                #8
                Thank you for your kind words, much appreciated.

                Checking to see if flat would see the first trade closed but still see the conditions that would allow a second trade to occur in the same direction?
                Correct. If your long order is closed by a target or stop loss you could still see the long entry if it triggers before the short.

                A bool flag would work well for this. You only need one or two depending on how exact with the sequence you want to get. The example below uses one. If you start with myBool = true, then this forces the first position to be long.

                If you want to keep with the strategy wizard, we have a sample on setting up sequences with user variables here:



                Code:
                if (CrossAbove(MACD(13, 26, 9), 0, 1) && Position.MarketPosition == MarketPosition.Flat && myBool)
                {
                EnterLong(DefaultQuantity, "");
                PlaySound(@"C:\Program Files\NinjaTrader 7\sounds\MACD crossed zero line.wav");
                myBool = false;
                }
                
                // Condition set 2
                if (CrossBelow(MACD(13, 26, 9), 0, 1) && Position.MarketPosition == MarketPosition.Flat && !myBool)
                {
                EnterShort(DefaultQuantity, "");
                PlaySound(@"C:\Program Files\NinjaTrader 7\sounds\MACD crossed zero line.wav");
                myBool = true;
                }
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  MACD Crossover

                  Thanks Ryan
                  I have to go out now but will continue later in the day.
                  Regards Karl

                  Comment


                    #10
                    MACD Crossover

                    Hi Ryan
                    This is like extracting teeth - slow and painful! I guess everyone had to start somewhere!

                    I have declared bool variables:

                    bool longtradetaken = false:
                    bool shorttradetaken = false;

                    My entry condition allows the trade if the bool is false.

                    if (CrossAbove(MACD(13, 26, 9), 0, 1) && longtradetaken == false
                    {
                    EnterLong(DefaultQuantity, "");
                    longtradetaken = true;
                    }

                    // Condition set 2
                    if (CrossBelow(MACD(13, 26, 9), 0, 1) && shorttradetaken == false
                    {
                    EnterShort(DefaultQuantity, "");
                    shorttradetaken = true;
                    }

                    So far so good?

                    What I must now do is set the bool back to 'false' when the MACD crosses back over the zero line after a trade has been taken.

                    I am struggling with the code/syntax to do this. Some help or guidance would be greatly appreciated.

                    Thanks Karl


                    Comment


                      #11
                      MACD Crossover

                      I think I have an answer. When the trade is triggered, it results in the bool in the direction of the trade turning to true which prevents a further trade in that direction If I also add to change the bool in the opposite direction back to false, that means that a trade in that direction will then be allowed. So:

                      //I have declared bool variables:

                      bool longtradetaken = false:
                      bool shorttradetaken = false;

                      //My entry condition allows the trade if the bool is false.
                      //Condition set 1

                      if (CrossAbove(MACD(13, 26, 9), 0, 1) && longtradetaken == false
                      {
                      EnterLong(DefaultQuantity, "");
                      longtradetaken = true;
                      shorttradetaken = false;
                      }

                      // Condition set 2
                      if (CrossBelow(MACD(13, 26, 9), 0, 1) && shorttradetaken == false
                      {
                      EnterShort(DefaultQuantity, "");
                      shorttradetaken = true;
                      longtradetaken = false;
                      }

                      Is my syntax correct?

                      Comment


                        #12
                        Yes Karl, the logic looks good - have you given it a testrun?
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          MACD Crossover

                          Thanks Bertrand
                          Unfortunately, my work as an architect interferes with the much more interesting pursuit of trading and I have not yet made the changes to the strategy. I shall do so tomorrow (its night here in Queensland) and I shall post the outcome.
                          Regards Karl

                          Comment


                            #14
                            Sounds good Karl, we look forward to hear how it goes.
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              MACD Crossover

                              Gentlemen
                              With a few changes to the syntax I have managed to con Ninjascript into compiling my strategy!! Punctuation marks can be a real pain at times! I also had to rid my computer of all of my earlier efforts as they were causing problems. I had to close NT and re-open it, then I loaded the new strategy into several charts and will now wait and see how successful it is. So far, so good. Thanks for the help you guys have given me.
                              Regards Karl

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by judysamnt7, 03-13-2023, 09:11 AM
                              4 responses
                              59 views
                              0 likes
                              Last Post DynamicTest  
                              Started by ScottWalsh, Today, 06:52 PM
                              4 responses
                              36 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by olisav57, Today, 07:39 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post olisav57  
                              Started by trilliantrader, Today, 03:01 PM
                              2 responses
                              21 views
                              0 likes
                              Last Post helpwanted  
                              Started by cre8able, Today, 07:24 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X