Figured I could finally make a thread since I've tried 3 or 4 different ways to limit it now with varying success and complexity. Built this bad boy for my tester.mq4 EA as a "Catch-all" for when building cheap tests.
The current issue is that it's spawning 11 trades before the first one can confirm, but to my knowledge OrderSelect(i, SELECT_BY_POS) has an implicit (default) parameter called Pool which can check either the MODE_TRADES or MODE_HISTORY. MODE_TRADES `should` show all Open Positions and Pending Orders. Mode History is any Closed or Expired Positions/Orders.
I also have this function limited to once per candle using an isnewCandle(Symbol()) Function that works beautifully.
Inserted Code
void createTrade(const string symbol, int direction, double lot_size, int magicNumber, string tradeComment,
datetime expiration=0, color colour=clrNONE, int openBuys = 1, int openSells = 1)
{
int buyCount = 0; //How many Longs are open
int sellCount = 0; //How many Shorts are open
int count = OrdersTotal(); //Total amount of alive positions
for(int i = count; i > 0; i--) //Checks the internal order manager for open positions...
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderSymbol() == symbol && OrderMagicNumber() == magicNumber) // ^ From this bot...
{
if(OrderType() == ORDER_TYPE_BUY) buyCount++; // ^ and their type
if(OrderType() == ORDER_TYPE_SELL) sellCount++;
}
}
}
RefreshRates();
if(buyCount < openBuys && direction == 1) //If less orders are open than the limit, place a new order of the assigned type
{
if(OrderSend(symbol, OP_BUYSTOP, lot_size, Bid,3,0,0,tradeComment,magicNumber,expiration,colour)){} //if statement hides a compiler warning
}
if(sellCount < openSells && direction == -1)
{
if(OrderSend(symbol, OP_SELLLIMIT, lot_size, Ask,3,0,0,tradeComment,magicNumber,expiration,colour)){} //if statement hides a compiler warning\
}
} The current issue is that it's spawning 11 trades before the first one can confirm, but to my knowledge OrderSelect(i, SELECT_BY_POS) has an implicit (default) parameter called Pool which can check either the MODE_TRADES or MODE_HISTORY. MODE_TRADES `should` show all Open Positions and Pending Orders. Mode History is any Closed or Expired Positions/Orders.
I also have this function limited to once per candle using an isnewCandle(Symbol()) Function that works beautifully.
Inserted Code
bool isNewCandle(const string symbol)
{
datetime currCandle = iTime(symbol, 0, 0);
static datetime prevCandle = currCandle;
if(prevCandle < currCandle)
{
prevCandle = currCandle;
return(true);
}
else
{
return(false);
}
}