namespace QuantConnect
{
public class LimitOrderPractice : QCAlgorithm
{
private string symbol = "FB";
private decimal price;
private int quantity = 10;
LimitOrder buyOrder = null;
private List<OrderTicket>_openLimitOrders = new List<OrderTicket>();
public override void Initialize()
{
SetStartDate(2016, 08, 22);
SetEndDate(2016, 08, 22);
SetCash(25000);
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
}
public void OnData(TradeBars data)
{
price = Securities[symbol].Close;
// if profit target is reached, liquidate.
if (Portfolio.HoldStock && price >= 124.30m)
{
Liquidate();
// if there is still any partial fill left on the limit order, cancel it.
if (_openLimitOrders != null)
{
_openLimitOrders.Clear();
}
}
// if limit order is filled, cancel it.
if (_openLimitOrders != null && buyOrder.Status == OrderStatus.Filled)
{
_openLimitOrders.Clear();
}
// if slate is clear and trigger price is met, enter new limit order to buy.
if (!Portfolio.HoldStock && price > 124.15m && _openLimitOrders == null)
{
var newTicket = LimitOrder(symbol, quantity, 124.10m);
_openLimitOrders.Add(newTicket);
buyOrder = (LimitOrder)Transactions.GetOrderById(newTicket);
}
}
}
}