#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include <Trade\Trade.mqh>
CTrade Trade;
//--- input parameters
input string titulo1="";
input double Contratos=0.0; // numero de contratos
input double Stoploss=0.0; // stoploss da operaçao
input double Take=0.0; // Take da operaçao
input string titulo2="";
input int Habertura=9; // hora de abertura
input int Mabertura=15; // minuto abertura
input int Hfechamento=17; // hora fechamento
input int Mfechamento=45; // minuto fechamento
input string titulo3="";
input int ma_fast_period=9;
input ENUM_MA_METHOD ma_fast_method=MODE_EMA;
input int ma_slow_period=21;
input ENUM_MA_METHOD ma_slow_method=MODE_EMA;
input int ma_long_period=50;
input ENUM_MA_METHOD ma_long_method=MODE_EMA;
//+------------------------------------------------------------------+
int g1_Tick=0;
int g1_Order=0;
string g1_TendenciaMa="INDEF";
//+------------------------------------------------------------------+
bool g1_OpenPosition=false;
long g1_PositionType=-1;
double g1_Contratos=0;
bool g1_DobraMao=false;
//+------------------------------------------------------------------+
//| Hendel |
//+------------------------------------------------------------------+
int iMA_fast_handle=INVALID_HANDLE;
int iMA_slow_handle=INVALID_HANDLE;
int iMA_long_handle=INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Buffers |
//+------------------------------------------------------------------+
double iMA_fast_buffer[];
double iMA_slow_buffer[];
double iMA_long_buffer[];
bool glIntihandle1=false;
bool glIntihandle2=false;
bool glIntihandle3=false;
bool glIntiChart1=false;
bool glIntiChart2=false;
bool glIntiChart3=false;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
ResetLastError();
Comment("");
if(Contratos<=0)
{
Print("O numero de contratos nao pode ser igual ou menor que zero. ");
return(INIT_FAILED);
}
if(Stoploss<=0)
{
Print("O stoploss nao pode ser igual ou menor que zero.");
return(INIT_FAILED);
}
if(Take<=0)
{
Print("O Take nao pode ser igual ou menor que zero.");
return(INIT_FAILED);
}
//+------------------------------------------------------------------+
//| Hendle |
//+------------------------------------------------------------------+
iMA_fast_handle=iMA(_Symbol,_Period,ma_fast_period,0,ma_fast_method,PRICE_CLOSE);
if(iMA_fast_handle==INVALID_HANDLE)
{
Print("Erro criaçao da ima." , GetLastError());
return(INIT_FAILED);
}
else
{
glIntihandle1=true;
}
iMA_slow_handle=iMA(_Symbol,_Period,ma_slow_period,0,ma_slow_method,PRICE_CLOSE);
if(iMA_slow_handle==INVALID_HANDLE)
{
Print("Erro criaçao da ima." , GetLastError());
return(INIT_FAILED);
}
else
{
glIntihandle2=true;
}
iMA_long_handle=iMA(_Symbol,_Period,ma_long_period,0,ma_long_method,PRICE_CLOSE);
if(iMA_long_handle==INVALID_HANDLE)
{
Print("Erro criaçao da ima." , GetLastError());
return(INIT_FAILED);
}
else
{
glIntihandle3=true;
}
if(!ArraySetAsSeries(iMA_fast_buffer,true))
{
Print("Erro na criaçao do ArraySetAsSeries para iMA_fast_buffer." , GetLastError());
return(INIT_FAILED);
}
if(!ArraySetAsSeries(iMA_slow_buffer,true))
{
Print("Erro na criaçao do ArraySetAsSeries para iMA_slow_buffer." , GetLastError());
return(INIT_FAILED);
}
if(!ArraySetAsSeries(iMA_long_buffer,true))
{
Print("Erro na criaçao do ArraySetAsSeries para iMA_long_buffer." , GetLastError());
return(INIT_FAILED);
}
if(!ChartIndicatorAdd(ChartID(),0,iMA_fast_handle))
{
Print("Erro na plotagem da iMA_fast_handle." , GetLastError());
return(INIT_FAILED);
}
else
{
glIntiChart1=true;
}
if(!ChartIndicatorAdd(ChartID(),0,iMA_slow_handle))
{
Print("Erro na plotagem da iMA_slow_handle." , GetLastError());
return(INIT_FAILED);
}
else
{
glIntiChart2=true;
}
if(!ChartIndicatorAdd(ChartID(),0,iMA_long_handle))
{
Print("Erro na plotagem da iMA_long_handle." , GetLastError());
return(INIT_FAILED);
}
else
{
glIntiChart3=true;
}
Print("Inicializaçao realizada com sucesso." );
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ResetLastError();
Comment("");
if(iMA_fast_handle != INVALID_HANDLE)
{
if(!IndicatorRelease(iMA_fast_handle))
Print("Erro no release do iMA_fast_handle: ", GetLastError());
}
if(iMA_slow_handle != INVALID_HANDLE)
{
if(!IndicatorRelease(iMA_slow_handle))
Print("Erro no release do iMA_slow_handle: ", GetLastError());
}
if(iMA_long_handle != INVALID_HANDLE)
{
if(!IndicatorRelease(iMA_long_handle))
Print("Erro no release do iMA_long_handle: ", GetLastError());
}
string iMA_fast_chart = ChartIndicatorName(0, 0, 0);
if(glIntiChart1 && !ChartIndicatorDelete(0, 0, iMA_fast_chart))
Print("Erro na remoção do iMA_fast_handle: ", GetLastError());
string iMA_slow_chart = ChartIndicatorName(0, 0, 1);
if(glIntiChart2 && !ChartIndicatorDelete(0, 0, iMA_slow_chart))
Print("Erro na remoção do iMA_slow_handle: ", GetLastError());
string iMA_long_chart = ChartIndicatorName(0, 0, 2);
if(glIntiChart3 && !ChartIndicatorDelete(0, 0, iMA_long_chart))
Print("Erro na remoção do iMA_long_handle: ", GetLastError());
Print("Denicializaçao realizada com sucesso.");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+´
void OnTick()
{
ResetLastError();
if(CopyBuffer(iMA_fast_handle,0,0,3, iMA_fast_buffer)!=3)
{
Print("Erro ao atualizar os valores da media movel rapida.", GetLastError());
return;
}
if(CopyBuffer(iMA_slow_handle,0,0,3, iMA_slow_buffer)!=3)
{
Print("Erro ao atualizar os valores da media movel lenta.", GetLastError());
return;
}
if(CopyBuffer(iMA_long_handle,0,0,3, iMA_long_buffer)!=3)
{
Print("Erro ao atualizar os valores da média móvel longa.", GetLastError());
return;
}
g1_Tick++;
g1_Order=0;
g1_OpenPosition=false;
g1_PositionType=-1;
g1_Contratos=0;
g1_DobraMao=false;
//+------------------------------------------------------------------+
g1_OpenPosition=PositionSelect(_Symbol);
if(g1_OpenPosition==true)
{
g1_PositionType=PositionGetInteger(POSITION_TYPE);
g1_Contratos=PositionGetDouble(POSITION_VOLUME);
}
else
{
g1_PositionType=WRONG_VALUE;
}
//+------------------------------------------------------------------+
//| COMPRA |
//+------------------------------------------------------------------+
if((iMA_fast_buffer[0]>iMA_slow_buffer[0]>iMA_long_buffer[0]) &&
(iMA_slow_buffer[1]<iMA_fast_buffer[1]<iMA_long_buffer[1]))
{
g1_Order=1;
g1_TendenciaMa = "ALTA";
Print("Medias crurzadas, acionado compra.");
}
//+------------------------------------------------------------------+
//| VENDA |
//+------------------------------------------------------------------+
if((iMA_fast_buffer[0]<iMA_slow_buffer[0]<iMA_long_buffer[0]) &&
(iMA_slow_buffer[1]>iMA_fast_buffer[1]>iMA_long_buffer[1]))
{
g1_Order=-1;//pode vende
g1_TendenciaMa = "BAIXA";
Print("Medias cruzadas, acionado venda.");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
if(g1_OpenPosition)
if((g1_PositionType==POSITION_TYPE_BUY && g1_Order==-1) ||
(g1_PositionType==POSITION_TYPE_SELL && g1_Order==1))
{
g1_DobraMao=true;
Print("acionado virada de mao.");
}
//+------------------------------------------------------------------+
Comment("[EM EXECUÇAO] NEGOCIOS. ",g1_Tick, "\nCUSTODIA", g1_Contratos );
if(g1_Order!=0)
{
MqlTick price_info;
ZeroMemory(price_info);
if(!SymbolInfoTick(_Symbol,price_info))
{
Print("falha na atualizaçao de preço",GetLastError());
return;
}
double loc_NumeroContrato = Contratos;
if(g1_DobraMao==true)
{
loc_NumeroContrato=Contratos*2;
Print("dobrando numero de contratos para operaçao");
}
if(g1_Order==1)
{
Trade.Buy(loc_NumeroContrato,_Symbol,price_info.ask,price_info.ask-Stoploss,price_info.ask + Take);
Print("Executada ordem de compra");
}
if(g1_Order==-1)
{
Trade.Sell(loc_NumeroContrato,_Symbol,price_info.bid,price_info.bid+Stoploss,price_info.bid - Take);
Print("Executada ordem de venda");
}
}
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
- Operações de Gráficos - ChartIndicatorDelete
- Funções Matemáticas - MathMax
- Séries Temporais e Acesso a Indicadores - IndicatorRelease
lucassrangel49:
nessa parte ai que cotei, eu não teria habito de pegar direto das variaveis principalmente os dados que voce esta manipulando mesmo que eles parecam certos recomendaria voce jogar numa função de normalização pelo tamanho do tick pode ver um exemplo na CSymbol::normalizePrice. Não sei os erros que esta dando no seu código já que não disse, mas nessa parte desconfiaria de invalid_price ou invalid_stop.
if(g1_Order==1)
{
Trade.Buy(loc_NumeroContrato,_Symbol,price_info.ask,price_info.ask-Stoploss,price_info.ask + Take);
Print("Executada ordem de compra");
}
if(g1_Order==-1)
{
Trade.Sell(loc_NumeroContrato,_Symbol,price_info.bid,price_info.bid+Stoploss,price_info.bid - Take);
Print("Executada ordem de venda");
}
Você está perdendo oportunidades de negociação:
- Aplicativos de negociação gratuitos
- 8 000+ sinais para cópia
- Notícias econômicas para análise dos mercados financeiros
Registro
Login
Você concorda com a política do site e com os termos de uso
Se você não tem uma conta, por favor registre-se