Пример #1
0
        void UpdatePositions()
        {
            OrderExecutionSourceStub stub    = _stub;
            FXCMConnectionManager    manager = Manager;

            if (stub == null || manager == null)
            {
                return;
            }
            List <AccountInfo> accounts = stub.Accounts;

            if (accounts.Count == 1)
            {// Update all positions on the account.
                // When a position is not returned, it means no position on that symbol.
                List <PositionInfo> positionsInfosList = manager.GetPositions();

                Dictionary <string, PositionInfo> inputPositionsInfos = new Dictionary <string, PositionInfo>();

                lock (this)
                {
                    foreach (PositionInfo info in positionsInfosList)
                    {
                        inputPositionsInfos.Add(info.Symbol.Name, info);

                        // Update history with latest input.
                        _positionsInfos[info.Symbol.Name] = info;
                    }

                    positionsInfosList.Clear();

                    // Check if any positions were not reported (meaning they have 0 value on them).
                    foreach (PositionInfo info in GeneralHelper.EnumerableToList <PositionInfo>(_positionsInfos.Values))
                    {
                        if (inputPositionsInfos.ContainsKey(info.Symbol.Name) == false &&
                            (info.Volume.HasValue == false || info.Volume != 0))
                        {// 0 position, update.
                            PositionInfo updatedInfo = info;
                            updatedInfo.Volume = 0;
                            _positionsInfos[updatedInfo.Symbol.Name] = updatedInfo;
                            positionsInfosList.Add(updatedInfo);
                        }
                        else
                        {
                            positionsInfosList.Add(info);
                        }
                    }
                }

                stub.UpdatePositionsInfo(accounts[0], positionsInfosList.ToArray());
            }
            else
            {
                SystemMonitor.OperationError("Failed to establish account for positions update.");
            }
        }
Пример #2
0
        /// <summary>
        /// Make sure to call on Invoke'd thread.
        /// </summary>
        /// <param name="account"></param>
        void PerformUpdate(MbtAccount account, bool updateAccount, bool updateOrders, bool updatePositions)
        {
            //SystemMonitor.CheckError(_messageLoopOperator.InvokeRequred == false, "Invoke must not be required in baseMethod call.");

            MbtOrderClient             orderClient = _orderClient;
            MBTradingConnectionManager manager     = _manager;
            MBTradingQuote             quotes      = null;

            if (orderClient == null || manager == null)
            {
                return;
            }

            quotes = manager.Quotes;
            if (quotes == null)
            {
                return;
            }

            AccountInfo info;
            Dictionary <string, OrderInfo> pendingOrderInfos = new Dictionary <string, OrderInfo>();
            List <PositionInfo>            positionsInfos    = new List <PositionInfo>();

            lock (this)
            {
                if (updateOrders)
                {
                    // We need to send up only the latest of each orders histories, since prev ones are for previous states.
                    foreach (MbtOrderHistory history in _orderClient.OrderHistories)
                    {
                        Order.UpdateTypeEnum updateType;
                        OrderInfo?           orderInfo = ConvertToOrderInfo(history, out updateType);
                        if (orderInfo.HasValue)
                        {
                            pendingOrderInfos[orderInfo.Value.Id] = orderInfo.Value;
                        }
                    }

                    // Make sure open orders orderInfo is always on top.
                    foreach (MbtOpenOrder pOrd in _orderClient.OpenOrders)
                    {
                        OrderInfo?orderInfo = ConvertToOrderInfo(pOrd);
                        if (orderInfo.HasValue)
                        {
                            pendingOrderInfos[orderInfo.Value.Id] = orderInfo.Value;
                        }
                    }
                }

                if (updatePositions)
                {
                    foreach (MbtPosition position in _orderClient.Positions)
                    {
                        PositionInfo?positionInfo = ConvertToPositionInfo(position);
                        if (positionInfo.HasValue)
                        {
                            positionsInfos.Add(positionInfo.Value);
                        }
                    }
                }

                if (_accountInfo.HasValue)
                {
                    info = _accountInfo.Value;
                }
                else
                {
                    info      = new AccountInfo();
                    info.Guid = Guid.NewGuid();
                }

                ConvertAccount(account, ref info);

                _accountInfo = info;
            }

            MBTradingAdapter adapter = _adapter;

            if (adapter != null)
            {
                OrderExecutionSourceStub stub = adapter.OrderExecutionSourceStub;
                if (stub != null)
                {
                    if (updateAccount)
                    {
                        stub.UpdateAccountInfo(_accountInfo.Value);
                    }

                    if (updateOrders)
                    {
                        stub.UpdateOrdersInfo(_accountInfo.Value,
                                              GeneralHelper.GenerateSingleValueArray <Order.UpdateTypeEnum>(pendingOrderInfos.Count, Order.UpdateTypeEnum.Update),
                                              GeneralHelper.EnumerableToArray <OrderInfo>(pendingOrderInfos.Values));
                    }

                    if (updatePositions)
                    {
                        stub.UpdatePositionsInfo(_accountInfo.Value, positionsInfos.ToArray());
                    }
                }
            }
        }