/
Candle.cs
666 lines (595 loc) · 17 KB
/
Candle.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#region S# License
/******************************************************************************************
NOTICE!!! This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.
Project: StockSharp.Algo.Candles.Algo
File: Candle.cs
Created: 2015, 11, 11, 2:32 PM
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.Algo.Candles
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using Ecng.Common;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
using StockSharp.Localization;
using DataType = Messages.DataType;
/// <summary>
/// Base candle class (contains main parameters).
/// </summary>
[DataContract]
[Serializable]
[KnownType(typeof(TickCandle))]
[KnownType(typeof(VolumeCandle))]
[KnownType(typeof(RangeCandle))]
[KnownType(typeof(TimeFrameCandle))]
[KnownType(typeof(PnFCandle))]
[KnownType(typeof(RenkoCandle))]
[Obsolete("Use ICandleMessage.")]
public abstract class Candle : Cloneable<Candle>, ICandleMessage
{
/// <summary>
/// Initializes a new instance of the <see cref="Candle"/>.
/// </summary>
protected Candle()
{
}
private SecurityId? _securityId;
SecurityId ISecurityIdMessage.SecurityId
{
get => _securityId ??= Security?.Id.ToSecurityId() ?? default;
set => throw new NotSupportedException();
}
DateTimeOffset IServerTimeMessage.ServerTime
{
get => OpenTime;
set => OpenTime = value;
}
DateTimeOffset ILocalTimeMessage.LocalTime => OpenTime;
/// <summary>
/// Security.
/// </summary>
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.SecurityKey,
Description = LocalizedStrings.SecurityKey,
GroupName = LocalizedStrings.GeneralKey)]
public Security Security { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.CandleOpenTimeKey,
Description = LocalizedStrings.CandleOpenTimeKey,
GroupName = LocalizedStrings.GeneralKey)]
public DateTimeOffset OpenTime { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.CandleCloseTimeKey,
Description = LocalizedStrings.CandleCloseTimeKey,
GroupName = LocalizedStrings.GeneralKey)]
public DateTimeOffset CloseTime { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.CandleHighTimeKey,
Description = LocalizedStrings.CandleHighTimeKey,
GroupName = LocalizedStrings.GeneralKey)]
public DateTimeOffset HighTime { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.CandleLowTimeKey,
Description = LocalizedStrings.CandleLowTimeKey,
GroupName = LocalizedStrings.GeneralKey)]
public DateTimeOffset LowTime { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.OpenPriceKey,
Description = LocalizedStrings.CandleOpenPriceKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal OpenPrice { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.ClosingPriceKey,
Description = LocalizedStrings.ClosePriceOfCandleKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal ClosePrice { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.HighestPriceKey,
Description = LocalizedStrings.HighPriceOfCandleKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal HighPrice { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.LowestPriceKey,
Description = LocalizedStrings.LowPriceOfCandleKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal LowPrice { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.TotalPriceKey,
Description = LocalizedStrings.TotalPriceKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal TotalPrice { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.OpenVolumeKey,
Description = LocalizedStrings.OpenVolumeKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal? OpenVolume { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.CloseVolumeKey,
Description = LocalizedStrings.CloseVolumeKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal? CloseVolume { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.HighVolumeKey,
Description = LocalizedStrings.HighVolumeKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal? HighVolume { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.LowVolumeKey,
Description = LocalizedStrings.LowVolumeKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal? LowVolume { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.VolumeKey,
Description = LocalizedStrings.TotalCandleVolumeKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal TotalVolume { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.RelativeVolumeKey,
Description = LocalizedStrings.RelativeVolumeKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal? RelativeVolume { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.BuyVolumeKey,
Description = LocalizedStrings.BuyVolumeKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal? BuyVolume { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.SellVolumeKey,
Description = LocalizedStrings.SellVolumeKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal? SellVolume { get; set; }
/// <inheritdoc />
public abstract object Arg { get; set; }
private DataType _dataType;
/// <inheritdoc />
public DataType DataType
{
get
{
if (_dataType is null)
{
var arg = Arg;
if (!arg.IsNull(true))
_dataType = DataType.Create(GetType().ToCandleMessageType(), arg);
}
return _dataType;
}
set => throw new NotSupportedException();
}
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.TicksKey,
Description = LocalizedStrings.TickCountKey,
GroupName = LocalizedStrings.GeneralKey)]
public int? TotalTicks { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.TickUpKey,
Description = LocalizedStrings.TickUpCountKey,
GroupName = LocalizedStrings.GeneralKey)]
public int? UpTicks { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.TickDownKey,
Description = LocalizedStrings.TickDownCountKey,
GroupName = LocalizedStrings.GeneralKey)]
public int? DownTicks { get; set; }
private CandleStates _state;
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.StateKey,
Description = LocalizedStrings.CandleStateKey,
GroupName = LocalizedStrings.GeneralKey)]
public CandleStates State
{
get => _state;
set
{
ThrowIfFinished();
_state = value;
}
}
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.PriceLevelsKey,
Description = LocalizedStrings.PriceLevelsKey,
GroupName = LocalizedStrings.GeneralKey)]
public IEnumerable<CandlePriceLevel> PriceLevels { get; set; }
/// <inheritdoc />
[DataMember]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.OIKey,
Description = LocalizedStrings.OpenInterestDescKey,
GroupName = LocalizedStrings.GeneralKey)]
public decimal? OpenInterest { get; set; }
/// <inheritdoc />
[DataMember]
public long SeqNum { get; set; }
/// <inheritdoc />
[DataMember]
public DataType BuildFrom { get; set; }
/// <inheritdoc />
public abstract Type ArgType { get; }
/// <inheritdoc />
public override string ToString()
{
return "{0:HH:mm:ss} {1} (O:{2}, H:{3}, L:{4}, C:{5}, V:{6})"
.Put(OpenTime, GetType().Name + "_" + Security + "_" + Arg, OpenPrice, HighPrice, LowPrice, ClosePrice, TotalVolume);
}
private void ThrowIfFinished()
{
if (State == CandleStates.Finished)
throw new InvalidOperationException(LocalizedStrings.CannotChangeFormedCandle);
}
/// <summary>
/// Copy the message into the <paramref name="destination" />.
/// </summary>
/// <typeparam name="TCandle">The candle type.</typeparam>
/// <param name="destination">The object, to which copied information.</param>
/// <returns>The object, to which copied information.</returns>
protected TCandle CopyTo<TCandle>(TCandle destination)
where TCandle : Candle
{
destination.Arg = Arg;
destination.ClosePrice = ClosePrice;
destination.CloseTime = CloseTime;
destination.CloseVolume = CloseVolume;
destination.DownTicks = DownTicks;
destination.HighPrice = HighPrice;
destination.HighTime = HighTime;
destination.HighVolume = HighVolume;
destination.LowPrice = LowPrice;
destination.LowTime = LowTime;
destination.LowVolume = LowVolume;
destination.OpenInterest = OpenInterest;
destination.OpenPrice = OpenPrice;
destination.OpenTime = OpenTime;
destination.OpenVolume = OpenVolume;
destination.RelativeVolume = RelativeVolume;
destination.Security = Security;
//destination.Series = Series;
//destination.Source = Source;
//destination.State = State;
destination.TotalPrice = TotalPrice;
destination.TotalTicks = TotalTicks;
destination.TotalVolume = TotalVolume;
destination.BuyVolume = BuyVolume;
destination.SellVolume = SellVolume;
//destination.VolumeProfileInfo = VolumeProfileInfo;
destination.PriceLevels = PriceLevels?./*Select(l => l.Clone()).*/ToArray();
destination.SeqNum = SeqNum;
destination.BuildFrom = BuildFrom;
return destination;
}
}
/// <summary>
/// Base candle class (contains main parameters).
/// </summary>
/// <typeparam name="TArg"></typeparam>
[Obsolete("Use ICandleMessage.")]
public abstract class Candle<TArg> : Candle
{
/// <summary>
/// Initializes a new instance of the <see cref="Candle"/>.
/// </summary>
protected Candle()
{
}
private TArg _typedArg;
/// <summary>
/// Arg.
/// </summary>
public TArg TypedArg
{
get => _typedArg;
set
{
Validate(value);
_typedArg = value;
}
}
/// <summary>
/// Validate value.
/// </summary>
/// <param name="value">Value.</param>
protected virtual void Validate(TArg value) { }
/// <inheritdoc />
public override object Arg
{
get => TypedArg;
set => TypedArg = (TArg)value;
}
/// <inheritdoc />
public override Type ArgType => typeof(TArg);
}
/// <summary>
/// Time-frame candle.
/// </summary>
[DataContract]
[Serializable]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.TimeFrameCandleKey)]
[Obsolete("Use TimeFrameCandleMessage.")]
public class TimeFrameCandle : Candle<TimeSpan>, ITimeFrameCandleMessage
{
/// <summary>
/// Time-frame.
/// </summary>
[DataMember]
public TimeSpan TimeFrame
{
get => TypedArg;
set => TypedArg = value;
}
/// <inheritdoc />
protected override void Validate(TimeSpan value)
{
if (value <= TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(value));
}
/// <summary>
/// Create a copy of <see cref="TimeFrameCandle"/>.
/// </summary>
/// <returns>Copy.</returns>
public override Candle Clone()
{
return CopyTo(new TimeFrameCandle());
}
}
/// <summary>
/// Tick candle.
/// </summary>
[DataContract]
[Serializable]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.TickCandleKey)]
[Obsolete("Use TickCandleMessage.")]
public class TickCandle : Candle<int>, ITickCandleMessage
{
/// <summary>
/// Maximum tick count.
/// </summary>
[DataMember]
public int MaxTradeCount
{
get => TypedArg;
set => TypedArg = value;
}
/// <inheritdoc />
protected override void Validate(int value)
{
if (value <= 0)
throw new ArgumentOutOfRangeException(nameof(value));
}
/// <summary>
/// Create a copy of <see cref="TickCandle"/>.
/// </summary>
/// <returns>Copy.</returns>
public override Candle Clone()
{
return CopyTo(new TickCandle());
}
}
/// <summary>
/// Volume candle.
/// </summary>
[DataContract]
[Serializable]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.VolumeCandleKey)]
[Obsolete("Use VolumeCandleMessage.")]
public class VolumeCandle : Candle<decimal>, IVolumeCandleMessage
{
/// <summary>
/// Maximum volume.
/// </summary>
[DataMember]
public decimal Volume
{
get => TypedArg;
set => TypedArg = value;
}
/// <inheritdoc />
protected override void Validate(decimal value)
{
if (value <= 0)
throw new ArgumentOutOfRangeException(nameof(value));
}
/// <summary>
/// Create a copy of <see cref="VolumeCandle"/>.
/// </summary>
/// <returns>Copy.</returns>
public override Candle Clone()
{
return CopyTo(new VolumeCandle());
}
}
/// <summary>
/// Range candle.
/// </summary>
[DataContract]
[Serializable]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.RangeCandleKey)]
[Obsolete("Use RangeCandleMessage.")]
public class RangeCandle : Candle<Unit>, IRangeCandleMessage
{
/// <summary>
/// Range of price.
/// </summary>
[DataMember]
public Unit PriceRange
{
get => TypedArg;
set => TypedArg = value;
}
/// <inheritdoc />
protected override void Validate(Unit value)
{
if (value is null)
throw new ArgumentNullException(nameof(value));
}
/// <summary>
/// Create a copy of <see cref="RangeCandle"/>.
/// </summary>
/// <returns>Copy.</returns>
public override Candle Clone()
{
return CopyTo(new RangeCandle());
}
}
/// <summary>
/// The candle of point-and-figure chart (tac-toe chart).
/// </summary>
[DataContract]
[Serializable]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.PnFCandleKey)]
[Obsolete("Use PnFCandleMessage.")]
public class PnFCandle : Candle<PnFArg>, IPnFCandleMessage
{
/// <summary>
/// Value of arguments.
/// </summary>
[DataMember]
public PnFArg PnFArg
{
get => TypedArg;
set => TypedArg = value;
}
/// <inheritdoc />
protected override void Validate(PnFArg value)
{
if (value is null)
throw new ArgumentNullException(nameof(value));
}
/// <summary>
/// Create a copy of <see cref="PnFCandle"/>.
/// </summary>
/// <returns>Copy.</returns>
public override Candle Clone()
{
return CopyTo(new PnFCandle());
}
}
/// <summary>
/// Renko candle.
/// </summary>
[DataContract]
[Serializable]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.RenkoCandleKey)]
[Obsolete("Use RenkoCandleMessage.")]
public class RenkoCandle : Candle<Unit>, IRenkoCandleMessage
{
/// <summary>
/// Possible price change range.
/// </summary>
[DataMember]
public Unit BoxSize
{
get => TypedArg;
set => TypedArg = value;
}
/// <inheritdoc />
protected override void Validate(Unit value)
{
if (value is null)
throw new ArgumentNullException(nameof(value));
}
/// <summary>
/// Create a copy of <see cref="RenkoCandle"/>.
/// </summary>
/// <returns>Copy.</returns>
public override Candle Clone()
{
return CopyTo(new RenkoCandle());
}
}
/// <summary>
/// Heikin ashi candle.
/// </summary>
[DataContract]
[Serializable]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.HeikinAshiKey)]
[Obsolete("Use HeikinAshiCandleMessage.")]
public class HeikinAshiCandle : TimeFrameCandle, IHeikinAshiCandleMessage
{
/// <summary>
/// Create a copy of <see cref="HeikinAshiCandle"/>.
/// </summary>
/// <returns>Copy.</returns>
public override Candle Clone()
{
return CopyTo(new HeikinAshiCandle());
}
}
}