Asynchronous closing of positions

Created at 29 Nov 2017, 11:48
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!
ST

Stokes Bay

Joined 18.04.2016

Asynchronous closing of positions
29 Nov 2017, 11:48


Can someone please advise how I amend the following code to close all positions at once using the asynchronous method?

Thanks.

 

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

namespace cAlgo.Robots
{
    [Robot()]
    public class PCAccountStophit : Robot
    {

        [Parameter(DefaultValue = "Account Stop Hit cBot")]
        public string cBotLabel { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }


        [Parameter("TargetBalance", DefaultValue = 10000)]
        public double TargetBalance { get; set; }


        protected override void OnStart()
        {

        }

        protected override void OnBar()
        {

            // Some condition to close all positions
            if (Account.Equity <= TargetBalance)
                foreach (var position in Positions)
                    ClosePosition(position);
        }



    }
}

 


@Stokes Bay
Replies

PanagiotisCharalampous
29 Nov 2017, 11:57

Dear Trader,

You can use the ClosePositionAsync method. See below

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
 
namespace cAlgo.Robots
{
    [Robot()]
    public class PCAccountStophit : Robot
    {
 
        [Parameter(DefaultValue = "Account Stop Hit cBot")]
        public string cBotLabel { get; set; }
 
        [Parameter()]
        public DataSeries SourceSeries { get; set; }
 
 
        [Parameter("TargetBalance", DefaultValue = 10000)]
        public double TargetBalance { get; set; }
 
 
        protected override void OnStart()
        {
 
        }
 
        protected override void OnBar()
        {
 
            // Some condition to close all positions
            if (Account.Equity <= TargetBalance)
                foreach (var position in Positions)
                    ClosePositionAsync(position);
        }
 
 
 
    }
}
 

Let me know if this helps you.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Stokes Bay
29 Nov 2017, 12:05

Perfect. Many thanks.


@Stokes Bay