take profit na média móvel

 

Olá


Estou tentando programar um take profit na média móvel e estou tentando usar a função trade.PositionModify porém sem sucesso até o momento. Criei uma funçao void para isso e usei NormalizeDouble e usei um array para armazenar os dados. 

Estou dando os primeiros passos em programação MQL se alguém puder me ajudar e me mostrar se estou no caminho certo. 


void TrailingTake(double preco)
   {
      for(int i = PositionsTotal()-1; i>=0; i--)
         {
            string symbol = PositionGetSymbol(i);
            ulong magic = PositionGetInteger(POSITION_MAGIC);
            if(symbol == _Symbol && magic==magicNum)
               {
                  ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
                  double PrecoEntrada = PositionGetDouble(POSITION_PRICE_OPEN);
                  double StopLossCorrente = PositionGetDouble(POSITION_SL);
                  double TakeProfitCorrente = PositionGetDouble(POSITION_TP);
                  if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
                     {
                        if(preco <= (TakeProfitCorrente) )
                           {
                              double novoTP = NormalizeDouble(PrecoEntrada +(smaArray[0] - PrecoEntrada), _Digits);
                              if(trade.PositionModify(PositionTicket, StopLossCorrente, novoTP))
                                 {
                                    Print("TrailingTake - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
                                 }
                              else
                                 {
                                    Print("TrailingTake - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
                                 }
                           }
                     }
                  else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
                     {
                        if(preco >= (TakeProfitCorrente) )
                           {
                              double novoTP = NormalizeDouble(PrecoEntrada -(smaArray[0] - PrecoEntrada), _Digits);
                              if(trade.PositionModify(PositionTicket, StopLossCorrente, novoTP))
                                 {
                                    Print("TrailingTake - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
                                 }
                              else
                                 {
                                    Print("TrailingTake - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
                                 }
                           }
                     }
               }
         }
   }
//---
Razão: