protected IPosition GetExisitingPosition(IOrder order) { var key = PositionKey.Create(order.Portfolio, order.SecurityMasterId); var existingPosition = this.positionService.GetPositionByKey(key); return(existingPosition); }
private void ProcessBuyOrder(IOrder order, PositionKey key) { IPosition position; if (this.positions.TryGetValue(key, out position)) { var pos = this.CreatePositionFromSource(position); pos.AvgPrice = ((position.AvgPrice * position.Quantity) + (order.Quantity * order.Price)) / (order.Quantity + position.Quantity); if (position.IsShort) { pos.IsShort = position.Quantity > order.Quantity; pos.Quantity = Math.Abs(position.Quantity - order.Quantity); } else { pos.IsShort = false; pos.Quantity += order.Quantity; } this.positions[key] = pos; } else { position = this.ToPosition(order); this.positions.Add(key, position); } }
public IPosition GetPositionByKey(PositionKey key) { IPosition result; if (this.positions.TryGetValue(key, out result)) { return(result); } return(null); }
public void SubmitOrder(IOrder order) { if (order == null) { return; } var key = PositionKey.Create(order.Portfolio, order.SecurityMasterId); switch (order.OrderType) { case OrderType.Buy: this.ProcessBuyOrder(order, key); break; case OrderType.Sell: this.ProcessSellOrder(order, key); break; } }