// Robô de tendência com stop móvel, alvo e auto reverse// Evita operar em mercado lateral usando ADX
// Definição de parâmetros de entradaInputs:PeriodoMME(20),PeriodoADX(14),LimiteADX(20),AlvoTicks(38),StopTicks(20);
// Declaração de variáveisVars:MediaMME(0.0),ValorADX(0.0),TendenciaAlta(false),TendenciaBaixa(false);
begin// Cálculo da MME e do ADXMediaMME = XAverage(Close, PeriodoMME);ValorADX = ADX(PeriodoADX);
// Verifica se mercado está com tendênciaif ValorADX > LimiteADX thenbeginTendenciaAlta = Close > MediaMME;TendenciaBaixa = Close < MediaMME;
// Se não estiver posicionadoif MarketPosition = 0 thenbeginif TendenciaAlta thenBuy ("Compra") next bar at marketelse if TendenciaBaixa thenSellShort ("Venda") next bar at market;end;end;
// Se estiver comprado, aplica alvo, stop móvel e auto reverseif MarketPosition = 1 thenbeginSell ("StopMovel") next bar at EntryPrice - StopTicks * MinMove / PriceScale stop;Sell ("Alvo") next bar at EntryPrice + AlvoTicks * MinMove / PriceScale limit;
// Auto reverseif Close <= EntryPrice - StopTicks * MinMove / PriceScale thenSellShort ("AutoReverseV") next bar at marketelse if Close >= EntryPrice + AlvoTicks * MinMove / PriceScale thenSellShort ("AutoReverseV") next bar at market;end;
// Se estiver vendido, aplica alvo, stop móvel e auto reverseif MarketPosition = -1 thenbeginBuyToCover ("StopMovel") next bar at EntryPrice + StopTicks * MinMove / PriceScale stop;BuyToCover ("Alvo") next bar at EntryPrice - AlvoTicks * MinMove / PriceScale limit;
// Auto reverseif Close >= EntryPrice + StopTicks * MinMove / PriceScale thenBuy ("AutoReverseC") next bar at marketelse if Close <= EntryPrice - AlvoTicks * MinMove / PriceScale thenBuy ("AutoReverseC") next bar at market;end;end;
Alguém pode ajudar? Grato
inputPeriodoMME(20);PeriodoADX(14);LimiteADX(20);AlvoTicks(38);StopTicks(20);tamanhoTick(1.0); // ajuste para WIN=1 ou WDO=0.5
varmme, adxValor : float;alvoCompra, stopCompra, alvoVenda, stopVenda : float;podeComprar, podeVender : boolean;
beginmme := MediaExp(PeriodoMME, Close);adxValor := ADX(PeriodoADX, 0); // ✅ Corrigido: 2 parâmetros
podeComprar := false;podeVender := false;
// Verifica tendênciaif adxValor > LimiteADX thenbeginif (Close > mme) thenpodeComprar := trueelse if (Close < mme) thenpodeVender := true;end;
// Entrada compradaif not HasPosition and podeComprar thenbeginBuyAtMarket;alvoCompra := Close + AlvoTicks * tamanhoTick;stopCompra := Close - StopTicks * tamanhoTick;end;
// Entrada vendidaif not HasPosition and podeVender thenbeginSellShortAtMarket;alvoVenda := Close - AlvoTicks * tamanhoTick;stopVenda := Close + StopTicks * tamanhoTick;end;
// Gestão posição compradaif IsBought thenbeginif Close <= stopCompra thenbeginSellToCoverAtMarket;SellShortAtMarket; // Auto reverseendelse if Close >= alvoCompra thenbeginSellToCoverAtMarket;SellShortAtMarket; // Auto reverseend;end;
// Gestão posição vendidaif IsSold thenbeginif Close >= stopVenda thenbeginBuyToCoverAtMarket;BuyAtMarket; // Auto reverseendelse if Close <= alvoVenda thenbeginBuyToCoverAtMarket;BuyAtMarket; // Auto reverseend;end;end;