Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools

Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools (http://www.expert-advisor.com/forum/index.php)
-   Programmierung MQL5 (http://www.expert-advisor.com/forum/forumdisplay.php?f=221)
-   -   Die ColorLine bringt mich zur Verzweiflung (http://www.expert-advisor.com/forum/showthread.php?t=5345)

Kronenchakra 17.12.16 01:01

Die ColorLine bringt mich zur Verzweiflung
 
Hallo MQL5-Gurus,
ich hab den ganzen Nachmitag versch......wendet wegen dieser ColorLine und schnall es immer noch nicht :mad:

Die Line und ColorLine sollen dynamisch erzeugt werden. Brauche ich für meinen OOP Bufferhandler.
Den hab ich ja bei den Codeschnipseln schon vor längerer Zeit angekündigt.

Ich habe aus der Doku ein Beispiel abgekupfert und umgebaut, das allerdings ColorCandles verwendet.
https://www.mql5.com/de/docs/customi...ndexgetinteger

Ich hoffe jemand kommt drauf was da nicht stimmt. Herr Doctor ?

Code:

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1

double  L_buffer[],    // linebuffer
        C_buffer[];    // colorbuffer

int      handle;

#define  nColors 2

color    colors[nColors] = {clrRed,clrLime};

//+------------------------------------------------------------------+
input int                  MA_Period = 20;            // Period
input ENUM_MA_METHOD      MA_Method = MODE_EMA;      // Mode
input ENUM_APPLIED_PRICE  MA_Price  = PRICE_TYPICAL; // Price
input bool                WithColor = false;        // Draw as ColorLine
//+------------------------------------------------------------------+

int OnInit()
{
  handle=iMA(_Symbol,PERIOD_CURRENT, MA_Period, 0, MA_Method, MA_Price);
  if(handle==INVALID_HANDLE) return(INIT_FAILED);
 
  SetIndexBuffer(0,L_buffer,INDICATOR_DATA);
  SetIndexBuffer(1,C_buffer,INDICATOR_COLOR_INDEX);

  if(WithColor)
      {
        PlotIndexSetInteger(0,PLOT_DRAW_TYPE,  DRAW_COLOR_LINE);
        PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,nColors);
        for(int i=0; i<nColors; i++)
            PlotIndexSetInteger(0,PLOT_LINE_COLOR,i, colors[i]);
      }
  else
      { 
        PlotIndexSetInteger(0,PLOT_DRAW_TYPE,  DRAW_LINE);
        PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrAqua);
      }
     
  PlotIndexSetInteger(0,PLOT_DRAW_BEGIN, MA_Period);
  PlotIndexSetDouble (0,PLOT_EMPTY_VALUE,NULL);
  PlotIndexSetString (0,PLOT_LABEL, "MA_Test");
 

  ArrayInitialize(L_buffer,NULL);
  ArrayInitialize(C_buffer,NULL);
 
  return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+

int OnCalculate (const int rates_total,      // size of input time series
                const int prev_calculated,  // bars handled in previous call
                const datetime& time[],    // Time
                const double& open[],      // Open
                const double& high[],      // High
                const double& low[],        // Low
                const double& close[],      // Close
                const long& tick_volume[],  // Tick Volume
                const long& volume[],      // Real Volume
                const int& spread[])        // Spread
{
  int bar, idxStart, to_copy;
 
  idxStart=StartIndex(rates_total,prev_calculated,to_copy,1);
 
  if(CopyBuffer(handle,0,0,to_copy,L_buffer)<0)
      return(0);

  if(idxStart>=1)
      for(bar=idxStart; bar<rates_total-1; bar++)
        {
            if(L_buffer[bar-1]<L_buffer[bar])          // IndiLine goes UP
              C_buffer[bar]=colors[1];
             
            if(L_buffer[bar-1]>L_buffer[bar])          // IndiLine goes DOWN
              C_buffer[bar]=colors[0];
        }

  return(rates_total);
}

//+------------------------------------------------------------------+

int StartIndex(int rates_total, int prev_calculated, int &to_copy, int add=1)
{
  add=(add>=1)?1:(add<0)?0:add;    // add ist entweder 0 oder 1
  if(prev_calculated<=0)
      {
        to_copy=rates_total;
        return(0);
      }
  to_copy=rates_total-prev_calculated+add;
  return(prev_calculated-1);
}

Grüße Otto

traderdoc 17.12.16 11:05

Was genau stimmt denn nun nicht?

traderdoc

Kronenchakra 17.12.16 11:50

Die ColorLine wechselt die Farbe nicht, sondern ist bei mir nur einfärbig.

Es gibt soger Beispiele mit Farbverläufen. Ich verstehe den Zusammenhang von Linie und Farbbuffer noch nicht wirklich.

Meinem derzeitigen Vermutungsstand, von Wissensstand kann ich noch nicht sprechen, sind die Zusammenhänge folgendermaßen:

Die ColorLine speichert in sich die verschiedenen Farben. Deshalb diese Definition:
Code:

color colors[2] = {clrRed,clrLime};    // ein Array von Farben

PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_COLOR_LINE);  // definiert den zu zeichnenden Typ
PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,nColors);      // die Anzahl der möglichen Farben
for(int i=0; i<nColors; i++)
  PlotIndexSetInteger(0,PLOT_LINE_COLOR,i, colors[i]); // hier werden die Farben eingetragen

