forked from tslab-hub/handlers
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConstGen.cs
More file actions
283 lines (233 loc) · 8.32 KB
/
ConstGen.cs
File metadata and controls
283 lines (233 loc) · 8.32 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using TSLab.Script.Handlers.Options;
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable ClassNeverInstantiated.Global
namespace TSLab.Script.Handlers
{
//[HandlerName("Constant")]
[HandlerCategory(HandlerCategories.TradeMath, "Const", true)]
[HelperName("Constant", Language = Constants.En)]
[HelperName("Êîíñòàíòà", Language = Constants.Ru)]
#region Àòðèáóòû ñ îïèñàíèåì è ññûëêàìè
[Description("Ïîñòîÿííîå çíà÷åíèå.")]
[HelperDescription("A constant value.", Constants.En)]
[HelperLink(@"http://www.tslab.ru/files/script/StochK.xml", "Ïðèìåð ïî èíäèêàòîðó Stochastic K", Constants.Ru)]
[HelperLink(@"http://www.tslab.ru/files/script/StochK.xml", "Example of Stochastic K", Constants.En)]
[HelperLink(@"http://www.tslab.ru/files/script/2MA_customStop.xml", "Ïðèìåð ñòðàòåãèè 2ÌÀ ñ íåñòàíäàðòíûì ñòîïîì", Constants.Ru)]
[HelperLink(@"http://www.tslab.ru/files/script/2MA_customStop.xml", "Example of 2ÌÀ (with nonstandard stop)", Constants.En)]
#endregion Àòðèáóòû ñ îïèñàíèåì è ññûëêàìè
public sealed class ConstGen : ConstGenBase<double>, IBar2DoubleHandler, IDouble1CalculatorHandler, IDouble2DoubleHandler
{
/// <summary>
/// \~english A value to return as output of a handler
/// \~russian Çíà÷åíèå íà âûõîäå áëîêà
/// </summary>
[HelperName("Value", Constants.En)]
[HelperName("Çíà÷åíèå", Constants.Ru)]
[Description("Çíà÷åíèå íà âûõîäå áëîêà")]
[HelperDescription("A value to return as output of a handler", Constants.En)]
[HandlerParameter]
public double Value { get; set; }
public IList<double> Execute(IContext context)
{
MakeList(context.BarsCount, Value);
return this;
}
public IList<double> Execute(ISecurity source)
{
MakeList(source.Bars.Count, Value);
return this;
}
public double Execute(double source1)
{
return Value;
}
public IList<double> Execute(IList<double> source)
{
MakeList(source.Count, Value);
return this;
}
}
//[HandlerName("Bool Constant")]
[HandlerCategory(HandlerCategories.TradeMath, "BoolConst", true)]
[HelperName("Boolean Constant", Language = Constants.En)]
[HelperName("Ëîãè÷åñêàÿ êîíñòàíòà", Language = Constants.Ru)]
[Description("Ïî àíàëîãèè ñ êîíñòàíòîé âûäàåò ôèêñèðîâàííîå ëîãè÷åñêîå çíà÷åíèå íà êàæäûé áàð.")]
[HelperDescription("This block sets a fixed boolean value for every bar.", Constants.En)]
public class BoolConst : ConstGenBase<bool>, IBar2BoolsHandler
{
/// <summary>
/// \~english A value to return as output of a handler
/// \~russian Çíà÷åíèå íà âûõîäå áëîêà
/// </summary>
[HelperName("Value", Constants.En)]
[HelperName("Çíà÷åíèå", Constants.Ru)]
[Description("Çíà÷åíèå íà âûõîäå áëîêà")]
[HelperDescription("A value to return as output of a handler", Constants.En)]
[HandlerParameter]
public bool Value { get; set; }
public IList<bool> Execute(IContext context)
{
MakeList(context.BarsCount, Value);
return this;
}
public IList<bool> Execute(ISecurity source)
{
MakeList(source.Bars.Count, Value);
return this;
}
}
// useless test class
//[HandlerName("Bool Breaker")]
[HandlerCategory(HandlerCategories.TradeMath, "BoolConst", true)]
[HelperName("Boolean Breaker", Language = Constants.En)]
[HelperName("Ëîãè÷åñêèé ðàçäåëèòåëü", Language = Constants.Ru)]
[Description("Èñïîëüçóåòñÿ ñîâìåñòíî ñ áëîêîì 'Êîíòðîëüíàÿ ïàíåëü' äëÿ îñóùåñòâëåíèÿ ðó÷íîãî óïðàâëåíèÿ êíîïêàìè íà íåé (ðåæèì ïîëóàâòîìàòè÷åñêîé òîðãîâëè). Ïðè íàæàòèè íà êíîïêó áóäåò âûäàí true òîëüêî äëÿ òåêóùåé ñâå÷è, ÷òî ãàðàíòèðóåò, ÷òî ñèãíàë áóäåò âûäàí âñåãäà íà òåêóùåé ñâå÷å.")]
[HelperDescription("Sets a FALSE value at every candle except the last one. For the last value sets a selected TRUE/FALSE value. This block is used together with the control pane buttons.", Constants.En)]
public sealed class BoolBreaker : BoolConst
{
public override IEnumerator<bool> GetEnumerator()
{
return new BreakerEnumerator(m_count, m_value);
}
public override bool this[int index]
{
get { return index == m_count - 1 && m_value; }
set { throw new InvalidOperationException(); }
}
private sealed class BreakerEnumerator : Enumerator
{
public BreakerEnumerator(int size, bool value)
: base(size, value)
{
}
public override bool Current
{
get { return m_cur >= m_size - 1 && m_value; }
}
}
}
/// <summary>
/// Ðóäèìåíòàðíàÿ ðåàëèçàöèÿ èíòåðôåéñà IList[T], êîòîðàÿ äëÿ ýêîíîìèè ïàìÿòè è ïîâûøåíèÿ ñêîðîñòè îáåñïå÷èâàåò òîëüêî ìèíèìàëüíûé ôóíêöèîíàë.
///  êà÷åñòâå âñåõ 'ýëåìåíòîâ ñïèñêà' âûñòóïàåò îäíî çíà÷åíèå, ïåðåäàííîå â êîíñòðóêòîðå.
/// </summary>
public class ConstGenBase<T> : IList<T>
where T : struct, IComparable
{
protected int m_count = int.MaxValue;
protected T m_value;
protected ConstGenBase()
{
}
public ConstGenBase(int count, T value)
{
m_count = count;
m_value = value;
}
protected void MakeList(int count, T value)
{
m_count = count;
m_value = value;
}
#region Implementation of IEnumerable
public virtual IEnumerator<T> GetEnumerator()
{
return new Enumerator(m_count, m_value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Implementation of ICollection<double>
public void Add(T item)
{
throw new InvalidOperationException();
}
public void Clear()
{
throw new InvalidOperationException();
}
public bool Contains(T item)
{
return item.Equals(m_value);
}
public void CopyTo(T[] array, int arrayIndex)
{
for (var i = arrayIndex; i < array.Length; i++)
array[i] = m_value;
}
public bool Remove(T item)
{
throw new InvalidOperationException();
}
public int Count
{
get { return m_count; }
}
public bool IsReadOnly
{
get { return true; }
}
#endregion
#region Implementation of IList<T>
public int IndexOf(T item)
{
return 0;
}
public void Insert(int index, T item)
{
throw new InvalidOperationException();
}
public void RemoveAt(int index)
{
throw new InvalidOperationException();
}
public virtual T this[int index]
{
get { return m_value; }
set { throw new InvalidOperationException(); }
}
#endregion
protected class Enumerator : IEnumerator<T>
{
protected readonly int m_size;
protected readonly T m_value;
protected int m_cur;
public Enumerator(int size, T value)
{
m_size = size;
m_cur = 0;
m_value = value;
}
#region Implementation of IDisposable
public void Dispose()
{
}
#endregion
#region Implementation of IEnumerator
public bool MoveNext()
{
return ++m_cur <= m_size;
}
public void Reset()
{
m_cur = 0;
}
public virtual T Current
{
get { return m_value; }
}
object IEnumerator.Current
{
get { return Current; }
}
#endregion
}
}
}