Exemple #1
0
        void UpdateAccounts()
        {
            OrderExecutionSourceStub stub    = _stub;
            FXCMConnectionManager    manager = Manager;

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

            // The newly produced accounts have no Guids.
            List <AccountInfo> accounts = manager.GetAvailableAccounts(IntegrationAdapter.AdvisedAccountDecimalsPrecision);

            foreach (AccountInfo info in accounts)
            {// Update / insert accounts.
                AccountInfo actualInfo = info;

                AccountInfo?existingInfo = stub.GetAccountInfo(info.Id);
                if (existingInfo.HasValue == false)
                {// New info, assign a Guid.
                    actualInfo.Guid = Guid.NewGuid();
                }
                else
                {
                    actualInfo.Guid = existingInfo.Value.Guid;
                }

                stub.UpdateAccountInfo(actualInfo);
            }
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        public FXCMAdapter()
        {
            DataSourceStub           dataSourceStub           = new DataSourceStub("FXCM Adapter Data", true);
            OrderExecutionSourceStub orderExecutionSourceStub = new OrderExecutionSourceStub("FXCM Adapter Execution", true);

            base.SetInitialParameters(dataSourceStub, orderExecutionSourceStub);
            Construct();
        }
Exemple #3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public FXCMOrders(FXCMAdapter adapter, OrderExecutionSourceStub stub)
        {
            _adapter = adapter;

            StatusSynchronizationEnabled = true;
            StatusSynchronizationSource  = adapter;

            _stub = stub;
            _stub.Initialize(this);
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        public FXCMAdapter()
        {
            _orderExecutionStub = new OrderExecutionSourceStub("FXCM Adapter Execution", false);
            _dataSourceStub     = new DataSourceStub("FXCM Adapter Data", true);

            base.SetStub(_dataSourceStub);
            base.SetStub(_orderExecutionStub);

            Construct();
        }
Exemple #5
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.");
            }
        }
Exemple #6
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public MBTradingAdapter()
            : base()
        {
            DataSourceStub           dataSourceStub           = new DataSourceStub("MBTrading Adapter Data", true);
            OrderExecutionSourceStub orderExecutionSourceStub = new OrderExecutionSourceStub("MBTrading Adapter Execution", false);

            base.SetStub(dataSourceStub);
            base.SetStub(orderExecutionSourceStub);

            Construct();
        }
Exemple #7
0
        /// <summary>
        /// Event executed on a new thread.
        /// </summary>
        void Manager_OrderUpdatedEvent(FXCMConnectionManager manager, string accountId, OrderInfo orderInfo)
        {
            OrderExecutionSourceStub stub = _stub;

            if (stub == null)
            {
                SystemMonitor.OperationWarning(string.Format("Failed to update order information, since account [{0}] not found.", accountId));
                return;
            }

            AccountInfo?accountInfo = stub.GetAccountInfo(accountId);

            if (accountInfo.HasValue)
            {
                stub.UpdateOrderInfo(accountInfo.Value, Order.UpdateTypeEnum.Update, orderInfo);
            }
            else
            {
                SystemMonitor.OperationWarning(string.Format("Failed to find account [{0}] for order [{1}].", accountId, orderInfo.Id));
            }

            UpdatePositions();
        }
Exemple #8
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());
                    }
                }
            }
        }
Exemple #9
0
 public void Dispose()
 {
     _adapter = null;
     _stub    = null;
 }