Regarding using current price against last running order to get Pip Difference

 
 double SL,TP,onePip;
 int Ticket;
 onePip = 0.0001;
 
 if( (Symbol()=="EURJPY") || (Symbol()=="AUDJPY") || (Symbol()=="GBPJPY") || (Symbol()=="USDJPY") || (Symbol()=="CADJPY") || (Symbol()=="NZDJPY") )
 {
 onePip = 0.01;
 }
//check if price match
   
   double currentPrice;
   currentPrice = MarketInfo("Symbol",MODE_BID);
  
  
  //GET LAST OPEN Order
 int pos;
 double lastPrice;
 lastPrice = 0.00;

    datetime lastTime  = 0;
    int      lastTicket = -1; // None open.
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderOpenTime()     >=  lastTime
    &&  OrderTicket()       >   lastTicket
    ){
      lastTime   = OrderOpenTime();
      lastTicket = OrderTicket();
      lastPrice = OrderOpenPrice();
    }


double priceDiff; 
priceDiff = 0.00;
bool validGap;

validGap = true;

if(OrdersTotal() > 0 )
{

priceDiff = currentPrice - lastPrice;

if(priceDiff < gapTrade *onePip)
{
validGap = false;
}
}
// END GET LAST ORDER   

Above is a code I think of, but it does not work as intended.

What i trying achieve is

1) If there is 0 "RUNNING" trade, validGap will return true.

2) If there is a trade "RUNNING", if "open price" of the running trades(could be more than 1 trade running) vs current price have a 30 points difference ( 30 pips ) = return true for validGap, else return false

What I trying achieve is ensure all my trades have at least 30 pips difference then its return valid. Comparing only with "Running/Open Order" and not closed order in history.

Also only check against the Symbol() that the EA is running on ( for opening trades ).

Can anyone help me with my code and see whats wrong as the calculation seems a bit off, I get stuff like -1.337 for my price diff and I think it compare with history price, valid gap always return false as of the code now.

Thanks for helping!

 

"1) If there is 0 "RUNNING" trade, validGap will return true.

What does this mean?

Do you mean if there are currently no open trades?

variable gaptrade does not appear to have been assigned a value.

You probably need the absolute value of priceDiff, if the order open price is above the current price, priceDiff will be negative, so smaller than gapTrade*onePip.

Reason: