forked from tslab-hub/handlers
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBarsCountForValuesSumHandler.cs
More file actions
82 lines (70 loc) · 2.77 KB
/
BarsCountForValuesSumHandler.cs
File metadata and controls
82 lines (70 loc) · 2.77 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.ComponentModel;
using TSLab.Script.Handlers.Options;
using TSLab.Utils;
namespace TSLab.Script.Handlers
{
[HandlerCategory(HandlerCategories.Indicators)]
[HelperName("Bars count for values sum", Language = Constants.En)]
[HelperName("Количество баров для суммы значений", Language = Constants.Ru)]
[Description("Количество баров для суммы значений")]
[HelperDescription("Bars count for values sum", Constants.En)]
public sealed class BarsCountForValuesSumHandler : DoubleStreamAndValuesHandler
{
private double[] m_source;
public override bool IsGapTolerant => false;
/// <summary>
/// \~english Indicator values sum
/// \~russian Сумма значений индикатора
/// </summary>
[HelperName("Values sum", Constants.En)]
[HelperName("Сумма значений", Constants.Ru)]
[Description("Сумма значений индикатора")]
[HelperDescription("Indicator values sum", Constants.En)]
[HandlerParameter(true, "1", Min = "0", Max = "2147483647", Step = "1", EditorMin = "1")]
public double ValuesSum { get; set; }
public override IList<double> Execute(IList<double> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (source.Count == 0)
return EmptyArrays.Double;
var results = Context.GetArray<double>(source.Count);
for (var i = 0; i < source.Count; i++)
results[i] = Calculate(source, i);
return results;
}
protected override void InitExecuteContext()
{
m_source = Context.GetArray<double>(Context.BarsCount);
}
protected override void ClearExecuteContext()
{
Context.ReleaseArray(m_source);
m_source = null;
}
protected override void InitForGap()
{
for (var i = m_executeContext.LastIndex; i < m_executeContext.Index; i++)
m_source[i] = m_executeContext.GetSourceForGap(i);
}
protected override double Execute()
{
var index = m_executeContext.Index;
m_source[index] = m_executeContext.Source;
return Calculate(m_source, index);
}
private double Calculate(IList<double> source, int index)
{
var valuesSum = 0D;
for (var j = index; j >= 0; j--)
{
valuesSum += source[j];
if (valuesSum >= ValuesSum)
return index - j + 1;
}
return 0;
}
}
}