Is it not possible to have direct control of the entry price when automating buy or sell trades?

 

I wanted to see if it was possible to make the EA enter a buy position only when the ask price is less than or equal to the moving average price.

NormalizeDouble(currentAsk, _Digits) <= NormalizeDouble(ma[0], _Digits) 

Full snippet:


if(allow_buy && trade_type == market_orders 
&& NormalizeDouble(currentAsk, _Digits) <= NormalizeDouble(ma[0], _Digits) 
&&  ConditionsMetForBuyTrade){          

   if(signalTime != barOpenTime){
      trade.Buy(tradeVolume, _Symbol, NormalizeDouble(currentAsk, _Digits), slBuy, tpBuy);   
      signalTime = barOpenTime;   
      }        
}            

if(allow_sell && trade_type == market_orders 
&& NormalizeDouble(currentBid, _Digits) >= NormalizeDouble(ma[0], _Digits) 
&& ConditionsMetForSellTrade){  
                                               
   if(signalTime != barOpenTime){
      trade.Sell(tradeVolume, _Symbol, NormalizeDouble(currentBid, _Digits), slSell, tpSell);   
      signalTime = barOpenTime;  
      }                               
} 


I set the MA buffer as series so that index 0 should reference the MA price at the current bar:

ArraySetAsSeries(ma, true);


But the result is that no trades are being placed when I apply this logic to the condition:

NormalizeDouble(currentAsk, _Digits) <= NormalizeDouble(ma[0], _Digits) 

If I simply take this line out of the condition...the EA will successfully place the buy trade, but it will do it at the random available ask price

 

We have no information about your

allow_buy 

and 

ConditionsMetForBuyTrade

Very probably, in one of them there is a condition that cannot be true together with price below the MA.

 

sorry nevermind, I realized that the boolean conditions were going false too soon, and true for only a split second, so I have it working now and to get it working I made the boolean variables static and applied state management this way:

static bool ConditionsMetForBuyTrade = false;
static bool ConditionsMetForSellTrade = false;
            
if(bullTrend[0] == 1 && bullTrend[1] == 1 && bullTrend[2] == 0 && bullTrend[3] == 0){
   ConditionsMetForBuyTrade = true; ConditionsMetForSellTrade = false;
}

if(bearTrend[0] == 1 && bearTrend[1] == 1 && bearTrend[2] == 0 && bearTrend[3] == 0){
   ConditionsMetForBuyTrade = false; ConditionsMetForSellTrade = true;
}