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

Some problem with Position.Quantity

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

    Some problem with Position.Quantity

    Hi, NT guys!

    I'd have two questions:

    1. Since I don't have live account at IB and MBT, but I'd like to know what properties can it be accessed of the IB and MBT accounts using AccountItem.
    with the AccountItem can I reach this properties: :
    AccountItem.BuyingPower -default
    AccountItem.CashValue -default
    AccountItem.RealizedProfitLoss -default
    AccountItem.ExcessEquity
    AccountItem.InitialMargin
    AccountItem.InitialMarginOvernight
    AccountItem.MaintenanceMargin
    AccountItem.MaintenanceMarginOvernight
    AccountItem.NetLiquidation
    AccountItem.NetLiquidationByCurrency
    AccountItem.TotalCashBalance
    which of them can I access besides default at IB, and MBT? when updates it?

    2. I'm in a big trouble. I tested the Position.Quantity(and AvgPrice, GetProfitLoss) properties. I had the value of them printed on chart and recognised the values don't correspond with the values of the Control Center ->Positions Tab->Quantity. as this can be seen on the picture. I'm using NinjaTrader 7.0.1000.2 version.
    the test strategy runs tick by tick and the Order Handling was set Entries Per Direcion 10 with 11 quantites per entrie.
    I don't why, in certain cases after the last entrie doesen't want to update until the next tick. at several times as this situation didn't accour the update. how can I get with this properties the right values? so I can't trust in Position.Quantity.
    Anyway, I don't care it, if I can access directly the right values of Control Center ->Positions Tab->Quantity but I haven't found it yet. please write to me precisely how can I access them with the required methods/properties.

    thanks for it!
    Attached Files

    #2
    Hello,

    Thanks for your note.

    Please see this guide for what information is provided from what broker.

    Broker Provided Information:



    Also, on your secondary question.

    This is a key strategy concept that you must understand when using NinjaTrader.

    You will not be able to get the account QTY values from the positions tab.

    Reason is if you had multiple strategies running on the same instrument the strategies would not function do to the conflicting position information.

    Each strategy has whats called its own Position information and each strategy is independant.

    There is a case where your strategy position is not in sync with the account position when you would want it to be:

    Strategy Position vs. Account Position:

    1st solution:
    You can include the following statement in your code to avoid the strategy to be calculated on historical data.
    // Only run on real-time data
    if (Historical)
    return;
    Add the statement to the top of OnBarUpdate()
    2nd solution:
    You can set 'On starting a real-time strategy' to 'Wait until flat before executing live'.
    This option can be found at Tools-->Options-->Strategies-->NinjaScript-tab.
    3rd solution, would be submitting manual orders to sync the strategy and account positions described in the link
    4th solution, would be to enable Sync account position to true.


    Let me know if I can be of further assistance.

    Comment


      #3
      Hello Brett,

      thanks for your detailed answer. I'll study it.

      Comment


        #4
        other question

        hello Brett

        I'd have other question that I don't understand yet. I followed your instructions and read those materials.
        but it want to succeed it. I explane what I made.
        I have the strategy run only real-time data using Wait until flat before executing live, Sync account position = true settings.
        Oder settings are:
        Order handling:
        - Entries per direction: 10
        - Calculate on bar close = true
        - if (entryOrder == null && Close[0] >= Open[0] || Close[0] <= Open[0])
        entryOrder = EnterLong(11,"Test");
        it was runing on real-time, at first the account was synchronize to flat state.(picture 1) during running I don't open any order manually.
        and there are some unespected behavior, that I don't understand.
        the entryOrder gives me always the correct quantity of the current executions.
        but the Position.Quantity doesen't want to update after immediately the execution but it will update at the next tick or at the next bar open.(picture 2) this update at the most cases doesen't want to update at the next tick (if Calculate on bar close = false) or at the next bar open (if Calculate on bar close = true) this can be seen on picture3 where after 30 min doesen't want to accored the update.
        the problem is, if I can't able to determine preciously the quatnity and avg price of current orders, I can't send the sell orders, and I can't manage the scale out and in.
        how can I reach the continuous updates? where should I it set? or what should I do in the interest of it? what made I wrong?
        I considered to use the $EURUSD Default_Sim101_position.txt that are written to the folder outgoing. but it won't be the right way.
        even if the traceorder shows me the correct amount at open of an order.
        please let me solve this problem.
        I hope, I wrote it understandably.
        I attached the testfile.
        thanks for answer
        Tom
        Attached Files
        Last edited by marotom; 03-03-2011, 03:10 PM.

        Comment


          #5
          Hello,

          This is a limitation of the Position object.

          It is always updating on the next OnBarUpdate and does not update intra bar with COBC = true.

          If you want intrabar updating you would have to move into an unmanaged approach and track the positions on your own when you recieve confirmation of a fill. You would need use OnExecution() for this. This gettings into some more advanced programming however. So mostly what happens is you have to be aware of the position limitation and know that it wont update until the next OnBarUpdate() and code around it.


          Comment


            #6
            Position Updating

            I just want to clarify a previous post about the Position.Quantity field. This value is synced to the actual position at the time of the OnBarUpdate. Also suppose I receive multiple fills on an entry order between OnBarUpdate calls. Let's say I'm bidding for a 10-lot from a flat position. Can you confirm that the following expressions in the series of callbacks below are true?

            OnBarUpdate()
            {
            Position.Quantitiy == 0
            EnterLong(10);
            }

            OnExecutionUpdate(IExecution exec)
            {
            exec.Quantity == 5;

            Position.Quantity == 0 //is this true?
            }

            OnPositionUpdate(IPosition pos)
            {
            pos.Quantity == 5 // is this true?
            }

            OnExecutionUpdate(IExecution exec)
            {
            exec.Quantity == 5;

            Position.Quantity == 5 // is this true?
            }

            OnPositionUpdate(IPosition pos)
            {
            pos.Quantity == 10 // is this true?
            }

            OnBarUpdate()
            {
            Position.Quantity == 10 // is this true?
            }


            Regards,

            TFH

            Comment


              #7
              TinFoilHat, I will have someone get back to you on Monday.
              AustinNinjaTrader Customer Service

              Comment


                #8
                Hello,

                You won’t be able to get an exact idea as what updates when. As all these methods can run Asynchronously.

                The position.Quantity used in OnBarUpdate is the only one that requires an OnBarUpdate() to pass before its updates. All other methods are called when a position change occurs therefore the local position variables would all be accurate in the methods you mentioned. Would recommend you always use the local quantity variables in these methods.

                We do not have any documentation of what updates when since methods can run Asynchronously.

                Let me know if I can be of further assistance.

                Comment


                  #9
                  Position Updating Follow up

                  I'm afraid I don't understand your reply. You said:
                  All other methods are called when a position change occurs therefore the local position variables would all be accurate in the methods you mentioned.

                  By "local position variables" do you mean the Position.Quantity value? Later you refer to "local quantity variables", is this the same thing? So if I understand you, and I'm not sure that I do, then going back to my original example I think you're saying it would produce the following output given two 5-lot partial fills for the original 10-lot order. (Note: I meant to say OnExecution rather than OnExecutionUpdate in my original example):

                  void override OnBarUpdate()
                  {
                  Print("OnBarUpdate: Pos.Qty = " + Position.Quantity);
                  EnterLong(10);
                  }


                  void override OnExecution(IExecution exec)
                  {
                  Print("OnExecution: FillQty + " + execution.Quantity + " Pos.Qty = " +
                  Position.Quantity);
                  }

                  void override OnPositionUpdate(IPosition pos)
                  {
                  Print("OnPositionUpdate: Pos.Qty = " + pos.Quantity);
                  }

                  OUTPUT WINDOW:

                  OnBarUpdate: Pos.Qty = 0
                  OnExecution: FillQty = 5 Pos.Qty = 5
                  OnPositionUpdate: Pos.Qty = 5
                  OnExecution: FillQty = 5 Pos.Qty = 10
                  OnPositionUpdate: Pos.Qty = 10

                  Please confirm the output above.

                  Regards,

                  TFH
                  Last edited by tinfoilhat; 05-10-2011, 11:19 AM.

                  Comment


                    #10
                    Hello,

                    The local variables would be in the method input object. Position is something that's relative to OnBarUpdate().

                    For example:

                    OnExecution()

                    would be the execution.Quantity and not Position.Quantity; Position.Quantity is really only something to be used inside of OnBarUpdate(). I do not see a reason why you would want to use Position.Quantity outside of OnBarUpdate() as its only updated after OnBarUpdate() runs.

                    Comment


                      #11
                      Updating of Position.Quantity

                      The reason I need to know my current position in OnExecution is that I must maintain the average entry price rather than use Position.AvgPrice because I hold positions overnight. If I hold a position overnight then my Pats connection resets the Position.AvgPrice to the previous day's settlement price. I need to know my acquisition price because the model has a stop loss rule that's a delta amount away from the avg entry price.

                      A minor, but important improvement to NinjaScript would be to distinguish the Position.AvgPrice from a new field called previous day's settlement price.

                      So getting back to my original question, are you telling me that Position.Quantity is not reliable to use inside of OnExecution? From the previous emails I think this is what your hinting at, but never explicitly answered yet.

                      If so my solution is to create a member variable intraBarPostion which is set to Position.Quantity at the end of OnBarUpdate and then update this variable with the execution.quantity that I can access in OnExecution.

                      Regards.

                      TFH

                      Comment


                        #12
                        Hello,

                        Heres what I suggest then.That you use the OnExecution() price data and store this in a public variable so you can access it from OnBarUpdate(). I would then also possible store this in a .txt file if you want it to persists if the strategy was to ever get cancelled however this gets a little more advanced.

                        You will need to store this in a public variable you have declared. yourself then do all the adding and averaging yourself when the executions come in.

                        This would also go hand in hand with the unmanaged approach if you wanted to also switch to this. However again this is for advanced programmers only.



                        Let me know if I can be of further assistance.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Kaledus, Today, 01:29 PM
                        5 responses
                        12 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by Waxavi, Today, 02:00 AM
                        1 response
                        8 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by alifarahani, Today, 09:40 AM
                        5 responses
                        23 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by gentlebenthebear, Today, 01:30 AM
                        3 responses
                        16 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by PhillT, Today, 02:16 PM
                        2 responses
                        7 views
                        0 likes
                        Last Post PhillT
                        by PhillT
                         
                        Working...
                        X