if(OrdersPerSymbol > 0) { i = total - 1; mmagic = 0; while(mmagic != magic && i >= 0) { result = OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(!result) return(0); if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic) { mmagic = magic; if(OrderType() == OP_BUY) { result = OrderClose(OrderTicket(),OrderLots(), Bid,3,Violet); if(!result) return(0); } else { return(0); } } }I is never changed so this results in a infinite loop. The OrdersPerSymbol is unnecessary, likewise your mmagic variable
// if(OrdersPerSymbol > 0) // { // i = total - 1; // mmagic = 0; // while(mmagic != magic && i >= 0) for(i=OrdersTotal()-1; i>=0; i--) if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == magic) { if(OrderType() == OP_BUY) { if (!OrderClose(OrderTicket(),OrderLots(), Bid,3,Violet) ) return(0); } else return(0); }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
For each index I use a different EA and different magicnumber.
The orders open correctly but afterwards all EA's exept 1 stop working.
I do not find out why and ask for help what to change and where I could find answer regarding my problem.
The code is as follows: (kindly amended by an EA posted in mql4.com earlier)
Any help would be very appreciate. I am sitting for days on it and not able to solve the problem.
//+------------------------------------------------------------------+
//| StochcrossEA.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"
extern double lots = 0.1;
extern int RSI_setting = 14;
extern int upline = 52;
extern int dnline = 48;
int magic = 1000;
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
int start()
{
double rsinya;
int total, mmagic, ticket;
int OrdersPerSymbol=0;
int i=0;
bool result, opbuy, opsell;
rsinya = iCustom(NULL, 0, "RSI",RSI_setting,0,1);
if(rsinya > upline)
{
opbuy = true;
total = OrdersTotal();
OrdersPerSymbol=0;
for(i=OrdersTotal();i>=0;i--)
{
result = OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol()==Symbol())
{
OrdersPerSymbol++;
}
}
if(OrdersPerSymbol > 0)
{
i = total - 1;
mmagic = 0;
while(mmagic != magic && i >= 0)
{
result = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(!result) return(0);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
{
mmagic = magic;
if(OrderType() == OP_SELL)
{
result = OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
if(!result) return(0);
}
else
{
return(0);
}
}
}
}
ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"EA",magic,0,Green);
}
else
{
if(rsinya < dnline)
{
opsell = true;
total = OrdersTotal();
OrdersPerSymbol=0;
for(i=OrdersTotal();i>=0;i--)
{
result = OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol()==Symbol())
{
OrdersPerSymbol++;
}
}
if(OrdersPerSymbol > 0)
{
i = total - 1;
mmagic = 0;
while(mmagic != magic && i >= 0)
{
result = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(!result) return(0);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
{
mmagic = magic;
if(OrderType() == OP_BUY)
{
result = OrderClose(OrderTicket(),OrderLots(),
Bid,3,Violet);
if(!result) return(0);
}
else
{
return(0);
}
}
}
}
ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,3,0,0,"EA",magic,0,Blue);
}
}
return(0);
}
//+------------------------------------------------------------------+