You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created a reference MACD implementation that is more than 2 x faster. It processes the 3 x Ema's simultaneously, thereby reducing the complexity from O(n*n) to O(n). The same idea can be used for a variety of indicators...
NOTE: I just realised you did the same in 3.0.0.... I was profiling 2.6.1 :(
public MacdResult[] GetMacd(Quote[] history, int _a, int _b, int _c)
{
SmaEma a = new SmaEma(_a), b = new SmaEma(_b), c = new SmaEma(_c);
var result = new MacdResult[history.Length];
for (int i = 0; i < history.Length; i++)
{
result[i] = new(history[i].Date);
var value = (double) history[i].Close;
a.Add(value);
b.Add(value);
if (a.Full())
{
result[i].FastEma = a.Ema();
}
if (b.Full())
{
result[i].SlowEma = b.Ema();
result[i].Macd = result[i].FastEma - result[i].SlowEma;
c.Add((double)result[i].Macd);
}
if(c.Full())
{
result[i].Signal = c.Ema();
result[i].Histogram = result[i].Macd - result[i].Signal;
}
}
return result;
}
public class SmaEma
{
double[] values;
double k, sum, ema = 0.0;
int i = 0;
public SmaEma(int n)
{
values = new double[n];
k = 2.0 / (1.0 + n);
}
public void Add(double value)
{
sum += value - values[i % values.Length];
values[i++ % values.Length] = value;
ema = i <= values.Length ? Sma() : value * k + ema * (1.0 - k);
}
public double Sma()
{
return sum / Math.Min(i, values.Length);
}
public double Ema()
{
return ema;
}
public bool Full()
{
return i >= values.Length;
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Dave and contributors,
I created a reference MACD implementation that is more than 2 x faster. It processes the 3 x Ema's simultaneously, thereby reducing the complexity from O(n*n) to O(n). The same idea can be used for a variety of indicators...
NOTE: I just realised you did the same in 3.0.0.... I was profiling 2.6.1 :(
Beta Was this translation helpful? Give feedback.
All reactions