Ejemplo n.º 1
0
        public int reqRealTimeBars(int strategy_id, Contract con, int bar_size, string whatToShow, string source)
        {
            int bar_id = _db.getBarDataId(con.ConId, whatToShow, bar_size, source);

            if (bar_id == 0)
            {
                bar_id = _db.addBarDataId(con.ConId, whatToShow, bar_size, source, "");
            }
            lock (_lock)
            {
                if (!_barTickerMap.ContainsKey(bar_id))
                {
                    int ticker = _tg.get();
                    _barTickerMap.Add(bar_id, ticker);
                    _tickerBarMap.Add(ticker, bar_id);
                    _tickerContractMap.Add(ticker, con.ConId);
                    _tickerStrategiesMap.Add(ticker, new HashSet <int>(new int[] { strategy_id }));
                    _socket.reqRealTimeBars(ticker, con, bar_size, whatToShow, false, new List <TagValue>());
                }
                else
                {
                    _tickerStrategiesMap[_barTickerMap[bar_id]].Add(strategy_id);
                }
            }
            return(_barTickerMap[bar_id]);
        }
Ejemplo n.º 2
0
 //Internal utility functions
 private void refreshContractsAndMktData()
 {
     _currDate = getExchangeTime(_socket.getCurrentLocalTime(), _zone).Date;
     foreach (int conId in _contracts)
     {
         int oldConId = _contractDetailMap.ContainsKey(conId) ? _contractDetailMap[conId].Summary.ConId : 0;
         int reqId    = _tg.get();
         lock (_lock)
             _reqContractMap.Add(reqId, conId);
         DateTime currd = getExchangeTime(_socket.getCurrentLocalTime(), _zone).Date;
         Contract con   = _db.getContractByDate(conId, currd);
         _socket.reqContractDetails(reqId, con);
         while (_reqContractMap.ContainsKey(reqId))
         {
             Thread.Sleep(1);
         }
         if (oldConId == 0)
         {
             _contractReqMap.Add(conId, _mm.reqRealTimeBars(id, _contractDetailMap[conId].Summary, 0, getBarType(), "IB"));
         }
         else if (oldConId != _contractDetailMap[conId].Summary.ConId)
         {
             _mm.cancelRealTimeBars(id, _contractReqMap[conId]);
             _contractReqMap[conId] = _mm.reqRealTimeBars(id, _contractDetailMap[conId].Summary, 0, getBarType(), "IB");
         }
     }
 }
Ejemplo n.º 3
0
        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();
            }
        }