Stop() Asynchronous?

Created at 23 May 2024, 15:50
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!
CL

Clark22

Joined 17.05.2024

Stop() Asynchronous?
23 May 2024, 15:50


Hi,

 

The Robot.Stop() function in references is not listed as Async, but the next line of code in my cBot is getting executed. I'd like to avoid that if possible?

 

Cheers,

Clark


@Clark22
Replies

PanagiotisCharalampous
24 May 2024, 05:42

Hi there,

Can you share some code and instructions to reproduce this behavior?

Best regards,

Panagiotis


@PanagiotisCharalampous

Clark22
24 May 2024, 20:19 ( Updated at: 26 May 2024, 06:44 )

RE: Stop() Asynchronous?
PanagiotisCharalampous said:

Hi there,

Can you share some code and instructions to reproduce this behavior?

Best regards,

Panagiotis

 

Please see below for the Stop() (it uses an underlying Executor Service so it seems async and behaves as async as per the output). 
 

I am also interested to know how to handle the Async calls for executing orders. Are the return types a Promise/Future? I have issues with the bot prematurely meeting exit conditions when using those calls which only return after the bot has shutdown.

I don't want to await each of them immediately (making them synchronous), but rather check up on them later (before stopping).

 

Cheers,

Clark

 

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class StopTest : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
            
            Stop();
            Print(Message);
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

24/05/2024 20:08:30.031 | Info | CBot instance [StopTest, AUDCAD_SB, h1] started.
24/05/2024 20:08:30.094 | Info | Hello world!
24/05/2024 20:08:30.140 | Info | CBot instance [StopTest, AUDCAD_SB, h1] stopped.

@Clark22

Spotware
27 May 2024, 08:04

Hi there,

This is an expected behavior when programming in .Net. If you want to break an execution immediately, throw an exception.

Regarding the Promise/Future question, read below

https://stackoverflow.com/questions/70992438/how-to-control-c-sharp-task-async-await-in-same-way-as-javascript-promise

Best regards,

Panagiotis


@Spotware

Clark22
27 May 2024, 11:10 ( Updated at: 27 May 2024, 11:23 )

RE: Stop() Asynchronous?

Spotware said: 

Hi there,

This is an expected behavior when programming in .Net. If you want to break an execution immediately, throw an exception.

Regarding the Promise/Future question, read below

https://stackoverflow.com/questions/70992438/how-to-control-c-sharp-task-async-await-in-same-way-as-javascript-promise

Best regards,

Panagiotis

Hi Panagiotis,

 

Are you saying that the TradeResult is a Task type?

Also, regarding the Stop() method behaviour. It's not listed as Async even though it behaves as such.

 

Cheers,

Clark 


@Clark22

PanagiotisCharalampous
27 May 2024, 11:50

RE: RE: Stop() Asynchronous?

Clark22 said: 

Spotware said: 

Hi there,

This is an expected behavior when programming in .Net. If you want to break an execution immediately, throw an exception.

Regarding the Promise/Future question, read below

https://stackoverflow.com/questions/70992438/how-to-control-c-sharp-task-async-await-in-same-way-as-javascript-promise

Best regards,

Panagiotis

Hi Panagiotis,

 

Are you saying that the TradeResult is a Task type?

Also, regarding the Stop() method behaviour. It's not listed as Async even though it behaves as such.

 

Cheers,

Clark 

Hi Clark,

Are you saying that the TradeResult is a Task type?

TradeResult is a type on its own

Also, regarding the Stop() method behaviour. It's not listed as Async even though it behaves as such.

It's not Async, the execution is stopped after the calling method has been executed too. If you don't want to execute the code after the Stop() method, you need to rethink your implementation.

Best regards,

Panagiotis


@PanagiotisCharalampous

Clark22
27 May 2024, 15:57 ( Updated at: 28 May 2024, 05:39 )

RE: RE: RE: Stop() Asynchronous?

PanagiotisCharalampous said: 

Clark22 said: 

Spotware said: 

Hi there,

This is an expected behavior when programming in .Net. If you want to break an execution immediately, throw an exception.

Regarding the Promise/Future question, read below

https://stackoverflow.com/questions/70992438/how-to-control-c-sharp-task-async-await-in-same-way-as-javascript-promise

Best regards,

Panagiotis

Hi Panagiotis,

 

Are you saying that the TradeResult is a Task type?

Also, regarding the Stop() method behaviour. It's not listed as Async even though it behaves as such.

 

Cheers,

Clark 

Hi Clark,

Are you saying that the TradeResult is a Task type?

TradeResult is a type on its own

Also, regarding the Stop() method behaviour. It's not listed as Async even though it behaves as such.

It's not Async, the execution is stopped after the calling method has been executed too. If you don't want to execute the code after the Stop() method, you need to rethink your implementation.

Best regards,

Panagiotis

Understood. Thanks 👍 


@Clark22