Using moving average as a stoploss... buy is working but sell doesnt... why ?

 

Hi friends,

I saw Mr Raimund Bauer's youtube video which he use moving average as a stoploss.

I tried to use 29 and 30 period moving average as a stoploss. Buy is working but Sell is not.

While searched to forum and see Mr. Watfords post at https://www.mql5.com/en/forum/146076 but still doesnt understand OrderModify Error 1 etc.

Any eyes which saw my mistake?


void OnTick()
  {
   static double BuyLastStopMovingAverageValue;
   static double SellLastStopMovingAverageValue;
   
   
   if (OrdersTotal() == 0)
   OrderSend(_Symbol,OP_BUY,0.10,Ask,3,NULL,NULL,NULL,0,0,Green);
   double BuyStopMovingAverageValue = iMA(_Symbol,_Period,30,0,MODE_EMA,PRICE_CLOSE,1);
   if (BuyStopMovingAverageValue > BuyLastStopMovingAverageValue)  
   if (BuyStopMovingAverageValue < Bid)
         {
         CheckSMABuyTrailingStop(BuyStopMovingAverageValue);
         BuyLastStopMovingAverageValue=BuyStopMovingAverageValue;
         }
   
   
   
   if (OrdersTotal() == 0)
   OrderSend(_Symbol,OP_SELL,0.10,Bid,3,NULL,NULL,NULL,0,0,Green);
   double SellStopMovingAverageValue = iMA(_Symbol,_Period,29,0,MODE_EMA,PRICE_CLOSE,1);
   if (SellStopMovingAverageValue < SellLastStopMovingAverageValue)
   if (SellStopMovingAverageValue > Ask)
         {
         CheckSMASellTrailingStop(SellStopMovingAverageValue);
         SellLastStopMovingAverageValue=SellStopMovingAverageValue;
         }     
 
    
    } // tick and
    
         
         
         
   void CheckSMABuyTrailingStop (double BuyStopMovingAverageValue)
   {
   for (int b= OrdersTotal() -1; b>=0; b--)
   {
   if (OrderSelect (b, SELECT_BY_POS,MODE_TRADES))
   if (OrderSymbol() == Symbol())
   if (OrderType () == OP_BUY)
   if (( OrderStopLoss() < BuyStopMovingAverageValue) || (OrderStopLoss() == 0)) 
   OrderModify( OrderTicket(), OrderOpenPrice(),BuyStopMovingAverageValue, OrderTakeProfit(),0,CLR_NONE);
   }
   }
    
    
          
   void CheckSMASellTrailingStop (double SellStopMovingAverageValue)
   {
   for (int b= OrdersTotal() -1; b>=0; b--)
   {
   if (OrderSelect (b, SELECT_BY_POS,MODE_TRADES))
   if (OrderSymbol() == Symbol())
   if (OrderType () == OP_SELL)
   if (( OrderStopLoss() > SellStopMovingAverageValue) || (OrderStopLoss() == 0)) 
   OrderModify( OrderTicket(), OrderOpenPrice(),SellStopMovingAverageValue, OrderTakeProfit(),0,CLR_NONE);
   }
   }
  
  
 
 
cuneytates:

Hi friends,

I saw Mr Raimund Bauer's youtube video which he use moving average as a stoploss.

I tried to use 29 and 30 period moving average as a stoploss. Buy is working but Sell is not.

While searched to forum and see Mr. Watfords post at https://www.mql5.com/en/forum/146076 but still doesnt understand OrderModify Error 1 etc.

Any eyes which saw my mistake?


his code only opens a BUY order and waits for this order until it is Stop forever. It is never updated after the first opening of the 30 EMA. It never reaches the SELL Order.

Write a separate function that tracks Stops after order openings are made.

Do not keep the last stop at the EA, it will reset at this value when the EA is restarted.