Your code could be simplified a lot. Also, there's no need to keep changing the name of the iterator variable in all of your loops. Typically (a good rule of thumb) 'i' is used as the iterator variable unless the loop is nested then inner loop names proceed as j,k,l, etc.
int CountByOrderType(int type) { int count=0; for(int i=OrdersTotal()-1;i>=0;i--) if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type) count++; return count; } bool CloseOrdersByType(int type) { bool res = true; for(int i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type) { if(type==OP_BUY) { if(!OrderClose(OrderTicket(),OrderLots(),Bid,10)) res = false; } else if(type==OP_SELL) { if(!OrderClose(OrderTicket(),OrderLots(),Ask,10)) res = false; } else { if(!OrderDelete(OrderTicket())) res = false; } } } return res; } void OnTick() { int buy_orders = CountByOrderType(OP_BUY); int buylimit_orders = CountByOrderType(OP_BUYLIMIT); if(buy_orders > 0 && buylimit_orders < 1) //open limit else if(buy_orders <=0) CloseOrdersByType(OP_BUYLIMIT); }
Your code could be simplified a lot. Also, there's no need to keep changing the name of the iterator variable in all of your loops. Typically (a good rule of thumb) 'i' is used as the iterator variable unless the loop is nested then inner loop names proceed as j,k,l, etc.
thanks for reply!
i tried to insert OrderSend(OP_BUY) also but now EA opens OP_BUY but don't open any Limit order
is there a mistake somewhere?
--- EDIT:
i rushed to reply, code is opening Buy Limits but not all instantly, some has few to more than 10 bars gap
void OnTick() { int buy_orders = CountByOrderType(OP_BUY); int buylimit_orders = CountByOrderType(OP_BUYLIMIT); if(buy_orders <= 0 /* && -OTHER CONDITIONS- */ ) { ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime); } if(buy_orders > 0 && buylimit_orders < 1) { //open limit ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,MAGIC,0,Lime); } else if(buy_orders <=0) CloseOrdersByType(OP_BUYLIMIT); }
Hello,
What could be wrong?
I made corrections but EA is not closing orders OP_BUY and OP_SELL
else if(buy_orders > 0 && RSI > 70) CloseOrdersByType(OP_BUY); else if(sell_orders > 0 && RSI < 30) CloseOrdersByType(OP_SELL);
Hello,
What could be wrong?
I made corrections but EA is not closing orders OP_BUY and OP_SELL:
else
if(buy_orders > 0 && RSI > 70)
CloseOrdersByType(OP_BUY);
else
if(sell_orders > 0 && RSI < 30)
CloseOrdersByType(OP_SELL);
General rules and best pratices of the Forum. - General - MQL5 programming forum
Nobody can help you unless you post all of your code. These micro-snippets tell us nothing. And also, have you stepped through your code in the debugger?
I have never used debugger :) i just test codes on demo accounts
posting whole code:
extern double MiniLots=0.01; extern double MaxLot=10; extern bool RiskControl = true; extern int MaxTrades=30; extern int SL=0; extern int TP=0; extern int BE=0; extern double MAGIC=20171206; double InitLots; int ticket; // ++++++++++++++++++++++++++++++++++++++ int init() { return(0); } // ++++++++++++++++++++++++++++++++++++++ int deinit() { return(0); } // ++++++++++++++++++++++++++++++++++++++ int CountByOrderType(int type) { int count=0; for(int i=OrdersTotal()-1;i>=0;i--) if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type) count++; return count; } bool CloseOrdersByType(int type) { bool res = true; for(int i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type) { if(type==OP_BUY) { if(!OrderClose(OrderTicket(),OrderLots(),Bid,10,Violet)) res = false; } else if(type==OP_SELL) { if(!OrderClose(OrderTicket(),OrderLots(),Ask,10,Violet)) res = false; } else { if(!OrderDelete(OrderTicket())) res = false; } } } return res; } void OnTick() { double PowerRisk; if(MaxLot > MarketInfo(Symbol(),MODE_MAXLOT)) MaxLot=MarketInfo(Symbol(),MODE_MAXLOT); if(InitLots < MarketInfo(Symbol(),MODE_MINLOT)) InitLots=MarketInfo(Symbol(),MODE_MINLOT); //cal Lot size if(RiskControl) { PowerRisk=AccountBalance()/5000; if(PowerRisk < 1) PowerRisk=1; InitLots = MiniLots *PowerRisk; if(InitLots > MaxLot) InitLots = MaxLot; } if(RiskControl==false) InitLots=MiniLots; int buy_orders = CountByOrderType(OP_BUY); int buylimit_orders = CountByOrderType(OP_BUYLIMIT); int sell_orders = CountByOrderType(OP_SELL); int selllimit_orders = CountByOrderType(OP_SELLLIMIT); if(buy_orders <= 0 /* && OTHER CONDITIONS */ ) { ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime); } else if(buy_orders > 0 && buylimit_orders < 1) { //open BUY limit ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,MAGIC,0,Lime); } else if(buy_orders <=0) CloseOrdersByType(OP_BUYLIMIT); else if(sell_orders <= 0 /* && OTHER CONDITIONS */ ) { ticket=OrderSend(Symbol(),OP_SELL,InitLots,Bid,10,SL,TP,0,MAGIC,0,Red); } else if(sell_orders > 0 && selllimit_orders < 1) { // open SELL limit ticket=OrderSend(Symbol(),OP_SELLLIMIT,InitLots,Bid+NormalizeDouble(atr,4),0,SL,TP,0,MAGIC,0,White); } else if(sell_orders <=0) CloseOrdersByType(OP_SELLLIMIT); else if(buy_orders > 0 && RSI > 70) CloseOrdersByType(OP_BUY); else if(sell_orders > 0 && RSI < 30) CloseOrdersByType(OP_SELL); return; }
I have never used debugger :) i just test codes on demo accounts
posting whole code:
extern double MiniLots=0.01;
extern double MaxLot=10;
extern bool RiskControl = true;
extern int MaxTrades=30;
extern int SL=0;
extern int TP=0;
extern int BE=0;
extern double MAGIC=20171206;
double InitLots;
int ticket;
// ++++++++++++++++++++++++++++++++++++++
int init()
{ return(0); }
// ++++++++++++++++++++++++++++++++++++++
int deinit()
{ return(0); }
// ++++++++++++++++++++++++++++++++++++++
int CountByOrderType(int type)
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type)
count++;
return count;
}
bool CloseOrdersByType(int type)
{
bool res = true;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type)
{
if(type==OP_BUY)
{ if(!OrderClose(OrderTicket(),OrderLots(),Bid,10,Violet))
res = false;
}
else
if(type==OP_SELL)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,10,Violet))
res = false;
}
else
{
if(!OrderDelete(OrderTicket()))
res = false;
}
}
}
return res;
}
void OnTick()
{
double PowerRisk;
if(MaxLot > MarketInfo(Symbol(),MODE_MAXLOT)) MaxLot=MarketInfo(Symbol(),MODE_MAXLOT);
if(InitLots < MarketInfo(Symbol(),MODE_MINLOT)) InitLots=MarketInfo(Symbol(),MODE_MINLOT);
//cal Lot size
if(RiskControl) {
PowerRisk=AccountBalance()/5000;
if(PowerRisk < 1) PowerRisk=1;
InitLots = MiniLots *PowerRisk;
if(InitLots > MaxLot) InitLots = MaxLot;
}
if(RiskControl==false) InitLots=MiniLots;
int buy_orders = CountByOrderType(OP_BUY);
int buylimit_orders = CountByOrderType(OP_BUYLIMIT);
int sell_orders = CountByOrderType(OP_SELL);
int selllimit_orders = CountByOrderType(OP_SELLLIMIT);
if(buy_orders <= 0 /* && OTHER CONDITIONS */ )
{ ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime); }
else
if(buy_orders > 0 && buylimit_orders < 1)
{ //open BUY limit
ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,MAGIC,0,Lime); }
else
if(buy_orders <=0)
CloseOrdersByType(OP_BUYLIMIT);
else
if(sell_orders <= 0 /* && OTHER CONDITIONS */ )
{ ticket=OrderSend(Symbol(),OP_SELL,InitLots,Bid,10,SL,TP,0,MAGIC,0,Red); }
else
if(sell_orders > 0 && selllimit_orders < 1)
{ // open SELL limit
ticket=OrderSend(Symbol(),OP_SELLLIMIT,InitLots,Bid+NormalizeDouble(atr,4),0,SL,TP,0,MAGIC,0,White); }
else
if(sell_orders <=0)
CloseOrdersByType(OP_SELLLIMIT);
else
if(buy_orders > 0 && RSI > 70)
CloseOrdersByType(OP_BUY);
else
if(sell_orders > 0 && RSI < 30)
CloseOrdersByType(OP_SELL);
return;
}
{ ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime); } ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,MAGIC,0,Lime); } { ticket=OrderSend(Symbol(),OP_SELL,InitLots,Bid,10,SL,TP,0,MAGIC,0,Red); } ticket=OrderSend(Symbol(),OP_SELLLIMIT,InitLots,Bid+NormalizeDouble(atr,4),0,SL,TP,0,MAGIC,0,White); }Check your return codes for errors and report them.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Check your return codes for errors and report them.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
I made this code:
if(buy_orders <= 0 /* && -OTHER CONDITIONS- */ ) { ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime); } if( ticket > 0 ) { Print("Order placed # ", ticket);} else { Print("Order Send failed, error # ", GetLastError() ); }
and i get error every second i.e. "EURUSD,M5: OrderSend failed, error # 0"
currently i have made EA to open sell, buy and sell_limit, buy_limit orders and close on conditions
next i will learn more about debugging, we'll see if code has some bugs

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
EA code is to open Buy Limit order if Open Buy Orders > 0.And close any Limit order when Open Buy Orders = 0
It does but it closes it after seconds and repeats this
How could we fix this? Thanks in advance!
Code: