Limit robot to open 5 positions max (not at same time)

Created at 23 Jun 2022, 16:44
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!
MA

Marin0ss

Joined 07.06.2014

Limit robot to open 5 positions max (not at same time)
23 Jun 2022, 16:44


Hi All,

I would like some guidance, or perhaps the solution to limit a robot to open max 5 times a position. I have been working with the bool function, however this is true/false. I would like to incoorporate a count function.

Start with tradecount = 0.

Add logic to first position opening to open when tradecount is 0 and off course add 1 to the tradecount after the position is opened,

so the robot can open a second position only if tradecount = 1.

The tradecount needs to be stored; else on every bar it will be deleted again.

Any ideas? Thanks!


@Marin0ss
Replies

Marin0ss
23 Jun 2022, 22:13

RE:

So another solution which would work for me is a snippet of code that stops the robot after the last 5 trades closed negative.

 


@Marin0ss

firemyst
24 Jun 2022, 05:35

RE: RE:

Marin0ss said:

So another solution which would work for me is a snippet of code that stops the robot after the last 5 trades closed negative.

 

Here's a snippet to help you get started.

This assumes you've created an event method when a position is closed called "Positions_Closed":

 

private void Positions_Closed(PositionClosedEventArgs args)
{
	Position p1 = args.Position;
//in case there are multiple, you can narrow it down by symbol name and label
	if (p1.SymbolName == Symbol.Name && p1.Label == _positionLabel)
	{
		//Consecutive Losing Trades
		if (p1.NetProfit < 0)
			_numberOfConsecutiveLosingTrades += 1;
		else
			_numberOfConsecutiveLosingTrades = 0;
	}
//
// blah blah blah with other stuff you may need to do
//
// then stop the bot if it's greater than your threshold
    if (_numberOfConsecutiveLosingTrades >= 5)
        Stop();
}

 


@firemyst

Marin0ss
24 Jun 2022, 09:25

RE: RE: RE:

firemyst said:

Marin0ss said:

So another solution which would work for me is a snippet of code that stops the robot after the last 5 trades closed negative.

 

Here's a snippet to help you get started.

This assumes you've created an event method when a position is closed called "Positions_Closed":

 

private void Positions_Closed(PositionClosedEventArgs args)
{
	Position p1 = args.Position;
//in case there are multiple, you can narrow it down by symbol name and label
	if (p1.SymbolName == Symbol.Name && p1.Label == _positionLabel)
	{
		//Consecutive Losing Trades
		if (p1.NetProfit < 0)
			_numberOfConsecutiveLosingTrades += 1;
		else
			_numberOfConsecutiveLosingTrades = 0;
	}
//
// blah blah blah with other stuff you may need to do
//
// then stop the bot if it's greater than your threshold
    if (_numberOfConsecutiveLosingTrades >= 5)
        Stop();
}

 

Thanks a lot!! Got it working.


@Marin0ss