예제 #1
0
 protected override void onRealtimeBar(int reqId, DateTime exchangeTime, double open, double high, double low, double close, long volume, double wap, int count)
 {
     if (!_initialized)
     {
         return;
     }
     lock (_lock)
     {
         if (_filledQty != 0)
         {
             if (_expireOrd == null && (exchangeTime >= _exitEndTime || exchangeTime >= _expireTime))
             {
                 double qty = _remainingFilledQty;
                 if (_remainingProfitQty > 0)
                 {
                     _socket.cancelOrder(_filledQty > 0 ? _profitLongOrd.OrderId : _profitShortOrd.OrderId);
                 }
                 else if (_remainingStopQty > 0)
                 {
                     _socket.cancelOrder(_filledQty > 0 ? _stopLongOrd.OrderId : _stopShortOrd.OrderId);
                 }
                 Order ord = new Order();
                 ord.Action        = _filledQty > 0 ? "SELL" : "BUY";
                 ord.TotalQuantity = qty;
                 ord.OrderType     = "MKT";
                 _expireOrd        = ord;
                 _om.placeOrder(id, getContractDetails(_con_id).Summary, _expireOrd, _con_id);
             }
         }
         else
         {
             if (exchangeTime > _entryEndTime)
             {
                 if (_entryLongOrd != null)
                 {
                     _socket.cancelOrder(_entryLongOrd.OrderId);
                 }
                 if (_entryShortOrd != null)
                 {
                     _socket.cancelOrder(_entryShortOrd.OrderId);
                 }
             }
             if (_entryLongOrd == null && _entryShortOrd == null && exchangeTime >= _entryStartTime && exchangeTime <= _entryEndTime)
             {
                 string ocaId = id.ToString() + "_E_" + exchangeTime.ToString("yyyyMMdd_hhmmss");
                 if (_side >= 0 && _entryLongOrd == null && ((_trendOrReversion && _longLevel > high) || (!_trendOrReversion && _longLevel < low))) //long side
                 {
                     Order ord = new Order();
                     ord.Action        = "BUY";
                     ord.TotalQuantity = _qty;
                     if (_trendOrReversion)
                     {
                         ord.OrderType = "STP";
                         ord.AuxPrice  = _longLevel;
                     }
                     else
                     {
                         ord.OrderType = "LMT";
                         ord.LmtPrice  = _longLevel;
                     }
                     ord.OcaType  = 1;   //reduce others qty with filled qty, overfill protection
                     ord.OcaGroup = ocaId;
                     ord.Transmit = true;
                     //_entryLongOrd = new BlockOrder(_socket, _zone, _con, ord, _entryEndTime, _unitSize, _maxBlockSize);
                     _entryLongOrd = ord;
                     List <Order> ordSet = OrderFactory.createBracketOrder(_entryLongOrd, _longLevel + _profitTarget, _longLevel - _stopTarget, DateTime.MinValue, null, _stopTemplate, null);
                     _profitLongOrd = ordSet[1];
                     _stopLongOrd   = ordSet[2];
                     _om.placeBracketOrders(id, getContractDetails(_con_id).Summary, ordSet, _con_id);
                 }
                 if (_side <= 0 && _entryShortOrd == null && ((_trendOrReversion && _shortLevel < low) || (!_trendOrReversion && _shortLevel > high))) //short side
                 {
                     Order ord = new Order();
                     ord.Action        = "SELL";
                     ord.TotalQuantity = _qty;
                     if (_trendOrReversion)
                     {
                         ord.OrderType = "STP";
                         ord.AuxPrice  = _shortLevel;
                     }
                     else
                     {
                         ord.OrderType = "LMT";
                         ord.LmtPrice  = _shortLevel;
                     }
                     ord.OcaType  = 1;   //reduce others qty with filled qty, overfill protection
                     ord.OcaGroup = ocaId;
                     ord.Transmit = true;
                     //_entryShortOrd = new BlockOrder(_socket, _zone, _con, ord, _entryEndTime, _unitSize, _maxBlockSize);
                     _entryShortOrd = ord;
                     List <Order> ordSet = OrderFactory.createBracketOrder(_entryShortOrd, _shortLevel - _profitTarget, _shortLevel + _stopTarget, DateTime.MinValue, null, _stopTemplate, null);
                     _profitShortOrd = ordSet[1];
                     _stopShortOrd   = ordSet[2];
                     _om.placeBracketOrders(id, getContractDetails(_con_id).Summary, ordSet, _con_id);
                 }
             }
         }
     }
 }