Die ColorLine nimmt automatisch den darauffolgenden Buffer als Indexbuffer.
Code:

  SetIndexBuffer(0,L_buffer,INDICATOR_DATA);
  SetIndexBuffer(1,C_buffer,INDICATOR_COLOR_INDEX);

Im ColorBuffer wird dann der Index der Farbe, die in der Colorline gespeichert ist eingetragen.
Sind alles mehr Vermutungen als Wissen.

Ein super Beispiel ist auch dieses https://www.mql5.com/de/code/16838
Ich begreife es trotzdem bisher nicht :confused:

Grüße Otto

Kronenchakra 17.12.16 12:54

Was ich nicht verstehe
 
Ich verstehe die Buffernummern (statisch Definition) und die Bufferindizierung(dynamische Definition) bei ColorLines nicht.
Daß die Buffernummern bei 1 anfangen und die Indizes bei 0 ist mir schon klar.

Code aus dem Beispiel: https://www.mql5.com/de/code/16838
Code:

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots  2

#property indicator_label1  "T3 velocity histo"
#property indicator_type1  DRAW_COLOR_HISTOGRAM
//#property indicator_color1  clrLimeGreen,clrOrange
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

#property indicator_label2  "T3 velocity line"
#property indicator_type2  DRAW_COLOR_LINE
//#property indicator_color2  clrLimeGreen,clrOrange
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

Die von mir auskommentierten Zeilen sind unnötig und verwirrend.
Bei normalen DRAW_LINE wäre das doch Bufferindex 0 und 1

Jetzt das Mirakel in OnInit:
Code:

int OnInit()
{
  SetIndexBuffer(0,velH,INDICATOR_DATA);
  SetIndexBuffer(1,colorBufferH,INDICATOR_COLOR_INDEX);
 
  SetIndexBuffer(2,velL,INDICATOR_DATA);
  SetIndexBuffer(3,colorBufferL,INDICATOR_COLOR_INDEX);
 
  cSteps = (ColorSteps>1) ? ColorSteps : 2;

  PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,cSteps+1);
  PlotIndexSetInteger(1,PLOT_COLOR_INDEXES,cSteps+1);

  for (int i=0;i<cSteps+1;i++) PlotIndexSetInteger(0,PLOT_LINE_COLOR,i,gradientColor(i,cSteps+1,ColorFrom,ColorTo));
  for (int i=0;i<cSteps+1;i++) PlotIndexSetInteger(1,PLOT_LINE_COLOR,i,gradientColor(i,cSteps+1,ColorFrom,ColorTo));

  return(INIT_SUCCEEDED);
}

Da werden doch die Bufferindizes verschoben ! :confused:
Es ist für mich vollkommen verwirrend.

Oh, ich glaube jetzt beim Schreiben habe ich es begriffen. :eek::D
Die Indizes bei SetIndexBuffer() haben nichts mit den Indizes der PlotIndexSet...() Funktionen zu tun.
Code:

SetIndexBuffer(3,colorBufferL,INDICATOR_COLOR_INDEX);
wird bei den PlotBuffern NICHT mitgezählt.

Ja, das ist es! Und es ist so einfach (wenn man es weis)

Grüße Otto

Kronenchakra 21.12.16 20:18

Indexbuffer / Plotbuffer ich denke ich hab das Problem durchschaut
 
Die Anzahl der Indexbuffer und Plottbuffer werden separat verwaltet und haben andere Indizes.
Das tritt nur dann auf wenn manColorLines etc definiert.
Ist nichts mit Color dabei sind diese Indizes ident.
Ich bin durch den T3_velocity indikator draufgekommen.
Der definiertColorline und ColorHistogram.
Ich habe noch Erklärungen als Comment hineingeschrieben
Hier der Code für MT5. Ist auf dynamische Definition der Buffer umgeschrieben
Code:

