Reduce time to close all position

Created at 12 Jun 2018, 17:49
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!
TH

thongbaogiaodich

Joined 19.05.2018

Reduce time to close all position
12 Jun 2018, 17:49


Dear cTrader,

I have code like this

if (Account.Equity <= Account.Balance * BalanceLossLevel)
{
Print(String.Format("Balance DROP -{0:0.0}% from $ {1} to $ {2}. Closing all buy position", (1 - (Account.Equity / Account.Balance)) * 100, Account.Balance, Account.Equity));
CloseAllPosition("Buy");
}
}

And the CloseAllPosition is

private void CloseAllPosition(string tradeDirection)
{
if (tradeDirection == "Buy")
{
var positions = Positions.FindAll(newsLabel, Symbol, TradeType.Buy);
for (int i = 1; i <= positions.Length; i++)
{
if (i != positions.Length)
ClosePositionAsync(positions[i - 1]);
else
ClosePosition(positions[i - 1]);
}}}

When I run robot, it ALWAYS takes 15ms from print out message to close first position (981 to 996)

I tried other code for CloseAllPosition as below

private void CloseAllPosition(string tradeDirection)
{
if (tradeDirection == "Buy")
{
int numPos = Positions.Count - 1;
                int i = 0;
                foreach (var pos in Positions)
                {
                    if (pos.TradeType == TradeType.Buy)
                    {
                        if (i != numPos)
                            ClosePositionAsync(pos);
                        else
                            ClosePosition(pos);
                    }
                    i++;
                }
}}

It still takes 15ms to excute close first position.

Could you please advice for me other way to close all position faster?

Thanks!


@thongbaogiaodich
Replies

PanagiotisCharalampous
12 Jun 2018, 17:55

Hi thongbaogiaodich,

Try skipping 

var positions = Positions.FindAll(newsLabel, Symbol, TradeType.Buy);

Just check if a position has TradeType.Buy before closing it.

Let me know if this helps,

Best Regards,

Panagiotis


@PanagiotisCharalampous

thongbaogiaodich
12 Jun 2018, 18:02

Dear Panagiotis Charalampous,

Do you mean I need to modify the code as the second code in my first post?

private void CloseAllPosition(string tradeDirection)
{
    if (tradeDirection == "Buy")
    {
        int numPos = Positions.Count - 1;
        int i = 0;
        foreach (var pos in Positions)
        {
            if (pos.TradeType == TradeType.Buy)
            {
                if (i != numPos)
                    ClosePositionAsync(pos);
                else
                    ClosePosition(pos);
            }
            i++;
            }
    }
}

I already tried this code but have same result, takes 15ms also. Do you have any suggestion?


@thongbaogiaodich

PanagiotisCharalampous
13 Jun 2018, 09:10

Hi thongbaogiaodich,

Could you please post the complete cBot code?

Best Regards,

Panagiotis


@PanagiotisCharalampous