Lean  $LEAN_TAG$
StopMarketOrder.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  /// Stop Market Order Type Definition
25  /// </summary>
26  public class StopMarketOrder : Order
27  {
28  /// <summary>
29  /// Stop price for this stop market order.
30  /// </summary>
31  [JsonProperty(PropertyName = "stopPrice")]
32  public decimal StopPrice { get; internal set; }
33 
34  /// <summary>
35  /// StopMarket Order Type
36  /// </summary>
37  public override OrderType Type
38  {
39  get { return OrderType.StopMarket; }
40  }
41 
42  /// <summary>
43  /// Default constructor for JSON Deserialization:
44  /// </summary>
45  public StopMarketOrder()
46  {
47  }
48 
49  /// <summary>
50  /// New Stop Market 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="stopPrice">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 StopMarketOrder(Symbol symbol, decimal quantity, decimal stopPrice, DateTime time, string tag = "", IOrderProperties properties = null)
59  : base(symbol, quantity, time, tag, properties)
60  {
61  StopPrice = stopPrice;
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.StopMarketOrder.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  // selling, so higher price will be used
80  if (Quantity < 0)
81  {
82  return Quantity * Math.Max(StopPrice, security.Price);
83  }
84 
85  // buying, so lower price will be used
86  if (Quantity > 0)
87  {
88  return Quantity * Math.Min(StopPrice, security.Price);
89  }
90 
91  return 0m;
92  }
93 
94  /// <summary>
95  /// Modifies the state of this order to match the update request
96  /// </summary>
97  /// <param name="request">The request to update this order object</param>
98  public override void ApplyUpdateOrderRequest(UpdateOrderRequest request)
99  {
100  base.ApplyUpdateOrderRequest(request);
101  if (request.StopPrice.HasValue)
102  {
103  StopPrice = request.StopPrice.Value;
104  }
105  }
106 
107  /// <summary>
108  /// Returns a string that represents the current object.
109  /// </summary>
110  /// <returns>
111  /// A string that represents the current object.
112  /// </returns>
113  /// <filterpriority>2</filterpriority>
114  public override string ToString()
115  {
116  return Messages.StopMarketOrder.ToString(this);
117  }
118 
119  /// <summary>
120  /// Creates a deep-copy clone of this order
121  /// </summary>
122  /// <returns>A copy of this order</returns>
123  public override Order Clone()
124  {
125  var order = new StopMarketOrder { StopPrice = StopPrice };
126  CopyTo(order);
127  return order;
128  }
129  }
130 }