Any equivalent to backgroundworker

Created at 15 Jun 2020, 17:58
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
12

1222Ht

Joined 11.11.2017

Any equivalent to backgroundworker
15 Jun 2020, 17:58


Hi, Thank you for reading my question .

After sometime of using cTrader ive understood that Calculate() is evoked :

1. when indicators first loaded - every bar .

2. then whenever a tick is updated. 

My question is , if in Calcuate() theres some forloop and then market becomes very active . many ticks get updated in a short period of time . this might then cause little lag on some less good computer . what im thinking is if at each tick . a backgroundworker can be used to run those loops. then if a few tick happens consecutively . maybe something like 

backgroundworker.isBusy can block out , given market price might not change a lot in such short time duration . duplicate run of the code could be less necessary . I have very few experience with backgroundworker but understands that it cannot 'cross-thread' access UI components in;  ie windows form application . would that be the same case here in chart.  maybe drawtext or something . or can i start a new thread (thread pooling ?) or is there anything equivalent that i can try out thats built in 

 

 

if you may give me some advice on this short code i write to test it out myself the isBusy part of backgroundworker: i set mousedown event, it appears that the indicator stops working after a few clicks

 

 

 

 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.ComponentModel;


namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewIndicator : Indicator
    {
        private System.ComponentModel.BackgroundWorker backgroundWorker1;

        protected override void Initialize()
        {
            while (Bars.Count <= 2000)
                Bars.LoadMoreHistory();

            backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(backgroundWorker1_DoWork);

            This_Timer = Timer;
            This_Timer.Start(1);
            This_Timer.TimerTick += OnThis_TimerTimerTick;
            Chart.MouseDown += OnChartMouseDown;
        }

        void OnThis_TimerTimerTick()
        {
            Chart.DrawStaticText("Time", string.Format("{0}\n{1}", DateTime.Now, DateTime.Now.Second), VerticalAlignment.Center, HorizontalAlignment.Right, Color.Yellow);
        }

        private Timer This_Timer;

        void OnChartMouseDown(ChartMouseEventArgs obj)
        {
            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();
            else
                Chart.DrawStaticText("Busy", string.Format("Busy {0}", DateTime.Now), VerticalAlignment.Bottom, HorizontalAlignment.Center, Color.Lime);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= Bars.Count - 1; i++)
                Chart.DrawText(string.Format("{0}", i), string.Format("{0}{1}", i, DateTime.Now.Second), i, Bars.LowPrices[i], Color.White);
        }

        public override void Calculate(int index)
        {

        }
    }
}

 

 

Thank you very much.


@1222Ht