Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Solution to Check if Strategies are in Sync

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

    Solution to Check if Strategies are in Sync

    Hi,
    So trying to check if strategies are in sync.
    I have an indicator that contains the code (after doing all necessary subscriptions etc).

    Testing with a single strategy for now.
    It works fine for stratQuantity.
    For accountQuantity it updates to the correct number when i buy or sell in the sim account for 1 or 2 prints then it goes back to printing the original blance whatever it was before i loaded up the indicator and im not sure why.

    this is the code, and my questions follow


    Code:
            private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
            {
                int stratQuantity = 0;
                int accountQuantity = 0;
            
                Print("===");
                foreach (StrategyBase strategy in myAccount.Strategies)
              {
                  if(strategy.Account.Name==accountName){
                     if(strategy.Position.MarketPosition==MarketPosition.Long){
                         stratQuantity+= strategy.Position.Quantity;
                     }else if(strategy.Position.MarketPosition==MarketPosition.Short){
                         stratQuantity-= strategy.Position.Quantity;
                     }
                    
                      if(strategy.PositionAccount.MarketPosition==MarketPosition.Long){
                         accountQuantity = strategy.PositionAccount.Quantity;
                     }else if(strategy.PositionAccount.MarketPosition==MarketPosition.Short){
                         accountQuantity = strategy.PositionAccount.Quantity;
                     }
                  }
              }
              
              Print(strategiesAreSynced);
              Print(stratQuantity);
              Print(accountQuantity);
              
              if(stratQuantity!=accountQuantity && strategiesAreSynced){
                  PostData("Strategies not Synced");
                  strategiesAreSynced = false;
              }
              if(stratQuantity==accountQuantity){
                  strategiesAreSynced = true;
              }
            }​




    1- is using OnAccountItemUpdate correct for both stratQuantity+= strategy.Position.Quantity and accountQuantity = strategy.PositionAccount.Quantity? Because one is printing correct value and continuously and the latter it briefly then reverts back randomly .
    2- Is there a way to access account quantities by instruments rather than from the strategy.PositionAccount.Quantity?
    3- general advice for how to this better? I suppose this will work for on one strategy which is useless, but it should be by finding a list of instruments from all active strategies and then adding positions. then finding list of instruments and all their actual PostionAccount quantities before making comparison.

    ​​​​​​​

    #2
    Hello madb123,

    The AccountItemUpdate event is just called for account value changes, not specifically position changes. If you are trying to drive logic based on positions you could use the position update event.


    I wouldn't be able to say what may be happening with what you described, the values you are accessing are the values that the strategy sees. You can access positions on the account by using the account object directly instead of the strategy instance.



    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    132 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    65 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X