//------------------------------------------------------------------
// https://www.mql5.com/en/code/16838 von Mladen Rakic
//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots  2

//#property indicator_label1  "T3 velocity histo"
//#property indicator_type1  DRAW_COLOR_HISTOGRAM
//#property indicator_style1  STYLE_SOLID
//#property indicator_width1  2
//
//#property indicator_label2  "T3 velocity line"
//#property indicator_type2  DRAW_COLOR_LINE
//#property indicator_style2  STYLE_SOLID
//#property indicator_width2  2

input double  T3Period    = 12;            // T3 velocity calculation period
input double  T3Hot      = 1.0;            // T3 hot value
input bool    T3Original  = false;          // T3 original Tillson calculation?
input int      ColorNorm  = 20;            // Colors normalization period
input color    ColorFrom  = clrRed;        // Color down
input color    ColorTo    = clrLime;        // Color Up
input int      ColorSteps  = 20;            // Color steps for drawing

double velL[];            // Line
double velH[];            // Histogram

double colorBufferL[];    // Line
double colorBufferH[];    // Histogram

int cSteps;

int OnInit()
{
  SetIndexBuffer(0,velH,INDICATOR_DATA);                      // Indexbuffer werden einfach durchnummeriert,egal was der TYÜ ist
  SetIndexBuffer(1,colorBufferH,INDICATOR_COLOR_INDEX);      // ColorIndexes werden bei INDICATOR_COLOR_INDEX NICHT mitgezählt, wachreinlich auch bei INDICATOR_CALCULATIONS
  SetIndexBuffer(2,velL,INDICATOR_DATA);                      // wird mitgezählt
  SetIndexBuffer(3,colorBufferL,INDDICATOR_COLOR_INDEX);      // wird nicht mitgezählt
 
  PlotIndexSetString (0,PLOT_LABEL,"T3 velocity histo");      // PlotIndexes gehen jetzt von 0 bis 1
  PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_COLOR_HISTOGRAM); // das muß man erst einmal behirnen !
  PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID);
  PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);
 
  PlotIndexSetString (1,PLOT_LABEL,"T3 velocity line");
  PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_COLOR_LINE);
  PlotIndexSetInteger(1,PLOT_LINE_STYLE,STYLE_SOLID);
  PlotIndexSetInteger(1,PLOT_LINE_WIDTH,2);
 
  cSteps = (ColorSteps>1) ? ColorSteps : 2;

  PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,cSteps+1);
  PlotIndexSetInteger(1,PLOT_COLOR_INDEXES,cSteps+1);

  for (int i=0;i<cSteps+1;i++) PlotIndexSetInteger(0,PLOT_LINE_COLOR,i,gradientColor(i,cSteps+1,ColorFrom,ColorTo));
  for (int i=0;i<cSteps+1;i++) PlotIndexSetInteger(1,PLOT_LINE_COLOR,i,gradientColor(i,cSteps+1,ColorFrom,ColorTo));

  return(INIT_SUCCEEDED);
}

int OnCalculate (const int rates_total,      // size of the price[] array
                const int prev_calculated,  // bars handled on a previous call
                const int begin,            // where the significant data start from
                const double& price[])      // array to calculate
{
  for (int i=(int)MathMax(prev_calculated-1,0); i<rates_total; i++)
      {
          velL[i] = iT3(price[i],T3Period,T3Hot    ,T3Original,i,rates_total,0)-
                    iT3(price[i],T3Period,T3Hot/2.0,T3Original,i,rates_total,1);     
          velH[i] = velL[i];
          double min = velL[i];
          double max = velL[i];
          double sto = 50;
          for (int k=1; k<ColorNorm && (i-k)>=0; k++)
            {
              min = MathMin(min,velL[i-k]);
              max = MathMax(max,velL[i-k]);
            }
            if (max!=min)
              sto = 100*(velL[i]-min)/(max-min);
              colorBufferL[i] = MathFloor(sto*cSteps/100.0);                                 
              colorBufferH[i] = MathFloor(sto*cSteps/100.0);                                 
      }
  return(rates_total);
}

double workT3      [][12];
double workT3Coeffs[][12];

#define _period 0
#define _c1    1
#define _c2    2
#define _c3    3
#define _c4    4
#define _alpha  5

