Hi,
I am trying to create Moving Average color.
When moving average is Increasing relative to previous bar - it is Blue color.
When moving average is Decreasing relative to previous bar - it is Red color.
I created 2 buffers and initialize them at OnInit function
:
at the OnCalculate, in the for loop (for every tick) I check if the Moving Average is increasing or decreasing:
The code seems to work , except when the Moving Average is changing from Decreasing to Increasing or vice versa (from Increasing to Decreasing).
At those cases the indicator does not paint anything on the chart (there is a gap at Moving average).
Attach chart for example.
Anyone have an idea why this is happen?
Thanks.
I'm not very knowledgeable so this may not be a valid point, but why 2 buffers for this ?
Can't a single buffer color be changed for this condition instead of 2 separate buffers ? This would likely solve the line for the missing date in that particular buffer.
From your image pick It looks each buffer is missing a bar on the end and not overlapping with the previous bar buffer etc.
I recommend to post your entire code so others can fully help you with this.
From my limited knowledge your code indicates that the second buffer is +1 shift so this sort of makes sense that current and +1 shift will be calculated for a different bar and show like your image indicates.
double current = iMA(Symbol(),Period(),MAPeriod,MAShift,MAMethod,MAAppliedTo,i); double previous = iMA(Symbol(),Period(),MAPeriod,MAShift,MAMethod,MAAppliedTo,i+1);
At those cases the indicator does not paint anything on the chart (there is a gap at Moving average).
Same as any colored line indicator. Look in the CodeBase. If you assign to one color buffer, make the other color buffer(s) EMPTY_VALUE. Then connect to the previous bar.
HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 programming forum (2016)
For MT4 I use:
- One buffer has the value, color set to CLR_NONE so not shown on chart, (but in data window and pop up.)
- Two buffers, one color each, with SetIndexLabel(i, NULL) so they don't show in data window.
- Then you need to connect the lines on color change. downBuffer[i]=value[i]; if(downBuffer[i+1]==EMPTY_VALUE) downBuffer[i+1]=value[i].
I'm not very knowledgeable so this may not be a valid point, but why 2 buffers for this ?
Can't a single buffer color be changed for this condition instead of 2 separate buffers ? This would likely solve the line for the missing date in that particular buffer.
From your image pick It looks each buffer is missing a bar on the end and not overlapping with the previous bar buffer etc.
I recommend to post your entire code so others can fully help you with this.
From my limited knowledge your code indicates that the second buffer is +1 shift so this sort of makes sense that current and +1 shift will be calculated for a different bar and show like your image indicates.
2 buffers because it seems that the function SetIndexStyle only work one time at the code (don't know if this is correct).
Meaning once you set the color of the indicator using the SetIndexStyle to blue, that set the color for all the indicator bars and not for that specific bar.
If that is not correct, I would be glad that someone will correct me.
Same as any colored line indicator. Look in the CodeBase. If you assign to one color buffer, make the other color buffer(s) EMPTY_VALUE. Then connect to the previous bar.
HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 programming forum (2016)
For MT4 I use:
- One buffer has the value, color set to CLR_NONE so not shown on chart, (but in data window and pop up.)
- Two buffers, one color each, with SetIndexLabel(i, NULL) so they don't show in data window.
- Then you need to connect the lines on color change. downBuffer[i]=value[i]; if(downBuffer[i+1]==EMPTY_VALUE) downBuffer[i+1]=value[i].
That actually worked.
This code using 2 buffers seems to be working:
//+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { IndicatorBuffers(2); // //--- indicator buffers mapping SetIndexBuffer(0,Uptrend); // associated array with buffer SetIndexStyle(0,DRAW_LINE); SetIndexLabel(0,"Uptrend MA"); SetIndexBuffer(1,Downtrend); // associated array with buffer SetIndexStyle(1,DRAW_LINE); SetIndexLabel(1,"Downtrend MA"); //--- 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[]) { //--- int limit = rates_total-prev_calculated; // calculate Green/Red MA if(prev_calculated==0) limit = limit-1; for(int i=limit-1;i>=0;i--) // calculate Green/Red { double current = iMA(Symbol(),Period(),MAPeriod,MAShift,MAMethod,MAAppliedTo,i); double previous = iMA(Symbol(),Period(),MAPeriod,MAShift,MAMethod,MAAppliedTo,i+1); if(current<previous) // MA is decreasing { Downtrend[i]=current; // Red Uptrend[i] = EMPTY_VALUE; if(Downtrend[i+1] == EMPTY_VALUE) { Downtrend[i+1]=previous; // } } else if(current>=previous) // MA is increasing { Uptrend[i]=current; // Green Downtrend[i] = EMPTY_VALUE; if(Uptrend[i+1] == EMPTY_VALUE) { Uptrend[i+1]=previous; // } } } return(rates_total); } //+------------------------------------------------------------------+
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I am trying to create Moving Average color.
When moving average is Increasing relative to previous bar - it is Blue color.
When moving average is Decreasing relative to previous bar - it is Red color.
I created 2 buffers and initialize them at OnInit function
:
at the OnCalculate, in the for loop (for every tick) I check if the Moving Average is increasing or decreasing:
The code seems to work , except when the Moving Average is changing from Decreasing to Increasing or vice versa (from Increasing to Decreasing).
At those cases the indicator does not paint anything on the chart (there is a gap at Moving average).
Attach chart for example.
Anyone have an idea why this is happen?
Thanks.