Exemplo n.º 1
0
        string OrderExecutionSourceStub.IImplementation.SubmitOrder(AccountInfo accountInfo, Symbol symbol,
                                                                    OrderTypeEnum orderType, int volume, decimal?allowedSlippage, decimal?desiredPrice, decimal?takeProfit,
                                                                    decimal?stopLoss, string comment, out string operationResultMessage)
        {
            operationResultMessage = string.Empty;
            string operationResultMessageCopy = string.Empty;

            PlaceOrderOperation operation = null;

            GeneralHelper.GenericReturnDelegate <string> operationDelegate = delegate()
            {
                string submitResult = DoSubmitOrder(accountInfo, symbol, orderType, volume, allowedSlippage, desiredPrice,
                                                    takeProfit, stopLoss, comment, out operation, out operationResultMessageCopy);

                return(submitResult);
            };

            object result;

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

            if (string.IsNullOrEmpty((string)result))
            {// Operation error.
                operationResultMessage = operationResultMessageCopy;
                return(null);
            }

            // Return the ID of the submitted order.
            return((string)result);
        }
Exemplo n.º 2
0
        bool OrderExecutionSourceStub.IImplementation.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)
        {
            string operationResultMessageCopy = string.Empty;

            PlaceOrderOperation operation = null;

            GeneralHelper.GenericReturnDelegate <bool> operationDelegate = delegate()
            {
                string submitResult = DoSubmitOrder(accountInfo, symbol, orderType, volume, allowedSlippage, desiredPrice,
                                                    takeProfit, stopLoss, comment, out operation, out operationResultMessageCopy);

                return(string.IsNullOrEmpty(submitResult) == false);
            };

            orderPlaced = null;

            object result;

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

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

            object operationResult;

            if (operation.WaitResult <object>(TimeSpan.FromSeconds(60), out operationResult) == false)
            {
                operationResultMessage = "Order place timeout.";
                return(false);
            }

            orderPlaced = (OrderInfo?)operationResult;

            if (operationResult == null || orderPlaced.HasValue == false)
            {
                operationResultMessage = "Order place failed.";
                return(false);
            }

            // Operation OK.
            operationResultMessage = string.Empty;
            orderPlaced            = operation.OrderResponce;

            if (orderPlaced.HasValue == false)
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Perform a login.
        /// </summary>
        public bool Login(string username, string password, string serviceUrl, string accountType, out string operationResultMessage)
        {
            _messageLoopOperator.Start();

            if (OperationalState != OperationalStateEnum.Initialized &&
                OperationalState != OperationalStateEnum.Initializing &&
                OperationalState != OperationalStateEnum.Constructed)
            {
                operationResultMessage = "Login already started.";
                return(false);
            }

            this._serviceUrl = serviceUrl;

            string operationResultMessageCopy = string.Empty;

            ChangeOperationalState(OperationalStateEnum.Initializing);

            object result = false;

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

                try
                {
                    _desk.Login(username, password, serviceUrl, accountType);

                    Managed_Subscribe();

                    SystemMonitor.Report("FXCM Service subscribed.");
                    ChangeOperationalState(OperationalStateEnum.Operational);
                }
                catch (Exception exception)
                {
                    operationResultMessageCopy = "Failed to log in [" + exception.Message + "].";
                    SystemMonitor.OperationError(operationResultMessageCopy);
                    ChangeOperationalState(OperationalStateEnum.NotOperational);
                }

                return(_desk.IsLoggedIn());
            };

            if (_messageLoopOperator.Invoke(del, TimeSpan.FromSeconds(180), out result) == false || (bool)result == false)
            {
                ChangeOperationalState(OperationalStateEnum.NotOperational);
                operationResultMessage = operationResultMessageCopy;
                return(false);
            }

            operationResultMessage = operationResultMessageCopy;
            return((bool)result);
        }
        /// <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);
        }
Exemplo n.º 5
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)
        {
            operationResultMessage = string.Empty;
            string operationResultMessageCopy = string.Empty;
            object orderId, psd;

            bool isBuy = OrderInfo.TypeIsBuy(orderType);

            OrderInfo?order = null;

            GeneralHelper.GenericReturnDelegate <bool> operationDelegate = delegate()
            {
                _manager.Desk.OpenTrade(accountInfo.Id, symbol.Name, isBuy, _adapter.DefaultLotSize, (double)desiredPrice.Value, (string)_adapter.GetInstrumentData(symbol.Name, "QuoteID"), 0, (double)stopLoss.Value, (double)takeProfit.Value, 0, out orderId, out psd);

                order = new OrderInfo();
                OrderInfo tempOrder = order.Value;
                tempOrder.Id = orderId.ToString();

                TableAut accountsTable = (FXCore.TableAut)_manager.Desk.FindMainTable("trades");

                RowAut item = (RowAut)accountsTable.FindRow("OrderID", orderId, 0);

                return(true);
            };

            orderPlaced = order;

            object result;

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

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

            return(true);
        }
Exemplo n.º 6
0
        bool OrderExecutionSourceStub.IImplementation.IsPermittedSymbol(AccountInfo accountInfo, Symbol symbol)
        {
            GeneralHelper.GenericReturnDelegate <bool> operation = delegate()
            {
                MbtAccount acc = GetAccountByInfo(accountInfo);
                if (acc == null)
                {
                    return(false);
                }

                return(acc.IsPermedForSymbol(symbol.Name));
            };

            object result;

            if (_messageLoopOperator.Invoke(operation, TimeSpan.FromSeconds(5), out result) == false)
            {
                return(false);
            }

            return((bool)result);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Submit an order.
        /// </summary>
        public string SubmitOrder(AccountInfo account, Symbol symbol, OrderTypeEnum orderType, int volume,
                                  decimal?allowedSlippage, decimal?desiredPrice, decimal?takeProfit, decimal?stopLoss,
                                  string comment, out string operationResultMessage)
        {
            operationResultMessage = string.Empty;
            string operationResultMessageCopy = string.Empty;
            object orderId, psd;
            bool   isBuy = OrderInfo.TypeIsBuy(orderType);

            GeneralHelper.GenericReturnDelegate <string> operationDelegate = delegate()
            {
                _manager.Desk.OpenTrade(account.Id, symbol.Name, isBuy,
                                        _adapter.DefaultLotSize, (double)desiredPrice.Value,
                                        (string)_adapter.GetInstrumentData(symbol.Name, "QuoteID"),
                                        0,
                                        stopLoss.HasValue ? (double)stopLoss.Value : 0,
                                        takeProfit.HasValue ? (double)takeProfit.Value : 0,
                                        0, out orderId, out psd);

                return(orderId.ToString());
            };

            object result;

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

            if (string.IsNullOrEmpty((string)result))
            {            // Operation error.
                operationResultMessage = operationResultMessageCopy;
                return(null);
            }

            // Return the ID of the submitted order.
            return((string)result);
        }
Exemplo n.º 8
0
        bool OrderExecutionSourceStub.IImplementation.CloseOrCancelOrder(AccountInfo accountInfo, string orderId,
                                                                         string orderTag, decimal?allowedSlippage, decimal?desiredPrice, out decimal closingPrice,
                                                                         out DateTime closingTime, out string modifiedId, out string operationResultMessage)
        {
            operationResultMessage = string.Empty;

            closingPrice = 0;
            closingTime  = DateTime.MinValue;
            modifiedId   = orderId;

            string operationResultMessageLocal = string.Empty;

            OperationInformation operation = null;

            GeneralHelper.GenericReturnDelegate <string> operationDelegate = delegate()
            {
                operationResultMessageLocal = "Operation not supported.";
                operation = null;

                MbtAccount pAcct = GetAccountByInfo(accountInfo);

                if (pAcct == null)
                {
                    operationResultMessageLocal = "Failed to retrieve account.";
                    SystemMonitor.OperationWarning(operationResultMessageLocal);
                    return(null);
                }

                string message = string.Empty;
                lock (this)
                {// Make sure to keep the entire package here locked, since the order operation get placed after the submit
                    // so we need to make sure we shall catch the responce in OnSubmit() too.
                    if (_orderClient.Cancel(orderTag, ref message) == false)
                    {// Error requestMessage.
                        operationResultMessageLocal = message;
                        return(null);
                    }

                    operation = new CancelOrderOperation()
                    {
                        Id = message
                    };
                    // The message, or operation Id is the order token (further stored in OrderInfo.id)
                    _operationStub.RegisterOperation(operation, false);
                }

                return(message);
            };

            object result;

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

            if (string.IsNullOrEmpty((string)result))
            {// Operation error.
                operationResultMessage = operationResultMessageLocal;
                return(false);
            }

            // Return the ID of the submitted order.
            return(true);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Request bar dataDelivery from entry.
        /// </summary>
        public bool RequestDataHistoryUpdate(DataSessionInfo sessionInfo, DataHistoryRequest request, bool waitResult)
        {
            DataStoreEntry entry = DataStore.Instance.GetEntryBySessionInfo(sessionInfo);

            if (this.OperationalState != OperationalStateEnum.Operational ||
                entry == null)
            {
                SystemMonitor.OperationError("Data history request received while not operational, or invalid session requrested.");
                return(false);
            }

            if (entry.Period != request.Period)
            {
                SystemMonitor.OperationError("Data history request received but period not recognized.");
                return(false);
            }

            if (request.MaxValuesRetrieved.HasValue == false)
            {
                request.MaxValuesRetrieved = int.MaxValue;
            }

            if (request.StartIndex.HasValue == false)
            {
                request.StartIndex = -1;
            }

            GeneralHelper.GenericReturnDelegate <bool> operationDelegate = delegate()
            {
                if (request.IsTickBased)
                {
                    DataReaderWriter <DataTick> readerWriter = entry.GetDataTickReaderWriter();

                    List <DataTick>   dataTicks;
                    DataHistoryUpdate update = new DataHistoryUpdate(request.Period, new DataTick[] { });
                    if (readerWriter.Read(request.StartIndex.Value, request.MaxValuesRetrieved.Value, out dataTicks))
                    {
                        update.DataTicksUnsafe.AddRange(dataTicks);

                        if (DataHistoryUpdateEvent != null)
                        {
                            DataHistoryUpdateEvent(this, sessionInfo, update);
                        }

                        return(true);
                    }
                }
                else
                {
                    DataReaderWriter <DataBar> readerWriter = entry.GetDataBarReaderWriter();
                    if (readerWriter == null)
                    {
                        SystemMonitor.OperationError("Failed to establish file reader writer for entry.");
                        return(false);
                    }

                    List <DataBar>    dataBars;
                    DataHistoryUpdate update = new DataHistoryUpdate(request.Period, new DataBar[] { });

                    bool readResult = false;
                    if (request.StartIndex.Value < 0)
                    {// Instruction is to read the last count items.
                        readResult = readerWriter.ReadLast(
                            request.MaxValuesRetrieved.Value, out dataBars);
                    }
                    else
                    {
                        readResult = readerWriter.Read(request.StartIndex.Value,
                                                       request.MaxValuesRetrieved.Value, out dataBars);
                    }

                    if (readResult)
                    {
                        update.DataBarsUnsafe.AddRange(dataBars);

                        if (DataHistoryUpdateEvent != null)
                        {
                            DataHistoryUpdateEvent(this, sessionInfo, update);
                        }

                        return(true);
                    }
                }

                return(false);
            };

            if (waitResult)
            {
                return(operationDelegate());
            }
            else
            {
                GeneralHelper.FireAndForget(operationDelegate);
                return(true);
            }
        }