Example #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);
        }
Example #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);
        }
Example #3
0
        /// <summary>
        /// Submits the order over to the orders interface, make sure to call in Invocation thread.
        /// </summary>
        string DoSubmitOrder(AccountInfo accountInfo, Symbol symbol, OrderTypeEnum orderType, int volume, Decimal?allowedSlippage, Decimal?desiredPrice,
                             Decimal?takeProfit, Decimal?stopLoss, string comment, out PlaceOrderOperation operation, out string operationResultMessage)
        {
            SystemMonitor.CheckError(_messageLoopOperator.InvokeRequred == false, "Invoke required.");

            operationResultMessage = "Operation not supported.";
            operation = null;

            MbtAccount pAcct = GetAccountByInfo(accountInfo);

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

            if (orderType != OrderTypeEnum.SELL_MARKET && orderType != OrderTypeEnum.BUY_MARKET)
            {
                operationResultMessage = "Order type [" + orderType.ToString() + "] not supported or tested by this provider.";
                return(null);
                //if (desiredPrice.HasValue)
                //{
                //    dStopPrice = (double)desiredPrice.Value;
                //}
                //else
                //{
                //    SystemMonitor.Error("Desired price not assigned, on placing order type [" + orderType.ToString() + " ], not submitted.");
                //    return null;
                //}
            }

            // ---
            int iVolume = volume;

            int iOrdType, iBuySell;

            double dPrice       = desiredPrice.HasValue ? (double)desiredPrice.Value : 0;
            double dPrice2      = 0;
            int    lTimeInForce = -1;

            if (ConvertToMBTOrderType(orderType, desiredPrice, allowedSlippage, out iOrdType, out iBuySell, out lTimeInForce) == false)
            {
                operationResultMessage = "Failed to convert type of order.";
                SystemMonitor.OperationWarning(operationResultMessage);
                return(null);
            }

            if (allowedSlippage.HasValue && dPrice != 0)
            {// Put the slippage in as a limit price.
                // This forms the "limit" price we are willing to pay for this order.
                if (OrderInfo.TypeIsBuy(orderType))
                {
                    dPrice = dPrice + (double)allowedSlippage.Value;
                }
                else
                {
                    dPrice = dPrice - (double)allowedSlippage.Value;
                }
            }

            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.Submit(iBuySell, iVolume, symbol.Name, dPrice, dStopPrice, (int)TifEnum.VALUE_GTC, 10020, iOrdType,
                //    10042, 0, pAcct, "MBTX", string.Empty, 0, 0, DateTime.FromBinary(0), DateTime.FromBinary(0), 0, 0, 0, 0, -1, ref message) == false)
                //{// Error requestMessage.
                //    operationResultMessage = message;
                //    return null;
                //}

                // Instead of using Market Orders, we shall use Limit Orders, since they allow to set an execution limit price.
                // The VALUE_IOC instructs to execute or cancel the order, instead of GTC (Good Till Cancel)
                if (_orderClient.Submit(iBuySell, iVolume, symbol.Name, dPrice, dPrice2, lTimeInForce, 10020, iOrdType,
                                        10042, 0, pAcct, "MBTX", string.Empty, 0, 0, DateTime.FromBinary(0), DateTime.FromBinary(0), 0, 0, 0, 0, -1, ref message) == false)
                {// Error requestMessage.
                    operationResultMessage = message;
                    return(null);
                }

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

            return(message);
        }
        /// <summary>
        /// Submits the order over to the orders interface, make sure to call in Invocation thread.
        /// </summary>
        string DoSubmitOrder(AccountInfo accountInfo, Symbol symbol, OrderTypeEnum orderType, int volume, Decimal? allowedSlippage, Decimal? desiredPrice,
            Decimal? takeProfit, Decimal? stopLoss, string comment, out PlaceOrderOperation operation, out string operationResultMessage)
        {
            SystemMonitor.CheckError(_messageLoopOperator.InvokeRequred == false, "Invoke required.");

            operationResultMessage = "Operation not supported.";
            operation = null;

            MbtAccount pAcct = GetAccountByInfo(accountInfo);

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

            if (orderType != OrderTypeEnum.SELL_MARKET && orderType != OrderTypeEnum.BUY_MARKET)
            {
                operationResultMessage = "Order type [" + orderType.ToString() + "] not supported or tested by this provider.";
                return null;
                //if (desiredPrice.HasValue)
                //{
                //    dStopPrice = (double)desiredPrice.Value;
                //}
                //else
                //{
                //    SystemMonitor.Error("Desired price not assigned, on placing order type [" + orderType.ToString() + " ], not submitted.");
                //    return null;
                //}
            }

            // ---
            int iVolume = volume;

            int iOrdType, iBuySell;

            double dPrice = desiredPrice.HasValue ? (double)desiredPrice.Value : 0;
            double dPrice2 = 0;
            int lTimeInForce = -1;

            if (ConvertToMBTOrderType(orderType, desiredPrice, allowedSlippage, out iOrdType, out iBuySell, out lTimeInForce) == false)
            {
                operationResultMessage = "Failed to convert type of order.";
                SystemMonitor.OperationWarning(operationResultMessage);
                return null;
            }

            if (allowedSlippage.HasValue && dPrice != 0)
            {// Put the slippage in as a limit price.
                // This forms the "limit" price we are willing to pay for this order.
                if (OrderInfo.TypeIsBuy(orderType))
                {
                    dPrice = dPrice + (double)allowedSlippage.Value;
                }
                else
                {
                    dPrice = dPrice - (double)allowedSlippage.Value;
                }
            }

            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.Submit(iBuySell, iVolume, symbol.Name, dPrice, dStopPrice, (int)TifEnum.VALUE_GTC, 10020, iOrdType,
                //    10042, 0, pAcct, "MBTX", string.Empty, 0, 0, DateTime.FromBinary(0), DateTime.FromBinary(0), 0, 0, 0, 0, -1, ref message) == false)
                //{// Error requestMessage.
                //    operationResultMessage = message;
                //    return null;
                //}

                // Instead of using Market Orders, we shall use Limit Orders, since they allow to set an execution limit price.
                // The VALUE_IOC instructs to execute or cancel the order, instead of GTC (Good Till Cancel)
                if (_orderClient.Submit(iBuySell, iVolume, symbol.Name, dPrice, dPrice2, lTimeInForce, 10020, iOrdType,
                    10042, 0, pAcct, "MBTX", string.Empty, 0, 0, DateTime.FromBinary(0), DateTime.FromBinary(0), 0, 0, 0, 0, -1, ref message) == false)
                {// Error requestMessage.
                    operationResultMessage = message;
                    return null;
                }

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

            return message;
        }