Cannot make EA to execute code from inside if statement

 

Hello,

I have a method called placePendingOrder() which should place:

  • an OP_BUYSTOP order if the last pending order was an OP_SELLSTOP
  • an OP_SELLSTOP order if the last pending order was an OP_BUYSTOP order.


void placePendingOrder() {
if( OrderSelect(lastPlacedOrderTicket, SELECT_BY_TICKET) == true) {
double newLotsForPending = 2 * OrderLots();

if(OrderType() == OP_SELLSTOP && OrderSymbol() == Symbol()) {
if (Bid <= priceForPendingSell) {
Alert("Bid <= priceForPendingSell ===> placed BuyStopOrder");
placeBuyStopOrder(newLotsForPending);
}
} else if(OrderType() == OP_BUYSTOP && OrderSymbol() == Symbol()) {
if (Ask >= priceForPendingBuy) {
Alert("Ask >= priceForPendingBuy ===> placed SellStopOrder");
placeSellStopOrder(newLotsForPending);
}
}
Print("Error = ",GetLastError());
}
}


The problem that I have with the above method is that I can't make the EA to execute the code from inside the colored if statements [ if (Bid <= priceForPendingSell) ] or [ if (Ask >= priceForPendingBuy) ]. When the Bid become equals to the priceForPendingSell or the Ask become equal to priceForPendingBuy the code from inside those ifs is not executed and the error code is still 0.

I place another method in which you can see the way I computed the priceForPendingSell and the priceForPendingBuy.


static double priceForPendingSell = 0.0;

static double priceForPendingBuy = 0.0;

static double startLevel = 0.0;

static double middleLevel = 0.0;

double distanceForHedge = 0.25;

double priceToStart; // it will be set to the price of ASK or BID


void computeLevels(double priceToStart) {
startLevel = NormalizeDouble(priceToStart, noOfDigits);
double spread = Ask - Bid;

if (shouldStartWithBuy) {
takeProfit = NormalizeDouble(priceToStart + takeProfitInPips, noOfDigits);
stopLoss = NormalizeDouble(priceToStart - 2 * distanceForHedge, noOfDigits);
middleLevel = NormalizeDouble(priceToStart - distanceForHedge, noOfDigits);

stopLossFosPendingSell = NormalizeDouble(takeProfit + spread, noOfDigits);
takeProfitFosPendingSell = NormalizeDouble(stopLoss + spread, noOfDigits);

stopLossForPendingBuy = NormalizeDouble(stopLoss, noOfDigits);
takeProfitForPendingBuy = NormalizeDouble(takeProfit, noOfDigits);

priceForPendingSell = NormalizeDouble(middleLevel, noOfDigits);
priceForPendingBuy = NormalizeDouble(startLevel, noOfDigits);

} else {
takeProfit = NormalizeDouble(priceToStart - takeProfitInPips, noOfDigits);
stopLoss = NormalizeDouble(priceToStart + 2 * distanceForHedge, noOfDigits);
middleLevel = NormalizeDouble(priceToStart + distanceForHedge, noOfDigits);


stopLossFosPendingSell = NormalizeDouble(stopLoss, noOfDigits);
takeProfitFosPendingSell = NormalizeDouble(takeProfit, noOfDigits);

stopLossForPendingBuy = NormalizeDouble(takeProfit - spread, noOfDigits);
takeProfitForPendingBuy = NormalizeDouble(stopLoss - spread, noOfDigits);

priceForPendingSell = NormalizeDouble(startLevel, noOfDigits);
priceForPendingBuy = NormalizeDouble(middleLevel, noOfDigits);

}
}


I attach the entire EA to this post. The default values are set for Oil.

I will appreciate very much if anybody could help me. I spend few days on this bug.

I apologize for my English.

Files:
 

Please use this to post code . . . it makes it easier to read.

 

johnAntony:

an OP_BUYSTOP order if the last pending order was an OP_SELLSTOP

  • an OP_SELLSTOP order if the last pending order was an OP_BUYSTOP order.


void placePendingOrder() {
if( OrderSelect(lastPlacedOrderTicket, SELECT_BY_TICKET) == true) {
double newLotsForPending = 2 * OrderLots();

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

if (Bid <= priceForPendingSell) {

:

What happens when the sell stop gets triggered? It's no longer a sell stop but a sell and your code rejects the type.
Reason: