Programmatically restarting a cBot

Created at 03 Nov 2020, 15:33
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

Programmatically restarting a cBot
03 Nov 2020, 15:33


Dear Panagiotis,

I know that there is a way to programmatically Stop() a cBot. But is there a way to "restart" a cBot, as well?

The reason of my question is that one of my cBots uses an automatic optimization method when started and a "drop-out" scenario under certain conditions. It would be great to just restart the cBot if those conditions are met instead of implement a second optimization logic for this case...

Many thanks and best regards,
Christian


@ctid2032775
Replies

PanagiotisCharalampous
03 Nov 2020, 15:59

Hi Christian,

There is no such option unfortunately.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

firemyst
05 Nov 2020, 14:20

RE:

ctid2032775 said:

Dear Panagiotis,

I know that there is a way to programmatically Stop() a cBot. But is there a way to "restart" a cBot, as well?

The reason of my question is that one of my cBots uses an automatic optimization method when started and a "drop-out" scenario under certain conditions. It would be great to just restart the cBot if those conditions are met instead of implement a second optimization logic for this case...

Many thanks and best regards,
Christian

Why "stop" and "Start" the bot?

I think a good work around in your case is to just set a global boolean flag.

Call it "bool _dropOut" and when the bot first starts, set it to "false".

In your OnTick and OnBar methods, check the flag before you do anything.

Eg:

protected override void OnTick()
{
    if (!_dropOut)
    {
       //do your normal ontick code logic
    }
    else
    {
        //check for the conditions to set _dropOut = false
    }
}

protected override void OnBar()
{
    if (!_dropOut)
    {
       //do your normal onbar code logic
    }
}

 

Your bot will always be running, it won't execute any OnTick or OnBar code unless set to false.

thus, no need to worry about stopping and restarting.

Easy peazy :-)

 


@firemyst