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


Avoid Wrong Entry Trades

  • Filter
  • Time
  • Show
Clear All
new posts

    Avoid Wrong Entry Trades

    Hi guys

    On my strategy I use code like this to exit trades

    if (BarsInProgress == 0 &&
    (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 350)
    ExitShortLimit((Position.AveragePrice + (600 * TickSize)), "Profit300", "Short");
    The problem is that whenever this condition is met it exit the trade, and automatically start a new trade in opposit direction.
    How do I make it so it doesn't start a new trade right away after my exit condition, it should only start a trade when my entry condition is true.

    Hello onlinebusiness,

    Thank you for your post.

    I suggest adding Print() statements to understand the behavior of your strategy. You could also enable TraceOrders to get more information about orders when they are submitted. You may need to check all of the values used in your entry condition, and if it suits your needs you could even add a BarsSinceExitExecution() value to your condition:

    For more information about Print() and TraceOrders:

    Please feel free to reach out with any additional questions or concerns.
    Emily C.NinjaTrader Customer Service


      I couldn't really fix it and keeps happening, here is a screenshot where it happened, and the code for that condition:

      else if (BarsInProgress == 1 &&  
      (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) < MaxLoss))
      ExitLong(100, "MaxLoss L", "Long");
      EnterShort(Contracts * 2, "Short");
      TradeTaken = true;
      Contracts == 5, so Contracts * 2 == 10

      What's happening in the screenshot is this:

      (1) Close Position 5 and Close Position 5: The previous Short trade (10 Contracts) was closed
      (2) Long 3 and Long 2: A new Long trade (5 contracts) started
      (3) MaxLoss L 4 and MaxLoss L 1: The MaxLoss is hit and closed the Long trade (5 contracts)
      (4) Short 4 and Short 6: based on my code, once MaxLoss is hit, a new opposite trade strates immediately, So a Short trade (10 contract) strated.

      (5) But then comes Close Position 3 and Close Position 2, this doesn't close anything but instead it starts new 5 contract Short trade. I did not intent for that trade to happen.

      Here my main Entry condition:

      if (BarsInProgress == 2 && TradeTaken == false
      && CrossAbove(LinReg1, VWMA1, 1))
      EnterShort(Contracts, "Short");
      My normal entry requires that the lines shown in the screenshot to cross each other to start a new trade or close old trade and start a new one.
      But that does not happen in this case, the lines did not cross at that point when my MaxLoss was hit, so this entry condition shouldn't have triggered. only the entry condition after MaxLoss Exit should trigger.
      Which leaves me confused to where that last trade came from???

      What do you think? any idea how to fix this please?


        Hello onlinebusiness,

        Check the possibility that your strategy is "out of sync" with your position.

        I have some questions though.
        I wonder why you initially were Short 10 contracts (as you say), since your Entry condition is for "Contracts"=5 contracts ?
        Plus, I can see: ...ExitLong(100, "MaxLoss L", "Long"); ... in your MaxLoss Condition. What is this quantity 100 ?
        Since "Contracts"=5, that is Contracts * 20 ". Is that correct ?
        What are your rules for normal closing ? With "normal" I mean before your MaxLoss is hit.
        Last edited by KonstantinosNT; 02-04-2023, 04:50 AM.


          Hi KonstantionsNT
          what do you mean strategy "Out of sync"? How can I check it and fix it if it was?

          The Short 10 contracts was a result of an opposite trade, because before that a normal Long 5 contracts trade hit a MaxLoss, so an opposite trade of Short 10 contracts started.

          About this ExitLong(100, "MaxLoss L", "Long"). I use 100 so the Exit close all contracts in open trades, because sometimes I have 5 Contracts in a trade and sometimes I have 10 contracts. so the algorithm try to close 100 contracts, that will close either 5 or 10 or any number of contracts open. This is the only way I found to close all contracts in open trades without specifying exactly how many contracts, I couldn't find a better way. if you know any better way please advise

          The normal closing, meaning the take profits, I use a series of Exit limit like this:

          ExitLongLimit((Position.AveragePrice - (600 * TickSize)), "Trail uPnL300", "Long");
          I use around 10 of these with $50 or $100 gap between them, so whenever the trade reach that uPnL it marks it true, and if the uPnL CrossBelow that it Exit the the trade.


            Hello onlinebusiness,

            You can read the NT8 Help about Syncing Account Positions​:

            I don't have the full code of your strategy so I can't tell for sure, but I think there are simpler solutions to accomplish what you want to do.

            Having said that, I have noticed for example that you are using different "BarsInProgress": 0, 1, 2, maybe more.
            Is this happening on purpose or by mistake ? I don't know your full script, so I can't tell for sure if that is correct.


              Yes I am doing that on purpose, I need different sections of my code to excute more often than others


                Hi again, you wrote in #5,

                The normal closing, meaning the take profits, I use a series of Exit limit like this:
                Code: ExitLongLimit((Position.AveragePrice - (600 * TickSize)), "Trail uPnL300", "Long");

                I don't see how you can take a profit from a Long position when exiting 600 Ticks below your Entry.


                  Hello onlinebusiness,

                  Thank you for your patience.

                  What information are you able to gather from your print statements? As KonstantinosNT mentioned, perhaps your profits are being taken at an unexpected value. I also noticed from your initial post that the ExitShortLimit seems to be triggered when the Unrealized PnL is less than or equal to 350, though it is possible you may actually be intended for that to occur when it is greater than or equal to 350:

                  if (BarsInProgress == 0 &&

                  (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) <= 350)

                  ExitShortLimit((Position.AveragePrice + (600 * TickSize)), "Profit300", "Short");

                  When adding your print statements to debug, it can be important to include any comparison operators and ensure that values are less than/equal to/greater than the values shown in the prints to understand when your conditions are being evaluated to true and why. As for the strategy vs. the account position, the following page has a detailed description and overview:

                  The link that KonstantinosNT previously shared describes the available start behavior settings which can tell your strategy whether the account needs to be flat or in sync before it starts submitting trades to the account the strategy is activated on:

                  Determining which start behavior to use will depend on your needs and requirements for the strategy. You could test out the different options on a simulated account to get more of an idea on how they behave.

                  Please let me know if I may be of further assistance.​
                  Emily C.NinjaTrader Customer Service


                  Latest Posts


                  Topics Statistics Last Post
                  Started by Orion815, Today, 07:36 AM
                  3 responses
                  Last Post NinjaTrader_Jesse  
                  Started by -=Edge=-, 01-31-2018, 09:10 AM
                  17 responses
                  Last Post tradeesh  
                  Started by WHICKED, 06-27-2023, 12:07 PM
                  89 responses
                  1 like
                  Last Post NinjaTrader_ChelseaB  
                  Started by Rxxar, Yesterday, 09:56 AM
                  3 responses
                  Last Post NinjaTrader_Clayton  
                  Started by ender_wiggum, 06-18-2024, 07:56 AM
                  9 responses
                  Last Post NinjaTrader_Zachary