MetaTrader5
Anpassbare Bollinger Bänder für MetaTrader 5 – So nutzen Sie den neuen Indikator
In der Welt des Tradings sind Bollinger Bänder ein bewährtes Werkzeug, um die Volatilität eines Marktes zu messen. Ich habe einen neuen Indikator entwickelt, der Ihnen eine interessante Alternative zu den standardmäßigen Berechnungsmethoden der Bollinger Bänder in MetaTrader 5 bietet. Während die Standardversion lediglich die "einfache" Methode verwendet, können Sie mit meinem Indikator aus mehreren Methoden wählen, darunter Exponential-, Glättungs- und Lineargewichtete Mittelwerte. Um diesen Indikator zu nutzen, müssen Sie ihn in ein Verzeichnis auf Ihrem Windows-Rechner verschieben, das folgendermaßen aussieht: C:\Users\IhrBenutzername\AppData\Roaming\MetaQuotes\Terminal\Indicators\Examples Zusätzliche Funktionen: Der Indikator ist standardmäßig auf Null gesetzt: Beispiel für die Anwendung mit LinearWeighted: CODE: //+------------------------------------------------------------------+
//| BBPersonalizada.mq5 |
//| Lucas Vidal |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Lucas Vidal"
#property link "https://www.mql5.com/en/users/lucasmoura00"
#property description "Anpassbare Bollinger Bänder"
#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 "Bänder Mitte"
#property indicator_label2 "Bänder Oben"
#property indicator_label3 "Bänder Unten"
//--- Eingabeparameter
enum MovingAverageMethod {
Einfach, // 0
Exponential, // 1
Geglättet, // 2
LinearGewichtet // 3
};
input MovingAverageMethod InpMaMethod = Einfach; // Methode der gleitenden Mittelwerte
input int InpBandsPeriod=20; // Zeitraum
input int InpBandsShift=0 // Verschiebung
input double InpBandsDeviations=2.0 // Abweichung
//--- globale Variablen
int ExtBandsPeriod,ExtBandsShift;
double ExtBandsDeviations;
int ExtPlotBegin=0;
//--- Indikator-Puffer
double ExtMLBuffer[];
double ExtTLBuffer[];
double ExtBLBuffer[];
double ExtStdDevBuffer[];
//+------------------------------------------------------------------+
//| Funktion zur Initialisierung des Indikators |
//+------------------------------------------------------------------+
void OnInit()
{
//--- Eingabewerte überprüfen
if(InpBandsPeriod<2)
{
ExtBandsPeriod=20;
PrintFormat("Ungültiger Wert für Eingabevariable InpBandsPeriod=%d. Der Indikator verwendet den Wert=%d für die Berechnungen.",InpBandsPeriod,ExtBandsPeriod);
}
else
ExtBandsPeriod=InpBandsPeriod;
if(InpBandsShift<0)
{
ExtBandsShift=0;
PrintFormat("Ungültiger Wert für Eingabevariable InpBandsShift=%d. Der Indikator verwendet den Wert=%d für die Berechnungen.",InpBandsShift,ExtBandsShift);
}
else
ExtBandsShift=InpBandsShift;
if(InpBandsDeviations==0.0)
{
ExtBandsDeviations=2.0;
PrintFormat("Ungültiger Wert für Eingabevariable InpBandsDeviations=%f. Der Indikator verwendet den Wert=%f für die Berechnungen.",InpBandsDeviations,ExtBandsDeviations);
}
else
ExtBandsDeviations=InpBandsDeviations;
//--- Puffer definieren
SetIndexBuffer(0,ExtMLBuffer);
SetIndexBuffer(1,ExtTLBuffer);
SetIndexBuffer(2,ExtBLBuffer);
SetIndexBuffer(3,ExtStdDevBuffer,INDICATOR_CALCULATIONS);
//--- Indexbezeichnungen setzen
PlotIndexSetString(0,PLOT_LABEL,"Bänder("+string(ExtBandsPeriod)+") Mitte");
PlotIndexSetString(1,PLOT_LABEL,"Bänder("+string(ExtBandsPeriod)+") Oben");
PlotIndexSetString(2,PLOT_LABEL,"Bänder("+string(ExtBandsPeriod)+") Unten");
//--- Indikatorname
IndicatorSetString(INDICATOR_SHORTNAME,"Bollinger Bänder");
//--- Einstellungen für das Zeichnen der Indizes
ExtPlotBegin=ExtBandsPeriod-1;
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtBandsPeriod);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtBandsPeriod);
PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtBandsPeriod);
//--- Einstellungen für die Verschiebung der Indizes
PlotIndexSetInteger(0,PLOT_SHIFT,ExtBandsShift);
PlotIndexSetInteger(1,PLOT_SHIFT,ExtBandsShift);
PlotIndexSetInteger(2,PLOT_SHIFT,ExtBandsShift);
//--- Anzahl der Ziffern des Indikatorwertes
IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
}
//+------------------------------------------------------------------+
//| Berechnung des gleitenden Durchschnitts |
//+------------------------------------------------------------------+
double CalculateMovingAverage(int position, int period, const double &price[]) {
switch(InpMaMethod) {
case Einfach:
return SimpleMA(position, period, price);
case Exponential:
// Aufruf der Funktion iMA mit den richtigen Parametern
return iMA(NULL, 0, period, 0, MODE_EMA, PRICE_CLOSE);
case Geglättet:
// Implementieren Sie hier Ihre SMMA-Funktion
break;
case LinearGewichtet:
return LinearWeightedMA(position, period, price);
}
return 0; // Standardrückgabe bei undefiniertem Methoden
}
//+------------------------------------------------------------------+
//| Bollinger Bänder |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
if(rates_total<ExtPlotBegin)
return(0);
//--- Einstellungen für den Beginn des Zeichnens, wenn wir den vorherigen Beginn erhalten haben
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);
}
//--- Berechnung starten
int pos;
if(prev_calculated>1)
pos=prev_calculated-1;
else
pos=0;
//--- Hauptschleife
for(int i=pos; i<rates_total && !IsStopped(); i++)
{
//--- Mittellinie
ExtMLBuffer[i]=CalculateMovingAverage(i, ExtBandsPeriod, price);
//--- StdDev berechnen und speichern
ExtStdDevBuffer[i]=StdDev_Func(i,price,ExtMLBuffer,ExtBandsPeriod);
//--- Obere Linie
ExtTLBuffer[i]=ExtMLBuffer[i]+ExtBandsDeviations*ExtStdDevBuffer[i];
//--- Untere Linie
ExtBLBuffer[i]=ExtMLBuffer[i]-ExtBandsDeviations*ExtStdDevBuffer[i];
}
//--- OnCalculate abgeschlossen. Rückgabe des neuen prev_calculated.
return(rates_total);
}
//+------------------------------------------------------------------+
//| Berechnung der Standardabweichung |
//+------------------------------------------------------------------+
double StdDev_Func(const int position,const double &price[],const double &ma_price[],const int period)
{
double std_dev=0.0;
//--- StdDev berechnen
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);
}
//--- Rückgabe des berechneten Wertes
return(std_dev);
}
//+------------------------------------------------------------------+
2024.04.28