Skip to content

Instantly share code, notes, and snippets.

@micclly
Last active October 29, 2020 16:20
Show Gist options
  • Save micclly/9043809 to your computer and use it in GitHub Desktop.
Save micclly/9043809 to your computer and use it in GitHub Desktop.
/*
* License: GNU General Public License Version 3
*/
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_color2 clrGreen
#property indicator_style2 STYLE_SOLID
#property indicator_color3 clrPurple
#property indicator_style3 STYLE_SOLID
input int i_EMAPeriod = 15;
double g_emaSingleSmoothed[];
double g_emaDoubleSmoothed[];
double g_emaTripleSmoothed[];
void calculateEMA(int rates_total, int prev_calculated, const double& price[], double& indexBuffer[])
{
int limit;
double smoothFactor = 2.0 / (1.0 + i_EMAPeriod);
if (prev_calculated == 0)
{
limit = i_EMAPeriod;
indexBuffer[0] = price[0];
for(int i = 1; i < limit; i++) {
indexBuffer[i] = price[i] * smoothFactor + indexBuffer[i-1] * (1.0 - smoothFactor);
}
}
else {
limit = prev_calculated - 1;
}
for(int i = limit; i < rates_total; i++) {
if (IsStopped()) {
break;
}
indexBuffer[i] = price[i] * smoothFactor + indexBuffer[i-1] * (1.0 - smoothFactor);
}
}
int OnInit()
{
IndicatorShortName("TRIX" + string(i_EMAPeriod) + ")");
IndicatorDigits(Digits);
SetIndexBuffer(0, g_emaSingleSmoothed);
SetIndexBuffer(1, g_emaDoubleSmoothed);
SetIndexBuffer(2, g_emaTripleSmoothed);
return INIT_SUCCEEDED;
}
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[])
{
if (rates_total < i_EMAPeriod - 1) {
return 0;
}
ArraySetAsSeries(g_emaSingleSmoothed, false);
ArraySetAsSeries(g_emaDoubleSmoothed, false);
ArraySetAsSeries(g_emaTripleSmoothed, false);
ArraySetAsSeries(close,false);
if (prev_calculated == 0) {
ArrayInitialize(g_emaSingleSmoothed, EMPTY_VALUE);
ArrayInitialize(g_emaDoubleSmoothed, EMPTY_VALUE);
ArrayInitialize(g_emaTripleSmoothed, EMPTY_VALUE);
}
calculateEMA(rates_total, prev_calculated, close, g_emaSingleSmoothed);
calculateEMA(rates_total, prev_calculated, g_emaSingleSmoothed, g_emaDoubleSmoothed);
calculateEMA(rates_total, prev_calculated, g_emaDoubleSmoothed, g_emaTripleSmoothed);
return rates_total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment