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

Position Management

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

    Position Management

    Hi,

    I have encountered an unexpected behavior with position management while using real-time data.

    A crossover strategy is supposed to go long or short with quantity 1. I have named the entry and exit signals consistently.

    I have set entries per direction = 1 and entry handling: "unique entries"

    I ended up with a Position of 2 shorts!!

    It seems that NT tried to close a previous long position (1 quantity) itself (outside the strategy), while the strategy shorty before was Exiting the long position itself (almost same time). I named all entries and exits and linked them correctly. I have no "Close Position" order name in my conditions.

    How can I avoid such behavior? It never happened with historical data. I used subscribed Kinetick data feed (15mins delayed).

    Kind regards,
    Stefan



    Attached Files
    Last edited by azuul; 03-02-2021, 08:25 AM.

    #2
    Hello azuul, thanks for your post.

    The "Close Position" order usually happens when you are, for instance, in a long position then EnterShort() is called. This method will automatically send a sell order to exit the long position, then enter a short position. So two orders happen from the one method call. How is your strategy opening and closing positions? I would need details on the OnBarUpdate method and how you are doing entries and exits. How is it different from the SampleMACrossover strategy in terms of entry and exit?

    I look forward to hearing from you.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Dear Chris,

      many thanks.
      I have basically set of 4 condition & actions, as the strategy not always intends to have a position. Using only the Strategy building blocks for now. (no unlocked code).

      1. if a =true, Enter Long (named signal)
      2. if b=true then Exit Long (named signal)

      3. if c= true then Enter Short (named signal)
      4. if d= true then Exit Short (named signal)


      As I allowed only 1 entry per direction, I suspect an overlap between Exiting a long and Entering a short, leading to the effect that NT tried to close a long position itself before entering a short by the strategy, although the strategy has met set 2 (exit Long) before itself.

      Strategy would have handled all correctly, but NT trying to close (outside the strategy) led to one false additional short.
      How to avoid such behavior? Can this be controlled / avoided with additional position management logic / code?

      Kind regards
      Last edited by azuul; 03-02-2021, 09:33 AM.

      Comment


        #4
        Hello azuul, thanks for your reply.

        You should make sure the strategy only submits entry orders when the position is flat. Also, make sure the strategy only submits ExitLong when the position is Long and ExitShort when the Position is Short. Add to your EnterLong and EnterShort condition:


        And for the ExitLong/ExitShort conditions. Make sure CurrentMarketPosition == Long or Short before calling the method.

        Please let me know if this does not resolve your inquiry.


        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Dear Chris,

          many thanks. I have added the following logic. Unfortunately I was able to reproduce.

          logic added in bold:

          1. if a =true && Position.MarketPosition != MarketPosition.Long then
          Enter Long (named signal)

          2. if b=true && Position.MarketPosition == MarketPosition.Long then
          Exit Long (named signal)

          3. if c= true && Position.MarketPosition != MarketPosition.Short then
          Enter Short (named signal)

          4. if d= true && Position.MarketPosition == MarketPosition.Short then
          Exit Short (named signal)

          I believe to understand what is happening:

          When condition 1 and 3 are met at the exact same time, the Strategy intends to switch from a long directly into a short position.
          Condition 2 is intended to close the long position via the strategy before entering a short via condition 3.

          However, almost same time or milliseconds after condition 2 (exit long) is met, NT initiates another close of long position.
          Impression: NT jumps the script order and despite having initiated a close of the Long condition based on condition 2 already, decides to close it a second time ( same time) before entering into a short via Condition 3. But this implicit (same time) closure does not have a name and is not wanted. -> I end up with 2 shorts instead of one
          One short entry is not named and hence will not be handled correctly afterwards, creating unwanted expsosure / risks.

          How can I avoid this behavior? (NT doing something outside the named entry / exit signals to manage positions).

          Kind regards,
          Stefan



          Comment


            #6
            Hello Stefan, thanks for your reply.

            When you call EnterShort and you are in a Long position NinjaTrader will close out of the long position for you and enter a short, so the reversal is automatic. If the ExitLong method is used strictly to exit the long position to get flat to enter short, you should just omit the "ExitLong" method and only use EnterShort to handle the reversal. See the SampleMACrossover strategy as an example. The Order anomaly you are seeing is still from your script, it's just the way the logic flow is happening that is messing up the position. Also, add Prints before each method call to see exactly how the code is being processed:

            Print("EnterLong");
            EnterLong();

            Print("ExitLong");
            ExitLong();

            // and so on.

            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Hi Chris,

              thanks, this more or less confirmed what I suspected / feared. However I do not always want to go short, sometimes a flat is intended, so the Exit long is required.

              In Historic simulation, these double entries never occurred to my knowledge. So the behavior was unexpected. If NT would strictly go in order of the script, and considered that fact, that a position is being exited already within the bar update, the additional /unwanted closure was avoided. (Which seems to work this way in historic simulation).

              Is there another solution other than splitting the strategy in 2 (1 for long / flat and one for short / flat?)

              Kind regards,
              Stefan




              Comment


                #8
                Hello Stefan, thanks for your reply.

                I wanted to give you information about the OnOrderUpdate and OnExecutionUpdate method where you can get an event method on every order and execution. We have a full reference sample linked here:
                https://ninjatrader.com/support/help...and_onexec.htm

                Also see the OnPositionUpdate method:
                https://ninjatrader.com/support/help...tionupdate.htm

                With these events, you can know the exact moment your strategy changes a state. So you can have more control over what time an order is placed.

                Another solution would be a simple boolean flip switch to make sure conditions are not hit in the same OnBarUpate event. If condition 1 becomes true set the bool to true, then in condition 3 make sure bool is not true, then set bool back to false at the end of OnBarUpdate. Its really going to depend on the flow of your OnBarUpdate method specifically, there is no "one size fits all" solution for it, so please test out an idea like this one or modify it to suit your needs.

                Please let me know if I can assist any further.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chris,

                  thank you for above, it is helpful. Despite this, I realized that this may not resolve the issue I am having. It may have a different root cause, which I do not yet understand fully.

                  What I observed today:
                  Simulation Account position was 0. I enabled a strategy which started out being flat (no virtual position to my knowledge).

                  Suddenly after the first trigger event to go short (1 unique entry allowed), I had 2 shorts - one based on "Enter Short" by strategy, one based (again unwanted) by a "close position" event.

                  General Strategy parameter is "wait until flat" before submitting order - has it maybe to do with that?

                  I am Posting Log in attachment.

                  What may I be missing here?

                  Kind regards,
                  Stefan

                  Attached Files

                  Comment


                    #10
                    Hello Stefan,

                    Thanks for your reply.

                    "close position" only occurs when an Entry method has been initiated AND there is a current position in the opposite direction. The entry method will see the opposite direction position and will first issue a market order to close the position called Close Position and then will place a 2nd order to leave the strategy in the intended direction.

                    If you are also calling an exit method at the same time as the entry in the opposite direction, you can end up with extra orders because again the entry method will see the opposite direction and try to close it at the same time that your strategy is trying to exit the position.

                    Wait until flat is a start-up behavior, here is what happens when you apply your strategy: The strategy will perform historical trades on all of the historical data on the chart. If the last historical trade has not closed when the next bar is a real-time bar, the strategy will wait until your strategy closes that last historical trade before it will place a live trade. So that wait until flat is not the cause of the issue, rather it is the strategy trying to enter/exit at the same time.

                    I would suggest reviewing your strategy and see where you have an exit occurring at the same point as an entry and make adjustments to your logic so that this cannot occur.
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Rapine Heihei, Today, 08:19 PM
                    1 response
                    3 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by Rapine Heihei, Today, 08:25 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post Rapine Heihei  
                    Started by f.saeidi, Today, 08:01 PM
                    1 response
                    4 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by Rapine Heihei, Today, 07:51 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post Rapine Heihei  
                    Started by frslvr, 04-11-2024, 07:26 AM
                    5 responses
                    96 views
                    1 like
                    Last Post caryc123  
                    Working...
                    X