Reduce time to close all position

12 Jun 2018, 17:49Reduce time to close all position#1
thongbaogiaodichposts: 10since: 19 May 2018

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!

12 Jun 2018, 17:55#2
Panagiotis Charalampousposts: 1003since: 13 Jan 2017

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


Head of Community Management at cTrader
12 Jun 2018, 18:02#3
thongbaogiaodichposts: 10since: 19 May 2018

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?

13 Jun 2018, 09:10#4
Panagiotis Charalampousposts: 1003since: 13 Jan 2017

Hi thongbaogiaodich,

Could you please post the complete cBot code?

Best Regards,

Panagiotis


Head of Community Management at cTrader