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

Suspected Multi-Threading Issue

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

    Suspected Multi-Threading Issue

    Hello,

    I have a strategy that adds one custom indicator. In that strategy, I access the data series of my indicator via the indicators public property called DataSeries. The trouble is, when I try to access the current value of the indicator, sometimes it returns 0. I highly suspect that this is a multi-threading issue or an event timing issue and that my strategy is accessing the indicator property before it is set internally by the indicator. Here is my pseudo code:

    Code:
    // My Indicator Pseudo-Code:
    // ---------------------------------------
    public Series<double> DataSeries { get { return Values[0]; } private set { Values[0] = value; } } // Property I am accessing from My Strategy.
    
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
        DataSeries[0] = Close[0];
    
        // DEBUG
        if (DataSeries[0] < 1)
        {
            ; // *Breakpoint* [B]never[/B] gets triggered. 
        }
    }
    Code:
    // My Strategy Pseudo-Code:
    // ---------------------------------------
    private MyIndicator _indicator = MyIndicator(); // This is actually properly instantiated when State == DataLoaded
    
    protected override void OnBarUpdate()
    {
        double y2 = _indicator.DataSeries[2]; // This value is always correct. 
        double y1 = _indicator.DataSeries[1]; // This value is always correct. 
        double y0 = _indicator.DataSeries[0]; // This value is sometimes 0.
    
        // DEBUG:
        if (y1 < 1 || y0 < 1)
        {
            ; // *Breakpoint* gets triggered and I can see that y0 is [B]indeed[/B] 0. 
        }
    }
    This is obviously a simplification of my source code, but it is an accurate representation of what I am doing. The debug section of my strategy is being triggered but the debug section of my indicator is not being triggered. This leads me to believe that this is a multi-threading issue wherein my strategy is accessing my indicator property before it is set for the current bar. I have tried using a lock in my indicator as well to make sure that other threads wait for DataSeries to be set before accessing it, but that did not work. My second guess is that it has to do with the timing between OnMarketData and OnBarUpdate, in that the strategy's OnBarUpdate is being called OnEachTick and is "too fast" for the indicator's OnMarketUpdate. Is this possible? I am a bit stumped on this one.

    Your help is highly appreciated.

    #2
    Where are you running this?

    Comment


      #3
      Hello jflaggs,

      Thank you for your post.

      Essentially you are running into a multi-threading scenario here as you suspected. More importantly it is the fact the Series is set in the OnMarketData section of indicator and then called in the OnBarUpdate() section of the Strategy.

      There is the possibility that the Series is not yet set to a valid value and therefore return 0. The solution would be to check for a valid data point before processing. You can do this with IsValidDataPoint(): http://ninjatrader.com/support/helpG...ddatapoint.htm

      Please let me know if you have any questions.

      Comment


        #4
        Patrick and Sledge, thanks for your replies.

        Checking for IsValidDataPoint() did in fact do the job. Thank you. As a side note, I have found that the behavior of built-in method Slope() is misleading or incorrect in this scenario. Passing in a data series with an invalid data point does not throw an error or return double.NAN as I believe it should. Instead, it quietly assumes that the invalid data point is equal to 0 and returns the slope based on that. I discovered this while making my own slope function that returns NAN if a data point is invalid. Perhaps that could be a candidate for review.

        Thanks again for the assistance. Always a pleasure.

        Comment


          #5
          Have you tried running Update() before returning your series?

          Code:
          public Series<double> DataSeries 
          { 
                 get 
                  {
                         Update(); // <---- here 
                        return Values[0]; 
                  }
                  private set { Values[0] = value; }
           }

          Comment


            #6
            Ah, I actually forgot about the Update() method. I just tried it but, unfortunately, it didn't do the trick.

            As a side note, using Update() in this way also "overrides" the Calculate property of the indicator. For example, if your indicator Calculate property is set to Calculate.OnBarClose but you are reading indicator.DataSeries[0] on OnEachTick in your strategy, your indicator is now calling OnBarUpdate() each tick of your strategy rather than OnBarClose as originally intended. Even if it did fix this particular issue, it might introduce other undesirable behaviors.

            I am okay with using IsValidDataPoint() for my particular application.

            Comment


              #7
              Originally posted by reach4thelasers View Post
              Have you tried running Update() before returning your series?

              Code:
              public Series<double> DataSeries
              {
              get
              {
              Update(); // <---- here
              return Values[0];
              }
              private set { Values[0] = value; }
              }
              I do coding NT indicators/strategies for 4 years and have never seen this method. It could have saved me from a lot of troubles with multi threading. Thanks.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by gentlebenthebear, Today, 01:30 AM
              2 responses
              13 views
              0 likes
              Last Post gentlebenthebear  
              Started by Kaledus, Today, 01:29 PM
              2 responses
              7 views
              0 likes
              Last Post Kaledus
              by Kaledus
               
              Started by frankthearm, Yesterday, 09:08 AM
              13 responses
              45 views
              0 likes
              Last Post frankthearm  
              Started by PaulMohn, Today, 12:36 PM
              2 responses
              16 views
              0 likes
              Last Post PaulMohn  
              Started by Conceptzx, 10-11-2022, 06:38 AM
              2 responses
              56 views
              0 likes
              Last Post PhillT
              by PhillT
               
              Working...
              X