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

<account>.PositionUpdate is reporting the wrong position state

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

    <account>.PositionUpdate is reporting the wrong position state

    Hi, I'm using the following code...

    Code:
    // When accountCurrent equals an Account, the .PositionUpdate event is subscribed to so that when the account is flat the RealizedProfitLoss is checked.
    accountCurrent.PositionUpdate += OnPositionUpdate;
    
    private void OnPositionUpdate(object sender, PositionEventArgs e)
    {[INDENT]Print(string.Format("OnPositionUpdate() Name: {0} \tMarketPosition: {1} ", e.Position.Account.DisplayName, e.MarketPosition.ToString().ToUpper()));
    if (e.MarketPosition == MarketPosition.Flat)
    {[/INDENT][INDENT=2]CheckAccountRealizedProfitLoss(accountCurrent);[/INDENT][INDENT]}[/INDENT]
     
     Print("OnPositionUpdate() - EXIT");
    }
    This is a portion of the output window for debugging...

    FastTraderButtons: #### MouseClick started >> Buy button clicked
    FastTraderButtons - Price clicked: 11115.25
    - orderFT = orderId='ae9369d28a15412790b5b829e47b0e8e' account='ac83410' name='FT: Buy Limit' orderState=Initialized instrument='MNQ 12-20' orderAction=Buy orderType='Limit' limitPrice=11115.25 stopPrice=0 quantity=1 tif=Day oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=13743 time='2020-09-25 13:05:47' gtd='2099-12-01' statementDate='2020-09-25'
    ResetButtonsAndCursor() - Started
    - ResetButtons: All is Good to trade. Resetting buttons.
    - ResetButtons: SetTextBlockMITState() started
    ResetButtonsAndCursor(): - EXIT
    OnPositionUpdate() Name: xxxxx0!AMP!xxxxxxx0 MarketPosition: LONG
    OnPositionUpdate() - EXIT
    FastTraderButtons: #### MouseClick started >> Sell button clicked
    FastTraderButtons - Price clicked: 11122.75 <<<<<<< This sell order puts the account flat
    - orderFT = orderId='4fde9ba2cc8c4378af2dabee16ebc522' account='ac83410' name='FT: Sell StopLimit' orderState=Initialized instrument='MNQ 12-20' orderAction=Sell orderType='Stop Limit' limitPrice=11122.75 stopPrice=11122.75 quantity=1 tif=Day oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=13744 time='2020-09-25 13:06:29' gtd='2099-12-01' statementDate='2020-09-25'
    ResetButtonsAndCursor() - Started
    - ResetButtons: All is Good to trade. Resetting buttons.
    - ResetButtons: SetTextBlockMITState() started
    ResetButtonsAndCursor(): - EXIT
    OnPositionUpdate() Name: xxxxx0!AMP!xxxxxxx0 MarketPosition: SHORT <<<<<<< The account is flat, Not Short.
    OnPositionUpdate() - EXIT


    OnPositionUpdate() event handler was working fine previously and firing MarketPosition.Flat when the account went flat, but today I noticed it stopped firing when flat. It is only firing for MarketPosition.Long or MarketPosition.Short. When a long position was opened a MarketPosition.Long had fired. The sell order, to put the account flat and verified in the Control Center Positions tab, was firing a MarketPosition.Short when it should be MarketPosition.Flat. I tested this several times today, and never saw a MarketPosition.Flat.
    Is there some detail I'm missing or not understanding about <account>.PositionUpdate?

    thanks.
    Last edited by zacharydw00; 09-25-2020, 02:13 PM.

    #2
    Hello zacharydw00, thanks for your post.

    A simple example I made works on my end. If you run the attached example does the account report flat? If it does print, there is a bug in your script that would need to be worked out. Are you removing the event method in State.Terminated?: e.g.

    protected override void OnStateChange()
    {
    else if (State == State.Terminated)
    {
    // Unsubscribe to events
    myAccount.PositionUpdate -= OnPositionUpdate;
    }
    }

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

    Comment


      #3
      Hi Chris,
      I am unsubscribing to all event handlers in State.Terminate. That's not the issue. After my first post I did some more digging. The issue is Ninja was reporting the position to be short when the position was actually flat. I called the broker's order desk and we confirmed a couple of orders that put my account flat, but Ninja was stating the position was short. Here is an example, of my last trade of the day, from the Log file.

      2020-09-25 13:06:30:409|1|32|Order='1562672806/xxx0' Name='FT: Sell StopLimit' New state='Filled' Instrument='MNQ 12-20' Action='Sell' Limit price=11122.75 Stop price=11122.75 Quantity=1 Type='Stop Limit' Time in force=DAY Oco='' Filled=1 Fill price=11122.75 Error='No error' Native error=''
      2020-09-25 13:06:30:410|1|8|Execution='348538069005' Instrument='MNQ 12-20' Account='xxx0' Exchange=Globex Price=11122.75 Quantity=1 Market position=Short Operation=Operation_Add Order='1562672806' Time='09/25/2020 13:06:30'

      That was my last order placed for the day, and the broker confirmed that sell order put my account flat. But as the log file shows a position=Short was returned.
      Maybe we should start with this question first, who or what is determining the Market position=??? state?
      Last edited by zacharydw00; 09-25-2020, 02:54 PM.

      Comment


        #4
        Hello zacharydw00, thanks for your reply.

        An AddOn Account object is a representation of the real world account, so it should reflect exactly what you see in the Accounts and Positions tab of the Control Center.

        What broker are you using? I could try to see if the same thing happens on my side with a paper account.

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

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          An AddOn Account object is a representation of the real world account, so it should reflect exactly what you see in the Accounts and Positions tab of the Control Center.
          Hi,
          It is looking like that is where the issue is. As mentioned above, the Positions tab showed flat, but the <account>.PositionUpdate event and the log file showed Short. The Accounts tab agreed with the Positions tab showing no Unrealized P&L and zero position.

          I am using AMP broker. After I write my first post, I called their tech support to let them know that the log statements were showing an incorrect market position, and of course I was told to talk you Ninja support first.

          So how do we fix this disconnect between the Positions & Accounts tabs stating flat, and the Log tab & NinjaScript stating short?
          Thanks.
          Last edited by zacharydw00; 09-26-2020, 12:41 PM.

          Comment


            #6
            Hello zacharydw00, thanks for your reply.

            What kind of output do you get with the test script I posted? Can you test this script with the account in a flat state? Also, check the myAccount.Positions[0]; within OnBarUpdate or OnPositionUpdate to check what this object reports. It has been tested and confirmed to be functional in the latest release.

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

            Comment


              #7
              Hi Chris,
              Here is the output generated...
              FastTraderButtons: #### MouseClick started >> Buy button clicked
              FastTraderButtons - Price clicked: 11410.25
              // A Buy order was submitted.
              - orderFT = orderId='64262809b6704fdbba74c9dff6bb9f67' account='acXXXX0' name='FT: Buy Limit' orderState=Initialized instrument='MNQ 12-20' orderAction=Buy orderType='Limit' limitPrice=11410.25 stopPrice=0 quantity=1 tif=Day oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=13785 time='2020-09-29 00:16:56' gtd='2099-12-01' statementDate='2020-09-29'
              ResetButtonsAndCursor() - Started
              - ResetButtons: All is Good to trade. Resetting buttons.
              - ResetButtons: SetTextBlockMITState() started
              ResetButtonsAndCursor(): - EXIT
              // These lines are from your TestAccoun.cs indicator code above. Buy order filled.
              ##### TestAccount - OnPositionUpdate() Name: XXXX0!AMP!acXXXX0 MarketPosition: LONG

              ##### TestAccount - OnPositionUpdate() Name: XXXX0!AMP!acXXXX0 MarketPosition: LONG

              OnPositionUpdate() Name: XXXX0!AMP!acXXXX0 MarketPosition: LONG
              OnPositionUpdate() accountCurrent.Positions[0]: INSTRUMENT='MNQ 12-20' ACCOUNT='ACXXXX0' AVGPRICE=11411 QUANTITY=1 MARKETPOSITION=LONG STATEMENTDATE='2099-12-01' .Positions[0].MarketPosition: LONG
              OnPositionUpdate() - EXIT
              FastTraderButtons: #### MouseClick started >> Sell button clicked
              FastTraderButtons - Price clicked: 11412.25
              // A Sell order was submitted to put the account flat.
              - orderFT = orderId='8edf517bfb41454c99c92f84f55cc110' account='acXXXX0' name='FT: Sell Limit' orderState=Initialized instrument='MNQ 12-20' orderAction=Sell orderType='Limit' limitPrice=11412.25 stopPrice=0 quantity=1 tif=Day oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=13786 time='2020-09-29 00:17:05' gtd='2099-12-01' statementDate='2020-09-29'
              ResetButtonsAndCursor() - Started
              - ResetButtons: All is Good to trade. Resetting buttons.
              - ResetButtons: SetTextBlockMITState() started
              ResetButtonsAndCursor(): - EXIT
              // Sell order filled. Account is now flat as shown in the Chart Trader and CC > Positions tab & Accouns tab.
              ##### TestAccount - OnPositionUpdate() Name: XXXX0!AMP!acXXXX0 MarketPosition: SHORT

              ##### TestAccount - OnPositionUpdate() Name: XXXX0!AMP!acXXXX0 MarketPosition: SHORT

              OnPositionUpdate() Name: XXXX0!AMP!acXXXX0 MarketPosition: SHORT
              // The remainder did not print because an error was thrown when MarketPosition was flat, because accountCurrent.Positions[0] was empty.



              Below is the code that prints these lines...
              OnPositionUpdate() Name: XXXX0!AMP!acXXXX0 MarketPosition: LONG
              OnPositionUpdate() accountCurrent.Positions[0]: INSTRUMENT='MNQ 12-20' ACCOUNT='ACXXXX0'

              Code:
               private void OnPositionUpdate(object sender, PositionEventArgs e)
              {[INDENT]Print(string.Format("OnPositionUpdate() Name: {0} \tMarketPosition: {1} ", e.Position.Account.DisplayName, e.MarketPosition.ToString().ToUpper()));
              Print(string.Format("OnPositionUpdate() accountCurrent.Positions[0]: {0} \t.Positions[0].MarketPosition: {1} ", accountCurrent.Positions[0].ToString().ToUpper(), accountCurrent.Positions[0].MarketPosition.ToString().ToUpper()));
              
              if (e.MarketPosition == MarketPosition.Flat)
              {[/INDENT][INDENT=2]CheckAccountRealizedProfitLoss(accountCurrent);[/INDENT][INDENT]}
              Print("OnPositionUpdate() - EXIT");[/INDENT]
               
                }
              Last edited by zacharydw00; 09-29-2020, 01:05 AM.

              Comment


                #8
                Hello zacharydw00, thanks for your reply.

                I do see the same issue on a test account. I'll report this to my supervisor and update you on the progress.

                Thanks in advance for your patience.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Chris.

                  Do you have a suggested work around, for now?

                  I'm thinking something like
                  if(accountCurrent.Positions.Count == 0)
                  to check for flat.

                  Comment


                    #10
                    That won't always work if you are doing it in OnPositionUpdate--it will depend on the timing of when Positions gets updated by NT--I tested this is real-time today (I literally went through this issue today and thought it was a Rithmic issue). My temporary solution has been this (not a good permanent solution, but it has everything working for me):

                    Code:
                    private int iDelayMisc = 200; // This seems to be a long enough delay for Account.Positions and e.Position.Account.Positions to update back to 0
                    private void OnPositionUpdate(object sender, PositionEventArgs e)
                    {
                        Task.Delay(iDelayMisc).ContinueWith(t=> CheckPosition(e.Position));
                    }
                    
                    private void CheckPosition(Position p)
                    {
                        bool xFlat = true;
                        if(p.Account.Positions.Count > 0)
                        {
                            for(int i = 0; i <= p.Account.Positions.Count-1; i++)
                            {
                                if(p.Account.Positions[i].Instrument == Instrument)
                                {
                                    xFlat = false;
                                    break;
                                }
                            }
                        }
                    
                        if(xFlat)
                        {
                    
                        }
                    }

                    Comment


                      #11
                      Hi zacharydw00, thanks for your reply.

                      Unfortunately, I do not have any supported workaround. Our team is looking into the issue now and I will update you on the situation as new information comes in.

                      Kind regards.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChrisL View Post
                        I do see the same issue on a test account. I'll report this to my supervisor and update you on the progress.
                        Has this been fixed?

                        Comment


                          #13
                          Hello zacharydw00, thanks for the follow up.

                          This was fixed back in October. Please install from here the latest version of NinjaTrader 8:

                          NinjaTrader is a futures trading platform that delivers integrated multi-device trading. Discover our best platform to trade futures for active futures traders.


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

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Skifree, Today, 03:41 AM
                          3 responses
                          12 views
                          0 likes
                          Last Post Skifree
                          by Skifree
                           
                          Started by traderqz, Yesterday, 09:06 AM
                          5 responses
                          32 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by guillembm, Today, 11:25 AM
                          1 response
                          6 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by owensd, 04-21-2024, 11:34 PM
                          9 responses
                          34 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by trilliantrader, 04-10-2024, 09:33 PM
                          7 responses
                          25 views
                          0 likes
                          Last Post NinjaTrader_BrandonH  
                          Working...
                          X