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

Load indicator in separate thread

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

    Load indicator in separate thread

    Hi

    I have an histogram indicator in its own panel. To load this indicator takes some intensive disk IO and it's holding up the price chart panel from loading.

    Is there an accepted or established way to spin off the loading indicator into its own thread and proceed to let the price panel load and operate normally ?

    Examples ?

    #2
    Hello Bidder,

    There is not a way to offload indicators into a different thread, they are already multi threaded based on how the platform operates so the chart waiting would be expected. The chart waits for the indicator to process because the indicator subscribes to the charts data. Once the indicator catches up it will enter realtime with the chart and continue to get updates.

    If the indicator takes a long time to load you would need to look at what you are doing in that time to see if that can be done more efficiently. If that cannot then the only options would be to load less data so it takes as short of a time as possible.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Ok... Thanks for the quick response.

      Comment


        #4
        Attached is a working proof of concept....

        Chart loaded quickly and the realtime bars were building as the background process was loading
        Click image for larger version

Name:	20240104_205218_WS2.png
Views:	98
Size:	8.2 KB
ID:	1284950

        Immediately after background process finished
        Click image for larger version

Name:	20240104_210829_WS2.png
Views:	76
Size:	9.0 KB
ID:	1284951


        Code:
        using System.Threading;
        using System.Threading.Tasks;
        
        Random rnd = new Random();
        int FirstRtIdx=0;
        
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                AddPlot(new Stroke(Brushes.DimGray, 1), PlotStyle.Bar, "Diff");
                ArePlotsConfigurable = false;
            }
        }
        
        protected override void OnBarUpdate()
        {
            if (State==State.Realtime)
            {
                if (FirstRtIdx==0)
                {
                    FirstRtIdx=CurrentBar;
                    LoadHistData();
                }
        
                //Start loading realtime bars
                int Rnd=rnd.Next(1, 100);
                Value[0]=Rnd;
                PlotBrushes[0][0] = Brushes.DarkRed;
        
            }                    
        }
        
        private void LoadHistData()
        {
            new Thread(() =>
            {
                Print("Retrieve and process historical data");
                Thread.Sleep(60000);
                Print("Done");
        
                TriggerCustomEvent(o =>{BuildHistPlots();}, null);
        
            }).Start();    
        }
        
        private void BuildHistPlots()  
        {
            for (int barIndex = 0; barIndex < FirstRtIdx; barIndex++)
            {
                int barsago=CurrentBar-barIndex;
                int Rnd=rnd.Next(1, 100);
                Print(Rnd);
                Value[barsago]=Rnd;
                PlotBrushes[0][barsago] = Brushes.DarkGreen;
            }
        }

        Comment


          #5
          Yes - however you go about it - the appropriate thing to do is not to hold up OnBarUpdate or OnStateChange at all and to load your data in the background. Once the data is loaded, some appropriate event such as a TriggerCustomEvent can be launched to synchronize with the bar series 0 and then you can paint the results back however far you need to if they are plots. If they are not plots and you are doing custom OnRender, you can just directly access your data from there in a thread-safe way and draw it straight onto the screen. In that case, all you would need to do is one ForceRefresh() after you get the data loaded to cause OnRender to run (in case there is no market data event).
          Bruce DeVault
          QuantKey Trading Vendor Services
          NinjaTrader Ecosystem Vendor - QuantKey

          Comment


            #6
            Hello Bidder,

            Just so it is clear from a support standpoint using custom threading in NinjaScript is not supported as that can lead to problems with how scripts work in NinjaTrader. For anyone who may come across this thread, if you try to use custom threading and need any sort of assistance with the script or platform you will need to remove that before our support can assist. Custom threading is not supported at all when using NinjaScript.

            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Pattontje, Yesterday, 02:10 PM
            2 responses
            14 views
            0 likes
            Last Post Pattontje  
            Started by flybuzz, 04-21-2024, 04:07 PM
            17 responses
            229 views
            0 likes
            Last Post TradingLoss  
            Started by agclub, 04-21-2024, 08:57 PM
            3 responses
            17 views
            0 likes
            Last Post TradingLoss  
            Started by TradingLoss, 04-21-2024, 04:32 PM
            4 responses
            43 views
            2 likes
            Last Post TradingLoss  
            Started by cre8able, 04-17-2024, 04:16 PM
            6 responses
            56 views
            0 likes
            Last Post cre8able  
            Working...
            X