Why this code is always closing orders? looking for a prompt:)

 
int start()
{
//----
int action = 0;
double loty = 1/100;
int course = Bid;
int lastOpenTime = 0, needleTicket = 0 ;
double point = Point;
double diference = 50*point;
bool result;
double price, Oprice;
int cmd, error;




for ( int i =   OrdersTotal () - 1   ; i >= 0 ; i -- ) 
{
if (OrderSelect ( i, SELECT_BY_POS, MODE_TRADES ))
{
int curOpenTime = OrderOpenTime () ;
if ( curOpenTime > lastOpenTime )
{
lastOpenTime = curOpenTime ; 
needleTicket = OrderTicket () ;
Oprice = OrderOpenPrice();
}



if ( course >= Oprice + diference )                                                     // this is the moment that it should check if the answer is positive and close
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,5,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
}
}
return (0);
}


I want it to close orders only when Bid is higher than last order open price + diference. But this is closing always. I don't know why....

looking for help:)

 

not logical. this is what your code does:

for ( int i =   OrdersTotal () - 1   ; i >= 0 ; i -- ) ----> you iterate from most recent to latest orders by virtue of order filling position --- (1)
{
if (OrderSelect ( i, SELECT_BY_POS, MODE_TRADES ))
{
int curOpenTime = OrderOpenTime () ;
if ( curOpenTime > lastOpenTime ) ---------------------------> by virtue of (1): this will always return FALSE ---(2)
{
lastOpenTime = curOpenTime ; 
needleTicket = OrderTicket () ;
Oprice = OrderOpenPrice();
}



if ( course >= Oprice + diference ) ------------> by virtue of (2): Oprice always remains 0.hence the eqn: course>=0+50*Point ------ always return true
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,5,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
 
int start()
{
//----
int action = 0;
double loty = 1/100;
int course = Bid ;
int lastOpenTime = 0, needleTicket = 0 ;
double point = MarketInfo(Symbol(),MODE_POINT);
double diference = 50*point;
bool result;
double price, Oprice;
int cmd, error;




for ( int i =   OrdersTotal () - 1   ; i >= 0 ; i -- ) 
{
if (OrderSelect ( i, SELECT_BY_POS, MODE_TRADES ))
{
int curOpenTime = OrderOpenTime () ;
Oprice = OrderOpenPrice();                                                  //I move here Oprice from the position below but nothing change.
if ( curOpenTime > lastOpenTime )
{
lastOpenTime = curOpenTime ; 
needleTicket = OrderTicket () ;

}



if ( course >= Oprice + diference) 
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,5,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
if (!OrderSelect ( i, SELECT_BY_POS, MODE_TRADES )
&& OrderOpenPrice () >= Oprice )
{
action=OrderSend(Symbol(),OP_SELL,loty,Bid,5,0,0,"sprzedaję",0,0,Red);
if(action<=0) Print("Error = ",GetLastError());
else { Print("action = ",action); break; }
//---- 1 seconds wait
Sleep(1000);
action=OrderSend(Symbol(),OP_BUY,loty,Ask,5,0,0,"kupuję",0,0,Blue);
if(action<=0) Print("Error = ",GetLastError());
else { Print("action = ",action); break; }
}
else
{
action=OrderSend(Symbol(),OP_SELL,loty,Bid,5,0,0,"sprzedaję",0,0,Red);
if(action<=0) Print("Error = ",GetLastError());
else { Print("action = ",action); break; }
}
//----
}
 
I thought that when I move up into selected order variable Oprice than it gonna be ok but now i think that it's still zero. It is still closing all orders.
 
BorysekPL:
I thought that when I move up into selected order variable Oprice than it gonna be ok but now i think that it's still zero. It is still closing all orders.
you are using fractional broker, no?
 
diostar:
you are using fractional broker, no?


Admiral Markets

They have microlots and for EUR/USD is now 1.3644

If that is what you are asking :) - I could missunderstanding

 

in the first place (from your original piece), shouldn't you close the "oPrice, etc, information" looping before processing further, as below:

for ( int i =   OrdersTotal () - 1   ; i >= 0 ; i -- ) 
{
if (OrderSelect ( i, SELECT_BY_POS, MODE_TRADES ))
{
int curOpenTime = OrderOpenTime () ;
if ( curOpenTime > lastOpenTime )
{
lastOpenTime = curOpenTime ; 
needleTicket = OrderTicket () ;
Oprice = OrderOpenPrice();
}

} <---- bracket to close this "information" loop, then proceed ONLY after confirmed that most recent order with its correct oPrice.



if ( course >= Oprice + diference )   
 

try this version (just cut and paste...dont change anything)

int start1()
{
//----
int action = 0;
double loty = 1/100;
int course = Bid;
int lastOpenTime = 0, needleTicket = 0 ;
double point = Point;
double diference = 50*point;
bool result;
double price, Oprice;
int cmd, error;




for ( int i =   OrdersTotal () - 1   ; i >= 0 ; i -- ) 
{
if (OrderSelect ( i, SELECT_BY_POS, MODE_TRADES ))
{
int curOpenTime = OrderOpenTime () ;
if ( curOpenTime > lastOpenTime )
{
lastOpenTime = curOpenTime ; 
needleTicket = OrderTicket () ;
Oprice = OrderOpenPrice();
}
}
}

if ( course >= Oprice + diference )                                                     // this is the moment that it should check if the answer is positive and close
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,5,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}

}

return (0);

}
 
diostar:

not logical. this is what your code does:

for ( int i =   OrdersTotal () - 1   ; i >= 0 ; i -- ) ----> you iterate from most recent to latest orders by virtue of order filling position --- (1)
{
if (OrderSelect ( i, SELECT_BY_POS, MODE_TRADES ))
{
int curOpenTime = OrderOpenTime () ;
if ( curOpenTime > lastOpenTime ) ---------------------------> by virtue of (1): this will always return FALSE ---(2)
{
lastOpenTime = curOpenTime ; 
needleTicket = OrderTicket () ;
Oprice = OrderOpenPrice();
}
Wrong diostar. On the first selected order, curOpenTime is what ever and lastOpenTime is zero so the body will be selected.
 
BorysekPL:
I want it to close orders only when Bid is higher than last order open price + diference. But this is closing always. I don't know why....
double diference = 50*point;
  1. On a 5 digit broker that's only 5 pips above order open price. EA's must adjust
    //++++ These are adjusted for 5 digit brokers.
    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 % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  2. if (OrderSelect ( i, SELECT_BY_POS, MODE_TRADES ))
    if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
    These makes the EA incompatible with any other including itself on other charts.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        ){
    
    Why are you trying to find the latest open order, loop through all of the EA's orders and close if needed.
    if(cmd==OP_BUY) price=Bid;
    else price=Ask;
    result=OrderClose(OrderTicket(),OrderLots(),price,5,CLR_NONE);
    Simplfy
    result=OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), 5*pips2points ,CLR_NONE);

  3. result=OrderClose(OrderTicket(),OrderLots(),price,5,CLR_NONE);
    if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
    else error=0;
    if(error==135) RefreshRates();
    else break;
    Simplify code were possible
    if (OrderClose(OrderTicket(),OrderLots(),price,5,CLR_NONE)) break;
    error=GetLastError(); Print("LastError = ",error);
    if(error==135) RefreshRates();
    else break;
 
WHRoeder:
Wrong diostar. On the first selected order, curOpenTime is what ever and lastOpenTime is zero so the body will be selected.


correct, thx for pointing out. one will be selected but on first and only instance and subsequent loops, the logic returns false.


Reason: