How do I save the latest order price and loop?

 

Hello,


I am trying to get the EA to create orders when the price moves x pips away from open order price and then use that price level to make the next trade based off that. I am trying to update 'LastPriceBuy' with the price from the newest order and only if price moves x pips away from that new updated price must the ea place a trade and repeat the process again if another trade is made. My code below does not seem to be working. Any ideas?


void OnTick()
  {
  
datetime lastTime  = 0;
int OrderCount=0;

for(int Counter = 0; Counter <= OrdersTotal(); Counter++)
{
if(OrderSelect(Counter,SELECT_BY_POS))

{

if(OrderSymbol() == Symbol() && OrderType() == OP_BUY){ 

if (OrderMagicNumber() == 0  &&  OrderOpenTime()>lastTime )
{

OrderCount++;
LastPriceBuy = OrderOpenPrice();
lastTime = OrderOpenTime();
Print(LastPriceBuy);
Print("Pricefromnew");


if(Ask > LastPriceBuy+XPips+(Ask-Bid)){
OrderModify(OrderTicket(),OrderOpenPrice(),LastPriceBuy,0,0,0);
if(OrderStopLoss() != 0){
TicketBuyXMain = OrderSend(Symbol(),OP_BUY,0.1,Ask,5,0,0,NULL,1111,0,clrAliceBlue);
}
if(OrderSelect(TicketBuyXMain,SELECT_BY_TICKET,MODE_TRADES)==True){
LastPriceBuy = OrderOpenPrice();
}


}





if (OrderMagicNumber() == 1111  &&  OrderOpenTime()>lastTime ){
if(Ask > LastPriceBuy+XPips+(Ask-Bid)){
OrderModify(OrderTicket(),OrderOpenPrice(),LastPriceBuy,0,0,0);
if(OrderStopLoss() != 0){
TicketBuyXMain = OrderSend(Symbol(),OP_BUY,0.1,Ask,5,0,0,NULL,1111,0,clrAliceBlue);
}
if(OrderSelect(TicketBuyXMain,SELECT_BY_TICKET,MODE_TRADES)==True){
LastPriceBuy = OrderOpenPrice();
}
}

}
}
}

}

}

}
Reason: