Lean  $LEAN_TAG$
StopLimitOrder.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 StopLimitOrder : 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  /// Signal showing the "StopLimitOrder" has been converted into a Limit Order
36  /// </summary>
37  [JsonProperty(PropertyName = "stopTriggered")]
38  public bool StopTriggered { get; internal set; }
39 
40  /// <summary>
41  /// Limit price for the stop limit order
42  /// </summary>
43  [JsonProperty(PropertyName = "limitPrice")]
44  public decimal LimitPrice { get; internal set; }
45 
46  /// <summary>
47  /// StopLimit Order Type
48  /// </summary>
49  public override OrderType Type
50  {
51  get { return OrderType.StopLimit; }
52  }
53 
54  /// <summary>
55  /// Default constructor for JSON Deserialization:
56  /// </summary>
57  public StopLimitOrder()
58  {
59  }
60 
61  /// <summary>
62  /// New Stop Market Order constructor -
63  /// </summary>
64  /// <param name="symbol">Symbol asset we're seeking to trade</param>
65  /// <param name="quantity">Quantity of the asset we're seeking to trade</param>
66  /// <param name="limitPrice">Maximum price to fill the order</param>
67  /// <param name="time">Time the order was placed</param>
68  /// <param name="stopPrice">Price the order should be filled at if a limit order</param>
69  /// <param name="tag">User defined data tag for this order</param>
70  /// <param name="properties">The order properties for this order</param>
71  public StopLimitOrder(Symbol symbol, decimal quantity, decimal stopPrice, decimal limitPrice, DateTime time, string tag = "", IOrderProperties properties = null)
72  : base(symbol, quantity, time, tag, properties)
73  {
74  StopPrice = stopPrice;
75  LimitPrice = limitPrice;
76  }
77 
78  /// <summary>
79  /// Gets the default tag for this order
80  /// </summary>
81  /// <returns>The default tag</returns>
82  public override string GetDefaultTag()
83  {
84  return Messages.StopLimitOrder.Tag(this);
85  }
86 
87  /// <summary>
88  /// Gets the order value in units of the security's quote currency
89  /// </summary>
90  /// <param name="security">The security matching this order's symbol</param>
91  protected override decimal GetValueImpl(Security security)
92  {
93  // selling, so higher price will be used
94  if (Quantity < 0)
95  {
96  return Quantity * Math.Max(LimitPrice, security.Price);
97  }
98 
99  // buying, so lower price will be used
100  if (Quantity > 0)
101  {
102  return Quantity * Math.Min(LimitPrice, security.Price);
103  }
104 
105  return 0m;
106  }
107 
108  /// <summary>
109  /// Modifies the state of this order to match the update request
110  /// </summary>
111  /// <param name="request">The request to update this order object</param>
112  public override void ApplyUpdateOrderRequest(UpdateOrderRequest request)
113  {
114  base.ApplyUpdateOrderRequest(request);
115  if (request.StopPrice.HasValue)
116  {
117  StopPrice = request.StopPrice.Value;
118  }
119  if (request.LimitPrice.HasValue)
120  {
121  LimitPrice = request.LimitPrice.Value;
122  }
123  }
124 
125  /// <summary>
126  /// Returns a string that represents the current object.
127  /// </summary>
128  /// <returns>
129  /// A string that represents the current object.
130  /// </returns>
131  /// <filterpriority>2</filterpriority>
132  public override string ToString()
133  {
134  return Messages.StopLimitOrder.ToString(this);
135  }
136 
137  /// <summary>
138  /// Creates a deep-copy clone of this order
139  /// </summary>
140  /// <returns>A copy of this order</returns>
141  public override Order Clone()
142  {
144  CopyTo(order);
145  return order;
146  }
147  }
148 }