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 judysamnt7, 03-13-2023, 09:11 AM
      4 responses
      56 views
      0 likes
      Last Post DynamicTest  
      Started by ScottWalsh, Today, 06:52 PM
      4 responses
      35 views
      0 likes
      Last Post ScottWalsh  
      Started by olisav57, Today, 07:39 PM
      0 responses
      7 views
      0 likes
      Last Post olisav57  
      Started by trilliantrader, Today, 03:01 PM
      2 responses
      19 views
      0 likes
      Last Post helpwanted  
      Started by cre8able, Today, 07:24 PM
      0 responses
      8 views
      0 likes
      Last Post cre8able  
      Working...
      X