Estou tentando configurar stop movel nesse robo mais o mesmo no testador de estratégia está entendendo como se o stop fosse fixo
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 | //+------------------------------------------------------------------
Usa isto para postar o código.
No backteste o stop movel esta funcionando como fixo
Leia minha resposta abaixo. Vai te ajudar.
Fórum de negociação, sistemas de negociação automatizados e testes de estratégias de negociação
Inserção de trailing stop em ordem ativa
Joscelino Celso de Oliveira, 2019.04.13 01:10
Seguem artigos e classes que abordam o tema.
Leia-os e programe de forma adequada a seu EA.
Classes:
https://www.mql5.com/pt/docs/standardlibrary/expertclasses/sampletrailingclasses/ctrailingfixedpips
https://www.mql5.com/pt/docs/standardlibrary/expertclasses/expertbaseclasses/cexperttrailing
Artigos:
https://www.mql5.com/pt/articles/442
https://www.mql5.com/pt/articles/171
https://www.mql5.com/pt/articles/134
https://www.mql5.com/pt/articles/2717
https://www.mql5.com/pt/articles/2411
- Aplicativos de negociação gratuitos
- 8 000+ sinais para cópia
- Notícias econômicas para análise dos mercados financeiros
Você concorda com a política do site e com os termos de uso
//| 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 |
//+------------------------------------------------------------------+