/// <summary> /// Processes a sell order. /// </summary> /// <param name="order">The order.</param> /// <param name="price">The price.</param> private void ProcessSellOrder(Order order, float price) { var proceeds = order.Quantity * price; this.CurrentBalance += proceeds; this.CurrentBalance -= BrokerageFee; this.CurrentPositionSize -= order.Quantity; this.NumTrades++; if (order.Position == Order.Positions.Short) { this.NumShortPositions++; this.NetProfitsFromShortPositions += proceeds; } else { this.NetProfitsFromLongPositions += proceeds; } if (order.ConditionalOrder != null) { // This order has been successfully processed. // So, queue the conditional order. this.QueueOrder(order.ConditionalOrder); } }
/// <summary> /// Processes a buy order. /// </summary> /// <param name="order">The order.</param> /// <param name="price">The price.</param> private void ProcessBuyOrder(Order order, float price) { var cost = order.Quantity * price; if (cost > this.CurrentBalance) { // Stop processing this order...it will be canceled. return; } this.CurrentBalance -= cost; this.CurrentBalance -= BrokerageFee; this.CurrentPositionSize += order.Quantity; this.NumTrades++; if (order.Position == Order.Positions.Long) { this.NumLongPositions++; this.NetProfitsFromLongPositions -= cost; } else { this.NetProfitsFromShortPositions -= cost; } if (order.ConditionalOrder != null) { // This order has been successfully processed. // So, queue the conditional order. this.QueueOrder(order.ConditionalOrder); } }
/// <summary> /// Queues an order for processing. /// </summary> /// <param name="order"> /// The order to queue. /// </param> public void QueueOrder(Order order) { order.IsPending = true; this.queuedOrders.Add(order); if (order.IsReplacementOrder) { // Cancel the order that this order is intended to replace. this.CancelOrder(order.ReplacementId); } }
/// <summary> /// Updates the short stop order. /// </summary> /// <param name="stopPrice">The stop price.</param> private void UpdateShortStopOrder(float stopPrice) { var order = new Order { Id = this.broker.GetNextOrderId(), Action = Order.Actions.Buy, ActivationPrice = stopPrice, GoodUntil = DateTime.Today + TimeSpan.FromDays(365), Position = Order.Positions.Short, Quantity = Math.Abs(this.broker.CurrentPositionSize), Type = Order.Types.StopMarket, IsReplacementOrder = true, ReplacementId = this.GetCurrentStopOrderId() }; this.broker.QueueOrder(order); this.currentStopPrice = stopPrice; }
/// <summary> /// Places an order for a short position. /// </summary> /// <param name="quantity">The quantity.</param> /// <param name="replaceCurrentStop">if set to <c>true</c> replace the current stop order.</param> private void PlaceOrderForShortPosition(int quantity, bool replaceCurrentStop) { var stopOrder = new Order { Id = this.broker.GetNextOrderId(), Action = Order.Actions.Buy, ActivationPrice = this.CalculateStopPriceForShortPosition(), GoodUntil = DateTime.Today + TimeSpan.FromDays(365), Position = Order.Positions.Short, Quantity = quantity + Math.Abs(this.broker.CurrentPositionSize), Type = Order.Types.StopMarket }; var order = new Order { Id = this.broker.GetNextOrderId(), Action = Order.Actions.Sell, GoodUntil = DateTime.Today + TimeSpan.FromDays(1), Position = Order.Positions.Short, Quantity = quantity, Type = Order.Types.Market, ConditionalOrder = stopOrder }; if (replaceCurrentStop) { stopOrder.IsReplacementOrder = true; stopOrder.ReplacementId = this.GetCurrentStopOrderId(); } this.currentStopPrice = stopOrder.ActivationPrice; this.broker.QueueOrder(order); }
/// <summary> /// Places a stop market order for a long position. /// </summary> /// <param name="quantity">The quantity.</param> /// <param name="replaceCurrentStop">if set to <c>true</c> replace the current stop order.</param> private void PlaceStopMarketOrderForLongPosition(int quantity, bool replaceCurrentStop) { var stopOrder = new Order { Id = this.broker.GetNextOrderId(), Action = Order.Actions.Sell, ActivationPrice = this.Today.Low, GoodUntil = this.Today.IntervalOpenTime + TimeSpan.FromDays(365), Position = Order.Positions.Long, Quantity = quantity + Math.Abs(this.broker.CurrentPositionSize), Type = Order.Types.StopMarket }; var order = new Order { Id = this.broker.GetNextOrderId(), Action = Order.Actions.Buy, ActivationPrice = this.Today.High * 1.015f, GoodUntil = this.Today.IntervalOpenTime + TimeSpan.FromDays(1), Position = Order.Positions.Long, Quantity = quantity, Type = Order.Types.StopMarket, ConditionalOrder = stopOrder }; if (replaceCurrentStop) { stopOrder.IsReplacementOrder = true; stopOrder.ReplacementId = this.GetCurrentStopOrderId(); } this.currentStopPrice = stopOrder.ActivationPrice; this.broker.QueueOrder(order); }