/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using Newtonsoft.Json;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
namespace QuantConnect.Orders
{
///
/// Stop Market Order Type Definition
///
public class StopMarketOrder : Order
{
///
/// Stop price for this stop market order.
///
[JsonProperty(PropertyName = "stopPrice")]
public decimal StopPrice { get; internal set; }
///
/// StopMarket Order Type
///
public override OrderType Type
{
get { return OrderType.StopMarket; }
}
///
/// Default constructor for JSON Deserialization:
///
public StopMarketOrder()
{
}
///
/// New Stop Market Order constructor -
///
/// Symbol asset we're seeking to trade
/// Quantity of the asset we're seeking to trade
/// Time the order was placed
/// Price the order should be filled at if a limit order
/// User defined data tag for this order
/// The order properties for this order
public StopMarketOrder(Symbol symbol, decimal quantity, decimal stopPrice, DateTime time, string tag = "", IOrderProperties properties = null)
: base(symbol, quantity, time, tag, properties)
{
StopPrice = stopPrice;
}
///
/// Gets the default tag for this order
///
/// The default tag
public override string GetDefaultTag()
{
return Messages.StopMarketOrder.Tag(this);
}
///
/// Gets the order value in units of the security's quote currency
///
/// The security matching this order's symbol
protected override decimal GetValueImpl(Security security)
{
// selling, so higher price will be used
if (Quantity < 0)
{
return Quantity * Math.Max(StopPrice, security.Price);
}
// buying, so lower price will be used
if (Quantity > 0)
{
return Quantity * Math.Min(StopPrice, security.Price);
}
return 0m;
}
///
/// Modifies the state of this order to match the update request
///
/// The request to update this order object
public override void ApplyUpdateOrderRequest(UpdateOrderRequest request)
{
base.ApplyUpdateOrderRequest(request);
if (request.StopPrice.HasValue)
{
StopPrice = request.StopPrice.Value;
}
}
///
/// Returns a string that represents the current object.
///
///
/// A string that represents the current object.
///
/// 2
public override string ToString()
{
return Messages.StopMarketOrder.ToString(this);
}
///
/// Creates a deep-copy clone of this order
///
/// A copy of this order
public override Order Clone()
{
var order = new StopMarketOrder { StopPrice = StopPrice };
CopyTo(order);
return order;
}
}
}