Changing EMA color problem

 

Hi

Im working on an ema ribbon indicator, wich would simply change the color of each ema, according to what the user types in as input. For example if the ema period is 5, it would change to dark violet, when its 20, it would change to blue, etc. It would be true to every ema

I tried this with switch operator but its acting strange :(

Can anyone help me out? Btw its my very first code so im not really an expert yet.

I post here my code, i simplified it to 1 ema. I deleted the default row from the switch operator because that would make me no matter what i set, the color would be the same as defined in the default row...

Thanks in advance!

#property indicator_chart_window
#property indicator_buffers 1

input int MA_Period = 5; // EMA period

double ExtMapBuffer[];

void OnInit()
{

SetIndexBuffer(0, ExtMapBuffer);
}

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[])
  {

switch (MA_Period)
{
case 5: SetIndexStyle(0, DRAW_LINE,STYLE_SOLID,2,clrOrangeRed);
case 10: SetIndexStyle(0, DRAW_LINE,STYLE_SOLID,2,clrDarkViolet);
}

 
int counted_bars = IndicatorCounted();

if(counted_bars > 0)
      counted_bars--;
int limit = Bars - counted_bars;

for(int i = 0; i < limit; i++)
{
ExtMapBuffer[i] = iMA(NULL, 0, MA_Period, 0, MODE_EMA, PRICE_CLOSE, i);
}
return rates_total;
}
 

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

 
  1. Why are you trying to change it in OnCalculate? MA_Period is constant. That belongs in OnInit.
  2. If you don't break from a case, it steps down into the next one.
 

Thank you very much, now it works! :) Im very grateful!


William Roeder #:
  1. Why are you trying to change it in OnCalculate? MA_Period is constant. That belongs in OnInit.
  2. If you don't break from a case, it steps down into the next one.