StockSharp

Форк
0
747 строк · 17.8 Кб
1
#region S# License
2
/******************************************************************************************
3
NOTICE!!!  This program and source code is owned and licensed by
4
StockSharp, LLC, www.stocksharp.com
5
Viewing or use of this code requires your acceptance of the license
6
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
7
Removal of this comment is a violation of the license agreement.
8

9
Project: StockSharp.BusinessEntities.BusinessEntities
10
File: Order.cs
11
Created: 2015, 11, 11, 2:32 PM
12

13
Copyright 2010 by StockSharp, LLC
14
*******************************************************************************************/
15
#endregion S# License
16
namespace StockSharp.BusinessEntities
17
{
18
	using System;
19
	using System.ComponentModel;
20
	using System.ComponentModel.DataAnnotations;
21
	using System.Runtime.Serialization;
22
	using System.Xml.Serialization;
23

24
	using Ecng.Common;
25
	using Ecng.Collections;
26
	using Ecng.ComponentModel;
27

28
	using StockSharp.Messages;
29
	using StockSharp.Localization;
30

31
	/// <summary>
32
	/// Order.
33
	/// </summary>
34
	[DataContract]
35
	[Serializable]
36
	[Display(
37
		ResourceType = typeof(LocalizedStrings),
38
		Name = LocalizedStrings.OrderKey,
39
		Description = LocalizedStrings.InfoAboutOrderKey)]
40
	public class Order : NotifiableObject, IOrderMessage
41
	{
42
		/// <summary>
43
		/// Initializes a new instance of the <see cref="Order"/>.
44
		/// </summary>
45
		public Order()
46
		{
47
		}
48

49
		private SecurityId? _securityId;
50

51
		SecurityId ISecurityIdMessage.SecurityId
52
		{
53
			get => _securityId ??= Security?.Id.ToSecurityId() ?? default;
54
			set => throw new NotSupportedException();
55
		}
56

57
		Messages.DataType IGeneratedMessage.BuildFrom { get; set; }
58

59
		private TimeSpan? _latencyRegistration;
60

61
		/// <summary>
62
		/// Time taken to register an order.
63
		/// </summary>
64
		//[TimeSpan]
65
		[Display(
66
			ResourceType = typeof(LocalizedStrings),
67
			Name = LocalizedStrings.RegistrationKey,
68
			Description = LocalizedStrings.OrderRegLatencyKey,
69
			GroupName = LocalizedStrings.LatencyKey,
70
			Order = 1000)]
71
		public TimeSpan? LatencyRegistration
72
		{
73
			get => _latencyRegistration;
74
			set
75
			{
76
				if (_latencyRegistration == value)
77
					return;
78

79
				_latencyRegistration = value;
80
				NotifyChanged();
81
			}
82
		}
83

84
		private TimeSpan? _latencyCancellation;
85

86
		/// <summary>
87
		/// Time taken to cancel an order.
88
		/// </summary>
89
		[Display(
90
			ResourceType = typeof(LocalizedStrings),
91
			Name = LocalizedStrings.CancellationKey,
92
			Description = LocalizedStrings.OrderCancelLatencyKey,
93
			GroupName = LocalizedStrings.LatencyKey,
94
			Order = 1001)]
95
		public TimeSpan? LatencyCancellation
96
		{
97
			get => _latencyCancellation;
98
			set
99
			{
100
				if (_latencyCancellation == value)
101
					return;
102

103
				_latencyCancellation = value;
104
				NotifyChanged();
105
			}
106
		}
107

108
		private TimeSpan? _latencyEdition;
109

110
		/// <summary>
111
		/// Time taken to edit an order.
112
		/// </summary>
113
		[Display(
114
			ResourceType = typeof(LocalizedStrings),
115
			Name = LocalizedStrings.EditionKey,
116
			Description = LocalizedStrings.EditionLatencyKey,
117
			GroupName = LocalizedStrings.LatencyKey,
118
			Order = 1002)]
119
		public TimeSpan? LatencyEdition
120
		{
121
			get => _latencyEdition;
122
			set
123
			{
124
				if (_latencyEdition == value)
125
					return;
126

127
				_latencyEdition = value;
128
				NotifyChanged();
129
			}
130
		}
131

132
		private long? _id;
133

134
		/// <summary>
135
		/// Order ID.
136
		/// </summary>
137
		[DataMember]
138
		[Display(
139
			ResourceType = typeof(LocalizedStrings),
140
			Name = LocalizedStrings.IdentifierKey,
141
			Description = LocalizedStrings.IdStringKey + LocalizedStrings.Dot,
142
			GroupName = LocalizedStrings.GeneralKey)]
143
		public long? Id
144
		{
145
			get => _id;
146
			set
147
			{
148
				if (_id == value)
149
					return;
150

151
				_id = value;
152
				NotifyChanged();
153
			}
154
		}
155

156
		private string _stringId;
157

158
		/// <summary>
159
		/// Order ID (as string, if electronic board does not use numeric order ID representation).
160
		/// </summary>
161
		[DataMember]
162
		[Display(
163
			ResourceType = typeof(LocalizedStrings),
164
			Name = LocalizedStrings.IdStringKey,
165
			Description = LocalizedStrings.OrderIdStringDescKey,
166
			GroupName = LocalizedStrings.GeneralKey)]
167
		public string StringId
168
		{
169
			get => _stringId;
170
			set
171
			{
172
				_stringId = value;
173
				NotifyChanged();
174
			}
175
		}
176

177
		private string _boardId;
178

179
		/// <summary>
180
		/// Board order id. Uses in case of <see cref="Id"/> and <see cref="StringId"/> is a brokerage system ids.
181
		/// </summary>
182
		[DataMember]
183
		[Display(
184
			ResourceType = typeof(LocalizedStrings),
185
			Name = LocalizedStrings.OrderBoardIdKey,
186
			Description = LocalizedStrings.OrderBoardIdDescKey,
187
			GroupName = LocalizedStrings.GeneralKey)]
188
		public string BoardId
189
		{
190
			get => _boardId;
191
			set
192
			{
193
				_boardId = value;
194
				NotifyChanged();
195
			}
196
		}
197

198
		private DateTimeOffset _time;
199

200
		/// <summary>
201
		/// Order placing time on exchange.
202
		/// </summary>
203
		[DataMember]
204
		[Display(
205
			ResourceType = typeof(LocalizedStrings),
206
			Name = LocalizedStrings.RegTimeKey,
207
			Description = LocalizedStrings.RegTimeDescKey,
208
			GroupName = LocalizedStrings.GeneralKey)]
209
		public DateTimeOffset Time
210
		{
211
			get => _time;
212
			set
213
			{
214
				if (_time == value)
215
					return;
216

217
				_time = value;
218
				NotifyChanged();
219
			}
220
		}
221

222
		/// <summary>
223
		/// Transaction ID. Automatically set when the <see cref="ITransactionProvider.RegisterOrder"/> method called.
224
		/// </summary>
225
		[DataMember]
226
		[Display(
227
			ResourceType = typeof(LocalizedStrings),
228
			Name = LocalizedStrings.TransactionKey,
229
			Description = LocalizedStrings.TransactionIdKey + LocalizedStrings.Dot,
230
			GroupName = LocalizedStrings.GeneralKey)]
231
		public long TransactionId { get; set; }
232

233
		/// <summary>
234
		/// Security, for which an order is being placed.
235
		/// </summary>
236
		[DataMember]
237
		[Display(
238
			ResourceType = typeof(LocalizedStrings),
239
			Name = LocalizedStrings.SecurityKey,
240
			Description = LocalizedStrings.OrderSecurityKey,
241
			GroupName = LocalizedStrings.GeneralKey)]
242
		public Security Security { get; set; }
243

244
		private OrderStates _state;
245

246
		/// <summary>
247
		/// Order state.
248
		/// </summary>
249
		[DataMember]
250
		[Display(
251
			ResourceType = typeof(LocalizedStrings),
252
			Name = LocalizedStrings.StateKey,
253
			Description = LocalizedStrings.OrderStateDescKey,
254
			GroupName = LocalizedStrings.GeneralKey)]
255
		public OrderStates State
256
		{
257
			get => _state;
258
			set
259
			{
260
				if (_state == value)
261
					return;
262

263
				_state = value;
264
				NotifyChanged();
265
			}
266
		}
267

268
		/// <summary>
269
		/// Portfolio, in which the order is being traded.
270
		/// </summary>
271
		[DataMember]
272
		[Display(
273
			ResourceType = typeof(LocalizedStrings),
274
			Name = LocalizedStrings.PortfolioKey,
275
			Description = LocalizedStrings.OrderPortfolioKey,
276
			GroupName = LocalizedStrings.GeneralKey)]
277
		public Portfolio Portfolio { get; set; }
278

279
		private DateTimeOffset _serverTime;
280

281
		/// <inheritdoc/>
282
		[DataMember]
283
		[Display(
284
			ResourceType = typeof(LocalizedStrings),
285
			Name = LocalizedStrings.ChangedKey,
286
			Description = LocalizedStrings.OrderLastChangeTimeKey,
287
			GroupName = LocalizedStrings.GeneralKey)]
288
		public DateTimeOffset ServerTime
289
		{
290
			get => _serverTime;
291
			set
292
			{
293
				if (_serverTime == value)
294
					return;
295

296
				_serverTime = value;
297
				NotifyChanged();
298
			}
299
		}
300

301
		/// <summary>
302
		/// Time of last order change (Cancellation, Fill).
303
		/// </summary>
304
		[Obsolete("Use LastChangeTime property.")]
305
		[Browsable(false)]
306
		public DateTimeOffset LastChangeTime
307
		{
308
			get => ServerTime;
309
			set => ServerTime = value;
310
		}
311

312
		private DateTimeOffset _localTime;
313

314
		/// <summary>
315
		/// Last order change local time (Cancellation, Fill).
316
		/// </summary>
317
		[DataMember]
318
		[Display(
319
			ResourceType = typeof(LocalizedStrings),
320
			Name = LocalizedStrings.LocalTimeKey,
321
			Description = LocalizedStrings.LocalTimeDescKey,
322
			GroupName = LocalizedStrings.GeneralKey)]
323
		public DateTimeOffset LocalTime
324
		{
325
			get => _localTime;
326
			set
327
			{
328
				if (_localTime == value)
329
					return;
330

331
				_localTime = value;
332
				NotifyChanged();
333
			}
334
		}
335

336
		/// <summary>
337
		/// Order price.
338
		/// </summary>
339
		[DataMember]
340
		[Display(
341
			ResourceType = typeof(LocalizedStrings),
342
			Name = LocalizedStrings.PriceKey,
343
			Description = LocalizedStrings.OrderPriceKey,
344
			GroupName = LocalizedStrings.GeneralKey)]
345
		public decimal Price { get; set; }
346

347
		/// <summary>
348
		/// Number of contracts in the order.
349
		/// </summary>
350
		[DataMember]
351
		[Display(
352
			ResourceType = typeof(LocalizedStrings),
353
			Name = LocalizedStrings.VolumeKey,
354
			Description = LocalizedStrings.OrderVolumeKey,
355
			GroupName = LocalizedStrings.GeneralKey)]
356
		public decimal Volume { get; set; }
357

358
		/// <summary>
359
		/// Visible quantity of contracts in order.
360
		/// </summary>
361
		[DataMember]
362
		[Display(
363
			ResourceType = typeof(LocalizedStrings),
364
			Name = LocalizedStrings.VisibleVolumeKey,
365
			Description = LocalizedStrings.VisibleVolumeDescKey,
366
			GroupName = LocalizedStrings.GeneralKey)]
367
		public decimal? VisibleVolume { get; set; }
368

369
		/// <inheritdoc/>
370
		[DataMember]
371
		[Display(
372
			ResourceType = typeof(LocalizedStrings),
373
			Name = LocalizedStrings.DirectionKey,
374
			Description = LocalizedStrings.OrderSideDescKey,
375
			GroupName = LocalizedStrings.GeneralKey)]
376
		public Sides Side { get; set; }
377

378
		/// <summary>
379
		/// Order side (buy or sell).
380
		/// </summary>
381
		[Browsable(false)]
382
		[Obsolete("Use Direction property.")]
383
		public Sides Direction
384
		{
385
			get => Side;
386
			set => Side = value;
387
		}
388

389
		private decimal _balance;
390

391
		/// <summary>
392
		/// Order contracts balance.
393
		/// </summary>
394
		[DataMember]
395
		[Display(
396
			ResourceType = typeof(LocalizedStrings),
397
			Name = LocalizedStrings.BalanceKey,
398
			Description = LocalizedStrings.OrderBalanceKey,
399
			GroupName = LocalizedStrings.GeneralKey)]
400
		public decimal Balance
401
		{
402
			get => _balance;
403
			set
404
			{
405
				if (_balance == value)
406
					return;
407

408
				_balance = value;
409
				NotifyChanged();
410
			}
411
		}
412

413
		private long? _status;
414

415
		/// <summary>
416
		/// System order status.
417
		/// </summary>
418
		[DataMember]
419
		[Browsable(false)]
420
		public long? Status
421
		{
422
			get => _status;
423
			set
424
			{
425
				if (_status == value)
426
					return;
427

428
				_status = value;
429
				NotifyChanged();
430
			}
431
		}
432

433
		private bool? _isSystem;
434

435
		/// <summary>
436
		/// Is a system trade.
437
		/// </summary>
438
		[DataMember]
439
		[Display(
440
			ResourceType = typeof(LocalizedStrings),
441
			Name = LocalizedStrings.SystemKey,
442
			Description = LocalizedStrings.IsSystemTradeKey,
443
			GroupName = LocalizedStrings.GeneralKey)]
444
		public bool? IsSystem
445
		{
446
			get => _isSystem;
447
			set
448
			{
449
				if (_isSystem == value)
450
					return;
451

452
				_isSystem = value;
453
				NotifyChanged();
454
			}
455
		}
456

457
		/// <summary>
458
		/// Placed order comment.
459
		/// </summary>
460
		[DataMember]
461
		[Display(
462
			ResourceType = typeof(LocalizedStrings),
463
			Name = LocalizedStrings.CommentKey,
464
			Description = LocalizedStrings.OrderCommentKey,
465
			GroupName = LocalizedStrings.GeneralKey)]
466
		public string Comment { get; set; }
467

468
		/// <summary>
469
		/// Order type.
470
		/// </summary>
471
		[DataMember]
472
		[Display(
473
			ResourceType = typeof(LocalizedStrings),
474
			Name = LocalizedStrings.OrderTypeKey,
475
			Description = LocalizedStrings.OrderTypeDescKey,
476
			GroupName = LocalizedStrings.GeneralKey)]
477
		public OrderTypes? Type { get; set; }
478

479
		private DateTimeOffset? _expiryDate;
480

481
		/// <summary>
482
		/// Order expiry time. The default is <see langword="null" />, which mean (GTC).
483
		/// </summary>
484
		/// <remarks>
485
		/// If the value is <see langword="null"/>, then the order is registered until cancel. Otherwise, the period is specified.
486
		/// </remarks>
487
		[DataMember]
488
		[Display(
489
			ResourceType = typeof(LocalizedStrings),
490
			Name = LocalizedStrings.ExpirationKey,
491
			Description = LocalizedStrings.OrderExpirationTimeKey,
492
			GroupName = LocalizedStrings.GeneralKey)]
493
		public DateTimeOffset? ExpiryDate
494
		{
495
			get => _expiryDate;
496
			set
497
			{
498
				if (_expiryDate == value)
499
					return;
500

501
				_expiryDate = value;
502
				NotifyChanged();
503
			}
504
		}
505

506
		/// <summary>
507
		/// Order condition (e.g., stop- and algo- orders parameters).
508
		/// </summary>
509
		[XmlIgnore]
510
		[Display(
511
			ResourceType = typeof(LocalizedStrings),
512
			Name = LocalizedStrings.ConditionKey,
513
			Description = LocalizedStrings.OrderConditionDescKey,
514
			GroupName = LocalizedStrings.ConditionalOrderKey)]
515
		public OrderCondition Condition { get; set; }
516

517
		/// <summary>
518
		/// Limit order time in force.
519
		/// </summary>
520
		[Display(
521
			ResourceType = typeof(LocalizedStrings),
522
			Name = LocalizedStrings.TimeInForceKey,
523
			Description = LocalizedStrings.LimitOrderTifKey,
524
			GroupName = LocalizedStrings.GeneralKey)]
525
		public TimeInForce? TimeInForce { get; set; }
526

527
		private Order _derivedOrder;
528

529
		/// <summary>
530
		/// Exchange order that was created by the stop-order when the condition is activated (<see langword="null" /> if a stop condition has not been activated).
531
		/// </summary>
532
		//[DataMember]
533
		[XmlIgnore]
534
		[Display(
535
			ResourceType = typeof(LocalizedStrings),
536
			Name = LocalizedStrings.LinkedOrderKey,
537
			Description = LocalizedStrings.LinkedOrderDescKey,
538
			GroupName = LocalizedStrings.ConditionalOrderKey)]
539
		[Obsolete("No longer used.")]
540
		public Order DerivedOrder
541
		{
542
			get => _derivedOrder;
543
			set
544
			{
545
				if (_derivedOrder == value)
546
					return;
547

548
				_derivedOrder = value;
549
				NotifyChanged();
550
			}
551
		}
552

553
		/// <summary>
554
		/// Commission (broker, exchange etc.).
555
		/// </summary>
556
		[DataMember]
557
		[Display(
558
			ResourceType = typeof(LocalizedStrings),
559
			Name = LocalizedStrings.CommissionKey,
560
			Description = LocalizedStrings.CommissionDescKey,
561
			GroupName = LocalizedStrings.GeneralKey)]
562
		public decimal? Commission { get; set; }
563

564
		/// <summary>
565
		/// Commission currency. Can be <see langword="null"/>.
566
		/// </summary>
567
		public string CommissionCurrency { get; set; }
568

569
		/// <summary>
570
		/// User's order ID.
571
		/// </summary>
572
		[DataMember]
573
		[Display(
574
			ResourceType = typeof(LocalizedStrings),
575
			Name = LocalizedStrings.UserIdKey,
576
			Description = LocalizedStrings.UserOrderIdKey,
577
			GroupName = LocalizedStrings.GeneralKey)]
578
		public string UserOrderId { get; set; }
579

580
		/// <summary>
581
		/// Strategy id.
582
		/// </summary>
583
		[DataMember]
584
		public string StrategyId { get; set; }
585

586
		/// <summary>
587
		/// Broker firm code.
588
		/// </summary>
589
		[DataMember]
590
		[Display(
591
			ResourceType = typeof(LocalizedStrings),
592
			Name = LocalizedStrings.BrokerKey,
593
			Description = LocalizedStrings.BrokerCodeKey,
594
			GroupName = LocalizedStrings.GeneralKey)]
595
		public string BrokerCode { get; set; }
596

597
		/// <summary>
598
		/// Client code assigned by the broker.
599
		/// </summary>
600
		[DataMember]
601
		[Display(
602
			ResourceType = typeof(LocalizedStrings),
603
			Name = LocalizedStrings.ClientCodeKey,
604
			Description = LocalizedStrings.ClientCodeDescKey,
605
			GroupName = LocalizedStrings.GeneralKey)]
606
		public string ClientCode { get; set; }
607

608
		/// <summary>
609
		/// Trading security currency.
610
		/// </summary>
611
		[DataMember]
612
		[Display(
613
			ResourceType = typeof(LocalizedStrings),
614
			Name = LocalizedStrings.CurrencyKey,
615
			Description = LocalizedStrings.CurrencyDescKey,
616
			GroupName = LocalizedStrings.GeneralKey)]
617
		public CurrencyTypes? Currency { get; set; }
618

619
		/// <summary>
620
		/// Is the order of market-maker.
621
		/// </summary>
622
		[DataMember]
623
		[Display(
624
			ResourceType = typeof(LocalizedStrings),
625
			Name = LocalizedStrings.MarketMakerKey,
626
			Description = LocalizedStrings.MarketMakerOrderKey + LocalizedStrings.Dot,
627
			GroupName = LocalizedStrings.GeneralKey)]
628
		public bool? IsMarketMaker { get; set; }
629

630
		/// <summary>
631
		/// Is margin enabled.
632
		/// </summary>
633
		[DataMember]
634
		[Display(
635
			ResourceType = typeof(LocalizedStrings),
636
			Name = LocalizedStrings.MarginKey,
637
			Description = LocalizedStrings.IsMarginKey,
638
			GroupName = LocalizedStrings.GeneralKey)]
639
		public bool? IsMargin { get; set; }
640

641
		/// <summary>
642
		/// Slippage in trade price.
643
		/// </summary>
644
		[DataMember]
645
		[Display(
646
			ResourceType = typeof(LocalizedStrings),
647
			Name = LocalizedStrings.SlippageKey,
648
			Description = LocalizedStrings.SlippageTradeKey,
649
			GroupName = LocalizedStrings.GeneralKey)]
650
		public decimal? Slippage { get; set; }
651

652
		/// <summary>
653
		/// Is order manual.
654
		/// </summary>
655
		[DataMember]
656
		[Display(
657
			ResourceType = typeof(LocalizedStrings),
658
			Name = LocalizedStrings.ManualKey,
659
			Description = LocalizedStrings.IsOrderManualKey,
660
			GroupName = LocalizedStrings.GeneralKey)]
661
		public bool? IsManual { get; set; }
662

663
		/// <summary>
664
		/// Average execution price.
665
		/// </summary>
666
		[DataMember]
667
		public decimal? AveragePrice { get; set; }
668

669
		/// <summary>
670
		/// Yield.
671
		/// </summary>
672
		[DataMember]
673
		public decimal? Yield { get; set; }
674

675
		/// <summary>
676
		/// Minimum quantity of an order to be executed.
677
		/// </summary>
678
		[DataMember]
679
		public decimal? MinVolume { get; set; }
680

681
		/// <summary>
682
		/// Position effect.
683
		/// </summary>
684
		[DataMember]
685
		public OrderPositionEffects? PositionEffect { get; set; }
686

687
		/// <summary>
688
		/// Post-only order.
689
		/// </summary>
690
		[DataMember]
691
		public bool? PostOnly { get; set; }
692

693
		/// <summary>
694
		/// Sequence number.
695
		/// </summary>
696
		/// <remarks>Zero means no information.</remarks>
697
		[DataMember]
698
		public long SeqNum { get; set; }
699

700
		/// <summary>
701
		/// Margin leverage.
702
		/// </summary>
703
		[DataMember]
704
		[Display(
705
			ResourceType = typeof(LocalizedStrings),
706
			Name = LocalizedStrings.LeverageKey,
707
			Description = LocalizedStrings.MarginLeverageKey,
708
			GroupName = LocalizedStrings.GeneralKey)]
709
		public int? Leverage { get; set; }
710

711
		/// <inheritdoc />
712
		public override string ToString()
713
		{
714
			var str = LocalizedStrings.OrderDetails
715
				.Put(TransactionId, Id == null ? StringId : Id.To<string>(), Security?.Id, Portfolio?.Name, Side == Sides.Buy ? LocalizedStrings.Buy2 : LocalizedStrings.Sell2, Price, Volume, State, Balance, Type);
716

717
			if (!UserOrderId.IsEmpty())
718
				str += $" UID={UserOrderId}";
719

720
			if (!StrategyId.IsEmpty())
721
				str += $" Strategy={StrategyId}";
722

723
			if (Condition != null)
724
				str += $" Condition={Condition}";
725

726
			if (AveragePrice != null)
727
				str += $" AvgPrice={AveragePrice}";
728

729
			if (MinVolume != null)
730
				str += $" MinVolume={MinVolume}";
731

732
			if (PositionEffect != null)
733
				str += $" PosEffect={PositionEffect.Value}";
734

735
			if (PostOnly != null)
736
				str += $",PostOnly={PostOnly.Value}";
737

738
			if (SeqNum != 0)
739
				str += $",SeqNum={SeqNum}";
740

741
			if (Leverage != null)
742
				str += $",Leverage={Leverage.Value}";
743

744
			return str;
745
		}
746
	}
747
}
748

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.