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?
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks, Jim!

      TriggerCustomEvent is exactly what I need.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by JTizz, Today, 01:20 PM
      0 responses
      2 views
      0 likes
      Last Post JTizz
      by JTizz
       
      Started by omermirza, Today, 12:39 PM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by mballagan, Today, 01:02 PM
      0 responses
      5 views
      0 likes
      Last Post mballagan  
      Started by larkmail, Today, 12:30 PM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by Danila, Today, 11:08 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X