예제 #2
0
파일: BlockOrder.cs 프로젝트: kwangphys/QT
        private void placeOrders(object dummy)
        {
            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
            int    iorder = 0;
            Random r      = new Random();
            long   nticks = _expiryStartTime != null && _expiryEndTime != null ? ((DateTime)(_expiryEndTime) - (DateTime)(_expiryStartTime)).Ticks : 0;

            while (iorder < Count())
            {
                DateTime t = Strategy.getLocalTime(_orderSchedule[iorder].Item2, _zone);
                _socket.sleepUntil(t, _placeOrderEvent);
                Order  ord   = _orderSchedule[iorder].Item1;
                double price = 0;
                if (_profitTarget != 0 || _stopTarget != 0)
                {
                    bool success = false;
                    while (!success)
                    {
                        int reqId = _tg.get();
                        lock (_lock)
                        {
                            _quotes[1] = 0;
                            _quotes[2] = 0;
                            _quotes[4] = 0;
                            _quotes[6] = 0;
                            _quotes[7] = 0;
                            _quotes[9] = 0;
                            _tickers.Add(reqId);
                        }
                        _socket.reqMktData(reqId, _con, "", true, null);
                        _mktDataEvents[1].WaitOne();
                        _mktDataEvents[2].WaitOne();
                        if (Math.Abs(_quotes[2] - _quotes[1]) <= _minTick * _maxBidAskUnit)
                        {
                            if (_quotes[1] == 0 || _quotes[2] == 0)
                            {
                                _mktDataEvents[4].WaitOne();
                                if (_quotes[4] != 0)
                                {
                                    price   = _quotes[4];
                                    success = true;
                                }
                                else
                                {
                                    string m = "Quote Discrepancy. Bid=" + _quotes[1].ToString() + "/Ask=" + _quotes[2].ToString() + "/Last=" + _quotes[4].ToString();
                                    _msg.logError(_socket.getCurrentLocalTime(), -3, "User", _con.ConId, _stg_id, m, "");
                                    Thread.Sleep(100);
                                }
                            }
                            else
                            {
                                price   = (_quotes[1] + _quotes[2]) / 2.0;
                                price   = Math.Round((price / _minTick), 0) * _minTick;
                                success = true;
                            }
                        }
                        else
                        {
                            string m = "Quote Discrepancy. Bid=" + _quotes[1].ToString() + "/Ask=" + _quotes[2].ToString();
                            _msg.logError(_socket.getCurrentLocalTime(), -3, "User", _con.ConId, _stg_id, m, "");
                            Thread.Sleep(100);
                        }
                    }
                }
                double       profit = _profitTarget == 0 ? 0 : price + (ord.Action == "BUY" ? _profitTarget : -_profitTarget);
                double       stop   = _stopTarget == 0 ? 0 : price + (ord.Action == "BUY" ? -_stopTarget : _stopTarget);
                DateTime     expiry = nticks == 0 ? DateTime.MinValue : (Strategy.getLocalTime((DateTime)(_expiryStartTime), _zone)).AddTicks((long)(r.NextDouble() * nticks));
                List <Order> ordSet = OrderFactory.createBracketOrder(ord, profit, stop, expiry, _profitTemplate, _stopTemplate, _expiryTemplate);
                _om.placeBracketOrders(_stg_id, _con, ordSet, _ref_con_id);
                _msg.logMessage(_socket.getCurrentLocalTime(), 1, "Information", "BlockOrder", _con.ConId, _stg_id, "Executed " + iorder.ToString() + ": " + ord.OrderId.ToString() + " " + ord.OrderType + " " + ord.Action + " " + ord.TotalQuantity.ToString());
                foreach (Order order in ordSet)
                {
                    lock (_lock)
                    {
                        _openOrders.Add(order.OrderId, order);
                        _filledQtys.Add(order.OrderId, 0);
                    }
                    assignOrderID(order.OrderId);
                }
                iorder++;
            }
            _placeOrderEvent.Set();
            if (_cancelAfter != null)
            {
                _socket.sleepUntil(Strategy.getLocalTime((DateTime)_cancelAfter, _zone), null);
                cancelUnfilledEntryOrders();
            }
        }