Das Konzept der Indicatorbuffer bzw Plottbuffer ist von Metaquotes beim MT5 leider ziemlich versiebt worden.
Da sind die Sünden der Vergangenheit vom MT4 übernommen worden.
Die statischen Definitionen durch Kompilerdirektiven sind wirklich Steinzeit.
Bei Indikatoren mit vielen Plotbuffern eine äussert fehleranfällige Angelegenheit.
Man hat allerdings mit PlotIndexSetInteger(..), PlotIndexSetDouble(...) etc. Möglichkeiten bereitgestell etwa dynamischer zu arbeiten.
Deshalb habe ich zwei Varianten geschaffen um mir das Programmiererdasein zu erleichtern.
Variante 1 stelle ich jetzt vor, die prozedurale Lösung.
Gehört natürlich in eine mqh Datei ausgelagert.
Code:
//+------------------------------------------------------------------+
//| Initialization of buffers as function |
//+------------------------------------------------------------------+
bool InitIndi(double &Buffer[], // the buffer
ENUM_INDEXBUFFER_TYPE BufType, // INDICATOR_DATA / INDICATOR_COLOR_INDEX / INDICATOR_CALCULATIONS
ENUM_DRAW_TYPE DrwType =DRAW_NONE, // DRAW_NONE .. DRAW_COLOR_CANDLES
ENUM_LINE_STYLE LineStyle=STYLE_SOLID, // STYLE_SOLID .. STYLE_DASHDOTDOT
int LineWidth=1, // PLOT_LINE_WIDTH - 1 ........
int LineColor=clrRed, // PLOT_LINE_COLOR - clr.......
int aIntValue=0, // for SMA,EMA,WMA the period / for Max/Min lookback / for ArrowCode
string PlotLabel="", // PLOT_LABEL - "Any String"
int PlotShift=0) // PLOT_SHIFT
{
static int BufIdx; // counter for bufferindex
SetIndexBuffer (BufIdx,Buffer, BufType );
PlotIndexSetInteger(BufIdx,PLOT_DRAW_TYPE, DrwType );
PlotIndexSetInteger(BufIdx,PLOT_LINE_STYLE, LineStyle);
PlotIndexSetInteger(BufIdx,PLOT_LINE_WIDTH, LineWidth);
PlotIndexSetString (BufIdx,PLOT_LABEL, PlotLabel);
PlotIndexSetDouble (BufIdx,PLOT_EMPTY_VALUE,NULL );
PlotIndexSetInteger(BufIdx,PLOT_SHIFT, PlotShift);
if(DrwType==DRAW_ARROW)
PlotIndexSetInteger(BufIdx,PLOT_ARROW, aIntValue);
if(LineColor==0)
PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,aIntValue+1);
else
PlotIndexSetInteger(BufIdx,PLOT_LINE_COLOR, LineColor);
BufIdx++; // increment bufferindex for next Init()
return(true);
}
Verwendet wird die Funktion dann etwa so:
Code:
#include <MyBuffers.mqh>
#define INDIS 5
#define PLOTS 3
#property indicator_buffers INDIS
#property indicator_plots PLOTS
input int inp_Period_1 = 6; // Period
input int inp_LWidth = 1; // Linewidth
input color inp_Color_1 = clrGold; // Color
double Dema1[], Ema11[], Ema12[];
double siUP1[], siDN1[];
void OnInit()
{
InitIndi(Dema1,INDICATOR_DATA,DRAW_LINE ,STYLE_SOLID,inp_LWidth,inp_Color_1, 0,"DEMA");
InitIndi(siUP1,INDICATOR_DATA,DRAW_ARROW,STYLE_SOLID,inp_LWidth,clrLime ,159,"Signal UP");
InitIndi(siDN1,INDICATOR_DATA,DRAW_ARROW,STYLE_SOLID,inp_LWidth,clrRed ,159,"Signal DN");
InitIndi(Ema11,INDICATOR_CALCULATIONS);
InitIndi(Ema12,INDICATOR_CALCULATIONS);
}
Der BufferIndex wird automatisch vergeben!
Für diese 4 Zeilen Programmcode wären auf konventionelle Art etwa VIERZIG Zeilen notwendig!
Variante 2 kommt das nächste mal, die OOP-Lösung.
Da bekommen die Plottbuffer Eigenschaften und Methoden.