Number of positions open at a time

Created at 24 Feb 2016, 16:21
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!
CR

croucrou

Joined 24.02.2016

Number of positions open at a time
24 Feb 2016, 16:21


Hi,

 

what command is there to use, to tell robot I want to have a number of maximum x positions open at one time for an account and for s

 

Also what happens when I use this command, while there have already been executing more than x positions. Would they be closed?

 

Thanks.

Regards.


@croucrou
Replies

Waxy
29 Feb 2016, 20:05

Hello,

You should try:

        int MaxNumber = 10;

        protected override void OnStart()
        {

            if (Positions.Count > MaxNumber)
            {
                foreach (var pos in Positions)
                {
                    ClosePosition(pos);
                }
            }
        }

 


@Waxy

croucrou
29 Feb 2016, 21:40

Yep, but this might still open hundreds of positions simoultanously and then close them, is that right?

 

I was hoping there is a way to set the limit of positions above which none new are allowed to open.


@croucrou

davidbtosh
10 Mar 2016, 14:22

Here is a quick example of how you might do this.  You need to put the cAlgo API  ExecuteMarketOrder() method in a "wrapper"

You then only ever call the wrapper method.  Here is my implementation of a wrapper method for this

 

        private const string botName = "MyBotName";  //these could be parameters
        private const int positionsLimit = 3;

        private TradeResult ExecuteMarketOrderLimited(TradeType tt, long vol, double? slPips, double? tpPips)
        {
            TradeResult result = null;

            try
            {                             
                List<Position> posList = Positions.FindAll(botName).ToList();
                Position pos = null;

                if (posList.Count < positionsLimit)
                {
                    result = ExecuteMarketOrder(tt, Symbol, vol, botName, slPips, tpPips);

                    if (result.IsSuccessful)
                    {
                        pos = result.Position;
                        Print("Position entry price is {0}", pos.EntryPrice);
                    }
                    else
                    {
                        Print("Execution unsuccessful.");
                    }
                }
                else
                {
                    Print("Set position limit of {0} reached.  Execution unsuccesful.", positionsLimit);
                }
            }
            catch (Exception e)
            { 
                //handle error
                //log
                //exit
                Print("Error: {0}", e.Message);
            }

            return result;
        }


@davidbtosh

davidbtosh
10 Mar 2016, 14:24

RE:

This looks better

private const string botName = "MyBotName";  //these could be parameters
        private const int positionsLimit = 3;

        private TradeResult ExecuteMarketOrderLimited(TradeType tt, long vol, 
                                                     double? slPips, double? tpPips)
        {
            TradeResult result = null;
            try
            {                             
                List posList = Positions.FindAll(botName).ToList();
                Position pos = null;

                if (posList.Count < positionsLimit)
                {
                    result = ExecuteMarketOrder(tt, Symbol, vol, botName, slPips, tpPips);

                    if (result.IsSuccessful)
                    {
                        pos = result.Position;
                        Print("Position entry price is {0}", pos.EntryPrice);
                    }
                    else
                    {
                        Print("Execution unsuccessful.");
                    }
                }
                else
                {
                    Print("Set position limit of {0} reached.  Execution unsuccesful.", positionsLimit);
                }
            }
            catch (Exception e)
            { 
                //handle error
                //log
                //exit
                Print("Error: {0}", e.Message);
            }

            return result;
        }

 


@davidbtosh

croucrou
20 Mar 2016, 14:04

Thanks for your reply. I regret it is not possible to do within just one line.


@croucrou

1007601
28 Mar 2016, 13:58

RE:

croucrou said:

Thanks for your reply. I regret it is not possible to do within just one line.

I try to have a "bool" condition that matches the conditions for entry...

ie.

if (c1 & c2 & c3 & c4 ... & c6)

{

do that thing!

}

 

; so one of the "bool" conditions could be

bool c5 = positions.Count < 100;

 

So that would mean all the conditions need to be met, ONE of them being the positions.count is less than your value;  in this case I said 100.  

I'm never sure if it looks clumsier in script or not; its functional for me... 

GH

 


@1007601