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

Need explanations about adopt account position

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

    Need explanations about adopt account position

    Hello,
    I cannot manage with idea of adopting account position. My aim is to sync my orders with real account when I enable strategy

    Let's look at the code

    Code:
        protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                  ......
                    IsAdoptAccountPositionAware = true;
                    StartBehavior = StartBehavior.AdoptAccountPosition;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {
                    AddChartIndicator(SMA(21));
                }
                else if (State == State.Realtime)
                {
                    // convert any old historical order object references
                    // to the new live order submitted to the real-time account
                    if (_BasicEntry != null)
                        _BasicEntry = GetRealtimeOrder(_BasicEntry);
                }
            }
    
    
            private Order _BasicEntry;
    
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < BarsRequiredToTrade)
                    return;
    
                if (Position.MarketPosition==MarketPosition.Flat)
                {
                    _BasicEntry = EnterLongStopMarket(1,1.3034,"Main Entry");
                }
                else if (Position.MarketPosition == MarketPosition.Long)
                {
                    Print("Already in long position");
                }
            }
    This is the simplest example of entering buy stop.
    My real account position is absolutely flat. When I enable the strategy it says "Already in long position".

    Could you explain how to sync the account position with strategy correctly?
    Attached Files

    #2
    Hello,

    Thank you for the question.

    The position this is referring to is a historical virtual strategy position, the Yellow/orange state would indicate the script is waiting for that position to close before being in sync with the actual account or flat. http://ninjatrader.com/support/helpG...%2Bhighlighted

    I would suggest reviewing the following document which describes the differences for Virtual versus Actual positions: http://ninjatrader.com/support/helpG...ual%2Bposition


    You can change the settings on how the strategy starts, or also prevent the strategy from placing historical trades which would make it start flat. It would really depend on what you ultimately want.



    To review the startup settings, please see the following guide: http://ninjatrader.com/support/helpG..._positions.htm

    To prevent the strategy from placing historical trades in general, you could compare the State and return if the state is historical.

    Using the builder, you would need to add a condition to each set that checks the Misc -> Current State != Misc State-> Historical.

    In manual programming that would be represented by:

    if(State == State.Historical) return;


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply. I read instruction again and understood that I was wrong.
      My account position must be long as I started the strategy because it had to be opened before.

      Could you check whether I think in right direction? Let's suppose it's a midde of the trade day. The position was already opened long and then NT8 is closed for some reason. While NT is off:

      · price may hits our stoploss or take profit => Account is flat. Upon re-enabling strategy, strategy position should also be flat

      · price remains in range => position is still long. At re-enabling, strategy should match the “live” account and see that position is still long

      Same to stop and limit orders: if strategy opened some stop/limit orders and then was disabled, after enabling it should match its orders with real account orders. If they were filled, then strategy must see filled position.

      And here is another question. I use reference to orders in my strategy, something like this:
      _BasicEntry = EnterLongStopMarket(1,1.3034,"Main Entry");
      ...
      if (_BasicEntry!=null)
      {
      //Do some action with it
      }

      So, I need references to all my stop/limit orders, stop loss/take profit orders and so on
      I try to get these references at strategy start:

      // convert any old historical order object references
      // to the new live order submitted to the real-time account
      if (_BasicEntry != null)
      _BasicEntry = GetRealtimeOrder(_BasicEntry);

      but I have null. So, I can't get the reference to it and I don't know why and what I should do

      Comment


        #4
        Hello rfsettling,

        I am responding to you on behalf of my colleague Jesse.

        · price may hits our stoploss or take profit => Account is flat. Upon re-enabling strategy, strategy position should also be flat

        · price remains in range => position is still long. At re-enabling, strategy should match the “live” account and see that position is still long
        It's important to note that the strategy's virtual position does not know it is flat when it restarts. It determines this by the start behavior after the strategy is enabled. It can either cancel pre-existing orders and wait until it is flat, or it can try to match pre-existing orders and immediately submit. Additionally, a reconciliary order could be placed to sync the position to the account if "Sync Account Position" is enabled.

        Adopt Account Position behaves similarly to Immediately submit with Synchronize Account enabled, except that it will not place any reconciliary orders and will expect you to manually close out any positions that did not come from the strategy.

        There are also behaviors for how NinjaTrader cancels orders when a strategy is disabled that should be noted as well. These behaviors could cancel orders when NinjaTrader is closed appropriately.


        if (_BasicEntry != null)
        _BasicEntry = GetRealtimeOrder(_BasicEntry);
        Here you are checking if the entry order has transitioned to a realtime order. This wouldn't tell you if the profit target and stop loss are placed and active. From the help guide: "If no associated order exists (i.e. OrderState is Filled, Canceled, Rejected, Unknown), a null value returns"


        You could use Order objects for your Profit Target and Stop Loss by placing your own protective orders and using GetRealtimeOrder() to check if these orders have transitioned. This is demonstrated in the SampleOnOrderUpdate sample strategy here: http://ninjatrader.com/support/forum...ead.php?t=7499

        Also, you may wish to check if the Order is null when it is transitioned to realtime to assist in debugging. Here is a thread that demonstrates how this can be checked: http://ninjatrader.com/support/forum...ad.php?t=91310

        More in-depth reading and sample code to monitor transitions from historical to realtime can be found in the Advanced Order Management section of the help guide here: https://ninjatrader.com/support/help...r_handling.htm

        Please let me know if I may be of further assistance.
        Last edited by NinjaTrader_Jim; 05-25-2017, 11:44 AM.
        JimNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jim View Post
          if "Sync Account Position" is enabled.
          I can't find this setting. Where is it?

          Comment


            #6
            Hello rfsettling,

            Sorry for any confusion. I was referring to Synchronize Account as a part of start behaviors for Strategy properties. I've corrected my post to reflect this.

            (Start behaviors are documented here: http://ninjatrader.com/support/helpG..._positions.htm)
            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              I read all the articles you mentioned. But I still do not understand what to do.
              I don't understand what start behavior I should use. There are tons of "you may do that or that", but what exactly I should do?

              Just give me a simple instructions how to re-enable my strategy and make it work with current account position.

              So, what I have in my code
              1. Pending orders and references to them, like that
              _BasicEntryPending = EnterLongStopMarket(0,true,1, aTop,"Main Long");

              2. Checking Position.MarketPosition, like that
              if (Position.MarketPosition==MarketPosition.Flat)


              Just tell me what settings I have to set and what code I have to write to reach my aim: when I re-enable my strategy it must see the current account state but not some virtual strategy state. And I don't want to flatten my account position. And I dont want to open and close any fictive orders manually and so on. I want just enable my strategy, and that's all

              Comment


                #8
                Hello rfsettling,

                Adopt Account Position behaves like immediately submit, except it ensures that the account position does not have manual orders that would leave the strategy un-synchronized. I believe this is the behavior you desire.

                Your syntax for enabling it looks correct. GetRealtimeOrder() will return null if the order has been filled canceled, rejected etc. and it will only transition the Order passed through to real-time. I would suggest to place manage your profit target and stop loss with Order objects so you can use GetRealtimeOrder() to transition them from historical to real-time in addition to your entry order.

                The Advanced Order Handling section of the help guide explains transitioning orders in slightly more depth than the GetRealtimeOrder() method's documentation. If you want to reference that section, you can find it here: https://ninjatrader.com/support/help...storicaltolive

                Please let me know if you have any additional questions.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  I also thought that adopt account position mode is suitable to me, but when I enable my strategy for the first time I get some weird error:

                  if (Position.MarketPosition==MarketPosition.Flat && _BasicEntryPending==null)
                  _BasicEntryPending = EnterLongStopMarket(0,true,1, aTop,"Main Long");

                  Method EnterLongStopMarket just returns null. Without any errors and traces. Just null.
                  My real account is flat and has no any orders.

                  I'm enclosing code to test

                  P.S.
                  I put the order using unmanaged approach, via SubmitOrderUnmanaged.
                  But another error occured. After the order was set, I disabled strategy, And checkbox "Enable" became inaccessible! It was inaccessible until I canceled pending order manually.

                  PPS. Is there any possibility to tell NT not to sync positions at all? I want do it manually, but do not see such option "keep as is". Each mode changes either account position or strategy position
                  Attached Files
                  Last edited by rfsettling; 05-25-2017, 04:44 PM.

                  Comment


                    #10
                    Hello rfsettling,

                    I am unable to compile the code in its current form and cannot perform the same calculation for aTop. Namely this.NormailizePrice and this.IsSamePrice.

                    I would suggest to add print statements for the arguments used in EnterLongStopMarket() to verify that it is getting proper values. I would also suggest to double check the log for any errors. For example, if you had isUnmanaged enabled, the Managed Approach order methods could be returning an error.

                    Additionally, I would suggest to enable Trace Orders so you can observe what happens when you call that method.

                    P.S.
                    I put the order using unmanaged approach, via SubmitOrderUnmanaged.
                    But another error occured. After the order was set, I disabled strategy, And checkbox "Enable" became inaccessible! It was inaccessible until I canceled pending order manually.
                    It looks like the Start Behavior is getting stuck when trying to check if previous orders that have been submitted from the strategy belong to that strategy or not. The help guide states:
                    • The account and instrument the strategy is started on must not have any working orders which were submitted outside of the strategy. If an order is detected, the strategy can not be started until these orders have been manually managed.
                    In my tests, it looks like the strategy can recognize previous orders with Immediately Submit, but it is not with AdoptAccountPosition. We are looking into this further on our end. Tracking ID: QA-3025

                    Could you confirm that you are able to re-enable your strategy when using Immediately Submit when you have orders submitted and working? I would suggest to use the simulated data feed and the SampleOnOrderUpdate strategy to test this. I've made a quick video going over the test as well: https://www.screencast.com/t/FUCm2zymF8Cd

                    PPS. Is there any possibility to tell NT not to sync positions at all? I want do it manually, but do not see such option "keep as is". Each mode changes either account position or strategy position
                    You could use Immediately submit and Wait until flat without Synchronize Account and you strategy will not place reconciliary orders that change account position. Immediately Submit will let you "resume" a strategy while Wait until flat will cancel any previous orders and will wait until the strategy is flat.

                    Immediately Submit would best fit the description of "Keep As Is."

                    I look forward to being of further assistance.


                    EDIT: The issue for AdoptAccountPosition is being tracked with ticket ID NTEIGHT-11740. You can observe this ticket ID in the release notes of the version of NinjaTrader 8 that has a fix for this issue.
                    Last edited by NinjaTrader_Jim; 06-02-2017, 03:09 PM.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      For AdoptAccountPosition to show in the UI Start Behavior box, IsAdoptAccountPositionAware = true;
                      must be set in State.SetDefaults section. The help instructions should say this.

                      Comment


                        #12
                        Hello Camdo,

                        The help documentation has this outlined in the Syncing Account Positions section.

                        Note: Adopt account position will only be available if the developer of a strategy has programmed the strategy to be aware of the real-world account position. If this setting is not available when starting your strategy, the strategy was not programmed in a manner capable of handling account positions. If you are a developer and would like your strategy to handle a real world position, please see the following article here on these properties.


                        When you say "The help instructions should say this." are you suggesting we add it to the help guide or are you making an observation that AdoptAccountPosition needs to be set to true and the information could be found in the help guide?
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          I am saying that the help instructions should state clearly that in order for "AdoptAccountPosition" to show in the enumeration list of the UI StartBehavior box, the statement IsAdoptAccountPositionAware = true; must be defined the Set.Defaults section.

                          One other matter that is confusing in the use of AdoptAccountPosition is if a strategy's parameters are saved as default, then the UI Start Behavior enumeration box will not list AdoptAccountPosition even though it was saved as default.

                          if Set.Defaults contains the statement:
                          StartBehavior = StartBehavior.AdoptAccountPosition;
                          Then the UI StartBehavior list will show "AdoptAccountPosition" as the first shown, but if the UI is saved as default "AdoptAccountPosition" will no longer be in the list.

                          Comment


                            #14
                            Hello Camdo,

                            I am willing to submit a suggest to the have the help guide amended, but I believe that the IsAdoptAccountPositionAware already has its the usage described as you are requesting. IsAdoptAccountPositionAware is linked in the Syncing Account Positions documentation so it can be referenced for developing a strategy for AdoptAccountPosition.

                            Could you provide some detail on what you would like to see documented differently in the IsAdoptAccountPositionAware documentation?

                            I could also submit a request for having the Syncing Account Positions documentation to clearly state "IsAdoptAccountPositionAware = true; must be defined the Set.Defaults section" where IsAdoptAccountPositionAware is referenced.

                            As for the other items regarding AdoptAccountPosition, I have not been able to observe the same behavior you are describing. I've created a video demonstrating: https://www.screencast.com/t/AM7zzRiVw

                            Could you verify that you do not trying to have more than one strategy configured with AdoptAccountPosition? You can only enable one strategy with AdoptAccountPosition on an account at a time.

                            I'll leave a link for IsAdoptAccountPositionAware for convenience: https://ninjatrader.com/support/help...itionaware.htm

                            Please let me know how you would like me to assist you further.
                            JimNinjaTrader Customer Service

                            Comment


                              #15
                              Thank you Jim for testing. I studied your video and repeated your procedure, but on my computer but I got different results. To reiterate, the procedure is:
                              1. Add strategy to chart, observe that UI Start Behavior lists AdoptAccountPosition
                              2. Save UI to a template "default". Remove Strategy, OK,
                              3. Add Strategy to chart, Observe Start Behavior does not list AdoptAccountPosition.
                              4. Delete the template, remove the strategy, OK,
                              5. Add the strategy to chart, Observe that UI StartBehavior list shows AdoptAccountPosition


                              On my computer, after saving UI settings to a template of any name, will cause the AdoptAccountPosition to be removed from the StartBehavior enumeration list when reloading the strategy.

                              I have tested this with only one strategy using AdoptAccountPosition and IsAdoptAccountPositionAware = true.

                              After saving the template "default", I can observe in file Default.xml that this line is present:
                              <StartBehavior>AdoptAccountPosition</StartBehavior>

                              Any suggestions to solve this problem would be much appreciated.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by samish18, Yesterday, 08:31 AM
                              4 responses
                              14 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Started by funk10101, Yesterday, 09:43 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                              5 responses
                              551 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by rtwave, 04-12-2024, 09:30 AM
                              5 responses
                              37 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by funk10101, Today, 12:02 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Working...
                              X