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

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 PhillT, Today, 02:16 PM
      2 responses
      6 views
      0 likes
      Last Post PhillT
      by PhillT
       
      Started by Kaledus, Today, 01:29 PM
      3 responses
      10 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by frankthearm, Yesterday, 09:08 AM
      14 responses
      47 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by gentlebenthebear, Today, 01:30 AM
      2 responses
      14 views
      0 likes
      Last Post gentlebenthebear  
      Started by PaulMohn, Today, 12:36 PM
      2 responses
      17 views
      0 likes
      Last Post PaulMohn  
      Working...
      X