Esempio n. 1
0
        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);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool Login(string username, string password, string serviceUrl, string accountType)
        {
            if (OperationalState != OperationalStateEnum.Initialized &&
                OperationalState != OperationalStateEnum.Initializing &&
                OperationalState != OperationalStateEnum.Constructed)
            {
                return(false);
            }

            object result = false;

            GeneralHelper.GenericReturnDelegate <bool> del = delegate()
            {
                if (_core == null)
                {
                    _core = new FXCore.CoreAutClass();
                    _desk = (FXCore.TradeDeskAut)_core.CreateTradeDesk("trader");
                }

                lock (this)
                {
                    _orders.Initialize(_adapter, this);
                }

                ChangeOperationalState(OperationalStateEnum.Initializing);

                try
                {
                    _desk.Login(username, password, serviceUrl, accountType);
                    Subscribe();
                    ChangeOperationalState(OperationalStateEnum.Operational);
                }
                catch (Exception exception)
                {
                    SystemMonitor.OperationError("Failed to log in [" + exception.Message + "].");
                    ChangeOperationalState(OperationalStateEnum.NotOperational);
                }

                return(_desk.IsLoggedIn());
            };

            _messageLoopOperator.Invoke(del, TimeSpan.FromSeconds(180), out result);

            return((bool)result);
        }