Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multithreaded events in strategy and CurrentBar value

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

    Multithreaded events in strategy and CurrentBar value

    Hello

    I subscribe my strategy to Account events (this account is different to the account the strategy is linked).

    Code:
    var MySomeAccount = Account.All.FirstOrDefault(....);
    MySomeAccount.OrderUpdate += AccountOnOrderUpdate;
    
    ...
    private void AccountOnOrderUpdate(object aSender, OrderEventArgs aArgs)
    {
      var aLastClose = this.Close[0]; <--- Here sometimes I have an Exception, because Bars.CurrentBar =0
    }
    I understand why it happens: while I try to get value from last bar, CurrentBar value is being changed (temporary) to 0 by the code of another thread (OnMarketData thread generates new tick).

    I need some way to synchronize my data flow with my account events. Something like that

    Code:
    private void AccountOnOrderUpdate(object aSender, OrderEventArgs aArgs)
    {
      lock (some_bars_sync_object)
      {
        var aLastClose = this.Close[0]; 
      }
    }
    I found SyncRealtimeData property in strategy class. It seems it is related to my issue, but I'm not sure.

    Please, point me out, how I can synchronize my account events to my market data updates

    #2
    Hello rfsettling,

    Thanks for opening the thread.

    These events are asynchronous of each other, so I am unsure if you are simply trying to access price values from another thread or if you are trying to accomplish something more by synchronizing these events.

    Could you confirm that accessing these values with the Series<T>.GetValueAt() method and using CurrentBar does not get you the results you are looking for?

    In these cases you may also use TriggerCustomEvent() to make sure all internal pointers are synchronized and updated before your code references its data.

    I've included publicly available documentation to the topics discussed.

    Series<T>.GetValueAt() - https://ninjatrader.com/support/help...getvalueat.htm

    TriggerCustomEvent() - https://ninjatrader.com/support/help...ustomevent.htm

    If this information does not lead you toward a resolution, could you share a reduced example of the code so I may observe the issue you are facing on my end?

    Comment


      #3
      Thanks, Jim!

      TriggerCustomEvent is exactly what I need.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      79 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      146 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      79 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      52 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      56 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X