Force output of DrawStaticText

Created at 30 Aug 2021, 11:13
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!
CT

ctid2032775

Joined 03.05.2020

Force output of DrawStaticText
30 Aug 2021, 11:13


Dear all,

maybe I am doing something wrong and someone of you can point me into the right direction...

My cBot runs an integrated optimization process OnStart and on specific circumstances (e. g. MDD increasing) that takes a lot of CPU and memory - i. e. quite "resource hungry". After this process is successfully finished the cBot is switched into "Trading" mode and starts trading OnBar.

At the beginning of the optimization process I want to print a static text (e. g. "Optimizing - EURUSD") and before start trading I want to print another static text (e. g. "Trading - EURUSD - Par1 = xxx - Par2 = yyy"; Par1 and Par2 are the results of the optimization) to the chart.

The small "beauty mistake" now is that the optimizing text is not output to the chart - is there a way to "force" the output even while the cBot is quite busy?

Many thanks in advance and best regards,
Christian


@ctid2032775
Replies

firemyst
30 Aug 2021, 17:29

RE:

ctid2032775 said:

Dear all,

maybe I am doing something wrong and someone of you can point me into the right direction...

My cBot runs an integrated optimization process OnStart and on specific circumstances (e. g. MDD increasing) that takes a lot of CPU and memory - i. e. quite "resource hungry". After this process is successfully finished the cBot is switched into "Trading" mode and starts trading OnBar.

At the beginning of the optimization process I want to print a static text (e. g. "Optimizing - EURUSD") and before start trading I want to print another static text (e. g. "Trading - EURUSD - Par1 = xxx - Par2 = yyy"; Par1 and Par2 are the results of the optimization) to the chart.

The small "beauty mistake" now is that the optimizing text is not output to the chart - is there a way to "force" the output even while the cBot is quite busy?

Many thanks in advance and best regards,
Christian

I would write the information out to the log because not all text/drawings on the chart are supported during back-testing as far as I'm aware.

Eg, if (IsBacktesting) { ... print your text to the log... } else { drawstatictext on chart }


@firemyst

ctid2032775
31 Aug 2021, 09:49

RE: RE:

firemyst said:

ctid2032775 said:

Dear all,

maybe I am doing something wrong and someone of you can point me into the right direction...

My cBot runs an integrated optimization process OnStart and on specific circumstances (e. g. MDD increasing) that takes a lot of CPU and memory - i. e. quite "resource hungry". After this process is successfully finished the cBot is switched into "Trading" mode and starts trading OnBar.

At the beginning of the optimization process I want to print a static text (e. g. "Optimizing - EURUSD") and before start trading I want to print another static text (e. g. "Trading - EURUSD - Par1 = xxx - Par2 = yyy"; Par1 and Par2 are the results of the optimization) to the chart.

The small "beauty mistake" now is that the optimizing text is not output to the chart - is there a way to "force" the output even while the cBot is quite busy?

Many thanks in advance and best regards,
Christian

I would write the information out to the log because not all text/drawings on the chart are supported during back-testing as far as I'm aware.

Eg, if (IsBacktesting) { ... print your text to the log... } else { drawstatictext on chart }

First of all many thanks for your reply!

As mentioned in my original post this cBot is running an "integrated" optimization process and not the "built-in" one (maybe this was a little bit misleading). This is done by looping through the parameters (e. g. Par1, Par2), simulating the trades and tracking the profit to find the optimal combination of Par1 and Par2 (i. e. maximum profit).

The reason for this procedure is that it's a dynamic process and not just executed before the cBot is started (afaik the "built-in" optimization cannot be triggered from the cBot itself). E. g. when MDD exceeds some specific threshold the trading is stopped, the optimization process is started again and then the trading is started with the "new" combination of Par1 and Par2 - but all of this is automatically executed by the cBot itself without any user interaction.

Therefore, as the cBot is always running in RunningMode = RealTime (see above) IsBacktesting would always return FALSE...

BR,
Christian


@ctid2032775

PanagiotisCharalampous
31 Aug 2021, 10:30

Hi Christian,

All changes for drawings are accumulated when user's code is executed and the text is drawn after the code completes execution. There is no command to force update.

A workaround is to you can push some action to the queue so it will be executed later  in the following way.

 

  1. Call Chart.DrawStaticText
  2. Call BeginInvokeOnMainThread and pass an action as an argument that will start optimization work. This method will be called after the current method has finished.

See below an example

protected override void OnStart()
{
    Chart.DrawStaticText("status", "Preparing...", VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
    BeginInvokeOnMainThread(DoHeavyWork);
    // OnStart method will be finished and DoHeavyWork() will be called later
}

private void DoHeavyWork()
{
    var endTime = DateTime.Now.AddSeconds(4);
    while (DateTime.Now < endTime)
        System.Threading.Thread.Sleep(100);

    Chart.DrawStaticText("status", "Started", VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
}

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

ctid2032775
01 Sep 2021, 10:20

RE:

PanagiotisCharalampous said:

Hi Christian,

All changes for drawings are accumulated when user's code is executed and the text is drawn after the code completes execution. There is no command to force update.

A workaround is to you can push some action to the queue so it will be executed later  in the following way.

 

  1. Call Chart.DrawStaticText
  2. Call BeginInvokeOnMainThread and pass an action as an argument that will start optimization work. This method will be called after the current method has finished.

See below an example

protected override void OnStart()
{
    Chart.DrawStaticText("status", "Preparing...", VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
    BeginInvokeOnMainThread(DoHeavyWork);
    // OnStart method will be finished and DoHeavyWork() will be called later
}

private void DoHeavyWork()
{
    var endTime = DateTime.Now.AddSeconds(4);
    while (DateTime.Now < endTime)
        System.Threading.Thread.Sleep(100);

    Chart.DrawStaticText("status", "Started", VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
}

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Hi Panagiotis,

many thanks for your reply and great support - I'll give it a try...

Best regards,
Christian


@ctid2032775

ctid2032775
25 Sep 2021, 10:54

RE: RE:

ctid2032775 said:

PanagiotisCharalampous said:

Hi Christian,

All changes for drawings are accumulated when user's code is executed and the text is drawn after the code completes execution. There is no command to force update.

A workaround is to you can push some action to the queue so it will be executed later  in the following way.

 

  1. Call Chart.DrawStaticText
  2. Call BeginInvokeOnMainThread and pass an action as an argument that will start optimization work. This method will be called after the current method has finished.

See below an example

protected override void OnStart()
{
    Chart.DrawStaticText("status", "Preparing...", VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
    BeginInvokeOnMainThread(DoHeavyWork);
    // OnStart method will be finished and DoHeavyWork() will be called later
}

private void DoHeavyWork()
{
    var endTime = DateTime.Now.AddSeconds(4);
    while (DateTime.Now < endTime)
        System.Threading.Thread.Sleep(100);

    Chart.DrawStaticText("status", "Started", VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
}

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Hi Panagiotis,

many thanks for your reply and great support - I'll give it a try...

Best regards,
Christian

Hi Panagiotis,

sorry for my late reply - I tested this and it's working as expected.

Just one final question or more a confirmation of my observations:

  • When I run this cBot while market opening hours I get (just) one tick after DoHeavyWork is finally finished.
  • When I do the same during the weekend I get no ticks (but this is logical).

Does this mean that this technique suspends all ticks received while DoHeavyWork is running and then executes the last - i. e. current active - tick event?

If yes, this would exactly meet my expectations and work as required for the functionality of my cBot(s)...

Many thanks and regards,
Christian


@ctid2032775