-
Notifications
You must be signed in to change notification settings - Fork 2
/
SMACalculator.cs
39 lines (34 loc) · 1.03 KB
/
SMACalculator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace PA.Trading.UAPI
{
public class SMACalculator
{
public static double[] Calculate(double[] price, int period)
{
var sma = new double[price.Length];
double sum = 0;
for (var i = 0; i < period; i++)
{
sum += price[i];
sma[i] = sum / (i + 1);
}
for (var i = period; i < price.Length; i++)
{
sum = 0;
for (var j = i; j > i - period; j--)
{
sum += price[j];
}
sma[i] = sum / period;
}
return sma;
}
public static double[] Calculate(UCandle[] candlesticks, int period)
{
var sma = new double[candlesticks.Length];
double[] src = new double[candlesticks.Length];
for (int i = 0; i < candlesticks.Length; i++)
src[i] = candlesticks[i].ClosePrice;
return Calculate(src, period);
}
}
}