StockSharp

Форк
0
779 строк · 16.6 Кб
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: Position.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.ComponentModel;
26

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

30
	/// <summary>
31
	/// The position by the instrument.
32
	/// </summary>
33
	[Serializable]
34
	[DataContract]
35
	[Display(
36
		ResourceType = typeof(LocalizedStrings),
37
		Name = LocalizedStrings.PositionKey,
38
		Description = LocalizedStrings.PositionDescKey)]
39
	public class Position : NotifiableObject, ILocalTimeMessage, IServerTimeMessage
40
	{
41
		DateTimeOffset IServerTimeMessage.ServerTime
42
		{
43
			get => LastChangeTime;
44
			set => LastChangeTime = value;
45
		}
46

47
		/// <summary>
48
		/// Initializes a new instance of the <see cref="Position"/>.
49
		/// </summary>
50
		public Position()
51
		{
52
		}
53

54
		private decimal? _beginValue;
55

56
		/// <summary>
57
		/// Position size at the beginning of the trading session.
58
		/// </summary>
59
		[DataMember]
60
		[Display(
61
			ResourceType = typeof(LocalizedStrings),
62
			Name = LocalizedStrings.BeginValueKey,
63
			Description = LocalizedStrings.PosBeginValueKey,
64
			GroupName = LocalizedStrings.StatisticsKey)]
65
		//[Nullable]
66
		[Browsable(false)]
67
		public decimal? BeginValue
68
		{
69
			get => _beginValue;
70
			set
71
			{
72
				if (_beginValue == value)
73
					return;
74

75
				_beginValue = value;
76
				NotifyChanged();
77
			}
78
		}
79

80
		private decimal? _currentValue;
81

82
		/// <summary>
83
		/// Current position size.
84
		/// </summary>
85
		[DataMember]
86
		[Display(
87
			ResourceType = typeof(LocalizedStrings),
88
			Name = LocalizedStrings.CurrentValueKey,
89
			Description = LocalizedStrings.CurrentPosSizeKey,
90
			GroupName = LocalizedStrings.StatisticsKey)]
91
		//[Nullable]
92
		//[Browsable(false)]
93
		public decimal? CurrentValue
94
		{
95
			get => _currentValue;
96
			set
97
			{
98
				if (_currentValue == value)
99
					return;
100

101
				_currentValue = value;
102
				NotifyChanged();
103
			}
104
		}
105

106
		private decimal? _blockedValue;
107

108
		/// <summary>
109
		/// Position size, registered for active orders.
110
		/// </summary>
111
		[DataMember]
112
		[Display(
113
			ResourceType = typeof(LocalizedStrings),
114
			Name = LocalizedStrings.BlockedKey,
115
			Description = LocalizedStrings.PosBlockedSizeKey,
116
			GroupName = LocalizedStrings.StatisticsKey)]
117
		//[Nullable]
118
		[Browsable(false)]
119
		public decimal? BlockedValue
120
		{
121
			get => _blockedValue;
122
			set
123
			{
124
				if (_blockedValue == value)
125
					return;
126

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

132
		private decimal? _currentPrice;
133

134
		/// <summary>
135
		/// Position price.
136
		/// </summary>
137
		//[Ignore]
138
		[XmlIgnore]
139
		[Display(
140
			ResourceType = typeof(LocalizedStrings),
141
			Name = LocalizedStrings.PosPriceKey,
142
			Description = LocalizedStrings.PosPriceDescKey,
143
			GroupName = LocalizedStrings.StatisticsKey)]
144
		[Browsable(false)]
145
		public decimal? CurrentPrice
146
		{
147
			get => _currentPrice;
148
			set
149
			{
150
				if (_currentPrice == value)
151
					return;
152

153
				_currentPrice = value;
154
				NotifyChanged();
155
			}
156
		}
157

158
		private decimal? _averagePrice;
159

160
		/// <summary>
161
		/// Average price.
162
		/// </summary>
163
		//[Ignore]
164
		[XmlIgnore]
165
		[Display(
166
			ResourceType = typeof(LocalizedStrings),
167
			Name = LocalizedStrings.AveragePriceKey,
168
			Description = LocalizedStrings.AveragePriceCalcTradesKey,
169
			GroupName = LocalizedStrings.StatisticsKey)]
170
		[Browsable(false)]
171
		public decimal? AveragePrice
172
		{
173
			get => _averagePrice;
174
			set
175
			{
176
				if (_averagePrice == value)
177
					return;
178

179
				_averagePrice = value;
180
				NotifyChanged();
181
			}
182
		}
183

184
		private decimal? _unrealizedPnL;
185

186
		/// <summary>
187
		/// Unrealized profit.
188
		/// </summary>
189
		//[Ignore]
190
		[XmlIgnore]
191
		[Display(
192
			ResourceType = typeof(LocalizedStrings),
193
			Name = LocalizedStrings.UnrealizedProfitKey,
194
			Description = LocalizedStrings.UnrealizedProfitDescKey,
195
			GroupName = LocalizedStrings.StatisticsKey)]
196
		[Browsable(false)]
197
		public decimal? UnrealizedPnL
198
		{
199
			get => _unrealizedPnL;
200
			set
201
			{
202
				if (_unrealizedPnL == value)
203
					return;
204

205
				_unrealizedPnL = value;
206
				NotifyChanged();
207
			}
208
		}
209

210
		private decimal? _realizedPnL;
211

212
		/// <summary>
213
		/// Realized profit.
214
		/// </summary>
215
		//[Ignore]
216
		[XmlIgnore]
217
		[Display(
218
			ResourceType = typeof(LocalizedStrings),
219
			Name = LocalizedStrings.RealizedProfitKey,
220
			Description = LocalizedStrings.RealizedProfitDescKey,
221
			GroupName = LocalizedStrings.StatisticsKey)]
222
		[Browsable(false)]
223
		public decimal? RealizedPnL
224
		{
225
			get => _realizedPnL;
226
			set
227
			{
228
				if (_realizedPnL == value)
229
					return;
230

231
				_realizedPnL = value;
232
				NotifyChanged();
233
			}
234
		}
235

236
		private decimal? _variationMargin;
237

238
		/// <summary>
239
		/// Variation margin.
240
		/// </summary>
241
		[DataMember]
242
		[Display(
243
			ResourceType = typeof(LocalizedStrings),
244
			Name = LocalizedStrings.VariationMarginKey,
245
			Description = LocalizedStrings.VariationMarginDescKey,
246
			GroupName = LocalizedStrings.StatisticsKey)]
247
		//[Nullable]
248
		[Browsable(false)]
249
		public decimal? VariationMargin
250
		{
251
			get => _variationMargin;
252
			set
253
			{
254
				if (_variationMargin == value)
255
					return;
256

257
				_variationMargin = value;
258
				NotifyChanged();
259
			}
260
		}
261

262
		private decimal? _commission;
263

264
		/// <summary>
265
		/// Total commission.
266
		/// </summary>
267
		[DataMember]
268
		[Display(
269
			ResourceType = typeof(LocalizedStrings),
270
			Name = LocalizedStrings.CommissionKey,
271
			Description = LocalizedStrings.TotalCommissionDescKey,
272
			GroupName = LocalizedStrings.StatisticsKey)]
273
		//[Nullable]
274
		[Browsable(false)]
275
		public decimal? Commission
276
		{
277
			get => _commission;
278
			set
279
			{
280
				if (_commission == value)
281
					return;
282

283
				_commission = value;
284
				NotifyChanged();
285
			}
286
		}
287

288
		private decimal? _settlementPrice;
289

290
		/// <summary>
291
		/// Settlement price.
292
		/// </summary>
293
		[DataMember]
294
		[Display(
295
			ResourceType = typeof(LocalizedStrings),
296
			Name = LocalizedStrings.SettlementPriceKey,
297
			Description = LocalizedStrings.SettlementPriceDescKey,
298
			GroupName = LocalizedStrings.StatisticsKey)]
299
		//[Nullable]
300
		[Browsable(false)]
301
		public decimal? SettlementPrice
302
		{
303
			get => _settlementPrice;
304
			set
305
			{
306
				if (_settlementPrice == value)
307
					return;
308

309
				_settlementPrice = value;
310
				NotifyChanged();
311
			}
312
		}
313

314
		private DateTimeOffset _lastChangeTime;
315

316
		/// <summary>
317
		/// Time of last position change.
318
		/// </summary>
319
		[DataMember]
320
		[Display(
321
			ResourceType = typeof(LocalizedStrings),
322
			Name = LocalizedStrings.ChangedKey,
323
			Description = LocalizedStrings.TimePosLastChangeKey,
324
			GroupName = LocalizedStrings.StatisticsKey)]
325
		[Browsable(false)]
326
		public DateTimeOffset LastChangeTime
327
		{
328
			get => _lastChangeTime;
329
			set
330
			{
331
				_lastChangeTime = value;
332
				NotifyChanged();
333
			}
334
		}
335

336
		private DateTimeOffset _localTime;
337

338
		/// <summary>
339
		/// Local time of the last position change.
340
		/// </summary>
341
		[DataMember]
342
		[Display(
343
			ResourceType = typeof(LocalizedStrings),
344
			Name = LocalizedStrings.LocalTimeKey,
345
			Description = LocalizedStrings.LocalTimeDescKey,
346
			GroupName = LocalizedStrings.StatisticsKey)]
347
		[Browsable(false)]
348
		public DateTimeOffset LocalTime
349
		{
350
			get => _localTime;
351
			set
352
			{
353
				_localTime = value;
354
				NotifyChanged();
355
			}
356
		}
357

358
		private string _description;
359

360
		/// <summary>
361
		/// Text position description.
362
		/// </summary>
363
		[DataMember]
364
		[Display(
365
			ResourceType = typeof(LocalizedStrings),
366
			Name = LocalizedStrings.DescriptionKey,
367
			Description = LocalizedStrings.PosTextKey,
368
			GroupName = LocalizedStrings.GeneralKey)]
369
		public string Description
370
		{
371
			get => _description;
372
			set
373
			{
374
				_description = value;
375
				NotifyChanged();
376
			}
377
		}
378

379
		private CurrencyTypes? _currency;
380

381
		/// <summary>
382
		/// Portfolio currency.
383
		/// </summary>
384
		[DataMember]
385
		[Display(
386
			ResourceType = typeof(LocalizedStrings),
387
			Name = LocalizedStrings.CurrencyKey,
388
			Description = LocalizedStrings.PortfolioCurrencyKey,
389
			GroupName = LocalizedStrings.GeneralKey)]
390
		public CurrencyTypes? Currency
391
		{
392
			get => _currency;
393
			set
394
			{
395
				_currency = value;
396
				NotifyChanged();
397
			}
398
		}
399

400
		private DateTimeOffset? _expirationDate;
401

402
		/// <summary>
403
		/// Expiration date.
404
		/// </summary>
405
		[DataMember]
406
		[Display(
407
			ResourceType = typeof(LocalizedStrings),
408
			Name = LocalizedStrings.ExpiryDateKey,
409
			Description = LocalizedStrings.ExpiryDateKey,
410
			GroupName = LocalizedStrings.GeneralKey)]
411
		public DateTimeOffset? ExpirationDate
412
		{
413
			get => _expirationDate;
414
			set
415
			{
416
				_expirationDate = value;
417
				NotifyChanged();
418
			}
419
		}
420

421
		/// <summary>
422
		/// Client code assigned by the broker.
423
		/// </summary>
424
		[DataMember]
425
		[Display(
426
			ResourceType = typeof(LocalizedStrings),
427
			Name = LocalizedStrings.ClientCodeKey,
428
			Description = LocalizedStrings.ClientCodeDescKey,
429
			GroupName = LocalizedStrings.GeneralKey)]
430
		public string ClientCode { get; set; }
431

432
		/// <summary>
433
		/// Portfolio, in which position is created.
434
		/// </summary>
435
		[DataMember]
436
		[Display(
437
			ResourceType = typeof(LocalizedStrings),
438
			Name = LocalizedStrings.PortfolioKey,
439
			Description = LocalizedStrings.PosPortfolioKey,
440
			GroupName = LocalizedStrings.GeneralKey)]
441
		public Portfolio Portfolio { get; set; }
442

443
		/// <summary>
444
		/// Security, for which a position was created.
445
		/// </summary>
446
		[DataMember]
447
		[Display(
448
			ResourceType = typeof(LocalizedStrings),
449
			Name = LocalizedStrings.SecurityKey,
450
			Description = LocalizedStrings.PosSecurityKey,
451
			GroupName = LocalizedStrings.GeneralKey)]
452
		public Security Security { get; set; }
453

454
		/// <summary>
455
		/// The depositary where the physical security.
456
		/// </summary>
457
		[Display(
458
			ResourceType = typeof(LocalizedStrings),
459
			Name = LocalizedStrings.DepoKey,
460
			Description = LocalizedStrings.DepoNameKey,
461
			GroupName = LocalizedStrings.GeneralKey)]
462
		[DataMember]
463
		public string DepoName { get; set; }
464

465
		/// <summary>
466
		/// Limit type for Т+ market.
467
		/// </summary>
468
		[Display(
469
			ResourceType = typeof(LocalizedStrings),
470
			Name = LocalizedStrings.LimitKey,
471
			Description = LocalizedStrings.PosLimitKey,
472
			GroupName = LocalizedStrings.GeneralKey)]
473
		//[Nullable]
474
		[DataMember]
475
		public TPlusLimits? LimitType { get; set; }
476

477
		/// <summary>
478
		/// Strategy id.
479
		/// </summary>
480
		[DataMember]
481
		[Display(
482
			ResourceType = typeof(LocalizedStrings),
483
			Name = LocalizedStrings.StrategyKey,
484
			Description = LocalizedStrings.IdentifierKey,
485
			GroupName = LocalizedStrings.GeneralKey,
486
			Order = 100)]
487
		public virtual string StrategyId { get; set; }
488

489
		/// <summary>
490
		/// Side.
491
		/// </summary>
492
		[DataMember]
493
		[Display(
494
			ResourceType = typeof(LocalizedStrings),
495
			Name = LocalizedStrings.SideKey,
496
			Description = LocalizedStrings.PosSideKey,
497
			GroupName = LocalizedStrings.GeneralKey,
498
			Order = 101)]
499
		public virtual Sides? Side { get; set; }
500

501
		private decimal? _leverage;
502

503
		/// <summary>
504
		/// Margin leverage.
505
		/// </summary>
506
		[DataMember]
507
		[Display(
508
			ResourceType = typeof(LocalizedStrings),
509
			Name = LocalizedStrings.LeverageKey,
510
			Description = LocalizedStrings.MarginLeverageKey,
511
			GroupName = LocalizedStrings.GeneralKey)]
512
		//[Nullable]
513
		public decimal? Leverage
514
		{
515
			get => _leverage;
516
			set
517
			{
518
				if (_leverage == value)
519
					return;
520

521
				//if (value < 0)
522
				//	throw new ArgumentOutOfRangeException(nameof(value), value, LocalizedStrings.InvalidValue);
523

524
				_leverage = value;
525
				NotifyChanged();
526
			}
527
		}
528

529
		private decimal? _commissionTaker;
530

531
		/// <summary>
532
		/// Commission (taker).
533
		/// </summary>
534
		[XmlIgnore]
535
		[Browsable(false)]
536
		public decimal? CommissionTaker
537
		{
538
			get => _commissionTaker;
539
			set
540
			{
541
				_commissionTaker = value;
542
				NotifyChanged();
543
			}
544
		}
545

546
		private decimal? _commissionMaker;
547

548
		/// <summary>
549
		/// Commission (maker).
550
		/// </summary>
551
		[XmlIgnore]
552
		[Browsable(false)]
553
		public decimal? CommissionMaker
554
		{
555
			get => _commissionMaker;
556
			set
557
			{
558
				_commissionMaker = value;
559
				NotifyChanged();
560
			}
561
		}
562

563
		private int? _buyOrdersCount;
564

565
		/// <summary>
566
		/// Orders (bids).
567
		/// </summary>
568
		[XmlIgnore]
569
		[Browsable(false)]
570
		public int? BuyOrdersCount
571
		{
572
			get => _buyOrdersCount;
573
			set
574
			{
575
				_buyOrdersCount = value;
576
				NotifyChanged();
577
			}
578
		}
579

580
		private int? _sellOrdersCount;
581

582
		/// <summary>
583
		/// Orders (asks).
584
		/// </summary>
585
		[XmlIgnore]
586
		[Browsable(false)]
587
		public int? SellOrdersCount
588
		{
589
			get => _sellOrdersCount;
590
			set
591
			{
592
				_sellOrdersCount = value;
593
				NotifyChanged();
594
			}
595
		}
596

597
		private decimal? _buyOrdersMargin;
598

599
		/// <summary>
600
		/// Margin (buy).
601
		/// </summary>
602
		[XmlIgnore]
603
		[Browsable(false)]
604
		public decimal? BuyOrdersMargin
605
		{
606
			get => _buyOrdersMargin;
607
			set
608
			{
609
				_buyOrdersMargin = value;
610
				NotifyChanged();
611
			}
612
		}
613

614
		private decimal? _sellOrdersMargin;
615

616
		/// <summary>
617
		/// Margin (sell).
618
		/// </summary>
619
		[XmlIgnore]
620
		[Browsable(false)]
621
		public decimal? SellOrdersMargin
622
		{
623
			get => _sellOrdersMargin;
624
			set
625
			{
626
				_sellOrdersMargin = value;
627
				NotifyChanged();
628
			}
629
		}
630

631
		private decimal? _ordersMargin;
632

633
		/// <summary>
634
		/// Orders (margin).
635
		/// </summary>
636
		[XmlIgnore]
637
		[Browsable(false)]
638
		public decimal? OrdersMargin
639
		{
640
			get => _ordersMargin;
641
			set
642
			{
643
				_ordersMargin = value;
644
				NotifyChanged();
645
			}
646
		}
647

648
		private int? _ordersCount;
649

650
		/// <summary>
651
		/// Orders.
652
		/// </summary>
653
		[XmlIgnore]
654
		[Browsable(false)]
655
		public int? OrdersCount
656
		{
657
			get => _ordersCount;
658
			set
659
			{
660
				_ordersCount = value;
661
				NotifyChanged();
662
			}
663
		}
664

665
		private int? _tradesCount;
666

667
		/// <summary>
668
		/// Trades.
669
		/// </summary>
670
		[XmlIgnore]
671
		[Browsable(false)]
672
		public int? TradesCount
673
		{
674
			get => _tradesCount;
675
			set
676
			{
677
				_tradesCount = value;
678
				NotifyChanged();
679
			}
680
		}
681

682
		private decimal? _liquidationPrice;
683

684
		/// <summary>
685
		/// Liquidation price.
686
		/// </summary>
687
		[DataMember]
688
		[Display(
689
			ResourceType = typeof(LocalizedStrings),
690
			Name = LocalizedStrings.LiquidationPriceKey,
691
			Description = LocalizedStrings.LiquidationPriceKey,
692
			GroupName = LocalizedStrings.StatisticsKey)]
693
		[Browsable(false)]
694
		public decimal? LiquidationPrice
695
		{
696
			get => _liquidationPrice;
697
			set
698
			{
699
				if (_liquidationPrice == value)
700
					return;
701

702
				_liquidationPrice = value;
703
				NotifyChanged();
704
			}
705
		}
706

707
		/// <summary>
708
		/// Create a copy of <see cref="Position"/>.
709
		/// </summary>
710
		/// <returns>Copy.</returns>
711
		public virtual Position Clone()
712
		{
713
			var clone = new Position();
714
			CopyTo(clone);
715
			return clone;
716
		}
717

718
		/// <summary>
719
		/// To copy fields of the current position to <paramref name="destination" />.
720
		/// </summary>
721
		/// <param name="destination">The position in which you should to copy fields.</param>
722
		public void CopyTo(Position destination)
723
		{
724
			if (destination == null)
725
				throw new ArgumentNullException(nameof(destination));
726

727
			destination.CurrentValue = CurrentValue;
728
			destination.BeginValue = BeginValue;
729
			destination.BlockedValue = BlockedValue;
730
			destination.Commission = Commission;
731
			destination.VariationMargin = VariationMargin;
732
			destination.RealizedPnL = RealizedPnL;
733
			destination.UnrealizedPnL = UnrealizedPnL;
734
			destination.AveragePrice = AveragePrice;
735
			destination.CurrentPrice = CurrentPrice;
736
			destination.SettlementPrice = SettlementPrice;
737
			destination.Description = Description;
738
			destination.Currency = Currency;
739
			destination.ExpirationDate = ExpirationDate;
740
			destination.ClientCode = ClientCode;
741
			//destination.LastChangeTime = LastChangeTime;
742
			//destination.LocalTime = LocalTime;
743

744
			destination.Portfolio = Portfolio;
745
			destination.Security = Security;
746
			destination.DepoName = DepoName;
747
			destination.LimitType = LimitType;
748
			destination.StrategyId = StrategyId;
749
			destination.Side = Side;
750

751
			destination.Leverage = Leverage;
752
			destination.CommissionMaker = CommissionMaker;
753
			destination.CommissionTaker = CommissionTaker;
754

755
			destination.BuyOrdersCount = BuyOrdersCount;
756
			destination.SellOrdersCount = SellOrdersCount;
757
			destination.BuyOrdersMargin = BuyOrdersMargin;
758
			destination.SellOrdersMargin = SellOrdersMargin;
759
			destination.OrdersCount = OrdersCount;
760
			destination.TradesCount = TradesCount;
761

762
			destination.LiquidationPrice = LiquidationPrice;
763
		}
764

765
		/// <inheritdoc />
766
		public override string ToString()
767
		{
768
			var str = $"{Portfolio}-{Security}";
769

770
			if (!StrategyId.IsEmpty())
771
				str += $"-{StrategyId}";
772

773
			if (Side != null)
774
				str += $"-{Side.Value}";
775

776
			return str;
777
		}
778
	}
779
}
780

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

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

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

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