Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to exit out of a long or short when one of the condition sets arn't aligning

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

    How to exit out of a long or short when one of the condition sets arn't aligning

    How do i exit out of a long or short position if my condition set alignment arn't met or just one of them isn't met in the condition set.

    ex. the MACD cross above the MACD isn't bullish and is crossed below but the ADX is Greater than 15 and DI+ is crossed above the DI- is still intact.

    I want to exit out of the long position if either or conditions arn't met. How do i sell if the one condition isn't met in the set instead of selling when they are both out of alignment in the set. How do i plan an exit long if one of the conditions arn't met for the long order and need to get out of the trade?


    Best Regards,

    Cameron

    #2
    Hi Cameron,

    Thanks for your note.


    From what I have read you would like to write some conditions that look for indicators crossing as well as not rising or above a certain amount.

    For example to trigger when the MACD(12, 26, 9) crosses above the MACD(6, 13, 5) use:
    if ( CrossAbove(MACD(12, 26, 9), MACD(6, 13, 5), 1) )
    {
    // execute code
    }

    To see if the MACD(12, 26, 9) is not rising or not bullish use:
    if ( Rising(MACD(12, 26, 9)) == false )
    {
    // execute code
    }

    To see if the DI+ is above the DI- use:
    if (DM(14).DiPlus[0] > DM(14).DiMinus[0])
    {
    // execute code
    }

    To combine these conditions into a condition set use && to join them.
    For example:
    if (Rising(MACD(12, 26, 9)) == false && DM(14).DiPlus[0] > DM(14).DiMinus[0])
    {
    // execute code
    }


    Please let me know if this does not resolve your inquiry.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      // Condition set 1
      if (CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1)
      && CrossAbove(ADX(Close, 14), ADXStrength, 1)
      && CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
      && CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
      {
      EnterLong(1, "");


      My question is this is basically what my strategy is going to look like at the end resualt besides adding a toggle function of going long or short. Which im trying to clarify how to do in the strategy wizard or if possible in code.

      I want to combine these and if one of them is failing id like to get out of the trade, so i guess combining them would be proper, Isn't this already done in my code? by using && and combining them.

      My question is if one of these like lets say the MACD falls or something below and im long, id like to get out of the trade, and only execute when they are all rising in tangent, if im looking to go long or short the trade? by using the COT reports

      Comment


        #4
        Hi Cameron,

        So from the code I see posted the strategy will enter a long position when all of these indicators cross above their pairs.

        By failing do you mean that these indicators CrossBelow their pair or do mean if they are falling or do you mean if they are just not rising?

        From your last sentence I believe you mean when any of these cross below their pair to exit the long (or enter short).

        Basically you would want to use "or" (||) instead of "and" (&&).

        Unfortunately, this is beyond the capability of the Strategy Wizard.

        The code would look like this:
        // Condition set 1
        if (CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1)
        || CrossBelow(ADX(Close, 14), ADXStrength, 1)
        || CrossBelow(DM(14).DiPlus, DM(14).DiMinus, 1)
        || CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
        {
        ExitLong();
        }


        Let me know if this is not what you were looking for.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          One last question to clarify

          Ok so i could i put this as condition set 3 and set one up the opposite on 4 for exiting a short. What i want to know if this is the parameter to exit a long if one of the indicators are not aligning so if the MACD crosses below and the ADX is up this is the code to exit out of the long, correct? and i have to set this for both long and short conditions, does it matter that they will be in different sets, or does the going long and exit long have to be in set 1 and 2 instead of 1 and 3 condition sets? Does this pattern matter in code? and the short and exit short will be condition set 2 and 4? Do you know where im coming from?


          Thanks for your time Chelsea, you've helped out a bunch and i hope others can make use of this as well

          Comment


            #6
            Does this look right? I keep getting errors?

            region Variables
            // Wizard generated variables
            private int ADXStrength = 18; // Default setting for ADXStrength
            private int trailingStop = 30; // Default setting for TrailingStop
            private int stoploss = 30; // Default setting for Stoploss
            private int slowMA = 25; // Default setting for SlowMA
            private int fastMA = 10; // Default setting for FastMA
            // User defined variables (add any user defined variables below)
            #endregion

            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
            SetTrailStop("", CalculationMode.Ticks, TrailingStop, false);
            SetStopLoss("", CalculationMode.Ticks, Stoploss, false);

            CalculateOnBarClose = true;
            }

            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
            // Condition set 1
            if && CrossAbove(SMA(FastPeriod), SMA(SlowPeriod), 1)
            && CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
            && CrossAbove(ADX(14), ADXStrength, 1)
            && CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
            && CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
            {
            EnterLong(1, "");
            }

            // Condition set 2
            if (CrossBelow(SMA(FastPeriod), SMA(SlowPeriod), 1)
            || CrossBelow(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
            || CrossBelow(ADX(14), ADXStrength, 1)
            || CrossBelow(DM(14).DiPlus, DM(14).DiMinus, 1)
            || CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
            {
            ExitLong();
            }

            // Condition set 3
            if && CrossBelow(SMA(FastPeriod), SMA(SlowPeriod), 1)
            && CrossBelow(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
            && CrossAbove(ADX(14), ADXStrength, 1)
            && CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1)
            && CrossAbove(Stochastics(7, 14, 3).D, Stochastics(7, 14, 3).K, 1))
            {
            EnterShort(DefaultQuantity, "");
            }

            // Condition set 4
            if (CrossAbove(SMA(FastPeriod), SMA(SlowPeriod), 1)
            || CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
            || CrossBelow(ADX(14), ADXStrength, 1)
            || CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
            || CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
            {
            ExitShort();
            }
            }

            Comment


              #7
              Hello Cameron,

              Moving forward I will be replying to you via email as you have a second open ticket with the same issue.

              Thanks.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Jimmyk, 01-26-2018, 05:19 AM
              6 responses
              834 views
              0 likes
              Last Post emuns
              by emuns
               
              Started by jxs_xrj, 01-12-2020, 09:49 AM
              6 responses
              3,290 views
              1 like
              Last Post jgualdronc  
              Started by Touch-Ups, Today, 10:36 AM
              0 responses
              10 views
              0 likes
              Last Post Touch-Ups  
              Started by geddyisodin, 04-25-2024, 05:20 AM
              11 responses
              62 views
              0 likes
              Last Post halgo_boulder  
              Started by Option Whisperer, Today, 09:55 AM
              0 responses
              8 views
              0 likes
              Last Post Option Whisperer  
              Working...
              X