Moving Average

Einloggen oder registrieren, um einen Kommentar zu schreiben
Arthur Singer
839
Arthur Singer  

Hallo, ich versuche aktuell einen Moving Average zu entwickeln, der mit zwei Buffern arbeitet, allerdings habe ich das Problem, dass Lücken entstehen anstatt eine durchgezogene Linie. Weis jemand, wie ich das Problem beheben kann? 

//+------------------------------------------------------------------+
//|                                         ColoredMovingAverage.mq5 |
//|                                        Copyright 2019, Arthur S. |
//|                    https://www.mql5.com/en/users/michael12345678 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Arthur S."
#property link      "https://www.mql5.com/en/users/michael12345678"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 2

#property indicator_label1 "Up"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlue

#property indicator_label2 "Down"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed

int MovingAverage;

double MovingAverageBufferBlue[];
double MovingAverageBufferRed[];
double MovingAverageBuffer[];

input int Periode=14;
input ENUM_MA_METHOD Methode=MODE_EMA;
input ENUM_APPLIED_PRICE Preis=PRICE_CLOSE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{ 
   SetIndexBuffer(0,MovingAverageBufferBlue,INDICATOR_DATA);
   SetIndexBuffer(1,MovingAverageBufferRed,INDICATOR_DATA);
   SetIndexBuffer(2,MovingAverageBuffer,INDICATOR_CALCULATIONS);
   
   MovingAverage=iMA(_Symbol,_Period,Periode,0,Methode,Preis);
   if(MovingAverage==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   ArraySetAsSeries(MovingAverageBuffer,true);
   ArraySetAsSeries(MovingAverageBufferBlue,true);
   ArraySetAsSeries(MovingAverageBufferRed,true);
   
   int copied=CopyBuffer(MovingAverage,0,0,rates_total,MovingAverageBuffer);
   int copied2=CopyBuffer(MovingAverage,0,0,rates_total,MovingAverageBufferBlue);
   int copied3=CopyBuffer(MovingAverage,0,0,rates_total,MovingAverageBufferRed);
   
   for(int i=0;i<rates_total-1;i++)
   {
      MovingAverageBufferBlue[i]=EMPTY_VALUE;
      MovingAverageBufferRed[i]=EMPTY_VALUE;
      
      if(MovingAverageBuffer[i]<MovingAverageBuffer[i+1])
      {
         MovingAverageBufferRed[i]=MovingAverageBuffer[i];
      }
      else if(MovingAverageBuffer[i]>MovingAverageBuffer[i+1])
      {
         MovingAverageBufferBlue[i]=MovingAverageBuffer[i];
      }
   }
   
   return(rates_total);
}
//+------------------------------------------------------------------+
Carl Schreiber
Moderator
8425
Carl Schreiber  
Bei einem Farb- bzw. einen Pufferwechsel musst Du einfach den letzten Wert der alten Farbe bzw. des alten Puffers an gleicher Stelle (Index) den neuen Puffers eintragen.
Arthur Singer
839
Arthur Singer  

Meinst Du so?

//+------------------------------------------------------------------+
//|                                         ColoredMovingAverage.mq5 |
//|                                        Copyright 2019, Arthur S. |
//|                    https://www.mql5.com/en/users/michael12345678 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Arthur S."
#property link      "https://www.mql5.com/en/users/michael12345678"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 2

#property indicator_label1 "Up"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlue

#property indicator_label2 "Down"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed

int MovingAverage;

double MovingAverageBufferBlue[];
double MovingAverageBufferRed[];
double MovingAverageBuffer[];

input int Periode=14;
input ENUM_MA_METHOD Methode=MODE_EMA;
input ENUM_APPLIED_PRICE Preis=PRICE_CLOSE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{ 
   SetIndexBuffer(0,MovingAverageBufferBlue,INDICATOR_DATA);
   SetIndexBuffer(1,MovingAverageBufferRed,INDICATOR_DATA);
   SetIndexBuffer(2,MovingAverageBuffer,INDICATOR_CALCULATIONS);
   
   MovingAverage=iMA(_Symbol,_Period,Periode,0,Methode,Preis);
   if(MovingAverage==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   ArraySetAsSeries(MovingAverageBuffer,true);
   ArraySetAsSeries(MovingAverageBufferBlue,true);
   ArraySetAsSeries(MovingAverageBufferRed,true);
   
   int copied=CopyBuffer(MovingAverage,0,0,rates_total,MovingAverageBuffer);
   int copied2=CopyBuffer(MovingAverage,0,0,rates_total,MovingAverageBufferBlue);
   int copied3=CopyBuffer(MovingAverage,0,0,rates_total,MovingAverageBufferRed);
   
   for(int i=0;i<rates_total;i++)
   {
      MovingAverageBufferBlue[i]=EMPTY_VALUE;
      MovingAverageBufferRed[i]=EMPTY_VALUE;
      
      if(MovingAverageBuffer[i]<MovingAverageBuffer[i+1])
      {
         MovingAverageBufferRed[i]=MovingAverageBuffer[i];
      }
      else if(MovingAverageBuffer[i]>MovingAverageBuffer[i+1])
      {
         MovingAverageBufferBlue[i]=MovingAverageBuffer[i];
      }
      
      if(MovingAverageBuffer[i+1]>MovingAverageBuffer[i+2])
      {
         MovingAverageBufferBlue[i+1]=MovingAverageBuffer[i+1];
      }
      else if(MovingAverageBuffer[i+1]<MovingAverageBuffer[i+2])
      {
         MovingAverageBufferRed[i+1]=MovingAverageBuffer[i+1];
      }
   }
   
   return(rates_total);
}
//+------------------------------------------------------------------+
Carl Schreiber
Moderator
8425
Carl Schreiber  
Ich weiß nicht - klappt's, wenn Du das ausprobierst?
Arthur Singer
839
Arthur Singer  
Ehrlich gesagt hat sich nichts verändert.
Arthur Singer
839
Arthur Singer  

Ich habe auch das versucht, aber es klappt ebenfalls nicht:

      if(MovingAverageBuffer[i]<MovingAverageBuffer[i+1])
      {
         MovingAverageBufferRed[i]=MovingAverageBuffer[i];
         
         if(MovingAverageBufferBlue[i+1]>0)
         {
            MovingAverageBufferRed[i+1]=MovingAverageBuffer[i+1];
         }
      }
      else if(MovingAverageBuffer[i]>MovingAverageBuffer[i+1])
      {
         MovingAverageBufferBlue[i]=MovingAverageBuffer[i];
         
         if(MovingAverageBufferRed[i+1]>0)
         {
            MovingAverageBufferBlue[i+1]=MovingAverageBuffer[i+1];
         }
      }
Carl Schreiber
Moderator
8425
Carl Schreiber  
Arthur Singer:

Ich habe auch das versucht, aber es klappt ebenfalls nicht:

geh mal mit dem Debugger (mit historische Wert) durch und kontrolliere die Werte, wenn die Puffer 'leer' sind, ihnen also noch keinen Wert zugewiesen wurde!!

Arthur Singer
839
Arthur Singer  
Jeder Candlestick hat einen Wert, aber es müsste glaube ich so sein, dass der letzte Candlestick zwei Werte hat, also um beide Farben zu verbinden.
amando
1894
amando  
Arthur Singer:

Ich habe auch das versucht, aber es klappt ebenfalls nicht:

Wenn mich nicht alles täuscht, da. Muss es i-1 sein, weil ind die zukunft schauen kann ein indikator nicht

Arthur Singer
839
Arthur Singer  
i-1 stimmt nicht, wegen ArraySetAsSeries. Wenn ich das einfüge, ist die komplette Linie rot.
Einloggen oder registrieren, um einen Kommentar zu schreiben