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

detect when a strategy is out of sync?

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

    detect when a strategy is out of sync?

    Hello,

    Is it possible to have a strategy or the system detect when a strategy is out of sync? I have a lot of overfill prevention and overfill fixing coded in, but sometimes it still happens and I end up with a strategy that is still running but out of sync with the account position. I am running hundreds of strategies overall, but only 1 per instrument. Is there a way to detect that the strategy is out of sync and then restart that strategy? If I can restart it, then it will assume the account position and resume as I want it to. Failing that, can an out of sync strategy close the account position for that instrument and terminate the strategy?

    Thank you!

    #2
    Hello RobotSyndicate,

    Thanks for your post.

    Position represents the strategy position and PositionAccount reflects the account position. You could compare these, but keep in mind, they are controlled by different events, so timing will have a factor. (Strategy positions are based on Executions and Account positions are based on PositionUpdates, so you could see an Execution happen first and the strategy would appear out of sync until the account position is updated from a PositionUpdate.)

    Position - https://ninjatrader.com/support/help.../?position.htm

    AccountPosition - https://ninjatrader.com/support/help...ionaccount.htm

    It would not be possible to restart a strategy programmatically, you could then consider the following:

    1. Track down how the desync occurs. If this happens based on how the strategy protects positions, for example, with partial fills, you may want to look into how the strategy handles orders in the event of partial fills. The SampleOnOrderUpdate strategy describes a good way to manage partial fills for connections that follow 1. Order 2. Execution 3. Position event ordering. Interactive Brokers and Rithmic connections do not follow this ordering so special considerations will need to be made on those connections. For example, the script could use the Unmanaged Approach, and use OnOrderUpdate alone for order tracking, calculating your own strategy Position and protecting positions.

    2. Have the strategy call some AddOn Framework code to submit an order outside the context of the strategy in an effort to resync it. (This would use Account.CreateOrder and Account.Submit to submit those orders outside the context of the strategy.)

    3. Call Account.Flatten(new [] {Instrument}); to flatten the account position and terminate the strategy.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    Account class - https://ninjatrader.com/support/help...ount_class.htm

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you for that very helpful answer Jim! I am trying to understand how to determine the quantity of a specific instrument in the account. It says the "Index value is based on the the array of Bars objects added via the AddDataSeries() method." But say I have 4 different strategies, each running on a different futures contract. None of them use the AddDataSeries() method. How is the index value of the PositionsAccount determined in that case? For example:

      Strategy 1: ES
      Strategy 2: GC
      Strategy 3: SI
      Strategy 4: HG

      I enable them sequentially from #1 - #4 and they all run simultaneously for a while, then at some point Strategy # 3 overfills and is out of sync. If I then want strategy 3 to compare positions with if(Position.Quantity != PositionsAccount[X].Quantity) How do I determine X? Or since each strategy only uses a single bar series, do I just use if(Position.Quantity != PositionAccount.Quantity) and will it only compare to the account position of the instrument being used in that specific strategy?

      Then it sounds like I could make sure it remains out of sync for a period of time, and if so then flatten the unwanted position for strategy #3 in this example by using: Account.Flatten(new [] { Instrument.GetInstrument("SI 03-21") });


      Then one other quick question, when using Account.CreateOrder or Account.Submit, the example in the help file is: myAccount.Submit(new[] { stopOrder });

      Would I use "myAccount"? or is that a reference to the actual account name of the account that I wish to submit an order for?


      Thanks so much.
      Last edited by RobotSyndicate; 02-12-2021, 05:51 PM.

      Comment


        #4
        Hello RobotSyndicate,

        Or since each strategy only uses a single bar series, do I just use if(Position.Quantity != PositionAccount.Quantity) and will it only compare to the account position of the instrument being used in that specific strategy?

        Then it sounds like I could make sure it remains out of sync for a period of time, and if so then flatten the unwanted position for strategy #3 in this example by using: Account.Flatten(new [] { Instrument.GetInstrument("SI 03-21") });
        Your understanding here is correct. You could also do Account.Flatten(new [] {Instrument}); since Instrument will reflect the Instrument the strategy is applied against.

        Then one other quick question, when using Account.CreateOrder or Account.Submit, the example in the help file is: myAccount.Submit(new[] { stopOrder });

        Would I use "myAccount"? or is that a reference to the actual account name of the account that I wish to submit an order for?
        The Help Guide describes creating an Account object called myAccount since the example code is relevant to AddOns where an Account object is not already available. In a strategy, we already have an Account object simply called Account, so Account.CreateOrder and Account.Submit would be suitable. (This Account object reflects the account we apply the strategy to.)

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by 00nevest, Today, 02:27 PM
        0 responses
        1 view
        0 likes
        Last Post 00nevest  
        Started by Jonafare, 12-06-2012, 03:48 PM
        5 responses
        3,986 views
        0 likes
        Last Post rene69851  
        Started by Fitspressorest, Today, 01:38 PM
        0 responses
        2 views
        0 likes
        Last Post Fitspressorest  
        Started by Jonker, Today, 01:19 PM
        0 responses
        2 views
        0 likes
        Last Post Jonker
        by Jonker
         
        Started by futtrader, Today, 01:16 PM
        0 responses
        9 views
        0 likes
        Last Post futtrader  
        Working...
        X