Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

sync account position

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

    sync account position

    Hello,

    I am trying to understand the sync account position behaviour and therefore I have written a simple sample strategy that I use for testing:

    This testorders strategy does open a long position and a simple stop loss order is set. In a second step this stop order is modified every 10 seconds (just for the sake of having the strategy do something)

    NinjaTrader Control Center is set up under options
    -> strategies->NinjaScript->Cancel entry Order... IS UNCHECKED
    ...................................-> Cancel exit Order .. IS UNCHECKED


    This means that positions and orders remain active even if the strategy is disabled.


    This all works fine, and the TestOrders strategy once enabled enters 1 long position and sets a stop loss order.
    Position and Order are maintained even after disabling the strategy.
    This is fine and corresponds for what I want in an overnight position, for example 6E or ES.

    Then I reenable the strategy (SyncAccountPosition ==true)and something strange happens:
    The stop loss order is cancelled the very moment the strategy was enabled!

    Here my questions:
    1. Is it expected behaviour that the Stop loss order is cancelled at the re- enable of the strategy?

    2. Minor bug: I set ES 03-10 as the default Instrument for the Strategy, but all the time the default comes back as FGBL 03-10. So it seems that set default for Instrument does not work.

    3. I expected to recover my Stop Loss order by the code
    Account.Orders.FindByToken(stoptoken)
    The stop loss order is found, but the status is cancelled, so it is not useable any more (or is it possible to reenable a cancelled order?)

    If (1. is expected behaviour, does this mean that I have to create a new Stop Loss order in order to have SetStopLoss(...) be sync'ed to my position?


    Thank you for your time and please try to answer slow, but looking deep into it.

    Thank you

    Andreas





    Then I disable the strategy and all
    Further the strategy
    Attached Files

    #2
    Hello,

    Thank you for your post.

    Someone from our NinjaScript department first thing Monday morning regarding your question.

    Thank you for your patience.
    Christopher J.NinjaTrader Customer Service

    Comment


      #3
      Andreas, thanks for sharing the test code, will reproduce here and look into.
      BertrandNinjaTrader Customer Service

      Comment


        #4
        Andreas,

        #1 is expected, as you work with if (Historical) there's no strategy position to have working stop orders for as you enabled the strategy, thus those are cancelled.

        #2 Thanks for the default instrument spot, will check into, could reproduce here on my end.

        #3 Correct, once those are cancelled they are in terminal state, thus not 'recoverable'.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hello Bertand,

          I still have questions, let me explain first what I have in mind:

          Usually my strategies are running only for few minutes or hours, rarely overnight. My strategies , once in position just trail the stop loss order generated.
          For all this the NinjaTrader strategy handling works fine as long as I do not exit NinjaTrader or by my own action or because I have a patsystems connection loss (and then I have to exit NinjaTrader because of patsystems dll implementation limits).

          So I am interested in a continuation of my strategy after a Disable - Enable cycle. To ensure continuation I set SyncAccountPostition=true

          The classical case of exiting NinjaTrader the evening and start NinjaTrader again the next day is just a special case of this cycle.


          Using the Testorders.cs I do the following:
          1. TestOrders is Enabled and generates a position 1L and a stop loss order.

          2. Now I disable the TestOrders strategy. The position is maintained as you can see from the attached screenshot of the Control Center.

          3. Now I edit the strategy parameters and set SyncAccountPosition =true

          4. Then I enable the strategy. I expect the strategy to sync to my account position (1L) , but this is not the case: As soon as the strategy is enabled the account position is closed.

          It looks like that the strategy just enabled does not have a strategy position and therefore it syncs the account position to the same, effectlivly closing the position.


          Here my questions:

          A. I deduce from your previous answer that you expect the strategy to (re-)generate the positions and orders while OnBarUpdate is looping through the historic bars.
          Do you expect the strategy to regenerate positions while looping through the historic bars?

          B. If A is correct: When OnBarUpdate is looping through the historic bars is it correct that only OHLC bar information is supplied. All the timing information of Bid, Ask sequence is not present in the looping.
          How do I reconstruct my Positions then with a different input?
          (same problem backtesting is different to market replay)


          C. Why does the disabled strategy show a position of 1L when this position is closed as soon as the strategy is reenabled again?
          (refers to the attached picture)

          D. General: As I see it your SyncAccountPosition will change the account state to fit the strategy state.
          What I need is actually the contrary: I need to change the strategy state base on the account state.

          Any thoughts on how to achieve this?

          regards

          (as you see I have stil not grasped the concept)
          What I need is the following:
          The next morning I want to open NinjaTrader, then I choose the strategies from the tab that I would like to continue, enable these and that it is.
          In my case I might have 5 - 10 strategies, and I do not want to end up with my logic firing orders all over the place to sync account(s) to strategies ,with the chance of loosing a substantial amount of equity in the morning when everybody (including me) is still a bit slow in its reflexes.
          Attached Files

          Comment


            #6
            Hi Andreas,

            the strategy position is the important reference here, not the account position you hold.

            A: Yes it would recalculate through the historical data set and then try to match strategy vs account position.

            B: Yes, this is expected, only OHLC info would be availabe, just like in a regular backtest.

            C: Expected too, it's just showing the strategy in the disabled state, thus for example you were long as you turned it off, it would then continue to show this position as 1L. On restart it would recalculate and then update accordingly.

            D: As noted above, the strategy position is the reference for the auto sync we offer in NT7, the contrary is unfortunately not supported.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Thank you for clarifying,


              Actually, for my purposes I will always take my account position as a reference. This is where my money is and I do not want to mess around too much...
              Therefore my approach is to have an account position as a reference and then I must always adapt my strategy to the account position. I will never do the other way around!!!

              please allow me to submit a modified example the next days for further discussion.
              .

              One question:

              When I re-enable TestOrders with syncAccountPosition=true, at what stage is the current position actually cancelled/modified/synched?

              1. Is this done after Initialize()?
              2. after OnStartUp()
              3. after OnBarUpdate() has finished Historic Bars?


              Thank you for any clarification, but it is important for me to grasp NinjaTrader logic properly. (To avoid uncontrolled Order-firing in the morning)

              Andreas

              Comment


                #8
                Andreas, you're welcome - it recalculates through the historical OnBarUpdate() and then sends the needed 'Syncronize' orders after determining the historical strategy position.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Forgot to clarify one thing you brought up Andreas - the default instrument you try setting for the strategy is not saved, this is expected behavior.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Bertrand,

                    I cannot confirm the Syncronize after OnBarUpdate() Historic has finished.

                    Here the Output:

                    **NT** Disabling NinjaScript strategy 'TestOrders/301
                    **NT** Enabling NinjaScript strategy TestOrders/301
                    SyncAccountPosition=True
                    **NT** Syncing account position on starting strategy 'TestOrders/301
                    Account position='1L FDAX 03-10' Strategy position=''
                    OnStartUp
                    RebuildPositionFromExecutions EnterLongLimit(5900.5)


                    Look at the 2 last lines: OnStartup is from the Print statement in OnStartUp()

                    It looks like that the sync call is done BEFORE OnStartup()

                    As you see I am experimenting with a function RebuildPositionFromExecutions() , this function is called in OnBarUpdate() ,Historical.


                    1.Any explanation for this?
                    Do you calculate the needed sync Orders before OnStartup()?

                    regards
                    andreas

                    Comment


                      #11
                      Andreas, any way I could test your sample script on my end - to determine needed sync orders the strategy would of course need to finish the historical OnBarUpdate() part to arrive at it's current strategy position from historical data....
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Here you go...

                        TestOrders.cs is however very "experimental" and pretty much unfinished...
                        Attached Files

                        Comment


                          #13
                          zweistein,

                          Unfortunately we cannot process your code as it is fairly complex. Please find attached a very simple example demonstrating expected behavior.

                          Code:
                          **NT** Enabling NinjaScript strategy 'SampleMACrossOver2/c6cc44eeb8c84fc7bfa74a4af9d67a05' : On starting a real-time strategy - StrategySync=SubmitImmediately SyncAccountPosition=True EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositions ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True MaxRestarts=4 in 5 minutes
                            OnStartUp: Flat 0
                            OnBarUpdate: True 3/3/2010 2:51:00 PM
                            OnBarUpdate: True 3/3/2010 2:52:00 PM
                            …
                            OnBarUpdate: True 3/9/2010 4:21:00 PM
                            OnBarUpdate: True 3/9/2010 4:22:00 PM
                            **NT** Syncing account position on starting strategy 'SampleMACrossOver2/c6cc44eeb8c84fc7bfa74a4af9d67a05'. Account position='' Strategy position='1L ES 03-10'
                          Notice how it syncs after it finishes going through all the historical = true bars.
                          Attached Files
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            @Josh,

                            I tried your SampleMACrossOver2 strategy.
                            Currently does not enter into position and I see the Print statements :

                            OnBarUpdate: True 3/10/2010 8:35:00 AM

                            I have some question about Historical: Why is Historical True also when the OnBarUpdate() is called in real-time?

                            Try yourself, look at the OnBarUpdate Print statements and you will see Historical is always true.

                            What is the logic behind historical?

                            Thank you

                            Comment


                              #15
                              @Josh,

                              I simplified your SampleMACrossOver2 slightly:

                              Now the strategy enters into position at a fixed time, (8march2010, 9.30 W.Euope Time) and does nothing else.

                              I cannot see any sync to account activity ...

                              Please check on your side

                              Best regards
                              andreas
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by manueldecastro, Today, 01:16 PM
                              3 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by tradingnasdaqprueba, Today, 03:42 AM
                              9 responses
                              35 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by llanqui, Yesterday, 03:51 PM
                              6 responses
                              27 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by AlgoDreamer, Today, 12:39 PM
                              2 responses
                              9 views
                              0 likes
                              Last Post AlgoDreamer  
                              Started by ninza33, Today, 12:31 PM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Working...
                              X