Beispiel #1
0
 /// <summary>
 /// Initializes a new instance of the TradingPosition class.
 /// </summary>
 /// <param name="status">Possible values include: 'Open',
 /// 'Closed'</param>
 /// <param name="direction">Possible values include: 'Buy',
 /// 'Sell'</param>
 /// <param name="closeReason">Possible values include: 'None', 'Close',
 /// 'StopLoss', 'TakeProfit', 'StopOut', 'Canceled',
 /// 'CanceledBySystem'</param>
 /// <param name="fillType">Possible values include: 'FillOrKill',
 /// 'PartialFill'</param>
 public TradingPosition(PositionStatus status, TradingOrderDirection direction, double volume, double openPrice, OrderCloseReason closeReason, double openCommission, double closeCommission, double swapCommission, OrderFillType fillType, string id = default(string), string traderCounterPartyId = default(string), string traderAccountId = default(string), string symbol = default(string), double?matchedVolume = default(double?), double?requestedPrice = default(double?), string traderAccountAssetId = default(string), double?matchedCloseVolume = default(double?), double?closePrice = default(double?), System.DateTime?openDate = default(System.DateTime?), System.DateTime?closeDate = default(System.DateTime?), double?fpl = default(double?), double?pnL = default(double?), double?pnlInUsd = default(double?), double?volumeUsdAtOpen = default(double?), double?volumeUsdAtClose = default(double?))
 {
     Id = id;
     TraderCounterPartyId = traderCounterPartyId;
     TraderAccountId      = traderAccountId;
     Symbol               = symbol;
     MatchedVolume        = matchedVolume;
     Status               = status;
     Direction            = direction;
     RequestedPrice       = requestedPrice;
     TraderAccountAssetId = traderAccountAssetId;
     Volume               = volume;
     MatchedCloseVolume   = matchedCloseVolume;
     OpenPrice            = openPrice;
     ClosePrice           = closePrice;
     OpenDate             = openDate;
     CloseDate            = closeDate;
     CloseReason          = closeReason;
     OpenCommission       = openCommission;
     CloseCommission      = closeCommission;
     SwapCommission       = swapCommission;
     FillType             = fillType;
     Fpl              = fpl;
     PnL              = pnL;
     PnlInUsd         = pnlInUsd;
     VolumeUsdAtOpen  = volumeUsdAtOpen;
     VolumeUsdAtClose = volumeUsdAtClose;
     CustomInit();
 }
Beispiel #2
0
        internal static string ToSerializedValue(this OrderCloseReason value)
        {
            switch (value)
            {
            case OrderCloseReason.None:
                return("None");

            case OrderCloseReason.Close:
                return("Close");

            case OrderCloseReason.StopLoss:
                return("StopLoss");

            case OrderCloseReason.TakeProfit:
                return("TakeProfit");

            case OrderCloseReason.StopOut:
                return("StopOut");

            case OrderCloseReason.Canceled:
                return("Canceled");

            case OrderCloseReason.CanceledBySystem:
                return("CanceledBySystem");
            }
            return(null);
        }
Beispiel #3
0
        public Task <Order> CloseActiveOrderAsync(string orderId, OrderCloseReason reason, string comment = null)
        {
            var order = GetActiveOrderForClose(orderId);

            var me = _meRouter.GetMatchingEngineForClose(order);

            return(CloseActiveOrderByMatchingEngineAsync(order, me, reason, comment));
        }
Beispiel #4
0
 public Order CancelPendingOrder(string orderId, OrderCloseReason reason, string comment = null)
 {
     using (_contextFactory.GetWriteSyncContext($"{nameof(TradingEngine)}.{nameof(CancelPendingOrder)}"))
     {
         var order = _ordersCache.WaitingForExecutionOrders.GetOrderById(orderId);
         CancelWaitingForExecutionOrder(order, reason, comment);
         return(order);
     }
 }
Beispiel #5
0
        private void SetOrderToClosingState(Order order, OrderCloseReason reason)
        {
            order.Status           = OrderStatus.Closing;
            order.StartClosingDate = DateTime.UtcNow;
            order.CloseReason      = reason;

            _ordersCache.ClosingOrders.Add(order);
            _ordersCache.ActiveOrders.Remove(order);
        }
Beispiel #6
0
        private void CancelWaitingForExecutionOrder(Order order, OrderCloseReason reason)
        {
            order.Status      = OrderStatus.Closed;
            order.CloseDate   = DateTime.UtcNow;
            order.CloseReason = reason;

            _ordersCache.WaitingForExecutionOrders.Remove(order);

            _orderCancelledEventChannel.SendEvent(this, new OrderCancelledEventArgs(order));
        }
Beispiel #7
0
        public async Task <List <IOrder> > CloseAccountOrders(string accountId, OrderCloseReason reason)
        {
            var openedOrders = _ordersCache.ActiveOrders.GetOrdersByAccountIds(accountId).ToArray();
            var closedOrders = new List <IOrder>();

            foreach (var order in openedOrders)
            {
                try
                {
                    var closedOrder = await _tradingEngine.CloseActiveOrderAsync(order.Id, reason);

                    closedOrders.Add(closedOrder);
                }
                catch (Exception e)
                {
                    await _log.WriteWarningAsync(nameof(AccountManager), "CloseAccountActiveOrders",
                                                 $"AccountId: {accountId}, OrderId: {order.Id}", $"Error closing order: {e.Message}");
                }
            }

            return(closedOrders);
        }
Beispiel #8
0
        private Task <Order> CloseActiveOrderByMatchingEngineAsync(Order order, OrderCloseReason reason, IMatchingEngineBase matchingEngine)
        {
            order.CloseOrderbookId = matchingEngine.Id;
            order.StartClosingDate = DateTime.UtcNow;
            order.CloseReason      = reason;

            matchingEngine.MatchMarketOrderForClose(order, matchedOrders =>
            {
                if (!matchedOrders.Any())
                {
                    order.CloseRejectReasonText = "No orders to match";
                    return(false);
                }

                order.MatchedCloseOrders.AddRange(matchedOrders);

                _equivalentPricesService.EnrichClosingOrder(order);

                if (!order.GetIsCloseFullfilled())
                {
                    order.Status = OrderStatus.Closing;
                    _ordersCache.ActiveOrders.Remove(order);
                    _ordersCache.ClosingOrders.Add(order);
                    _orderClosingEventChannel.SendEvent(this, new OrderClosingEventArgs(order));
                }
                else
                {
                    order.Status    = OrderStatus.Closed;
                    order.CloseDate = DateTime.UtcNow;
                    _ordersCache.ActiveOrders.Remove(order);
                    _orderClosedEventChannel.SendEvent(this, new OrderClosedEventArgs(order));
                }

                return(true);
            });

            return(Task.FromResult(order));
        }