MetaTrader5
Bollinger Bands Personalizadas para MetaTrader 5: Aprenda a Usar
Se você é trader e busca otimizar suas análises, as Bollinger Bands Personalizadas para MetaTrader 5 podem ser uma ótima adição ao seu arsenal. Desenvolvi este indicador como uma alternativa às tradicionais bandas de Bollinger, que só oferecem o método simples de média móvel. Com a minha versão, você pode escolher entre métodos adicionais como Exponencial, Suavizada e Linear Ponderada. Para instalar o indicador, basta colocá-lo em um diretório que siga o seguinte caminho no Windows: C:\Users\seu_usuario\AppData\Roaming\MetaQuotes\Terminal\Indicators\Examples Características adicionais: Por padrão, o indicador é definido como zero: Veja um exemplo de execução usando a média Linear Ponderada: CÓDIGO: //+------------------------------------------------------------------+
//| BBPersonalizada.mq5 |
//| Lucas Vidal |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Lucas Vidal"
#property link "https://www.mql5.com/en/users/lucasmoura00"
#property description "Bollinger Bands Personalizada"
#include <MovingAverages.mqh>
//---
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 3
#property indicator_type1 DRAW_LINE
#property indicator_color1 LightSeaGreen
#property indicator_type2 DRAW_LINE
#property indicator_color2 LightSeaGreen
#property indicator_type3 DRAW_LINE
#property indicator_color3 LightSeaGreen
#property indicator_label1 "Banda Média"
#property indicator_label2 "Banda Superior"
#property indicator_label3 "Banda Inferior"
//--- parâmetros de entrada
enum MovingAverageMethod {
Simples, // 0
Exponencial, // 1
Suavizada, // 2
LinearPonderada // 3
};
input MovingAverageMethod InpMaMethod = Simples; // Método da Média Móvel
input int InpBandsPeriod=20; // Período
input int InpBandsShift=0 // Deslocamento
input double InpBandsDeviations=2.0 // Desvio
//--- variáveis globais
int ExtBandsPeriod,ExtBandsShift;
double ExtBandsDeviations;
int ExtPlotBegin=0;
//--- buffer do indicador
double ExtMLBuffer[];
double ExtTLBuffer[];
double ExtBLBuffer[];
double ExtStdDevBuffer[];
//+------------------------------------------------------------------+
//| Função de inicialização do indicador |
//+------------------------------------------------------------------+
void OnInit()
{
//--- verificação dos valores de entrada
if(InpBandsPeriod<2)
{
ExtBandsPeriod=20;
PrintFormat("Valor incorreto para a variável de entrada InpBandsPeriod=%d. O indicador usará o valor=%d para cálculos.",InpBandsPeriod,ExtBandsPeriod);
}
else
ExtBandsPeriod=InpBandsPeriod;
if(InpBandsShift<0)
{
ExtBandsShift=0;
PrintFormat("Valor incorreto para a variável de entrada InpBandsShift=%d. O indicador usará o valor=%d para cálculos.",InpBandsShift,ExtBandsShift);
}
else
ExtBandsShift=InpBandsShift;
if(InpBandsDeviations==0.0)
{
ExtBandsDeviations=2.0;
PrintFormat("Valor incorreto para a variável de entrada InpBandsDeviations=%f. O indicador usará o valor=%f para cálculos.",InpBandsDeviations,ExtBandsDeviations);
}
else
ExtBandsDeviations=InpBandsDeviations;
//--- definir buffers
SetIndexBuffer(0,ExtMLBuffer);
SetIndexBuffer(1,ExtTLBuffer);
SetIndexBuffer(2,ExtBLBuffer);
SetIndexBuffer(3,ExtStdDevBuffer,INDICATOR_CALCULATIONS);
//--- definir rótulos dos índices
PlotIndexSetString(0,PLOT_LABEL,"Banda("+string(ExtBandsPeriod)+") Média");
PlotIndexSetString(1,PLOT_LABEL,"Banda("+string(ExtBandsPeriod)+") Superior");
PlotIndexSetString(2,PLOT_LABEL,"Banda("+string(ExtBandsPeriod)+") Inferior");
//--- nome do indicador
IndicatorSetString(INDICATOR_SHORTNAME,"Bollinger Bands");
//--- configurações de início de desenho dos índices
ExtPlotBegin=ExtBandsPeriod-1;
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtBandsPeriod);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtBandsPeriod);
PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtBandsPeriod);
//--- configurações de deslocamento dos índices
PlotIndexSetInteger(0,PLOT_SHIFT,ExtBandsShift);
PlotIndexSetInteger(1,PLOT_SHIFT,ExtBandsShift);
PlotIndexSetInteger(2,PLOT_SHIFT,ExtBandsShift);
//--- número de dígitos do valor do indicador
IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
}
//+------------------------------------------------------------------+
//| Calcular Média Móvel |
//+------------------------------------------------------------------+
double CalculateMovingAverage(int position, int period, const double &price[]) {
switch(InpMaMethod) {
case Simples:
return SimpleMA(position, period, price);
case Exponencial:
// Corrigindo a chamada da função iMA com os parâmetros corretos
return iMA(NULL, 0, period, 0, MODE_EMA, PRICE_CLOSE);
case Suavizada:
// Implemente sua função SMMA aqui
break;
case LinearPonderada:
return LinearWeightedMA(position, period, price);
}
return 0; // Retorno padrão em caso de método indefinido
}
//+------------------------------------------------------------------+
//| Bollinger Bands |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
if(rates_total<ExtPlotBegin)
return(0);
//--- configurações de início de desenho dos índices, quando recebemos o início anterior
if(ExtPlotBegin!=ExtBandsPeriod+begin)
{
ExtPlotBegin=ExtBandsPeriod+begin;
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPlotBegin);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtPlotBegin);
PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtPlotBegin);
}
//--- início do cálculo
int pos;
if(prev_calculated>1)
pos=prev_calculated-1;
else
pos=0;
//--- ciclo principal
for(int i=pos; i<rates_total && !IsStopped(); i++)
{
//--- linha do meio
ExtMLBuffer[i]=CalculateMovingAverage(i, ExtBandsPeriod, price);
//--- calcular e escrever o desvio padrão
ExtStdDevBuffer[i]=StdDev_Func(i,price,ExtMLBuffer,ExtBandsPeriod);
//--- linha superior
ExtTLBuffer[i]=ExtMLBuffer[i]+ExtBandsDeviations*ExtStdDevBuffer[i];
//--- linha inferior
ExtBLBuffer[i]=ExtMLBuffer[i]-ExtBandsDeviations*ExtStdDevBuffer[i];
}
//--- Cálculo concluído. Retorne o novo prev_calculated.
return(rates_total);
}
//+------------------------------------------------------------------+
//| Calcular Desvio Padrão |
//+------------------------------------------------------------------+
double StdDev_Func(const int position,const double &price[],const double &ma_price[],const int period)
{
double std_dev=0.0;
//--- calcular Desvio Padrão
if(position>=period)
{
for(int i=0; i<period; i++)
std_dev+=MathPow(price[position-i]-ma_price[position],2.0);
std_dev=MathSqrt(std_dev/period);
}
//--- retornar o valor calculado
return(std_dev);
}
//+------------------------------------------------------------------+
2024.04.28