Hi guys, I swear I'm trying to train my coding and trying a simple hilo EA that with each new bar close signal above or below the indicator it checks how many signals (be above or below) there were in the last X previous bars (only bars independing the time) and if this number of signals/toggle is greater than Y in the range of X bars make the trade, else not, and check it again each new signal.
I'm new to this but I SWEAR I've been trying for DAYS, I've tried all the searches and even chatGPT (useless I think lol). I know it must be something simple to do, but I feel like when I get it right on one side I get it wrong on the other haha. I will attach part of the code below with the function that I call for this "check".
Any help would be welcome. Thank you very much in advance!! And sorry for the rusty English...
//+------------------------------------------------------------------+//| Canais01.mq5 |//| Copyright 2023, MetaQuotes Ltd. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2023, MetaQuotes Ltd."#property link"https://www.mql5.com"#property version"1.00"#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
CTrade trade;
CSymbolInfo simbolo; // Classe responsãvel pelos dados do ativoinputint Periodo = 20; // Período Média//input int PeriodoCurto = 20; // Período Média Curtainputdouble SL = 0.0; // Stop Lossinputdouble TP = 0.0; // Take Profitinputdouble lotes = 1; // Volume Inicialinputstring inicio = "09:00"; // Horário de Início (entradas)inputstring termino = "17:00"; // Horário de Término (entradas)inputstring fechamento = "17:30"; // Horário de Fechamento (posições)inputint intervaloBarras = 10; // Intervalo de Barrasinputint alternanciaMinima = 1; // Alternancia Minimabool HabilitarVenda = true;
bool HabilitarCompra = true;
int count_above_ma = 0;
int contadorTrocas = 0;
int handleHilo; // Manipuladores dos dois indicadores de média móveldouble hilo[];
// Estruturas de tempo para manipulação de horáriosMqlDateTime horario_inicio, horario_termino, horario_fechamento, horario_atual;
MqlRates rates[];
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//---if(!simbolo.Name(_Symbol))
{
printf("Ativo Inválido!");
returnINIT_FAILED;
}
// Criação dos manipuladores com Períodos curto e longo// Criação das structs de tempoTimeToStruct(StringToTime(inicio), horario_inicio);
TimeToStruct(StringToTime(termino), horario_termino);
TimeToStruct(StringToTime(fechamento), horario_fechamento);
//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//---
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
handleHilo = iCustom(Symbol(), Period(), "GannHilo", Periodo);
// Cópia dos buffers dos indicadores de média móvel com períodos curto e longoArraySetAsSeries(hilo, true);
ArraySetAsSeries(rates, true);
CopyBuffer(handleHilo, 0, 0, 50, hilo);
CopyRates(Symbol(), Period(), 0, 50, rates);
//---if(HorarioEntrada())
{
// Compra em caso de cruzamento da média curta para cima da média longaif(rates[1].close > hilo[1] && rates[2].close < hilo[2] && HabilitarCompra == true){
bool alternador = VerificarPrecoAcimaDoHiLo();
if (alternador) {
// Prossiga com a ordem de compra
CloseAllSellPositions();
Compra();
Print("#############SINAIS TROCADOS NAS ULTIMAS BARRAS###################: ", contadorTrocas);
HabilitarCompra = false;
HabilitarVenda = true;
contadorTrocas = 0;
}
}
// Venda em caso de cruzamento da média curta para baixo da média longaif(rates[1].close < hilo[1] && rates[2].close > hilo[2] && HabilitarVenda == true){
bool alternador = VerificarPrecoAcimaDoHiLo();
if (alternador) {
// Prossiga com a ordem de venda
CloseAllBuyPositions();
Venda();
Print("#############SINAIS TROCADOS NAS ULTIMAS BARRAS###################: ", contadorTrocas);
HabilitarVenda = false;
HabilitarCompra = true;
contadorTrocas = 0;
}
}
}
bool VerificarPrecoAcimaDoHiLo() {
for (int i = 0; i < intervaloBarras; i++) {
// Verifica se o preço de fechamento de cada barra está acima do valor HiLo correspondenteif (rates[i].close > hilo[i] && rates[i+1].close < hilo[i+1]) {
contadorTrocas++; // Pelo menos uma barra fechou abaixo do HiLo
}
elseif(rates[i].close < hilo[i] && rates[i+1].close > hilo[i+1]){
contadorTrocas++;
}
}
if(contadorTrocas > alternanciaMinima){
returntrue;
}
else{
contadorTrocas = 0;
returnfalse;
}
}
I don't know if I should make an ARRAY or if just doing a correctly FOR would be enough... Thanks again!
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hi guys, I swear I'm trying to train my coding and trying a simple hilo EA that with each new bar close signal above or below the indicator it checks how many signals (be above or below) there were in the last X previous bars (only bars independing the time) and if this number of signals/toggle is greater than Y in the range of X bars make the trade, else not, and check it again each new signal.
I'm new to this but I SWEAR I've been trying for DAYS, I've tried all the searches and even chatGPT (useless I think lol). I know it must be something simple to do, but I feel like when I get it right on one side I get it wrong on the other haha. I will attach part of the code below with the function that I call for this "check".
Any help would be welcome. Thank you very much in advance!! And sorry for the rusty English...
I don't know if I should make an ARRAY or if just doing a correctly FOR would be enough... Thanks again!