double iT3(double price, double period, double hot, bool original, int r, int bars, int instanceNo=0)
{
  if (ArrayRange(workT3,0) != bars)                ArrayResize(workT3,bars);
  if (ArrayRange(workT3Coeffs,0) < (instanceNo+1)) ArrayResize(workT3Coeffs,instanceNo+1);

  if (workT3Coeffs[instanceNo][_period] != period)
  {
    workT3Coeffs[instanceNo][_period] = period;
        double a = hot;
            workT3Coeffs[instanceNo][_c1] = -a*a*a;
            workT3Coeffs[instanceNo][_c2] = 3*a*a+3*a*a*a;
            workT3Coeffs[instanceNo][_c3] = -6*a*a-3*a-3*a*a*a;
            workT3Coeffs[instanceNo][_c4] = 1+3*a+a*a*a+3*a*a;
            if (original)
                workT3Coeffs[instanceNo][_alpha] = 2.0/(1.0 + period);
            else workT3Coeffs[instanceNo][_alpha] = 2.0/(2.0 + (period-1.0)/2.0);
  }
 
  int buffer = instanceNo*6;
  if (r == 0)
      {
        workT3[r][0+buffer] = price;
        workT3[r][1+buffer] = price;
        workT3[r][2+buffer] = price;
        workT3[r][3+buffer] = price;
        workT3[r][4+buffer] = price;
        workT3[r][5+buffer] = price;
      }
  else
      {
        workT3[r][0+buffer] = workT3[r-1][0+buffer]+workT3Coeffs[instanceNo][_alpha]*(price              -workT3[r-1][0+buffer]);
        workT3[r][1+buffer] = workT3[r-1][1+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][0+buffer]-workT3[r-1][1+buffer]);
        workT3[r][2+buffer] = workT3[r-1][2+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][1+buffer]-workT3[r-1][2+buffer]);
        workT3[r][3+buffer] = workT3[r-1][3+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][2+buffer]-workT3[r-1][3+buffer]);
        workT3[r][4+buffer] = workT3[r-1][4+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][3+buffer]-workT3[r-1][4+buffer]);
        workT3[r][5+buffer] = workT3[r-1][5+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][4+buffer]-workT3[r-1][5+buffer]);
      }

  return(workT3Coeffs[instanceNo][_c1]*workT3[r][5+buffer] +
          workT3Coeffs[instanceNo][_c2]*workT3[r][4+buffer] +
          workT3Coeffs[instanceNo][_c3]*workT3[r][3+buffer] +
          workT3Coeffs[instanceNo][_c4]*workT3[r][2+buffer]);
}

color getColor(int stepNo, int totalSteps, color from, color to)
{
  double stes = (double)totalSteps-1.0;
  double step = (from-to)/(stes);
  return((color)round(from-step*stepNo));
}

color gradientColor(int step, int totalSteps, color from, color to)
{
  color newBlue  = getColor(step,totalSteps,(from & 0XFF0000)>>16,(to & 0XFF0000)>>16)<<16;
  color newGreen = getColor(step,totalSteps,(from & 0X00FF00)>> 8,(to & 0X00FF00)>> 8) <<8;
  color newRed  = getColor(step,totalSteps,(from & 0X0000FF)    ,(to & 0X0000FF)    )    ;
  return(newBlue+newGreen+newRed);
}

Vielleicht hift es anderen auch.
Grüße
Otto

PS: absolut geil ist der Farbverlauf
Code:

color getColor(int stepNo, int totalSteps, color from, color to)
{
  double stes = (double)totalSteps-1.0;
  double step = (from-to)/(stes);
  return((color)round(from-step*stepNo));
}

color gradientColor(int step, int totalSteps, color from, color to)
{
  color newBlue  = getColor(step,totalSteps,(from & 0XFF0000)>>16,(to & 0XFF0000)>>16)<<16;
  color newGreen = getColor(step,totalSteps,(from & 0X00FF00)>> 8,(to & 0X00FF00)>> 8) <<8;
  color newRed  = getColor(step,totalSteps,(from & 0X0000FF)    ,(to & 0X0000FF)    )    ;
  return(newBlue+newGreen+newRed);
}



Alle Zeitangaben in WEZ +2. Es ist jetzt 04:51 Uhr.

Powered by vBulletin® Version 3.8.5 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.1
Powered by vBCMS® 2.7.0 ©2002 - 2024 vbdesigns.de
Copyright ©2009 - 2023 by Expert-Advisor.com - Das Metatrader Forum