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…
Code: Select all
void OnTick()
{
handleHilo = iCustom(Symbol(), Period(), "GannHilo", Periodo);
// Cópia dos buffers dos indicadores de média móvel com períodos curto e longo
ArraySetAsSeries(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 longa
if(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 longa
if(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;
}
}
}
Code: Select all
bool VerificarPrecoAcimaDoHiLo() {
for (int i = 0; i < intervaloBarras; i++) {
// Verifica se o preço de fechamento de cada barra está acima do valor HiLo correspondente
if (rates[i].close > hilo[i] && rates[i+1].close < hilo[i+1]) {
contadorTrocas++; // Pelo menos uma barra fechou abaixo do HiLo
}
else if(rates[i].close < hilo[i] && rates[i+1].close > hilo[i+1]){
contadorTrocas++;
}
}
if(contadorTrocas > alternanciaMinima){
return true;
}
else{
contadorTrocas = 0;
return false;
}
}
I don't know if I should make an ARRAY or if just doing a correctly FOR would be enough... Thanks again!