
Abordamos o tema de automatização de estratégias em NTSL, MQL5 e NinjaScript!
estou com um código de divergencia, mas estou apanhando pacas pq não está indo do jeito que quero no renko. só faz compra e não compra nos lugares certos.
var Maxima1, Maxima2, Maxima3, Minima1, Minima2, Minima3: Float; Maxima4, Maxima5, Maxima6, Minima4, Minima5, Minima6: Float; ADXValor, DIPlus, DIMinus: Float; RSI1MAXIMA, RSI2MAXIMA, RSI1MINIMA, RSI2MINIMA: Float; MAX1, MAX2, MIN1, MIN2: Float; Parte1Concluida: Boolean; StopLoss, StopGain: Float; // Função para verificar se um Renko é branco function RenkoBranco(index: Integer): Boolean; begin Result := Close[index] > Open[index]; end; // Função para verificar se um Renko é preto function RenkoPreto(index: Integer): Boolean; begin Result := Close[index] < Open[index]; end; begin // Inicialização de variáveis Maxima1 := 0; Maxima2 := 0; Maxima3 := 0; Minima1 := 0; Minima2 := 0; Minima3 := 0; Maxima4 := 0; Maxima5 := 0; Maxima6 := Highest(High, 55); Minima4 := 0; Minima5 := 0; Minima6 := Lowest(Low, 55); ADXValor := 0; DIPlus := 0; DIMinus := 0; RSI1MAXIMA := 0; RSI2MAXIMA := 0; RSI1MINIMA := 0; RSI2MINIMA := 0; MAX1 := 0; MAX2 := 0; MIN1 := 0; MIN2 := 0; Parte1Concluida := False; StopLoss := 0; StopGain := 0; // Cálculo do ADX e DI+/- (Índice de Movimento Direcional) ADXValor := ADX(14, 9); DIPlus := DiPDim(14)|0|; // DI+ (índice positivo) DIMinus := DiPDiM(14)|1|; // DI- (índice negativo) // Reset das condições se ADX ou DI+/- caírem abaixo de 32 if (ADXValor < 32) or (DIPlus < 32) or (DIMinus < 32) then begin Parte1Concluida := False; MAX1 := 0; MAX2 := 0; MIN1 := 0; MIN2 := 0; end; // Parte 1: Identificação do primeiro swing (MAX2 e MIN2) if RenkoBranco(2) and RenkoBranco(1) and RenkoPreto(0) then begin Maxima2 := High[1]; MAX2 := Maxima2; end; if RenkoPreto(2) and RenkoPreto(1) and RenkoBranco(0) then begin Minima2 := Low[1]; MIN2 := Minima2; end; Parte1Concluida := (MAX2 > 0) and (MIN2 > 0); // Parte 2: Identificação do segundo swing (MAX1 e MIN1) if (Parte1Concluida) then begin if RenkoBranco(2) and RenkoBranco(1) and RenkoPreto(0) then begin Maxima5 := High[1]; MAX1 := Maxima5; end; if RenkoPreto(2) and RenkoPreto(1) and RenkoBranco(0) then begin Minima5 := Low[1]; MIN1 := Minima5; end; end; // Cálculo do Stop Loss e Stop Gain if (Maxima5 > 0) and (Minima5 > 0) then begin StopLoss := Abs(Maxima5 - Minima5); StopGain := StopLoss * 3; end; // Regra de Venda (Reversão de Alta para Baixa) if (MAX2 < MAX1) then begin RSI1MAXIMA := RSI(9, 0); // RSI atual RSI2MAXIMA := RSI(9, 0)[1]; // RSI anterior // Divergência negativa: RSI caindo enquanto o preço sobe if (RSI2MAXIMA > RSI1MAXIMA) and (Minima6 > 0) then begin SellShortLimit(Minima6); // Venda limite na mínima atual StopLoss := Maxima5; // Stop na Maxima5 StopGain := Minima6 - (StopLoss * 3); end; end; // Regra de Compra (Reversão de Baixa para Alta) if (MIN2 > MIN1) then begin RSI1MINIMA := RSI(9, 0); // RSI atual RSI2MINIMA := RSI(9, 0)[1]; // RSI anterior // Divergência positiva: RSI subindo enquanto o preço cai if (RSI2MINIMA < RSI1MINIMA) and (Maxima6 > 0) then begin BuyLimit(Maxima6); // Compra limite na máxima atual StopLoss := Minima5; // Stop na Minima5 StopGain := Maxima6 + (StopLoss * 3); end; end; // Adicionando os Plots para visualizar os valores Plot(MAX1); Plot(MAX2); Plot(MIN1); Plot(MIN2); Plot(Minima6); Plot(Maxima6); Plot(StopLoss); Plot(StopGain); end.