public void Dispose()
 {
     _adapter = null;
     _manager = null;
 }
        /// <summary>
        /// 
        /// </summary>
        public bool Initialize(FXCMAdapter adapter, FXCMConnectionManager manager)
        {
            SystemMonitor.CheckError(_messageLoopOperator.InvokeRequred == false, "Init must better be called on message loop method.");

            //TODO: Guess what this piece does
            StatusSynchronizationEnabled = true;
            StatusSynchronizationSource = manager;

            _adapter = adapter;
            _manager = manager;

            ChangeOperationalState(OperationalStateEnum.Operational);

            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        public void UnInitialize()
        {
            _manager = null;
            _adapter = null;

            ChangeOperationalState(OperationalStateEnum.NotOperational);
        }
        protected override bool OnStart(out string operationResultMessage)
        {
            if (OperationalState == OperationalStateEnum.Operational
                || OperationalState == OperationalStateEnum.Initializing
                || OperationalState == OperationalStateEnum.Initializing)
            {
                operationResultMessage = "Adapter already started.";
                return false;
            }

            if (_manager == null)
            {
                _manager = new FXCMConnectionManager();
            }

            // FXCM API is COM, and sometimes fails to release itself, so make sure we shall not hang.
            SeppukuWatchdog.Activate();

            StatusSynchronizationEnabled = true;
            StatusSynchronizationSource = _manager;

            FXCMConnectionManager manager = _manager;
            if (manager.OperationalState != OperationalStateEnum.Initialized
                && manager.OperationalState != OperationalStateEnum.Constructed)
            {
                operationResultMessage = "The FXCM Adapter can only be started once each application session. Restart the Open Forex Platform to start it again.";
                return false;
            }

            if (manager.Login(Username, Password, _serviceUrl, _accountType, out operationResultMessage) == false)
            {
                operationResultMessage = "Failed to log in to FXCM [" + operationResultMessage + "].";
                SystemMonitor.OperationError(operationResultMessage);
                return false;
            }
            else
            {
                if (_data.Initialize() == false || _orders.Initialize() == false)
                {
                    operationResultMessage = "Failed to initialize FXCMOrders or FXCMData.";
                    SystemMonitor.OperationError(operationResultMessage);
                    return false;
                }

                operationResultMessage = string.Empty;

                StartSources();
                return true;
            }
        }
Example #5
0
        public void UnInitialize()
        {
            FXCMConnectionManager manager = Manager;

            manager.QuoteUpdatedEvent -= new FXCMConnectionManager.QuoteUpdateDelegate(manager_QuoteUpdatedEvent);
        }
 /// <summary>
 /// Event executed on a new thread.
 /// </summary>
 void Manager_AccountUpdateEvent(FXCMConnectionManager manager, string accountId)
 {
     UpdateAccounts();
     UpdatePositions();
 }
        /// <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();
        }
Example #8
0
        public bool ExecuteMarketOrder(AccountInfo accountInfo, Symbol symbol, OrderTypeEnum orderType, int volume,
                                       decimal?allowedSlippage, decimal?desiredPrice, decimal?takeProfit, decimal?stopLoss,
                                       string comment, out OrderInfo?orderPlaced, out string operationResultMessage)
        {
            orderPlaced = null;

            FXCMConnectionManager manager = Manager;

            if (manager == null)
            {
                operationResultMessage = "Order system not ready to operate.";
                return(false);
            }

            orderPlaced = manager.SubmitOrder(accountInfo, symbol, orderType, volume, allowedSlippage, desiredPrice,
                                              takeProfit, stopLoss, out operationResultMessage);


            if (orderPlaced.HasValue)
            {
                lock (this)
                {
                    _orders[orderPlaced.Value.Id] = orderPlaced.Value;
                }
                return(true);
            }
            else
            {
                return(false);
            }

            //operationResultMessage = string.Empty;
            //string operationResultMessageCopy = string.Empty;

            //bool isBuy = OrderInfo.TypeIsBuy(orderType);

            //OrderInfo? order = null;
            //GeneralHelper.GenericReturnDelegate<bool> operationDelegate = delegate()
            //{
            //    double realValue = (double)_adapter.GetInstrumentData(symbol.Name, "Ask");
            //    if (!isBuy)
            //    {
            //        realValue = (double)_adapter.GetInstrumentData(symbol.Name, "Bid");
            //    }

            //    try
            //    {
            //        order = CreateOrder(accountInfo,
            //                                symbol,
            //                                orderType,
            //                                volume,
            //                                allowedSlippage,
            //                                desiredPrice,
            //                                takeProfit,
            //                                stopLoss);
            //    }
            //    catch (Exception ex)
            //    {
            //        operationResultMessageCopy = ex.Message;
            //        return false;
            //    }

            //    return true;
            //};

            //orderPlaced = order;

            //object result;
            //if (_messageLoopOperator.Invoke(operationDelegate, TimeSpan.FromSeconds(120), out result) == false)
            //{// Timed out.
            //    operationResultMessage = "Timeout submiting order.";
            //    return false;
            //}

            //if (!(bool)result)
            //{// Operation error.
            //    operationResultMessage = operationResultMessageCopy;
            //    return false;
            //}

            //orderPlaced = order;

            //return true;
        }
Example #9
0
 /// <summary>
 /// Event executed on a new thread.
 /// </summary>
 void Manager_AccountUpdateEvent(FXCMConnectionManager manager, string accountId)
 {
     UpdateAccounts();
     UpdatePositions();
 }
        bool Construct()
        {
            if (_manager != null)
            {
                return false;
            }

            StatusSynchronizationEnabled = true;
            _subscribedSymbols = new Dictionary<string, Symbol>();

            lock (this)
            {
                _manager = new FXCMConnectionManager(this);

                _dataSourceStub.Initialize(this);
                _orderExecutionStub.Initialize(_manager.Orders);

                foreach (string symbol in ForexSymbols)
                {
                    _dataSourceStub.AddSuggestedSymbol(new Symbol("Forex", symbol));
                }
            }

            return true;
        }
 protected void DisposeManager()
 {
     if (_manager != null)
     {
         _manager.Logout();
         _manager.Dispose();
         _manager = null;
     }
 }
Example #12
0
 public void Dispose()
 {
     _adapter = null;
     _manager = null;
 }