-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModifiedMovingAverage.cs
42 lines (38 loc) · 1.38 KB
/
ModifiedMovingAverage.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
40
41
42
//==============================================================================
// Copyright (c) 2012-2023 Fiats Inc. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the solution folder for
// full license information.
// https://www.fiats.asia/
// Fiats Inc. Nakano, Tokyo, Japan
//
namespace Financier;
public static partial class IndicatorExtensions
{
/// <summary>
/// Modified moving average (MMA)
/// </summary>
/// <param name="source"></param>
/// <param name="period"></param>
/// <returns></returns>
public static IObservable<double> ModifiedMovingAverage(this IObservable<double> source, int period)
{
return source.Publish(s => s.Take(period).Average().Concat(s)
.Scan(
(last, value) => (last * (period - 1) + value) / period
));
}
public static IObservable<decimal> ModifiedMovingAverage(this IObservable<decimal> source, int period)
{
return source.Publish(s => s.Take(period).Average().Concat(s)
.Scan(
(last, value) => (last * (period - 1) + value) / period
));
}
public static IObservable<float> ModifiedMovingAverage(this IObservable<float> source, int period)
{
return source.Publish(s => s.Take(period).Average().Concat(s)
.Scan(
(last, value) => (last * (period - 1) + value) / period
));
}
}