StockSharp
747 строк · 17.8 Кб
1#region S# License2/******************************************************************************************
3NOTICE!!! This program and source code is owned and licensed by
4StockSharp, LLC, www.stocksharp.com
5Viewing or use of this code requires your acceptance of the license
6agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
7Removal of this comment is a violation of the license agreement.
8
9Project: StockSharp.BusinessEntities.BusinessEntities
10File: Order.cs
11Created: 2015, 11, 11, 2:32 PM
12
13Copyright 2010 by StockSharp, LLC
14*******************************************************************************************/
15#endregion S# License16namespace StockSharp.BusinessEntities17{
18using System;19using System.ComponentModel;20using System.ComponentModel.DataAnnotations;21using System.Runtime.Serialization;22using System.Xml.Serialization;23
24using Ecng.Common;25using Ecng.Collections;26using Ecng.ComponentModel;27
28using StockSharp.Messages;29using StockSharp.Localization;30
31/// <summary>32/// Order.33/// </summary>34[DataContract]35[Serializable]36[Display(37ResourceType = typeof(LocalizedStrings),38Name = LocalizedStrings.OrderKey,39Description = LocalizedStrings.InfoAboutOrderKey)]40public class Order : NotifiableObject, IOrderMessage41{42/// <summary>43/// Initializes a new instance of the <see cref="Order"/>.44/// </summary>45public Order()46{47}48
49private SecurityId? _securityId;50
51SecurityId ISecurityIdMessage.SecurityId52{53get => _securityId ??= Security?.Id.ToSecurityId() ?? default;54set => throw new NotSupportedException();55}56
57Messages.DataType IGeneratedMessage.BuildFrom { get; set; }58
59private TimeSpan? _latencyRegistration;60
61/// <summary>62/// Time taken to register an order.63/// </summary>64//[TimeSpan]65[Display(66ResourceType = typeof(LocalizedStrings),67Name = LocalizedStrings.RegistrationKey,68Description = LocalizedStrings.OrderRegLatencyKey,69GroupName = LocalizedStrings.LatencyKey,70Order = 1000)]71public TimeSpan? LatencyRegistration72{73get => _latencyRegistration;74set75{76if (_latencyRegistration == value)77return;78
79_latencyRegistration = value;80NotifyChanged();81}82}83
84private TimeSpan? _latencyCancellation;85
86/// <summary>87/// Time taken to cancel an order.88/// </summary>89[Display(90ResourceType = typeof(LocalizedStrings),91Name = LocalizedStrings.CancellationKey,92Description = LocalizedStrings.OrderCancelLatencyKey,93GroupName = LocalizedStrings.LatencyKey,94Order = 1001)]95public TimeSpan? LatencyCancellation96{97get => _latencyCancellation;98set99{100if (_latencyCancellation == value)101return;102
103_latencyCancellation = value;104NotifyChanged();105}106}107
108private TimeSpan? _latencyEdition;109
110/// <summary>111/// Time taken to edit an order.112/// </summary>113[Display(114ResourceType = typeof(LocalizedStrings),115Name = LocalizedStrings.EditionKey,116Description = LocalizedStrings.EditionLatencyKey,117GroupName = LocalizedStrings.LatencyKey,118Order = 1002)]119public TimeSpan? LatencyEdition120{121get => _latencyEdition;122set123{124if (_latencyEdition == value)125return;126
127_latencyEdition = value;128NotifyChanged();129}130}131
132private long? _id;133
134/// <summary>135/// Order ID.136/// </summary>137[DataMember]138[Display(139ResourceType = typeof(LocalizedStrings),140Name = LocalizedStrings.IdentifierKey,141Description = LocalizedStrings.IdStringKey + LocalizedStrings.Dot,142GroupName = LocalizedStrings.GeneralKey)]143public long? Id144{145get => _id;146set147{148if (_id == value)149return;150
151_id = value;152NotifyChanged();153}154}155
156private 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(163ResourceType = typeof(LocalizedStrings),164Name = LocalizedStrings.IdStringKey,165Description = LocalizedStrings.OrderIdStringDescKey,166GroupName = LocalizedStrings.GeneralKey)]167public string StringId168{169get => _stringId;170set171{172_stringId = value;173NotifyChanged();174}175}176
177private 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(184ResourceType = typeof(LocalizedStrings),185Name = LocalizedStrings.OrderBoardIdKey,186Description = LocalizedStrings.OrderBoardIdDescKey,187GroupName = LocalizedStrings.GeneralKey)]188public string BoardId189{190get => _boardId;191set192{193_boardId = value;194NotifyChanged();195}196}197
198private DateTimeOffset _time;199
200/// <summary>201/// Order placing time on exchange.202/// </summary>203[DataMember]204[Display(205ResourceType = typeof(LocalizedStrings),206Name = LocalizedStrings.RegTimeKey,207Description = LocalizedStrings.RegTimeDescKey,208GroupName = LocalizedStrings.GeneralKey)]209public DateTimeOffset Time210{211get => _time;212set213{214if (_time == value)215return;216
217_time = value;218NotifyChanged();219}220}221
222/// <summary>223/// Transaction ID. Automatically set when the <see cref="ITransactionProvider.RegisterOrder"/> method called.224/// </summary>225[DataMember]226[Display(227ResourceType = typeof(LocalizedStrings),228Name = LocalizedStrings.TransactionKey,229Description = LocalizedStrings.TransactionIdKey + LocalizedStrings.Dot,230GroupName = LocalizedStrings.GeneralKey)]231public long TransactionId { get; set; }232
233/// <summary>234/// Security, for which an order is being placed.235/// </summary>236[DataMember]237[Display(238ResourceType = typeof(LocalizedStrings),239Name = LocalizedStrings.SecurityKey,240Description = LocalizedStrings.OrderSecurityKey,241GroupName = LocalizedStrings.GeneralKey)]242public Security Security { get; set; }243
244private OrderStates _state;245
246/// <summary>247/// Order state.248/// </summary>249[DataMember]250[Display(251ResourceType = typeof(LocalizedStrings),252Name = LocalizedStrings.StateKey,253Description = LocalizedStrings.OrderStateDescKey,254GroupName = LocalizedStrings.GeneralKey)]255public OrderStates State256{257get => _state;258set259{260if (_state == value)261return;262
263_state = value;264NotifyChanged();265}266}267
268/// <summary>269/// Portfolio, in which the order is being traded.270/// </summary>271[DataMember]272[Display(273ResourceType = typeof(LocalizedStrings),274Name = LocalizedStrings.PortfolioKey,275Description = LocalizedStrings.OrderPortfolioKey,276GroupName = LocalizedStrings.GeneralKey)]277public Portfolio Portfolio { get; set; }278
279private DateTimeOffset _serverTime;280
281/// <inheritdoc/>282[DataMember]283[Display(284ResourceType = typeof(LocalizedStrings),285Name = LocalizedStrings.ChangedKey,286Description = LocalizedStrings.OrderLastChangeTimeKey,287GroupName = LocalizedStrings.GeneralKey)]288public DateTimeOffset ServerTime289{290get => _serverTime;291set292{293if (_serverTime == value)294return;295
296_serverTime = value;297NotifyChanged();298}299}300
301/// <summary>302/// Time of last order change (Cancellation, Fill).303/// </summary>304[Obsolete("Use LastChangeTime property.")]305[Browsable(false)]306public DateTimeOffset LastChangeTime307{308get => ServerTime;309set => ServerTime = value;310}311
312private DateTimeOffset _localTime;313
314/// <summary>315/// Last order change local time (Cancellation, Fill).316/// </summary>317[DataMember]318[Display(319ResourceType = typeof(LocalizedStrings),320Name = LocalizedStrings.LocalTimeKey,321Description = LocalizedStrings.LocalTimeDescKey,322GroupName = LocalizedStrings.GeneralKey)]323public DateTimeOffset LocalTime324{325get => _localTime;326set327{328if (_localTime == value)329return;330
331_localTime = value;332NotifyChanged();333}334}335
336/// <summary>337/// Order price.338/// </summary>339[DataMember]340[Display(341ResourceType = typeof(LocalizedStrings),342Name = LocalizedStrings.PriceKey,343Description = LocalizedStrings.OrderPriceKey,344GroupName = LocalizedStrings.GeneralKey)]345public decimal Price { get; set; }346
347/// <summary>348/// Number of contracts in the order.349/// </summary>350[DataMember]351[Display(352ResourceType = typeof(LocalizedStrings),353Name = LocalizedStrings.VolumeKey,354Description = LocalizedStrings.OrderVolumeKey,355GroupName = LocalizedStrings.GeneralKey)]356public decimal Volume { get; set; }357
358/// <summary>359/// Visible quantity of contracts in order.360/// </summary>361[DataMember]362[Display(363ResourceType = typeof(LocalizedStrings),364Name = LocalizedStrings.VisibleVolumeKey,365Description = LocalizedStrings.VisibleVolumeDescKey,366GroupName = LocalizedStrings.GeneralKey)]367public decimal? VisibleVolume { get; set; }368
369/// <inheritdoc/>370[DataMember]371[Display(372ResourceType = typeof(LocalizedStrings),373Name = LocalizedStrings.DirectionKey,374Description = LocalizedStrings.OrderSideDescKey,375GroupName = LocalizedStrings.GeneralKey)]376public Sides Side { get; set; }377
378/// <summary>379/// Order side (buy or sell).380/// </summary>381[Browsable(false)]382[Obsolete("Use Direction property.")]383public Sides Direction384{385get => Side;386set => Side = value;387}388
389private decimal _balance;390
391/// <summary>392/// Order contracts balance.393/// </summary>394[DataMember]395[Display(396ResourceType = typeof(LocalizedStrings),397Name = LocalizedStrings.BalanceKey,398Description = LocalizedStrings.OrderBalanceKey,399GroupName = LocalizedStrings.GeneralKey)]400public decimal Balance401{402get => _balance;403set404{405if (_balance == value)406return;407
408_balance = value;409NotifyChanged();410}411}412
413private long? _status;414
415/// <summary>416/// System order status.417/// </summary>418[DataMember]419[Browsable(false)]420public long? Status421{422get => _status;423set424{425if (_status == value)426return;427
428_status = value;429NotifyChanged();430}431}432
433private bool? _isSystem;434
435/// <summary>436/// Is a system trade.437/// </summary>438[DataMember]439[Display(440ResourceType = typeof(LocalizedStrings),441Name = LocalizedStrings.SystemKey,442Description = LocalizedStrings.IsSystemTradeKey,443GroupName = LocalizedStrings.GeneralKey)]444public bool? IsSystem445{446get => _isSystem;447set448{449if (_isSystem == value)450return;451
452_isSystem = value;453NotifyChanged();454}455}456
457/// <summary>458/// Placed order comment.459/// </summary>460[DataMember]461[Display(462ResourceType = typeof(LocalizedStrings),463Name = LocalizedStrings.CommentKey,464Description = LocalizedStrings.OrderCommentKey,465GroupName = LocalizedStrings.GeneralKey)]466public string Comment { get; set; }467
468/// <summary>469/// Order type.470/// </summary>471[DataMember]472[Display(473ResourceType = typeof(LocalizedStrings),474Name = LocalizedStrings.OrderTypeKey,475Description = LocalizedStrings.OrderTypeDescKey,476GroupName = LocalizedStrings.GeneralKey)]477public OrderTypes? Type { get; set; }478
479private 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(489ResourceType = typeof(LocalizedStrings),490Name = LocalizedStrings.ExpirationKey,491Description = LocalizedStrings.OrderExpirationTimeKey,492GroupName = LocalizedStrings.GeneralKey)]493public DateTimeOffset? ExpiryDate494{495get => _expiryDate;496set497{498if (_expiryDate == value)499return;500
501_expiryDate = value;502NotifyChanged();503}504}505
506/// <summary>507/// Order condition (e.g., stop- and algo- orders parameters).508/// </summary>509[XmlIgnore]510[Display(511ResourceType = typeof(LocalizedStrings),512Name = LocalizedStrings.ConditionKey,513Description = LocalizedStrings.OrderConditionDescKey,514GroupName = LocalizedStrings.ConditionalOrderKey)]515public OrderCondition Condition { get; set; }516
517/// <summary>518/// Limit order time in force.519/// </summary>520[Display(521ResourceType = typeof(LocalizedStrings),522Name = LocalizedStrings.TimeInForceKey,523Description = LocalizedStrings.LimitOrderTifKey,524GroupName = LocalizedStrings.GeneralKey)]525public TimeInForce? TimeInForce { get; set; }526
527private 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(535ResourceType = typeof(LocalizedStrings),536Name = LocalizedStrings.LinkedOrderKey,537Description = LocalizedStrings.LinkedOrderDescKey,538GroupName = LocalizedStrings.ConditionalOrderKey)]539[Obsolete("No longer used.")]540public Order DerivedOrder541{542get => _derivedOrder;543set544{545if (_derivedOrder == value)546return;547
548_derivedOrder = value;549NotifyChanged();550}551}552
553/// <summary>554/// Commission (broker, exchange etc.).555/// </summary>556[DataMember]557[Display(558ResourceType = typeof(LocalizedStrings),559Name = LocalizedStrings.CommissionKey,560Description = LocalizedStrings.CommissionDescKey,561GroupName = LocalizedStrings.GeneralKey)]562public decimal? Commission { get; set; }563
564/// <summary>565/// Commission currency. Can be <see langword="null"/>.566/// </summary>567public string CommissionCurrency { get; set; }568
569/// <summary>570/// User's order ID.571/// </summary>572[DataMember]573[Display(574ResourceType = typeof(LocalizedStrings),575Name = LocalizedStrings.UserIdKey,576Description = LocalizedStrings.UserOrderIdKey,577GroupName = LocalizedStrings.GeneralKey)]578public string UserOrderId { get; set; }579
580/// <summary>581/// Strategy id.582/// </summary>583[DataMember]584public string StrategyId { get; set; }585
586/// <summary>587/// Broker firm code.588/// </summary>589[DataMember]590[Display(591ResourceType = typeof(LocalizedStrings),592Name = LocalizedStrings.BrokerKey,593Description = LocalizedStrings.BrokerCodeKey,594GroupName = LocalizedStrings.GeneralKey)]595public string BrokerCode { get; set; }596
597/// <summary>598/// Client code assigned by the broker.599/// </summary>600[DataMember]601[Display(602ResourceType = typeof(LocalizedStrings),603Name = LocalizedStrings.ClientCodeKey,604Description = LocalizedStrings.ClientCodeDescKey,605GroupName = LocalizedStrings.GeneralKey)]606public string ClientCode { get; set; }607
608/// <summary>609/// Trading security currency.610/// </summary>611[DataMember]612[Display(613ResourceType = typeof(LocalizedStrings),614Name = LocalizedStrings.CurrencyKey,615Description = LocalizedStrings.CurrencyDescKey,616GroupName = LocalizedStrings.GeneralKey)]617public CurrencyTypes? Currency { get; set; }618
619/// <summary>620/// Is the order of market-maker.621/// </summary>622[DataMember]623[Display(624ResourceType = typeof(LocalizedStrings),625Name = LocalizedStrings.MarketMakerKey,626Description = LocalizedStrings.MarketMakerOrderKey + LocalizedStrings.Dot,627GroupName = LocalizedStrings.GeneralKey)]628public bool? IsMarketMaker { get; set; }629
630/// <summary>631/// Is margin enabled.632/// </summary>633[DataMember]634[Display(635ResourceType = typeof(LocalizedStrings),636Name = LocalizedStrings.MarginKey,637Description = LocalizedStrings.IsMarginKey,638GroupName = LocalizedStrings.GeneralKey)]639public bool? IsMargin { get; set; }640
641/// <summary>642/// Slippage in trade price.643/// </summary>644[DataMember]645[Display(646ResourceType = typeof(LocalizedStrings),647Name = LocalizedStrings.SlippageKey,648Description = LocalizedStrings.SlippageTradeKey,649GroupName = LocalizedStrings.GeneralKey)]650public decimal? Slippage { get; set; }651
652/// <summary>653/// Is order manual.654/// </summary>655[DataMember]656[Display(657ResourceType = typeof(LocalizedStrings),658Name = LocalizedStrings.ManualKey,659Description = LocalizedStrings.IsOrderManualKey,660GroupName = LocalizedStrings.GeneralKey)]661public bool? IsManual { get; set; }662
663/// <summary>664/// Average execution price.665/// </summary>666[DataMember]667public decimal? AveragePrice { get; set; }668
669/// <summary>670/// Yield.671/// </summary>672[DataMember]673public decimal? Yield { get; set; }674
675/// <summary>676/// Minimum quantity of an order to be executed.677/// </summary>678[DataMember]679public decimal? MinVolume { get; set; }680
681/// <summary>682/// Position effect.683/// </summary>684[DataMember]685public OrderPositionEffects? PositionEffect { get; set; }686
687/// <summary>688/// Post-only order.689/// </summary>690[DataMember]691public bool? PostOnly { get; set; }692
693/// <summary>694/// Sequence number.695/// </summary>696/// <remarks>Zero means no information.</remarks>697[DataMember]698public long SeqNum { get; set; }699
700/// <summary>701/// Margin leverage.702/// </summary>703[DataMember]704[Display(705ResourceType = typeof(LocalizedStrings),706Name = LocalizedStrings.LeverageKey,707Description = LocalizedStrings.MarginLeverageKey,708GroupName = LocalizedStrings.GeneralKey)]709public int? Leverage { get; set; }710
711/// <inheritdoc />712public override string ToString()713{714var str = LocalizedStrings.OrderDetails715.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
717if (!UserOrderId.IsEmpty())718str += $" UID={UserOrderId}";719
720if (!StrategyId.IsEmpty())721str += $" Strategy={StrategyId}";722
723if (Condition != null)724str += $" Condition={Condition}";725
726if (AveragePrice != null)727str += $" AvgPrice={AveragePrice}";728
729if (MinVolume != null)730str += $" MinVolume={MinVolume}";731
732if (PositionEffect != null)733str += $" PosEffect={PositionEffect.Value}";734
735if (PostOnly != null)736str += $",PostOnly={PostOnly.Value}";737
738if (SeqNum != 0)739str += $",SeqNum={SeqNum}";740
741if (Leverage != null)742str += $",Leverage={Leverage.Value}";743
744return str;745}746}747}
748