Questions from Beginners MQL4 MT4 MetaTrader 4 - page 227

 

Hi, the problem is probably not the zero crossing but the crossing from one buffer to another. At one bar the signal ends and at the next bar the signal is already in another buffer, what colour would you like to see between the two adjacent bars? It is not clear.

If you want to make a third buffer with a yellow line and put it under these two, it would be beautiful!

 
Aleksei Stepanenko:

Hi, the problem is probably not the zero crossing but the crossing from one buffer to another. At one bar the signal ends and at the next bar the signal is already in another buffer, what colour would you like to see between the two adjacent bars? It is not clear.

If you want, make a third buffer with a yellow line and put it under these two, it will be beautiful!

Alexey, thanks for the help.

I don't need three colours, two is quite enough. I would like the empty spaces to be drawn with a line of the previous colour. I do not understand what you mean by " On one bar the signal is over, and on the next bar the signal is in a different buffer". At each bar, there is a value in one of the buffers, so it should also be displayed on the chart, or am I missing something?

 

Well look, time is discrete on the chart, the minimum unit is 1 bar. That is, there is no partial time between bars, as it were. If you replace the rendering with a bar chart, you have two bars - two bars of different colours. But there is nothing between them. And the line is drawn in such a way (such a property is made) that it connects values of the same buffer, but not different ones. That's why there are holes.

If you want it for beauty, then you can try giving the end values overlapping on both buffers. You signal to the new buffer, but still signal to the old one bar.

But if you then take the signal from such indicator into Expert Advisor, it will be inaccurate.


Or a histogram is a good option without any extra hassle. I vote for the bar chart.

 
Grigori.S.B:

Alexei, thank you for your help.

I don't need three colours, two is quite enough. I would like the empty spaces to be drawn with a line of the previous colour. I don't understand what you mean by " At one bar the signal ends, and at the next bar the signal is already in another buffer". At each bar, there is a value in one of the buffers, it means that it should be displayed on the chart, or am I missing something?

leave one buffer without conditions and overlay the second one with conditions on the first one

#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 clrGreen
#property indicator_color2 clrRed

//---- input parameters
extern int    FastEMA=12;  
extern int    SlowEMA=26;  
extern int    Signal=9;  

//---- buffers
double DiffBuffer_up[];
double DiffBuffer_dn[];
//
double MainBuffer[];
double SignalBuffer[];

ENUM_TIMEFRAMES TimeFrame;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   int    draw_begin=MathMax(FastEMA,SlowEMA);
   string short_name="MACD Stephen";
   //---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,DiffBuffer_up);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,DiffBuffer_dn);
   short_name=StringConcatenate(short_name," ("+(string)FastEMA+","+(string)SlowEMA+","+(string)Signal+")");
   IndicatorShortName(short_name);
   SetIndexDrawBegin(0,draw_begin);
   SetIndexDrawBegin(1,draw_begin);
   SetIndexLabel(0,"Up");
   SetIndexLabel(1,"Down");
   IndicatorDigits(6);
   SetLevelValue(0,0);
   
   TimeFrame=GetTF();
   
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  int limit, iChart, iTF, delta=0;
  datetime TimeArray[];
  //if(TimeFrame>Period()) delta=(int)MathCeil(TimeFrame/Period());
  int counted_bars=IndicatorCounted();
  //---- check for possible errors
  if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
  if(counted_bars>0) counted_bars--;
  limit=Bars-counted_bars+delta;

  ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);
  ArraySetAsSeries(MainBuffer,true);
  ArraySetAsSeries(SignalBuffer,true);
  ArrayResize(MainBuffer,  100000);
  ArrayResize(SignalBuffer,100000);
  
  iTF=0;
  for(iChart=0; iChart<limit; iChart++)
  {
      while(Time[iChart]<TimeArray[iTF]) iTF++;
      MainBuffer[iChart]=EMPTY_VALUE;
      SignalBuffer[iChart]=EMPTY_VALUE;
      MainBuffer[iChart]  =iMACD(Symbol(),TimeFrame,FastEMA,SlowEMA,Signal,PRICE_CLOSE,MODE_MAIN,iTF);
      SignalBuffer[iChart]=iMACD(Symbol(),TimeFrame,FastEMA,SlowEMA,Signal,PRICE_CLOSE,MODE_SIGNAL,iTF);
      double diff=MainBuffer[iChart]-SignalBuffer[iChart];
      DiffBuffer_up[iChart]=diff;
      
      
      if (diff>0) //{
        DiffBuffer_dn[iChart]=diff; 
         //DiffBuffer_dn[iChart]=EMPTY_VALUE;
      //}
      //else {
         
         //DiffBuffer_up[iChart]=EMPTY_VALUE;
      //}
  }
  return(0);
}
 
Aleksei Stepanenko:

Well look, time is discrete on the chart, the minimum unit is 1 bar. That is, there is no partial time between bars, as it were. If you replace the rendering with a bar chart, you have two bars - two bars of different colours. But there is nothing between them. And the line is drawn in such a way (such a property is made) that it connects values of the same buffer, but not different ones. That's why there are holes.

If you want it for beauty, then you can try giving the end values overlapping on both buffers. You signal to the new buffer, but still signal to the old one bar.

But if you then take the signal from such indicator into Expert Advisor, it will be inaccurate.

Or a histogram is a good option without any extra hassle. I vote for the bar chart.

My sincere thanks Aleksey.

Now even my head is all sorted out. I could have done it myself, but not as short and elegant as Yuri's subsequent advice.

 
Iurii Tokman:

leave one buffer unconditional, and overlay the second with conditions on the first

Hats off to you, Yuri.

Such an elegant and concise solution I couldn't have imagined.

 
Grigori.S.B:

Hats off to you, Yuri.

I couldn't have imagined such an elegant and concise solution.

it was not my idea, here on the forum, just like you found out
there are a lot of solutions in the code base, and not only that

 
Grigori.S.B:


Thanks for the thanks!

 
Why do my questions keep getting ignored?
 
darirunu1:
Why do my questions keep getting ignored?

depends on the questions...

Reason: