need help! EA cannot close or trailing or lock profit when there is other floating trade

 

Need help! my EA working well when there is no other floating trade, but if there is any other floating order (manually make entry) and even not same pair with which pair I attached my EA, my EA only will open order but cannot close or trailing or lock profit, can help me to see what is wrong in my code?



int start()


{

int ticket, total, cnt;
int MagicNumberValue = 0;
double StopLossValue = 0;
double TakeProfitValue = 0;
int OrderOpenedTime = 0;

if (UseMagicNumber == true)
{
MagicNumberValue = MagicNumber;
}



total = OrdersCount();

if (total > 0)
{
for(cnt = 0; cnt < total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
OrderOpenedTime = TimeCurrent()-OrderOpenTime();

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumberValue)
{
Print("close post ready - check close - ", OrderOpenedTime);

if(OrderType()==OP_BUY)

{
if(CloseBuySignal() == true)
{
Print("check buy order");
// overcome requote
while (OrderCloseTime()==0)
{
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);
return(0);
}
}
// check for lock profit level 1
if (LockProfiT1 > 0) {
if (Bid - OrderOpenPrice() >= Point * Target1 && OrderStopLoss() < OrderOpenPrice() + Point * LockProfiT1) {
RefreshRates();
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + Point * LockProfiT1, OrderTakeProfit(), 0, Green);
return (0);
}
}

// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingAfterProfitPips)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
RefreshRates();
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
if(CloseSellSignal() == true)
{
Print("check sell order");
while (OrderCloseTime()==0)
{
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet);
return(0);
}
}
// check for lock profit level 1
if (LockProfiT1 > 0) {
if (OrderOpenPrice() - Ask >= Point * Target1 && OrderStopLoss() > OrderOpenPrice() - Point * LockProfiT1 || OrderStopLoss() == 0.0) {
RefreshRates();
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - Point * LockProfiT1, OrderTakeProfit(), 0, Red);
return (0);
}
}
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingAfterProfitPips))
{
if(OrderStopLoss()>(Ask+Point*TrailingStop))
{
RefreshRates();
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}}
}
return (0);
}
}


return (0);
}
 

> can help me to see what is wrong in my code?

Not easily as it is not all there...

Problem could be in the OrdersCount() function (not seen) &/or the use of 0 as a MagicNumber

Manual trades will have a MagicNumber of 0

-BB-

 

  1. total = OrdersCount();
    
    if (total > 0)
    {
    for(cnt = 0; cnt < total; cnt++)
    
    This makes the EA incompatable with any other EA including itself on other charts. You MUST count down when closing
       for( int pos = OrdersTotal() - 1; pos >= 0; pos -- ) if(
            OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        &&  OrderType()<=OP_SELL // Needed if EA opens pending orders
        ){
    

  2. // overcome requote
    while (OrderCloseTime()==0)
    {
    RefreshRates();
    OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);
    return(0);
    }
    
    This doesn't over come anything. OCT will always be zero or it wouldn't have been selected in the for loop. Always check return codes
    if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Violet))
       Alert("OrderClose(",OrderTicket(),") failed: ",GetLastError();

  3. if(lotstep==1) lotPrecision=0;
    if(lotstep==0.1) lotPrecision=1;
    if(lotstep==0.01) lotPrecision=2;
    if(lotstep==0.001) lotPrecision=3;
    ..
    lot=NormalizeDouble(AccountBalance()/10000,lotPrecision)*MaximumRisk;
    This code breaks except for those 4 exact values.
        double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
                lotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
                lot=AccountBalance()/10000*MaximumRisk;
         lot = MathFloor(lot/lotStep)*lotStep;
         if(lot>Maxlot)lot=Maxlot;
         if(lot<minLot)...

  4. int OrdersCount() doesn't filter on magic number
  5. EAs should adjust for 5 digit brokers (TP, SL, AND slippage) On ECN brokers you must open the order and THEN set tp/sl
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

 
if(OrdersCount()>0){
    for (int pos = OrdersTotal() - 1; pos >= 0; pos--){ 
         if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) 
         if(OrderMagicNumber()==MagicNumberValue){


I use this, look like working till now, can you explain more detail for item 4?

Reason: