Kaique Mitsuo Silva Yamamoto
Mercado financeiroAutomação de EstratégiasProfit / Nelogica NTSL

NTSL — Indicadores Técnicos

Referência dos principais indicadores técnicos disponíveis em NTSL no Profit Nelogica: médias móveis, osciladores, volatilidade, volume, tendência e como usar em estratégias e robôs.

NTSL — Indicadores Técnicos

O Profit disponibiliza mais de 150 indicadores técnicos prontos para uso em código NTSL. Eles retornam Float (ou Serie) e podem ser usados diretamente em condições, plots e cálculos.

Fundamentos: NTSL — Fundamentos | Controle de fluxo: NTSL — Controle de Fluxo


Médias Móveis

Media — Média Móvel Simples (SMA)

Função Media(Periodo: Integer; Fonte: Serie): Float;

// Exemplo
var sMA21 : Float;
begin
  sMA21 := Media(21, Close);
  Plot(sMA21);
end;

MediaExp — Média Exponencial (EMA)

Função MediaExp(Periodo: Integer; Fonte: Serie): Float;

begin
  Plot(MediaExp(9, Close));
  Plot2(MediaExp(21, Close));
end;

MediaPonderada — Média Ponderada (WMA)

Função MediaPonderada(Periodo: Integer; Fonte: Serie): Float;

Welles — Média Welles Wilder (RMA/SMMA)

Função Welles(Periodo: Integer; Fonte: Serie): Float;

// Usada internamente pelo ATR e ADX
begin
  Plot(Welles(14, Close));
end;

Combinando Médias — Cruzamento

input
  PeriodoRapida(9);
  PeriodoLenta(21);

var
  sRapida, sLenta : Serie;

begin
  sRapida := Media(PeriodoRapida, Close);
  sLenta  := Media(PeriodoLenta,  Close);

  Plot(sRapida);
  Plot2(sLenta);

  if (sRapida > sLenta) and (sRapida[1] <= sLenta[1]) then
    PaintBar(clVerde)
  else if (sRapida < sLenta) and (sRapida[1] >= sLenta[1]) then
    PaintBar(clVermelho);
end;

Osciladores

RSI / IFR

Função RSI(Periodo: Integer): Float;
// Equivalente em português: IFR()

input PeriodoRSI(14);

begin
  Plot(RSI(PeriodoRSI));

  if (RSI(PeriodoRSI) < 30) then
    PaintBar(clVerde);   // sobrevendido
  if (RSI(PeriodoRSI) > 70) then
    PaintBar(clVermelho); // sobrecomprado
end;

MACD

Função MACD(Rapida: Integer; Lenta: Integer; Sinal: Integer): Float;
// Retorna a linha MACD (diferença das médias)
// Para acessar a linha de sinal: MACD(12,26,9)|1|
// Para acessar o histograma: MACD(12,26,9)|2|

begin
  Plot(MACD(12, 26, 9));         // linha MACD
  Plot2(MACD(12, 26, 9)|1|);    // linha de sinal
  Plot3(MACD(12, 26, 9)|2|);    // histograma
end;

Stochastic / Estocástico

Função Stochastic(Periodo: Integer; SuavizacaoK: Integer; SuavizacaoD: Integer): Float;
// |0| = %K, |1| = %D

begin
  Plot(Stochastic(14, 3, 3));         // %K
  Plot2(Stochastic(14, 3, 3)|1|);    // %D
end;

CCI — Commodity Channel Index

Função CCI(Periodo: Integer): Float;

begin
  Plot(CCI(20));
  if (CCI(20) > 100) then PaintBar(clVerde);
  if (CCI(20) < -100) then PaintBar(clVermelho);
end;

Williams %R

Função WilliamsR(Periodo: Integer): Float;

begin
  Plot(WilliamsR(14));
end;

Volatilidade

ATR — Average True Range

Função ATR(Periodo: Integer): Float;

input PeriodoATR(14);

var fAtr : Float;
begin
  fAtr := ATR(PeriodoATR);
  Plot(fAtr);

  // Stop baseado em ATR
  if IsBought then
    SellToCoverStop(BuyPrice - 2 * fAtr, BuyPrice - 2 * fAtr);
end;

Bollinger Bands

Função BollingerBands(Periodo: Integer; Desvios: Float): Float;
// |0| = banda superior, |1| = média, |2| = banda inferior

input
  PeriodoBB(20);
  DesviosBB(2.0);

begin
  Plot(BollingerBands(PeriodoBB, DesviosBB));          // superior
  Plot2(BollingerBands(PeriodoBB, DesviosBB)|1|);     // média
  Plot3(BollingerBands(PeriodoBB, DesviosBB)|2|);     // inferior

  // Squeeze: quando as bandas estão próximas
  var fLargura : Float;
  fLargura := BollingerBands(PeriodoBB, DesviosBB) -
              BollingerBands(PeriodoBB, DesviosBB)|2|;
  Plot4(fLargura);
end;

Tendência

ADX / DMI

Função ADX(Periodo: Integer): Float;
// |0| = ADX, |1| = +DI, |2| = -DI

begin
  Plot(ADX(14));         // linha ADX
  Plot2(ADX(14)|1|);    // +DI
  Plot3(ADX(14)|2|);    // -DI

  // Filtro de tendência: ADX > 25 = tendência definida
  if (ADX(14) > 25) then
    PaintBar(clAzul);
end;

HiLo Activator

Função HiloActivator(Periodo: Integer): Float;
// |0| = linha HiLo, |1| = tendência (1=alta, -1=baixa)

begin
  Plot(HiloActivator(3));

  if (HiloActivator(3)|1| = 1) then
    SetPlotColor(1, clVerde)
  else
    SetPlotColor(1, clVermelho);
end;

Volume e Preço Médio

VWAP — Volume Weighted Average Price

Função VWAP(): Float;
// Preço médio ponderado pelo volume do dia

begin
  Plot(VWAP);

  if (Close > VWAP) then
    PaintBar(clVerde)
  else
    PaintBar(clVermelho);
end;

OBV — On Balance Volume

Função OBV(): Float;

begin
  Plot(OBV);
end;

MedianPrice

Função MedianPrice(): Float;
// (High + Low) / 2

begin
  Plot(MedianPrice);
end;

TypicalPrice

Função TypicalPrice(): Float;
// (High + Low + Close) / 3

Outros Indicadores Comuns

Parabolic SAR

Função ParabolicSAR(Aceleracao: Float; Maximo: Float): Float;

begin
  Plot(ParabolicSAR(0.02, 0.20));
end;

Momentum

Função Momentum(Periodo: Integer; Fonte: Serie): Float;

begin
  Plot(Momentum(12, Close));
end;

ROC — Rate of Change

Função ROC(Periodo: Integer): Float;

begin
  Plot(ROC(9));
end;

Usando Indicadores em Estratégias de Execução

Exemplo: Robô RSI + Médias

input
  PeriodoMA(21);
  PeriodoRSI(14);
  NivelSobrevenda(30);
  NivelSobrecompra(70);

var
  fMA  : Float;
  fRSI : Float;

begin
  if CurrentBar < PeriodoMA then exit;

  fMA  := Media(PeriodoMA, Close);
  fRSI := RSI(PeriodoRSI);

  // Entrada: close acima da MA e RSI sobrevendido
  if not HasPosition then
  begin
    if (Close > fMA) and (fRSI < NivelSobrevenda) then
      BuyAtMarket;

    if (Close < fMA) and (fRSI > NivelSobrecompra) then
      SellShortAtMarket;
  end;

  // Saída no cruzamento contrário da MA
  if IsBought and (Close < fMA) then
    ClosePosition;
  if IsSold and (Close > fMA) then
    ClosePosition;

  // Encerrar no fim do dia
  if (Time >= 1655) then ClosePosition;
end;

Acessando Múltiplos Retornos de Indicadores

Alguns indicadores retornam múltiplos valores via |N|:

// MACD
MACD(12,26,9)       // linha MACD
MACD(12,26,9)|1|    // linha de sinal
MACD(12,26,9)|2|    // histograma

// Bollinger
BollingerBands(20,2)       // banda superior
BollingerBands(20,2)|1|    // média central
BollingerBands(20,2)|2|    // banda inferior

// ADX
ADX(14)       // ADX
ADX(14)|1|    // +DI
ADX(14)|2|    // -DI

// HiLo Activator
HiloActivator(3)       // linha de preço
HiloActivator(3)|1|    // tendência (+1 ou -1)

// Stochastic
Stochastic(14,3,3)       // %K
Stochastic(14,3,3)|1|    // %D

Referências

Referências externas

On this page