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

Avoid Wrong Entry Trades

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

    Avoid Wrong Entry Trades

    Hi guys

    On my strategy I use code like this to exit trades

    Code:
    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.

    #2
    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

    Comment


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

      Code:
      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:

      Code:
      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?


      Comment


        #4
        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.

        Comment


          #5
          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:

          Code:
          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.

          Comment


            #6
            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.

            Comment


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

              Comment


                #8
                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.

                Comment


                  #9
                  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

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Fran888, Today, 11:14 AM
                  1 response
                  4 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by lcgamarra_pinnacle, 06-15-2024, 09:33 AM
                  8 responses
                  94 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by AH369, Today, 11:09 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Started by wanderingjade, Yesterday, 08:21 AM
                  2 responses
                  27 views
                  0 likes
                  Last Post Tkamp
                  by Tkamp
                   
                  Started by Rxxar, Yesterday, 09:56 AM
                  5 responses
                  27 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Working...
                  X