Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        bool GetProviders(Symbol symbol, out IQuoteProvider quotes, out IDataBarHistoryProvider bars)
        {
            quotes = _manager.ObtainQuoteProvider(_dataDelivery.SourceId, symbol);
            RuntimeDataSessionInformation sessionInformation = _dataDelivery.GetSymbolRuntimeSessionInformation(symbol);

            if (sessionInformation.AvailableDataBarPeriods == null || sessionInformation.AvailableDataBarPeriods.Count == 0)
            {
                quotes = null;
                bars   = null;
                SystemMonitor.OperationError("Can not close order since no suitable data provider sessions found.");
                return(false);
            }

            bars = _manager.ObtainDataBarHistoryProvider(_dataDelivery.SourceId, symbol, sessionInformation.AvailableDataBarPeriods[0]);

            if (quotes == null || quotes.Ask.HasValue == false || bars == null)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        public virtual bool SetInitialParameters(ISourceManager manager, ComponentId sourceId, ExpertSession session)
        {
            _sessionInfo = session.Info;

            _manager = manager;

            _sourceId = sourceId;

            _dataDelivery = manager.ObtainDataDelivery(sourceId);

            _quote = manager.ObtainQuoteProvider(sourceId, _sessionInfo.Symbol);

            _tickProvider = manager.ObtainDataTickHistoryProvider(sourceId, _sessionInfo.Symbol);

            _dataBarProvider = null;

            bool result = _dataDelivery != null && _quote != null && _tickProvider != null;

            SystemMonitor.CheckError(result, "Failed to initialize data provider.");

            return(result);
        }
Exemplo n.º 3
0
        void _executor_OrderUpdatedEvent(IOrderSink providerSink, AccountInfo accountInfo, string[] previousOrdersIds,
                                         OrderInfo[] ordersInfos, Order.UpdateTypeEnum[] updatesType)
        {
            ISourceOrderExecution provider = _provider;

            if (providerSink != _provider)
            {
                SystemMonitor.Warning("Provider mismatch.");
                return;
            }

            List <Order> updatedOrders = new List <Order>();
            List <Order.UpdateTypeEnum> updatedOrdersUpdateTypes = new List <Order.UpdateTypeEnum>();

            for (int i = 0; i < ordersInfos.Length; i++)
            {
                if (string.IsNullOrEmpty(ordersInfos[i].Id))
                {
                    SystemMonitor.Warning("Order update of order with no ID.");
                    continue;
                }

                if (previousOrdersIds.Length > i && previousOrdersIds[i] != ordersInfos[i].Id &&
                    string.IsNullOrEmpty(previousOrdersIds[i]) == false)
                {// Order Id has changed, remove old order.
                    Order superceededOrder = GetOrderById(previousOrdersIds[i]);
                    RemoveOrder(superceededOrder);
                }

                Order order = GetOrderById(ordersInfos[i].Id);
                if (order == null)
                {// Create new order based on incoming information.
                    if (provider.SupportsActiveOrderManagement)
                    {
                        order = new ActiveOrder(_manager, provider, _manager.ObtainQuoteProvider(_delivery.SourceId, ordersInfos[i].Symbol),
                                                _delivery.SourceId, ordersInfos[i].Symbol, true);
                    }
                    else
                    {
                        order = new PassiveOrder(_manager, _delivery.SourceId, provider.SourceId);
                    }

                    order.AdoptInfo(ordersInfos[i]);

                    if (AddOrder(order) == false)
                    {
                        SystemMonitor.OperationError("Failed to add order to keeper (id [" + order.Id + "] already used for another order).", TracerItem.PriorityEnum.Medium);
                    }
                }
                else
                {// Existing order, to be updated.
                    OrderInfo info = ordersInfos[i];

                    // First, check for critical modifications (price changes).
                    if (order.Info.IsCriticalUpdate(info))
                    {
                        SystemMonitor.Report(string.Format("Order has received a critical data modication Id[{0}], Open[{1} / {2}], Close[{3} / {4}].", order.Id, order.OpenPrice.ToString(), info.OpenPrice.ToString(), order.ClosePrice.ToString(),
                                                           info.ClosePrice.ToString()), TracerItem.PriorityEnum.High);

                        if (OrdersCriticalInformationChangedEvent != null)
                        {
                            OrdersCriticalInformationChangedEvent(this, accountInfo, order, info);
                        }
                    }

                    if (order.UpdateInfo(info) == false)
                    {
                        SystemMonitor.OperationError("Failed to update order [" + order.Id + "].");
                        continue;
                    }

                    lock (this)
                    {
                        // Remove from any of the sub arrays it may be in.
                        foreach (OrderStateEnum state in Enum.GetValues(typeof(OrderStateEnum)))
                        {
                            if (_ordersByState.ContainsKey(state) && _ordersByState[state].Contains(order))
                            {
                                _ordersByState[state].Remove(order);
                            }
                        }

                        _ordersByState[info.State].Add(order);
                    }

                    updatedOrders.Add(order);
                    updatedOrdersUpdateTypes.Add(updatesType[i]);
                }
            }

            if (updatedOrders.Count > 0 && OrdersUpdatedEvent != null)
            {
                OrdersUpdatedEvent(this, accountInfo, updatedOrders.ToArray(), updatedOrdersUpdateTypes.ToArray());
            }
        }
        /// <summary>
        /// Update position parameters.
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="volumeChange">*Directional* volume change, 0 for no change.</param>
        /// <param name="operationResultMessage"></param>
        /// <returns></returns>
        bool UpdatePosition(Symbol symbol, int volumeChange, out string operationResultMessage)
        {
            if (_manager == null || _dataDelivery == null || _dataDelivery.SourceId.IsEmpty)
            {
                operationResultMessage = "Not initialized properly for operation.";
                return(false);
            }

            IQuoteProvider quotes = _manager.ObtainQuoteProvider(_dataDelivery.SourceId, symbol);

            if (quotes == null)
            {
                operationResultMessage = "Failed to find corresponding quotation provider.";
                return(false);
            }

            // Update the position corresponding to this order.
            Position position = _tradeEntities.GetPositionBySymbol(symbol, true);

            if (position == null)
            {
                operationResultMessage = "Failed to find corresponding position.";
                return(false);
            }

            PositionInfo updatedPositionInfo = position.Info;

            if (updatedPositionInfo.Volume.HasValue == false)
            {
                updatedPositionInfo.Volume = 0;
            }

            updatedPositionInfo.PendingBuyVolume  = 0;
            updatedPositionInfo.PendingSellVolume = 0;

            if (volumeChange != 0)
            {
                //decimal initialPositionResult = updatedPositionInfo.Result.Value;

                decimal?operationBasis = quotes.GetOrderOpenQuote(volumeChange > 0);
                if (operationBasis.HasValue == false)
                {
                    operationResultMessage = "Failed to establish order operation basis price.";
                    return(false);
                }
                else
                {
                    SystemMonitor.CheckError(updatedPositionInfo.Basis.HasValue, "Position has no properly assigned basis price.");
                }

                if (RecalculatePositionBasis(ref updatedPositionInfo, volumeChange, operationBasis.Value) == false)
                {
                    operationResultMessage = "Failed to recalculate position parameters.";
                    return(false);
                }
            }

            decimal?pendingPrice = quotes.GetOrderCloseQuote(updatedPositionInfo.Volume > 0);

            if (updatedPositionInfo.Volume.HasValue && updatedPositionInfo.Volume != 0 &&
                pendingPrice.HasValue && updatedPositionInfo.Basis.HasValue)
            {
                updatedPositionInfo.Result = (pendingPrice.Value - updatedPositionInfo.Basis.Value) * updatedPositionInfo.Volume.Value;
            }
            else
            {
                updatedPositionInfo.Result = 0;
            }

            bool positionClosing = (position.Volume > 0 && volumeChange < 0) || (position.Volume < 0 && volumeChange > 0);

            if (volumeChange != 0 && positionClosing && position.Info.Result.HasValue && updatedPositionInfo.Result.HasValue)
            {// Update the account upon closing some (or all) of the position volume.
                _runningAccountBalance += position.Info.Result.Value - updatedPositionInfo.Result.Value;
            }

            position.UpdateInfo(updatedPositionInfo);

            operationResultMessage = string.Empty;
            return(true);
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual bool SetInitialParameters(ISourceManager manager, ComponentId sourceId, ExpertSession session)
        {
            _sessionInfo = session.Info;

            _manager = manager;

            _sourceId = sourceId;

            _dataDelivery = manager.ObtainDataDelivery(sourceId);

            _quote = manager.ObtainQuoteProvider(sourceId, _sessionInfo.Symbol);

            _tickProvider = manager.ObtainDataTickHistoryProvider(sourceId, _sessionInfo.Symbol);

            _dataBarProvider = null;

            bool result = _dataDelivery != null && _quote != null && _tickProvider != null;

            SystemMonitor.CheckError(result, "Failed to initialize data provider.");

            return result;
        }