Controlling number of trades

 

Has anyone got code i can use to control the number of trades and EA opens in a day.

Such as open just 3 trades per day and do not open anymore trades until the next trading day.

 
forextrend:
Has anyone got code i can use to control the number of trades and EA opens in a day. Such as open just 3 trades per day and do not open anymore trades until the next trading day.

Call this function :

int DailyCount()

{

int i, DailyCount = 0;

for(i = OrdersTotal()-1; i >= 0; i --)

{

if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != Magic) continue;

if(TimeDayOfYear(OrderOpenTime()) == DayOfYear()) DailyCount ++;

}

return(DailyCount);

}

 

Call the function where in the code.

 
forextrend:
Call the function where in the code.

It depends of your code. If you do not know, copy your code here.

 
Michel:
Call this function :

int DailyCount()

{

int i, DailyCount = 0;

for(i = OrdersTotal()-1; i >= 0; i --)

{

if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != Magic) continue;

if(TimeDayOfYear(OrderOpenTime()) == DayOfYear()) DailyCount ++;

}

return(DailyCount);

}

Very good and clean code! I like this function.

 

here:

// Closing of Open Orders

void OpenOrdClose()

{

int total=OrdersTotal();

for (int cnt=0;cnt<total;cnt++)

{

OrderSelect(total-cnt-1, SELECT_BY_POS);

int mode=OrderType();

bool res = false;

bool condition = false;

if (OrderMagicNumber()==Magic ) condition = true;

if (condition && ( mode==OP_BUY || mode==OP_SELL ))

{

// - BUY Orders

if(mode==OP_BUY)

{

res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Yellow);

if( !res )

{

Print(" BUY: OrderClose failed with error #",GetLastError());

Print(" Ticket=",OrderTicket());

Sleep(3000);

}

}

else

// - SELL Orders

if( mode == OP_SELL)

{

res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,White);

if( !res )

{

Print(" SELL: OrderClose failed with error #",GetLastError());

Print(" Ticket=",OrderTicket());

Sleep(3000);

}

}

}

}

}

void TotalProfit()

{

int total=OrdersTotal();

totalPips = 0;

totalProfits = 0;

for (int cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS);

int mode=OrderType();

bool condition = false;

if ( Magic>0 && OrderMagicNumber()==Magic ) condition = true;

else if ( Magic==0 ) condition = true;

if (condition)

{

switch (mode)

{

case OP_BUY:

totalPips += MathRound((MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT));

//totalPips += MathRound((Bid-OrderOpenPrice())/Point);

totalProfits += OrderProfit();

break;

case OP_SELL:

totalPips += MathRound((OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/MarketInfo(OrderSymbol(),MODE_POINT));

//totalPips += MathRound((OrderOpenPrice()-Ask)/Point);

totalProfits += OrderProfit();

break;

}

}

if (UseSwap) {

SwapProfit();

totalProfits = totalProfits + valueswap;

}

}

}

void sidePips()

{

int total=OrdersTotal();

shortPips = 0;

longPips = 0;

for (int cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS);

int mode=OrderType();

bool condition = false;

if ( Magic>0 && OrderMagicNumber()==Magic ) condition = true;

else if ( Magic==0 ) condition = true;

if (condition)

{

switch (mode)

{

case OP_BUY:

longPips += MathRound((MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT));

break;

case OP_SELL:

shortPips += MathRound((OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/MarketInfo(OrderSymbol(),MODE_POINT));

break;

}

}

}

}

 
forextrend:
here:

// Closing of Open Orders

void OpenOrdClose()

{

int total=OrdersTotal();

for (int cnt=0;cnt<total;cnt++)

{

OrderSelect(total-cnt-1, SELECT_BY_POS);

int mode=OrderType();

bool res = false;

bool condition = false;

if (OrderMagicNumber()==Magic ) condition = true;

if (condition && ( mode==OP_BUY || mode==OP_SELL ))

{

// - BUY Orders

if(mode==OP_BUY)

{

res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Yellow);

if( !res )

{

Print(" BUY: OrderClose failed with error #",GetLastError());

Print(" Ticket=",OrderTicket());

Sleep(3000);

}

}

else

// - SELL Orders

if( mode == OP_SELL)

{

res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,White);

if( !res )

{

Print(" SELL: OrderClose failed with error #",GetLastError());

Print(" Ticket=",OrderTicket());

Sleep(3000);

}

}

}

}

}

void TotalProfit()

{

int total=OrdersTotal();

totalPips = 0;

totalProfits = 0;

for (int cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS);

int mode=OrderType();

bool condition = false;

if ( Magic>0 && OrderMagicNumber()==Magic ) condition = true;

else if ( Magic==0 ) condition = true;

if (condition)

{

switch (mode)

{

case OP_BUY:

totalPips += MathRound((MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT));

//totalPips += MathRound((Bid-OrderOpenPrice())/Point);

totalProfits += OrderProfit();

break;

case OP_SELL:

totalPips += MathRound((OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/MarketInfo(OrderSymbol(),MODE_POINT));

//totalPips += MathRound((OrderOpenPrice()-Ask)/Point);

totalProfits += OrderProfit();

break;

}

}

if (UseSwap) {

SwapProfit();

totalProfits = totalProfits + valueswap;

}

}

}

void sidePips()

{

int total=OrdersTotal();

shortPips = 0;

longPips = 0;

for (int cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS);

int mode=OrderType();

bool condition = false;

if ( Magic>0 && OrderMagicNumber()==Magic ) condition = true;

else if ( Magic==0 ) condition = true;

if (condition)

{

switch (mode)

{

case OP_BUY:

longPips += MathRound((MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT));

break;

case OP_SELL:

shortPips += MathRound((OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/MarketInfo(OrderSymbol(),MODE_POINT));

break;

}

}

}

}

There is nowhere here the part of the code which eventualy open a position. That's there that the code has to be modified, so please past your whole code.

It will be more lisible if you enclose it in the "PHP" brackets, but you can also attach the mq4 file.

 
codersguru:
Very good and clean code! I like this function.

Thanks, Guru ! What an honour for me to have your approbation !

 

Attached is the whole EA i am using. Also plaese explain how to use your code in detail so i can use it with other EA's in the furture.

Files:
samuray_v7.mq4  24 kb
 
forextrend:
Attached is the whole EA i am using. Also plaese explain how to use your code in detail so i can use it with other EA's in the furture.

Here is the modified one, all modifications have "Michel" as comment so you can easily find them. I didn't try it, tell me if it works ok.

Files:
 

I have this situation:

MaxTrades=20, EA already opened 15 positions.

My total profit is -200 I don't want to open more trades.

Thanks for the codes.

B.

Reason: