Exemplo n.º 1
0
        public void TrySubmitOrder(long size)
        {
            if (size == 0)
            {
                return;
            }
            orderLog.Notice("SubmitOrder(" + size + ")");
            long   lVol       = Math.Abs(size);
            int    lBuySell   = size > 0 ? MBConst.VALUE_BUY : MBConst.VALUE_SELL;
            string sSym       = symbol.Symbol;
            double dPrice     = size > 0 ? lastAsk : lastBid;
            int    lTIF       = MBConst.VALUE_DAY;
            int    lOrdType   = MBConst.VALUE_MARKET;
            int    lVolType   = MBConst.VALUE_NORMAL;
            string sRoute     = "MBTX";
            string bstrRetMsg = null;

            System.DateTime dDte   = new System.DateTime(0);
            string          sOrder = String.Format("{0} {1} {2} at {3:c} on {4}",
                                                   lBuySell == MBConst.VALUE_BUY ? "Buy" : "Sell",
                                                   lVol, sSym, dPrice, sRoute);

            orderLog.Notice(sOrder);
            bool bSubmit = m_OrderClient.Submit(lBuySell, (int)lVol, sSym, dPrice, 0.0, lTIF, 0, lOrdType, lVolType, 0, m_account, sRoute, "", 0, 0, dDte, dDte, 0, 0, 0, 0, 0, ref bstrRetMsg);

            if (bSubmit)
            {
                orderLog.Notice(
                    String.Format("Order submission successful! bstrRetMsg = [{0}]", bstrRetMsg));                     // wide string!
                orderSubmitted = true;
            }
            else
            {
                orderLog.Notice(
                    String.Format("Order submission failed! bstrRetMsg = [{0}]", bstrRetMsg));                     // wide string!
            }
        }
Exemplo n.º 2
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);
        }