Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help Using Variables For Exits

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

    Help Using Variables For Exits

    Hello Everyone,


    I'm at an impasse with what I'm trying to code and am really frustrated.

    When I enter a trade on the 25 tick, I have exit criteria and pass criteria (both based on the 25 tick). If my exit criteria is triggered first, then I should exit the trade. However, if my pass criteria is triggered first, then I should only exit the trade based on criteria that gets triggered on the 200 tick chart.

    The problem I'm having occurs when I enter a trade and my pass criteria gets triggered first (which means I should now only exit based on the 200 tick chart criteria), BUT, the 25 exit criteria gets triggered at least once before I can exit based on the 200 tick criteria. So, I end up exiting the trade much earlier and based on the 25 exit criteria instead of exiting based on the 200 tick criteria. I hope that wasn't confusing.

    To alleviate this problem, I've tried using variables in numerous ways but am unable to get the correct results. I don't think what I'm trying to do is very difficult, but I do think it requires the use of variables, and I don't think I'm coding or resetting the variables correctly.

    I am coding via NinjaScript. Can someone please be patient and explain how to accomplish the above results using variables OR if you can explain how to accomplish this without using variables, I would be eternally grateful!


    Thank you.


    Ttrade12​

    #2
    Hello Ttrade12,

    You can likely use a bool.

    "However, if my pass criteria is triggered first, then I should only exit the trade based on criteria that gets triggered on the 200 tick chart."

    If the "pass criteria" is triggered set the bool to true.

    "BUT, the 25 exit criteria gets triggered at least once before I can exit based on the 200 tick criteria."

    Require the bool to be false in the "25 exit criteria" so this cannot evaluate as true.

    Once the exit occurs, set the bool back to false.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you, ChelseaB!! I set the "pass criteria" bool to be true when its triggered and I set the bool to be false in the "25 exit criteria" BUT I wasn't setting the bool back to false when the exit occurred. Got it. I'll try this!!!

      Comment


        #4
        Hi ChelseaB,


        This worked. Thanks! I also have 'SetStopLoss' under the State == State.DataLoaded section of my NinjaScript. We have to reset our variables back to 'false' whenever we exit a trade...yes? So, when I exit a trade via my StopLoss, do I need to put somewhere in the code that the variables are reset to false? Or when the StopLoss is triggered, does it automatically reset all of the variables to false? Thanks for any guidance you can provide.

        Comment


          #5
          Hello Ttrade12,

          For that use case you can make a new a condition checking if you are flat and move that to be set 1. Then reset the variable there when flat before any other conditions can be evaluated. The SetStopLoss execution can also be observed in OnExecutionUpdate so if you are using that you could use that override as well.

          Comment


            #6
            Thanks, Jesse!


            1. I don't have 'OnExecutionUpdate' in my NinjaScript. Should I have this? Is it OK that I don't have this section in my code?

            2. For having a new condition in Set 1 for variable reset, does below accurately depict what you've suggested:

            // Set 1 - If MP = flat then all variables reset
            if (Position.MarketPosition == MarketPosition.Flat)
            {
            25PassBuy = false;
            25PassSell = false;
            50PassBuy = false;
            50PassSell = false;
            }

            3. If Set 1 above is correct, does that mean I can remove all of the variable resets I've added to my Exits? Or should I keep the variable resets in my Exits? An example of variable resets in one of my Exits is below:

            if ((Condition....)
            && (Condition...)
            && (Condition...))
            {
            ExitLong(Convert.ToInt32(DefaultContracts), @"50LongExit", @"");
            25PassBuy = false;
            50PassBuy = false;
            }


            Thanks for any guidance you can provide.
            ​​

            Comment


              #7
              Hello Ttrade12,

              Using OnExecutionUpdate would be an alternative.

              The code you have shown does appear to be correct. set 1 just means before any other conditions you have below that point. You likely still need to use the resets in other situations, that is just a catchall to make sure when your flat the variable is being reset to account for the target which you don't have a condition for.

              Comment


                #8
                Got it! Thanks, Jesse!!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                62 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                134 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                75 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X