EA Compounding - Price calculated vs Price real

 

Hi all,


thanks for this space.

I'm currently building an EA that increments  the operation's volume when the price goes above half of take profit's distance: stop loss is modified  accordingly maximum loss admitted.

Just to be more clear, here an example of a long position:

Price Situation
1,1220 Original Take Profit
 1,1210  Half distance: volume increase
 1,1200  Entry Point
 1,xxx  New Stop Loss
 1,1150  Original stop loss


In order to reach this goal, my EA has a Buy button that open a long position(picture 1.PNG attached). When I press this button, EA takes the ASK value of price and open a position, as showed in this code (I've remove code lines unecessary for this discussion):

void OperationManager::Buy(bool MAIL_ALERT,bool NOTIFICATION_ALERT)
  {
...
   Operation *operation= new Operation();
   operation.direction="Long";
   double priceActualAsk= hintsManager.NormalizePrice(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   operation.initialLevel=priceActualAsk;
   trade.Buy(operation.volume,NULL,operation.initialLevel,operation.stopLoss,operation.takeProfit,NULL);
...

  }

I'm pretty happy of this, because if I go on metatrader console I can see that position is opened effectively at level inside priceActualAsk variable (picture 2.PNG).

After the position is open, a method inside the OnTick() function is called: on every tick I'm taking the ask value of price and if this is above a threshold (half of distance to TP), I increment my position size with another buy operation (As before, I've removed lines not useful for this discussion):
void OperationManager::EngineOnLong(bool MAIL_ALERT,bool NOTIFICATION_ALERT)
  {
   double priceActualAsk= hintsManager.NormalizePrice(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   Operation *operation=GetOpenPosition();

  if(priceActualAsk==(operation.initialLevel+(operation.initialDistanceToTP/2*_Point*10)))
     {
...
      trade.Buy(((operation.volume*stepCompounding)-operation.volume),NULL,priceActualAsk,operation.stopLoss,operation.takeProfit,NULL);
      operation=GetOpenPosition();
...
     }


  }

But the problem I'm having is related to price where this buy is executed: in fact, I see two different value on console (picture 3.PNG, just check "done at:" of CTRADE log) and on first page of metatrader (picture 4.PNG). As you can see,I would like to have this buy executed to priceActualAsk variable value (as happen to original buy), but I see that the real price execution is always 2/3 pips less. 

Anybody knows why I'm having this strange behaviour, and how I can manage it? I would like to understand why this behaviour happens only on this second phase and not also on the original buy.


Thank you,

Mirko

Files:
1.PNG  70 kb
2.PNG  55 kb
3.PNG  78 kb
4.PNG  48 kb
Reason: