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 NullPointStrategies, Today, 05:17 AM
    0 responses
    44 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    65 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X