forked from tslab-hub/handlers
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTEMA.cs
More file actions
292 lines (248 loc) · 10 KB
/
TEMA.cs
File metadata and controls
292 lines (248 loc) · 10 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using TSLab.Script.Handlers.Options;
using TSLab.Script.Helpers;
// ReSharper disable InconsistentNaming
namespace TSLab.Script.Handlers
{
[HandlerCategory(HandlerCategories.Indicators)]
[HelperName("TEMA", Language = Constants.En)]
[HelperName("TEMA", Language = Constants.Ru)]
[InputsCount(1)]
[Input(0, TemplateTypes.DOUBLE)]
[OutputsCount(1)]
[OutputType(TemplateTypes.DOUBLE)]
[Description("Тройное экспоненциальное сглаженное скользящее среднее.")]
[HelperDescription("The Triple Exponential Moving Average.", Constants.En)]
public sealed class TEMA : DoubleStreamAndValuesHandlerWithPeriod
{
private EMA m_ema1;
private EMA m_ema2;
private EMA m_ema3;
public override bool IsGapTolerant
{
get { return IsSimple; }
}
public override IList<double> Execute(IList<double> source)
{
return Calc(source, Period, Context);
}
protected override void InitExecuteContext()
{
if (IsSimple)
return;
m_ema1 = new EMA { Context = Context, Period = Period };
m_ema2 = new EMA { Context = Context, Period = Period };
m_ema3 = new EMA { Context = Context, Period = Period };
}
protected override void ClearExecuteContext()
{
m_ema1 = null;
m_ema2 = null;
m_ema3 = null;
}
protected override void InitForGap()
{
if (IsSimple)
return;
var firstIndex = Math.Max(m_executeContext.LastIndex + 1, m_executeContext.Index - GapTolerancePeriodMultiplier * Period);
for (var i = firstIndex; i < m_executeContext.Index; i++)
{
var source = m_executeContext.GetSourceForGap(i);
var ema1 = m_ema1.Execute(source, i);
var ema2 = m_ema2.Execute(ema1, i);
var ema3 = m_ema3.Execute(ema2, i);
}
}
protected override double Execute()
{
if (IsSimple)
return m_executeContext.Source;
var ema1 = m_ema1.Execute(m_executeContext.Source, m_executeContext.Index);
var ema2 = m_ema2.Execute(ema1, m_executeContext.Index);
var ema3 = m_ema3.Execute(ema2, m_executeContext.Index);
var result = 3 * ema1 - 3 * ema2 + ema3;
return result;
}
public static IList<double> Calc(IList<double> source, int period, IMemoryContext context = null)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (period <= 0)
throw new ArgumentOutOfRangeException(nameof(period));
var result = context?.GetArray<double>(source.Count) ?? new double[source.Count];
if (result.Length > 0)
{
if (period == 1 || result.Length == 1)
source.CopyTo(result, 0);
else
{
var ema1 = Series.EMA(source, period, context);
var ema2 = Series.EMA(ema1, period, context);
var ema3 = Series.EMA(ema2, period, context);
for (var i = 0; i < result.Length; i++)
result[i] = 3 * ema1[i] - 3 * ema2[i] + ema3[i];
context?.ReleaseArray((Array)ema1);
context?.ReleaseArray((Array)ema2);
context?.ReleaseArray((Array)ema3);
}
}
return result;
}
}
[HandlerCategory(HandlerCategories.Indicators)]
[HelperName("DEMA", Language = Constants.En)]
[HelperName("DEMA", Language = Constants.Ru)]
[InputsCount(1)]
[Input(0, TemplateTypes.DOUBLE)]
[OutputsCount(1)]
[OutputType(TemplateTypes.DOUBLE)]
[Description("Двойное экспоненциальное сглаженное скользящее среднее.")]
[HelperDescription("The Double Exponential Moving Average.", Constants.En)]
public sealed class DEMA : DoubleStreamAndValuesHandlerWithPeriod
{
private EMA m_ema1;
private EMA m_ema2;
public override bool IsGapTolerant
{
get { return IsSimple; }
}
public override IList<double> Execute(IList<double> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
var result = Context?.GetArray<double>(source.Count) ?? new double[source.Count];
if (result.Length > 0)
{
if (Period == 1 || result.Length == 1)
source.CopyTo(result, 0);
else
{
var ema1 = Series.EMA(source, Period, Context);
var ema2 = Series.EMA(ema1, Period, Context);
for (var i = 0; i < result.Length; i++)
result[i] = 2 * ema1[i] - ema2[i];
Context?.ReleaseArray((Array)ema1);
Context?.ReleaseArray((Array)ema2);
}
}
return result;
}
protected override void InitExecuteContext()
{
if (IsSimple)
return;
m_ema1 = new EMA { Context = Context, Period = Period };
m_ema2 = new EMA { Context = Context, Period = Period };
}
protected override void ClearExecuteContext()
{
m_ema1 = null;
m_ema2 = null;
}
protected override void InitForGap()
{
if (IsSimple)
return;
var firstIndex = Math.Max(m_executeContext.LastIndex + 1, m_executeContext.Index - GapTolerancePeriodMultiplier * Period);
for (var i = firstIndex; i < m_executeContext.Index; i++)
{
var source = m_executeContext.GetSourceForGap(i);
var ema1 = m_ema1.Execute(source, i);
var ema2 = m_ema2.Execute(ema1, i);
}
}
protected override double Execute()
{
if (IsSimple)
return m_executeContext.Source;
var ema1 = m_ema1.Execute(m_executeContext.Source, m_executeContext.Index);
var ema2 = m_ema2.Execute(ema1, m_executeContext.Index);
var result = 2 * ema1 - ema2;
return result;
}
}
[HandlerCategory(HandlerCategories.Indicators)]
//[HandlerName("TEMA (Zero Lag)")]
//[HandlerName("TEMA (без задержки)", Language = "ru-RU")]
[HelperName("TEMA (Zero Lag)", Language = Constants.En)]
[HelperName("TEMA (без задержки)", Language = Constants.Ru)]
[InputsCount(1)]
[Input(0, TemplateTypes.DOUBLE)]
[OutputsCount(1)]
[OutputType(TemplateTypes.DOUBLE)]
[Description("Тройное экспоненциальное сглаженное скользящее среднее с ограниченной задержкой.")]
[HelperDescription("The Triple Exponential Moving Average with a limited lag. ", Constants.En)]
[SuppressMessage("StyleCopPlus.StyleCopPlusRules", "SP0100:AdvancedNamingRules", Justification = "Reviewed. Suppression is OK here.")]
public sealed class ZeroLagTEMA : DoubleStreamAndValuesHandlerWithPeriod
{
private TEMA m_tema1;
private TEMA m_tema2;
public override bool IsGapTolerant
{
get { return IsSimple; }
}
public override IList<double> Execute(IList<double> source)
{
return Calc(source, Period, Context);
}
protected override void InitExecuteContext()
{
if (IsSimple)
return;
m_tema1 = new TEMA { Context = Context, Period = Period };
m_tema2 = new TEMA { Context = Context, Period = Period };
}
protected override void ClearExecuteContext()
{
m_tema1 = null;
m_tema2 = null;
}
protected override void InitForGap()
{
if (IsSimple)
return;
var firstIndex = Math.Max(m_executeContext.LastIndex + 1, m_executeContext.Index - GapTolerancePeriodMultiplier * Period);
for (var i = firstIndex; i < m_executeContext.Index; i++)
{
var source = m_executeContext.GetSourceForGap(i);
var tema1 = m_tema1.Execute(source, i);
var tema2 = m_tema2.Execute(tema1, i);
}
}
protected override double Execute()
{
if (IsSimple)
return m_executeContext.Source;
var tema1 = m_tema1.Execute(m_executeContext.Source, m_executeContext.Index);
var tema2 = m_tema2.Execute(tema1, m_executeContext.Index);
var result = 2 * tema1 - tema2;
return result;
}
public static IList<double> Calc(IList<double> source, int period, IMemoryContext context = null)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (period <= 0)
throw new ArgumentOutOfRangeException(nameof(period));
var result = context?.GetArray<double>(source.Count) ?? new double[source.Count];
if (result.Length > 0)
{
if (period == 1 || result.Length == 1)
source.CopyTo(result, 0);
else
{
var tema1 = TEMA.Calc(source, period, context);
var tema2 = TEMA.Calc(tema1, period, context);
for (var i = 0; i < source.Count; i++)
result[i] = 2 * tema1[i] - tema2[i];
context?.ReleaseArray((Array)tema1);
context?.ReleaseArray((Array)tema2);
}
}
return result;
}
}
}