/// <summary>
        /// New Execution Arrived
        /// </summary>
        /// <param name="execution"></param>
        public void ExecutionReceived(Execution execution)
        {
            try
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info(execution.ToString(), _type.FullName, "ExecutionReceived");
                }

                // Check if the order is already cancelled
                if (_cancelOrdersMap.ContainsKey(execution.Order.OrderID))
                {
                    Order order;
                    _cancelOrdersMap.TryRemove(execution.Order.OrderID, out order);
                    return;
                }

                // Rasie Execution Event
                if (ExecutionArrived != null)
                {
                    ExecutionArrived.Invoke(execution);
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception, _type.FullName, "ExecutionReceived");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Called to check and execute limitorder
        /// </summary>
        /// <param name="limitOrder"></param>
        /// <param name="bar"></param>
        private bool ExecuteLimitOrder(LimitOrder limitOrder, Bar bar)
        {
            if (limitOrder.OrderDateTime.ToString().Equals(bar.DateTime.ToString()))
            {
                return(false);
            }

            #region BUY Order

            if (limitOrder.OrderSide == OrderSide.BUY)
            {
                if (bar.Low <= limitOrder.LimitPrice)
                {
                    decimal price = BuySidePriceCalculation(limitOrder, bar);
                    if (price != 0)
                    {
                        // Get Order Index
                        var index = _limitOrders.IndexOfValue(limitOrder);

                        // Remove from local list
                        _limitOrders.RemoveAt(index);

                        // Add OHLC values
                        limitOrder.Remarks += (" | OHLC: " + _latestBar.Open + ":" + _latestBar.High + ":" + _latestBar.Low +
                                               ":" + _latestBar.Close);
                        limitOrder.OrderStatus = OrderStatus.EXECUTED;
                        var newExecution =
                            new Execution(
                                new Fill(limitOrder.Security, limitOrder.OrderExecutionProvider,
                                         limitOrder.OrderID)
                        {
                            ExecutionDateTime      = bar.DateTime,
                            ExecutionPrice         = price + limitOrder.Slippage,
                            ExecutionSize          = limitOrder.OrderSize,
                            AverageExecutionPrice  = price - limitOrder.Slippage,
                            ExecutionId            = Guid.NewGuid().ToString(),
                            ExecutionSide          = limitOrder.OrderSide,
                            ExecutionType          = ExecutionType.Fill,
                            LeavesQuantity         = 0,
                            OrderExecutionProvider = OrderExecutionProvider.SimulatedExchange,
                            CummalativeQuantity    = limitOrder.OrderSize
                        }, limitOrder);
                        //newExecution.BarClose = bar.Close;

                        if (_asyncClassLogger.IsDebugEnabled)
                        {
                            _asyncClassLogger.Debug("Limit Order: " + limitOrder.OrderID + " Executed using: " + bar,
                                                    _type.FullName, "ExecuteLimitOrder");
                        }

                        // Raise Event to notify listeners
                        if (ExecutionArrived != null)
                        {
                            ExecutionArrived(newExecution);
                        }

                        return(true);
                    }
                }
            }

            #endregion

            #region SELL Order

            if (limitOrder.OrderSide == OrderSide.SELL)
            {
                if (bar.High >= limitOrder.LimitPrice)
                {
                    decimal price = SellSidePriceCalculation(limitOrder, bar);
                    if (price != 0)
                    {
                        // Get Order Index
                        var index = _limitOrders.IndexOfValue(limitOrder);

                        // Remove order from local list
                        _limitOrders.RemoveAt(index);

                        // Add OHLC values
                        limitOrder.Remarks += (" | OHLC: " + _latestBar.Open + ":" + _latestBar.High + ":" + _latestBar.Low +
                                               ":" + _latestBar.Close);
                        limitOrder.OrderStatus = OrderStatus.EXECUTED;
                        var newExecution =
                            new Execution(
                                new Fill(limitOrder.Security, limitOrder.OrderExecutionProvider,
                                         limitOrder.OrderID)
                        {
                            ExecutionDateTime      = bar.DateTime,
                            ExecutionPrice         = price - limitOrder.Slippage,
                            ExecutionSize          = limitOrder.OrderSize,
                            AverageExecutionPrice  = price + limitOrder.Slippage,
                            ExecutionId            = Guid.NewGuid().ToString(),
                            ExecutionSide          = limitOrder.OrderSide,
                            ExecutionType          = ExecutionType.Fill,
                            LeavesQuantity         = 0,
                            OrderExecutionProvider = OrderExecutionProvider.SimulatedExchange,
                            CummalativeQuantity    = limitOrder.OrderSize
                        }, limitOrder);
                        //newExecution.BarClose = bar.Close;

                        if (_asyncClassLogger.IsDebugEnabled)
                        {
                            _asyncClassLogger.Debug("Limit Order: " + limitOrder.OrderID + " Executed using: " + bar,
                                                    _type.FullName, "ExecuteLimitOrder");
                        }

                        // Raise Event to notify listeners
                        if (ExecutionArrived != null)
                        {
                            ExecutionArrived.Invoke(newExecution);
                        }
                        return(true);
                    }
                }
            }

            #endregion

            return(false);
        }