Estou tentando configurar stop movel nesse robo mais o mesmo no testador de estratégia está entendendo como se o stop fosse fixo

 
//+------------------------------------------------------------------+
//|                                                        MM EA.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MAGICMA = 11;
extern int periodo_menor = 4;
extern int periodo_maior = 60;
extern double LoteEntrada = 0.01;
extern double fator_exp_lote = 1.4;
extern bool OperarSELL = true;
extern bool OperarBUY = true;
extern bool PararTrade = false;
extern int TakeProfit = 800;
extern int TralingStop = 100;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      CheckForOpen(MAGICMA);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool sinal_media_moveis(int BUY_SELL)
  { 
   bool retorno = false;

   double media_menor     = iMA(NULL, 0, periodo_menor, 0, MODE_SMA, PRICE_CLOSE, 1);
   double media_menor_ant = iMA(NULL, 0, periodo_menor, 0, MODE_SMA, PRICE_CLOSE, 2);
   double media_maior     = iMA(NULL, 0, periodo_maior, 0, MODE_SMA, PRICE_CLOSE, 1);
   double media_maior_ant = iMA(NULL, 0, periodo_maior, 0, MODE_SMA, PRICE_CLOSE, 2);

   if(BUY_SELL == OP_SELL)
     {
      if(media_menor_ant > media_maior_ant && media_menor < media_maior)
        {retorno = true;}
      else
        {retorno = false;}
     }
   else if(BUY_SELL == OP_BUY)
     {
      if(media_menor_ant < media_maior_ant && media_menor > media_maior)
        {retorno = true;}
      else
        {retorno = false;}
     }

   return(retorno);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int total_ordem_aberta (int MAGICMA_n, int BUY_SELL)
  {
   int totalordens = OrdersTotal();
   int contador = 0;

   for(int i = 0; i < totalordens; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
        {break;}
      if(OrderMagicNumber() == MAGICMA_n && OrderType() == BUY_SELL)
        {contador++;}
     }

   return(contador);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckForOpen(int MAGICMA_n)
  {
   int res;
   int total_de_ordem_venda  = total_ordem_aberta(MAGICMA_n, OP_SELL);
   int total_de_ordem_compra = total_ordem_aberta(MAGICMA_n, OP_BUY);
   double lote_ent = 0;

   if(PararTrade == true && total_de_ordem_venda == 0 && total_de_ordem_compra == 0) {return;}
   if(Volume[0] > 1) {return;}
   if(IsTradeContextBusy() == true) {return;}

   RefreshRates();

   if(OperarSELL == true)
     {
      if(sinal_media_moveis(OP_SELL) == true && total_de_ordem_venda == 0 && total_de_ordem_compra == 0)
        {
         lote_ent = get_lotes(LoteEntrada, MAGICMA_n, fator_exp_lote);
         res = OrderSend(Symbol(), OP_SELL, lote_ent, Bid, 150, Bid + TralingStop * Point, Bid - TakeProfit * Point, "", MAGICMA_n, 0, clrRed);
        }
     }
   if(OperarBUY == true)
     {
      if(sinal_media_moveis(OP_BUY) == true && total_de_ordem_compra == 0 && total_de_ordem_venda == 0)
        {
         lote_ent = get_lotes(LoteEntrada, MAGICMA_n, fator_exp_lote);
         res = OrderSend(Symbol(), OP_BUY, lote_ent, Ask, 150, Ask - TralingStop * Point, Ask + TakeProfit * Point, "", MAGICMA_n, 0, clrBlue);
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double get_lotes(double lote_inicial, int MAGIC, double fator_incremento)
  {
   int maior_ticket = 0;
   double tamanho_lote = 0, maior_lote = 0, Profit = 0;

   for(int i = 0; i < OrdersHistoryTotal(); i++)
     { 
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false)
        {break;}
      if(OrderMagicNumber() == MAGIC && OrderSymbol() == Symbol())
        {
         if (OrderTicket()> maior_ticket)
           {
            maior_ticket = OrderTicket();
            maior_lote = NormalizeDouble(OrderLots(), 2);
            Profit = OrderProfit();
           }
        }
     }

   if(OrderSelect(maior_ticket, SELECT_BY_TICKET, MODE_HISTORY) == false)
     {return(lote_inicial);}
   if(maior_lote == 0)
     {tamanho_lote = lote_inicial;}
   else
     { 
      if(Profit > 0)
        {tamanho_lote = lote_inicial;}
      else
        {tamanho_lote = maior_lote * fator_incremento;}
     }

return(NormalizeDouble(tamanho_lote, 2));
  }
//+------------------------------------------------------------------+
//| Expert End                                                       |
//+------------------------------------------------------------------+
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
  • www.mql5.com
Tire dúvidas sobre análise técnica, fale com outros traders sobre sistemas de negociação e melhore suas habilidades de programação de estratégias em linguagem MQL5. Participe do fórum, compartilhe ideias com traders de todo o mundo e ajude com feedback os novatos — nossa comunidade está crescendo junto com você. Robot Ea Tenho um robot ea já...
 

Usa isto para postar o código.

 
//+------------------------------------------------------------------+
//|                                                        MM EA.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MAGICMA = 11;
extern int periodo_menor = 4;
extern int periodo_maior = 60;
extern double LoteEntrada = 0.01;
extern double fator_exp_lote = 1.4;
extern bool OperarSELL = true;
extern bool OperarBUY = true;
extern bool PararTrade = false;
extern int TakeProfit = 800;
extern int StopLoss = 500;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      CheckForOpen(MAGICMA);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool sinal_media_moveis(int BUY_SELL)
  {  
   bool retorno = false; 

   double media_menor     = iMA(NULL, 0, periodo_menor, 0, MODE_SMA, PRICE_CLOSE, 1);
   double media_menor_ant = iMA(NULL, 0, periodo_menor, 0, MODE_SMA, PRICE_CLOSE, 2);
   double media_maior     = iMA(NULL, 0, periodo_maior, 0, MODE_SMA, PRICE_CLOSE, 1);
   double media_maior_ant = iMA(NULL, 0, periodo_maior, 0, MODE_SMA, PRICE_CLOSE, 2);

   if(BUY_SELL == OP_SELL)
     {
      if(media_menor_ant > media_maior_ant && media_menor < media_maior)
        {retorno = true;}
      else
        {retorno = false;}
     }
   else if(BUY_SELL == OP_BUY)
     {
      if(media_menor_ant < media_maior_ant && media_menor > media_maior)
        {retorno = true;}
      else
        {retorno = false;}
     }

   return(retorno);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int total_ordem_aberta (int MAGICMA_n, int BUY_SELL)
  {
   int totalordens = OrdersTotal();
   int contador = 0;

   for(int i = 0; i < totalordens; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
        {break;}
      if(OrderMagicNumber() == MAGICMA_n && OrderType() == BUY_SELL)
        {contador++;}
     }

   return(contador);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckForOpen(int MAGICMA_n)
  {
   int res;
   int total_de_ordem_venda  = total_ordem_aberta(MAGICMA_n, OP_SELL);
   int total_de_ordem_compra = total_ordem_aberta(MAGICMA_n, OP_BUY);
   double lote_ent = 0;

   if(PararTrade == true && total_de_ordem_venda == 0 && total_de_ordem_compra == 0) {return;}
   if(Volume[0] > 1) {return;}
   if(IsTradeContextBusy() == true) {return;}

   RefreshRates();

   if(OperarSELL == true)
     {
      if(sinal_media_moveis(OP_SELL) == true && total_de_ordem_venda == 0 && total_de_ordem_compra == 0)
        {
         lote_ent = get_lotes(LoteEntrada, MAGICMA_n, fator_exp_lote);
         res = OrderSend(Symbol(), OP_SELL, lote_ent, Bid, 150, Bid + StopLoss * Point, Bid - TakeProfit * Point, "", MAGICMA_n, 0, clrRed);
        }
     }
   if(OperarBUY == true)
     {
      if(sinal_media_moveis(OP_BUY) == true && total_de_ordem_compra == 0 && total_de_ordem_venda == 0)
        {
         lote_ent = get_lotes(LoteEntrada, MAGICMA_n, fator_exp_lote);
         res = OrderSend(Symbol(), OP_BUY, lote_ent, Ask, 150, Ask - StopLoss * Point, Ask + TakeProfit * Point, "", MAGICMA_n, 0, clrBlue);
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double get_lotes(double lote_inicial, int MAGIC, double fator_incremento)
  {
   int maior_ticket = 0;
   double tamanho_lote = 0, maior_lote = 0, Profit = 0;

   for(int i = 0; i < OrdersHistoryTotal(); i++)
     {  
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false)
        {break;}
      if(OrderMagicNumber() == MAGIC && OrderSymbol() == Symbol())
        {
         if (OrderTicket()> maior_ticket)
           {
            maior_ticket = OrderTicket();
            maior_lote = NormalizeDouble(OrderLots(), 2);
            Profit = OrderProfit();
           }
        }
     } 

   if(OrderSelect(maior_ticket, SELECT_BY_TICKET, MODE_HISTORY) == false) 
     {return(lote_inicial);}
   if(maior_lote == 0)
     {tamanho_lote = lote_inicial;}
   else 
     {  
      if(Profit > 0)
        {tamanho_lote = lote_inicial;}
      else
        {tamanho_lote = maior_lote * fator_incremento;}
     }

return(NormalizeDouble(tamanho_lote, 2));
  }
//+------------------------------------------------------------------+
//| Expert End                                                       |
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//|                                                        MM EA.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MAGICMA = 11;
extern int periodo_menor = 4;
extern int periodo_maior = 60;
extern double LoteEntrada = 0.01;
extern double fator_exp_lote = 1.4;
extern bool OperarSELL = true;
extern bool OperarBUY = true;
extern bool PararTrade = false;
extern int TakeProfit = 800;
extern int TralingStop = 500;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      CheckForOpen(MAGICMA);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool sinal_media_moveis(int BUY_SELL)
  {  
   bool retorno = false; 

   double media_menor     = iMA(NULL, 0, periodo_menor, 0, MODE_SMA, PRICE_CLOSE, 1);
   double media_menor_ant = iMA(NULL, 0, periodo_menor, 0, MODE_SMA, PRICE_CLOSE, 2);
   double media_maior     = iMA(NULL, 0, periodo_maior, 0, MODE_SMA, PRICE_CLOSE, 1);
   double media_maior_ant = iMA(NULL, 0, periodo_maior, 0, MODE_SMA, PRICE_CLOSE, 2);

   if(BUY_SELL == OP_SELL)
     {
      if(media_menor_ant > media_maior_ant && media_menor < media_maior)
        {retorno = true;}
      else
        {retorno = false;}
     }
   else if(BUY_SELL == OP_BUY)
     {
      if(media_menor_ant < media_maior_ant && media_menor > media_maior)
        {retorno = true;}
      else
        {retorno = false;}
     }

   return(retorno);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int total_ordem_aberta (int MAGICMA_n, int BUY_SELL)
  {
   int totalordens = OrdersTotal();
   int contador = 0;

   for(int i = 0; i < totalordens; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
        {break;}
      if(OrderMagicNumber() == MAGICMA_n && OrderType() == BUY_SELL)
        {contador++;}
     }

   return(contador);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckForOpen(int MAGICMA_n)
  {
   int res;
   int total_de_ordem_venda  = total_ordem_aberta(MAGICMA_n, OP_SELL);
   int total_de_ordem_compra = total_ordem_aberta(MAGICMA_n, OP_BUY);
   double lote_ent = 0;

   if(PararTrade == true && total_de_ordem_venda == 0 && total_de_ordem_compra == 0) {return;}
   if(Volume[0] > 1) {return;}
   if(IsTradeContextBusy() == true) {return;}

   RefreshRates();

   if(OperarSELL == true)
     {
      if(sinal_media_moveis(OP_SELL) == true && total_de_ordem_venda == 0 && total_de_ordem_compra == 0)
        {
         lote_ent = get_lotes(LoteEntrada, MAGICMA_n, fator_exp_lote);
         res = OrderSend(Symbol(), OP_SELL, lote_ent, Bid, 150, Bid + TralingStop * Point, Bid - TakeProfit * Point, "", MAGICMA_n, 0, clrRed);
        }
     }
   if(OperarBUY == true)
     {
      if(sinal_media_moveis(OP_BUY) == true && total_de_ordem_compra == 0 && total_de_ordem_venda == 0)
        {
         lote_ent = get_lotes(LoteEntrada, MAGICMA_n, fator_exp_lote);
         res = OrderSend(Symbol(), OP_BUY, lote_ent, Ask, 150, Ask - TralingStop * Point, Ask + TakeProfit * Point, "", MAGICMA_n, 0, clrBlue);
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double get_lotes(double lote_inicial, int MAGIC, double fator_incremento)
  {
   int maior_ticket = 0;
   double tamanho_lote = 0, maior_lote = 0, Profit = 0;

   for(int i = 0; i < OrdersHistoryTotal(); i++)
     {  
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false)
        {break;}
      if(OrderMagicNumber() == MAGIC && OrderSymbol() == Symbol())
        {
         if (OrderTicket()> maior_ticket)
           {
            maior_ticket = OrderTicket();
            maior_lote = NormalizeDouble(OrderLots(), 2);
            Profit = OrderProfit();
           }
        }
     } 

   if(OrderSelect(maior_ticket, SELECT_BY_TICKET, MODE_HISTORY) == false) 
     {return(lote_inicial);}
   if(maior_lote == 0)
     {tamanho_lote = lote_inicial;}
   else 
     {  
      if(Profit > 0)
        {tamanho_lote = lote_inicial;}
      else
        {tamanho_lote = maior_lote * fator_incremento;}
     }

return(NormalizeDouble(tamanho_lote, 2));
  }
//+------------------------------------------------------------------+
//| Expert End                                                       |
//+------------------------------------------------------------------
 
Lucas Tavares:

Usa isto para postar o código.

Comecei a aprender sobre robos agora. Criei um primeiro que deu certo. Depois tentei modificar o stop fixo para movel e não consegui. A ultima postagem é o robo alterado a penultima é o primeiro que criei
 
No backteste o stop movel esta funcionando como fixo
Razão: