Lean  $LEAN_TAG$
LimitOrder.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using Newtonsoft.Json;
20 
21 namespace QuantConnect.Orders
22 {
23  /// <summary>
24  /// Limit order type definition
25  /// </summary>
26  public class LimitOrder : Order
27  {
28  /// <summary>
29  /// Limit price for this order.
30  /// </summary>
31  [JsonProperty(PropertyName = "limitPrice")]
32  public decimal LimitPrice { get; internal set; }
33 
34  /// <summary>
35  /// Limit Order Type
36  /// </summary>
37  public override OrderType Type
38  {
39  get { return OrderType.Limit; }
40  }
41 
42  /// <summary>
43  /// Added a default constructor for JSON Deserialization:
44  /// </summary>
45  public LimitOrder()
46  {
47  }
48 
49  /// <summary>
50  /// New limit order constructor
51  /// </summary>
52  /// <param name="symbol">Symbol asset we're seeking to trade</param>
53  /// <param name="quantity">Quantity of the asset we're seeking to trade</param>
54  /// <param name="time">Time the order was placed</param>
55  /// <param name="limitPrice">Price the order should be filled at if a limit order</param>
56  /// <param name="tag">User defined data tag for this order</param>
57  /// <param name="properties">The order properties for this order</param>
58  public LimitOrder(Symbol symbol, decimal quantity, decimal limitPrice, DateTime time, string tag = "", IOrderProperties properties = null)
59  : base(symbol, quantity, time, tag, properties)
60  {
61  LimitPrice = limitPrice;
62  }
63 
64  /// <summary>
65  /// Gets the default tag for this order
66  /// </summary>
67  /// <returns>The default tag</returns>
68  public override string GetDefaultTag()
69  {
70  return Messages.LimitOrder.Tag(this);
71  }
72 
73  /// <summary>
74  /// Gets the order value in units of the security's quote currency
75  /// </summary>
76  /// <param name="security">The security matching this order's symbol</param>
77  protected override decimal GetValueImpl(Security security)
78  {
79  return CalculateOrderValue(Quantity, LimitPrice, security.Price);
80  }
81 
82  /// <summary>
83  /// Modifies the state of this order to match the update request
84  /// </summary>
85  /// <param name="request">The request to update this order object</param>
86  public override void ApplyUpdateOrderRequest(UpdateOrderRequest request)
87  {
88  base.ApplyUpdateOrderRequest(request);
89  if (request.LimitPrice.HasValue)
90  {
91  LimitPrice = request.LimitPrice.Value;
92  }
93  }
94 
95  /// <summary>
96  /// Returns a string that represents the current object.
97  /// </summary>
98  /// <returns>
99  /// A string that represents the current object.
100  /// </returns>
101  /// <filterpriority>2</filterpriority>
102  public override string ToString()
103  {
104  return Messages.LimitOrder.ToString(this);
105  }
106 
107  /// <summary>
108  /// Creates a deep-copy clone of this order
109  /// </summary>
110  /// <returns>A copy of this order</returns>
111  public override Order Clone()
112  {
113  var order = new LimitOrder { LimitPrice = LimitPrice };
114  CopyTo(order);
115  return order;
116  }
117 
118  internal static decimal CalculateOrderValue(decimal quantity, decimal limitPrice, decimal price)
119  {
120  // selling, so higher price will be used
121  if (quantity < 0)
122  {
123  return quantity * Math.Max(limitPrice, price);
124  }
125 
126  // buying, so lower price will be used
127  if (quantity > 0)
128  {
129  return quantity * Math.Min(limitPrice, price);
130  }
131 
132  return 0m;
133  }
134  }
135 }