# Perhaps my logic is flawed

Guys please take a look at this.

This line of code is supposed to count all the positions and return the number of open positions... It does so perfectly.

But then I added, if the number of positions is > 5 then don't open positions

stt = 5

The problem is that it just goes ahead to open multiple positions until I run out of money. What do you think? Perhaps my logic is flawed because I need it to open only 5 POSITIONS WHEN SET CONDITIONS ARE MET.

If you don't mind, please assist. Thank you!

```//... more codes above
for(int i =PositionsTotal()-1;i>=0;i--)
{
string symbols = PositionGetSymbol(i);
string pos_type = EnumToString(ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE)));

if ((_Symbol == symbols) && (pos_type == EnumToString(POSITION_TYPE_SELL)))
{pcount++;
{
if((p_close<p_open)&&(pcount<=stt))
}
}
}
```

Guys please take a look at this.

This line of code is supposed to count all the positions and return the number of open positions... It does so perfectly.

But then I added, if the number of positions is > 5 then don't open positions

stt = 5

The problem is that it just goes ahead to open multiple positions until I run out of money. What do you think? Perhaps my logic is flawed because I need it to open only 5 POSITIONS WHEN SET CONDITIONS ARE MET.

If you don't mind, please assist. Thank you!

This code is executed on each tick I guess ?

Alain Verleyen #:
This code is executed on each tick I guess ?
Yes you are correct. Should I use a different event handler? But I would need it to constantly check how many trades are open.

Yes you are correct. Should I use a different event handler? But I would need it to constantly check how many trades are open.

No, the problem is PositionTotal() isn't update directly when you open a new trade. There is a delay which can be longer than receiving new tick(s).

Discussed many times on the forum, please do a search about it.

Chioma Obunadike: But then I added, if the number of positions is > 5 then don't open positions

Guys please take a look at this.

This line of code is supposed to count all the positions and return the number of open positions... It does so perfectly.

But then I added, if the number of positions is > 5 then don't open positions

stt = 5

The problem is that it just goes ahead to open multiple positions until I run out of money. What do you think? Perhaps my logic is flawed because I need it to open only 5 POSITIONS WHEN SET CONDITIONS ARE MET.

If you don't mind, please assist. Thank you!

If the goal is OP (Open Position) as much as STT, why don't you simplify it by making a loop as much as STT?

like this:

`for(int i=stt-1; i>=0; i--)`

And leaves just one condition for OP:

` if((p_close<p_open) trade.Sell(lotsize,NULL,Bid,0,0,NULL); `

If there is no OP then you just check p_close and p_open value.

I hope this helps. Have a nice coding :-)

Nino Guevara Ruwano #:

If the goal is OP (Open Position) as much as STT, why don't you simplify it by making a loop as much as STT?

like this:

And leaves just one condition for OP:

If there is no OP then you just check p_close and p_open value.

I hope this helps. Have a nice coding :-)

Yes. This was the solution I finally used. Thank you.