DrawStaticText not displaying immediately

Created at 24 May 2023, 14:27
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!
CO

couscousmckoy

Joined 07.02.2018

DrawStaticText not displaying immediately
24 May 2023, 14:27


Good morning

I'm developing a Cbot that calls a bunch of stuff during OnStart that can take 20 seconds to complete.  I wanted to put a warning on the screen while this process was occurring.  I tried using  Chart.DrawStaticText (as the first command in OnStart) - but for some reason it displays only after OnStart has completed.  Do I need to add repaint command or something?

Thanks

  


@couscousmckoy
Replies

firemyst
25 May 2023, 03:27

No, you shouldn't have to do that.

If you're running multiple instances of your bot and they all take 20 seconds to complete, what could be happening is cTrader and the CPU are going back/forth between bot threads and not getting to it right away. If you are running multiple instances, instead try running 2 or 3 sessions of cTrader with only a few instances each.

If you're just running the one bot instance, are you able to provide any sample code that can reproduce the behavior? For all we know, you might have code that erases chart objects, and then redraws later, or something else.


@firemyst

couscousmckoy
25 May 2023, 11:53

Thanks firemyst

Its just a simple one bot instance.

This example exhibits the same behaviour. My question:  why does the static text show after the fake pause and not before it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class DrawStaticTextExample : Robot
    {

        protected override void OnStart()
        {
            Print("Starting long job");
            Chart.DrawStaticText("LongJobWarning", "Warning: Long Job. Wait until this message disappears!!", VerticalAlignment.Center, HorizontalAlignment.Center,Color.Red);
            
            var WakeUpTime = Server.Time.AddSeconds(10);
            while (WakeUpTime > Server.Time)
            {
            //waste some time
            }
          
            Print("Finished Long Job");
        }
    }
}

 


@couscousmckoy

pick
25 May 2023, 13:50

What sort of tasks are you performing during this time consuming method? Can they be ran on another thread? Ideally I wouldn't want to block the main thread for so long. Below is an example of running the task on another thread:

protected override void Initialize()
{
	Print("Starting long job");
	Chart.DrawStaticText("LongJobWarning", "Warning: Long Job. Wait until this message disappears!!", VerticalAlignment.Center, HorizontalAlignment.Center, Color.Red);
	Task.Run(doLongJob);
}

async Task doLongJob()
{
	await Task.Delay(10000); //10 second delay

	BeginInvokeOnMainThread(() =>
	{
		Print("Finished Long Job");
		Chart.RemoveObject("LongJobWarning");
	});
}

 


@pick

couscousmckoy
25 May 2023, 16:09

Thanks Pick - I'll give that a try. Part of the problem was length of time it took to scroll through all historical bars to colour code certain bars.  I've managed to irradicate this half of the issue by only updating bar colour on visible bars. Still leaves the 10seconds it takes to calculate which bars should be colour coded and unfortunately to work that out it has to work through a lot of bars from the start.  I'll give threads a go though. Thank you for the help.

couscous


@couscousmckoy

pick
25 May 2023, 16:24

RE:

couscousmckoy said:

Thanks Pick - I'll give that a try. Part of the problem was length of time it took to scroll through all historical bars to colour code certain bars.  I've managed to irradicate this half of the issue by only updating bar colour on visible bars. Still leaves the 10seconds it takes to calculate which bars should be colour coded and unfortunately to work that out it has to work through a lot of bars from the start.  I'll give threads a go though. Thank you for the help.

couscous

No worries - hopefully that can provide some benefit. Ten seconds just for the visible bars still sounds like a rather long time to compute. If you need any further help reducing this time, please could you elaborate more on what conditions you're colouring the bars with.


@pick