well this construction will lead to diaster,you have to use function which determine how many open position current ea keeps on current symbol; examples;
//---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Finds All open positions from magic number and current symbol
int totalopenpos(int m)
{
int cnt=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS)==false) continue;
if (OrderMagicNumber()==m && OrderSymbol()==Symbol() && (OrderType()==OP_BUY || OrderType()==OP_SELL)) { cnt++;}
}
return (cnt);
}
//---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
you may call like
int currentopen = totalopenpos(mymagic);
if (currentopen<2)
{
}
also can use similar functions like
//---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Finds market and pending positions from indicated magic
int totalpos(int m)
{
int cnt=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS)==false) continue;
if (OrderMagicNumber()==m && OrderSymbol()==Symbol()) { cnt++;}
}
return (cnt);
}
You HAVE TO WORK with MAGIC and Symbol() in every EA
well this construction will lead to diaster,you have to use function which determine how many open position current ea keeps on current symbol; examples;
//---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Finds All open positions from magic number and current symbol
int totalopenpos(int m)
{
int cnt=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS)==false) continue;
if (OrderMagicNumber()==m && OrderSymbol()==Symbol() && (OrderType()==OP_BUY || OrderType()==OP_SELL)) { cnt++;}
}
return (cnt);
}
//---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
you may call like
int currentopen = totalopenpos(mymagic);
if (currentopen<2)
{
}
also can use similar functions like
//---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Finds market and pending positions from indicated magic
int totalpos(int m)
{
int cnt=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS)==false) continue;
if (OrderMagicNumber()==m && OrderSymbol()==Symbol()) { cnt++;}
}
return (cnt);
}
You HAVE TO WORK with MAGIC and Symbol() in every EAt
thank you for yoru reply,but i m so sorry,i still don't understand .i m new with ea program,i still got a lot of thing need to learn.my program as following :
totalopen=OrdersTotal();
if (totalopen<2)
{
if((MacdPPrevious-SignalPPrevious)<0 && diff>dea)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,ask,3,0,0,"try2 sample",999,0,Green);
if(ticket<0)
{RefreshRates();}
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if((MacdPPrevious-SignalPPrevious)0 && diff<dea)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,bid,3,0,0,"try2 sample",999,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt<totalopen;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
if((diffpprevious-deapprevious)<0 && diff<dea )
{
OrderClose(OrderTicket(),OrderLots(),bid,3,Violet);
Print("30mins buy cross",OrderClosePrice());
return(0); // exit
}
if((MacdPPrevious-SignalPPrevious)>0 && diff>dea &&(TimeCurrent()-OrderOpenTime())>300)
{
if( OrderStopLoss()!=NormalizeDouble(open-modifysl*point,digits)&& OrderTakeProfit()!=NormalizeDouble(open+modifytp*point,digits))
}
}
else
{
if(OrderType()==OP_SELL)
{
if((diffpprevious-deapprevious)*(diffprevious-deaprevious)<0 && diff>dea &&(TimeCurrent()-OrderOpenTime())>300)
{
OrderClose(OrderTicket(),OrderLots(),ask,3,Violet);
Print("30mins sell cross",OrderClosePrice());
return(0); // exit
}
if((MacdPPrevious-SignalPPrevious)<0 && diff<dea &&(TimeCurrent()-OrderOpenTime())>300)
{
if( OrderStopLoss()!=NormalizeDouble(open+modifysl*point,digits )
{
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(open+modifysl*point,digits),NormalizeDouble(open-modifytp*point,digits),0,Green);
}
}
}
return(0);
} .......... pls,i really need your help.thank you very much .
He answered you question, Now read it and learn. He counted orders by magic number.
Don't post your code twice. We're not going to do YOUR coding for free.
thank you for yoru reply,but i m so sorry,i still don't understand .i m new with ea program,i still got a lot of thing need to learn.my program as following :
totalopen=OrdersTotal();
if (totalopen<2)
{
if((MacdPPrevious-SignalPPrevious)<0 && diff>dea)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,ask,3,0,0,"try2 sample",999,0,Green);
if(ticket<0)
{RefreshRates();}
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if((MacdPPrevious-SignalPPrevious)0 && diff<dea)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,bid,3,0,0,"try2 sample",999,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt<totalopen;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
if((diffpprevious-deapprevious)<0 && diff<dea )
{
OrderClose(OrderTicket(),OrderLots(),bid,3,Violet);
Print("30mins buy cross",OrderClosePrice());
return(0); // exit
}
if((MacdPPrevious-SignalPPrevious)>0 && diff>dea &&(TimeCurrent()-OrderOpenTime())>300)
{
if( OrderStopLoss()!=NormalizeDouble(open-modifysl*point,digits)&& OrderTakeProfit()!=NormalizeDouble(open+modifytp*point,digits))
}
}
else
{
if(OrderType()==OP_SELL)
{
if((diffpprevious-deapprevious)*(diffprevious-deaprevious)<0 && diff>dea &&(TimeCurrent()-OrderOpenTime())>300)
{
OrderClose(OrderTicket(),OrderLots(),ask,3,Violet);
Print("30mins sell cross",OrderClosePrice());
return(0); // exit
}
if((MacdPPrevious-SignalPPrevious)<0 && diff<dea &&(TimeCurrent()-OrderOpenTime())>300)
{
if( OrderStopLoss()!=NormalizeDouble(open+modifysl*point,digits )
{
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(open+modifysl*point,digits),NormalizeDouble(open-modifytp*point,digits),0,Green);
}
}
}
return(0);
} .......... pls,i really need your help.thank you very much .
Put it simple, the answer was provided, in this code
if(OrderSelect(i,SELECT_BY_POS)==false) continue; if (OrderMagicNumber()==m && OrderSymbol()==Symbol())
You can do the OrderTotal, then limit the listing by doing the select statement and then controlling each EA with a magic number, the magic number is a global variable that you set and it makes it unique for each chart. So on EURUSD, you might have a Magic Number called 1, on the other pair, a unique magic number of 2. With the if condition above, because it is only counting or selecting the orders that have m (magic number), the automation will not select or count any of the orders that do not belong to that magic number.
The other condition in the if statement is limiting the results based on their symbol. The combination of the two means you can actually have 2 GBPCHF's open and running at the same time, yet each one is set as unique according to the magic number and symbol combination.
So as you can see, he is right and gave some really very good code snippets to support it.
the headache I have found from using the function though is that you can perform a lot of various actions on the open orders, and when you box it into a function, it limits the data you can get out of it. For example the one above is designed to return the count of the selection, maybe you want to collect the profit on that specific selection, now you have to go and rewrite the function again. Pain in the back side.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
help!!! I am trying to test my program that will run two ea on two different pairs at one time. For example, ea1 run in eur/usd,another run in gbp/usd. i attach two ea to different chart,it's succesful,but there is another problem appeared. one ea continue open two orders,another ea doesn't work...
total=OrdersTotal();
if(total<2)
{
if((MacdPPrevious-SignalPPrevious)*(MacdPrevious-SignalPrevious)<0
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,ask,3,0,0,"try2 sample",10009,0,Green);
.......
as show,my two ea almost same in this part of program.i suspected that is the question.but i have no idea to solve this kind of problem.pls help if you have idea about this..thanks