Take profit at Moving average

 

Hello my dear friends 

actually I'm trying to code an EA but I can't code the take profit part... 

I would like to take profit if the price hit the MA5 at that point so I wrote something like this :

 if(!PositionSelect(_Symbol))
     {
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.position = position_ticket;          // ticket of the position
      mrequest.price = NormalizeDouble(Ask,_Digits);    // Lastest Ask price
      mrequest.sl =(Ask-(_Point*250)) ; // Stop Loss
      mrequest.tp =mahi5Val[0];    // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume =LOT;    // Number of lots to trade
      mrequest.magic = EA_Magic;                                // Magic Number
      mrequest.type = ORDER_TYPE_BUY;                    // Buy Order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      OrderSend(mrequest,mresult);                       // Send order
      if(mrequest.volume)
        {
         mrequest.volume=NormalizeDouble(mrequest.volume,2);
         Print("Lots to buy: ",mrequest.volume);
        }
      else
         Print("Problem to buy: ",mrequest.volume);
     }

  }

and I thought each new candle my take profit amount will be update to new mahi5Val[0] if it dose't triggered before, but I'm wrong....

if you can help I'll appreciate you

 

I have a code example like this: Traffic

get the iMA value on bar #0, we pass the received value to the Trailing procedure

   if(InpTrailingFrequency<10)
     {
      double level;
      if(FreezeStopsLevels(level))
        {
         double ma[];
         if(iGetArray(handle_iMA_Trailing,0,0,1,ma))
            Trailing(level,ma[0]);
        }
     }


Trailing procedure:

//+------------------------------------------------------------------+
//| Trailing                                                         |
//|   InpTrailingStop: min distance from price to Stop Loss          |
//+------------------------------------------------------------------+
void Trailing(const double stop_level,const double ima_price)
  {
/*
     Buying is done at the Ask price                 |  Selling is done at the Bid price
   ------------------------------------------------|----------------------------------
   TakeProfit        >= Bid                        |  TakeProfit        <= Ask
   StopLoss          <= Bid                      |  StopLoss          >= Ask
   TakeProfit - Bid  >= SYMBOL_TRADE_STOPS_LEVEL   |  Ask - TakeProfit  >= SYMBOL_TRADE_STOPS_LEVEL
   Bid - StopLoss    >= SYMBOL_TRADE_STOPS_LEVEL   |  StopLoss - Ask    >= SYMBOL_TRADE_STOPS_LEVEL
*/
   if(InpTrailingStop==0)
      return;
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_position.SelectByIndex(i))
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               if(m_position.PriceCurrent()-ima_price>ExtTrailingStop+ExtTrailingStep)
                  if(m_position.StopLoss()<ima_price && !CompareDoubles(m_position.StopLoss(),ima_price,m_symbol.Digits()))
                     if(m_position.PriceCurrent()-ima_price>=stop_level)
                       {
                        if(!m_trade.PositionModify(m_position.Ticket(),
                           m_symbol.NormalizePrice(ima_price),
                           m_position.TakeProfit()))
                           Print("Modify ",m_position.Ticket(),
                                 " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
                                 ", description of result: ",m_trade.ResultRetcodeDescription());
                        RefreshRates();
                        m_position.SelectByIndex(i);
                        PrintResultModify(m_trade,m_symbol,m_position);
                        continue;
                       }
              }
            else
              {
               if(ima_price-m_position.PriceCurrent()>ExtTrailingStop+ExtTrailingStep)
                  if((m_position.StopLoss()>ima_price && !CompareDoubles(m_position.StopLoss(),ima_price,m_symbol.Digits())) ||
                     (m_position.StopLoss()==0))
                     if(ima_price-m_position.PriceCurrent()>=stop_level)
                       {
                        if(!m_trade.PositionModify(m_position.Ticket(),
                           m_symbol.NormalizePrice(ima_price),
                           m_position.TakeProfit()))
                           Print("Modify ",m_position.Ticket(),
                                 " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
                                 ", description of result: ",m_trade.ResultRetcodeDescription());
                        RefreshRates();
                        m_position.SelectByIndex(i);
                        PrintResultModify(m_trade,m_symbol,m_position);
                       }
              }

           }
  }
Traffic
Traffic
  • www.mql5.com
При открытии у позиции Стоп лосс устанавливается в ноль. Затем производится трейлинг по индикатору iMA (Moving Average, MA). Торговые настройки В советнике можно как включать так и выключать уровни Take Profit и Trailing Stop. Для отключения выбранного параметра достаточно выбранный параметр установить в "0.0". Трейлинг можно задавать двумя...
 
Vladimir Karputov:

I have a code example like this: Traffic

get the iMA value on bar #0, we pass the received value to the Trailing procedure


Trailing procedure:

Thanks bro I'm trying to fix that code into mine 👍
Reason: