Esempio n. 1
0
 void quote_OnRtnTick(object sender, TickEventArgs e)
 {
 }
        // 行情触发
        void _q_OnRtnTick(object sender, TickEventArgs e)
        {
            if (_time == new TimeSpan(0, 0, 0))
            {
                _time = TimeSpan.Parse(e.Tick.UpdateTime);
                _watch.Restart();
            }

            if (!_timer.Enabled)
            {
                _q.OnRtnTick -= _q_OnRtnTick;
                return;
            }

            TimeSpan now = _time.Add(_watch.Elapsed);
            //时间过滤
            if (_noTouch1 && now >= _noTouchT1 && now <= _noTouchT1_2)
                return;
            if (_noTouch2 && now >= _noTouchT2 && now <= _noTouchT2_2)
                return;

            foreach (Stra stra in _dicStra.Values)
            {
                if (stra.Instrument1 != e.Tick.InstrumentID && stra.Instrument2 != e.Tick.InstrumentID)
                    continue;
                if (stra.Status != ArbStatus.NotTouch)
                    continue;

                InstrumentField instField1, instField2;
                if (!_t.DicInstrumentField.TryGetValue(stra.Instrument1, out instField1))
                    continue;
                if (!_t.DicInstrumentField.TryGetValue(stra.Instrument2, out instField2))
                    continue;
                //交易时段过滤
                ExchangeStatusType excStatus;
                if (!_t.DicExcStatus.TryGetValue(instField1.ProductID, out excStatus) || excStatus != ExchangeStatusType.Trading)
                    continue;
                if (!_t.DicExcStatus.TryGetValue(instField2.ProductID, out excStatus) || excStatus != ExchangeStatusType.Trading)
                    continue;

                MarketData t1, t2;
                if (!_q.DicTick.TryGetValue(stra.Instrument1, out t1))
                    continue;
                if (!_q.DicTick.TryGetValue(stra.Instrument2, out t2))
                    continue;

                double ask = t1.AskPrice * stra.Rate1 - t2.BidPrice * stra.Rate2;
                double bid = t1.BidPrice * stra.Rate1 - t2.AskPrice * stra.Rate2;
                if (stra.Status != ArbStatus.NotTouch)
                    continue;	//防止重复发单(两个合约数据同时到达)
                //是否启动过滤
                if (_listStarted.IndexOf(stra.StraID) < 0)
                    continue;

                if (stra.Direction == DirectionType.Buy)
                {
                    //if (bid <= stra.Price)
                    if ((stra.IsMarket ? ask : bid) <= stra.Price)
                    {
                        stra.Status = ArbStatus.Normal;
                        _queueModifiedStra.Enqueue(new Tuple<Stra, string>(stra, "Status")); //用于刷新
                        if (stra.IsMarket)// && ask <= stra.Price)
                        {
                            if (instField1.ExchangeID == "SHFE")
                                _t.ReqOrderInsert(stra.Instrument1, DirectionType.Buy, stra.Offset, t1.UpperLimitPrice, stra.Volume1, pCustom: stra.StraID);
                            else
                                _t.ReqOrderInsert(stra.Instrument1, DirectionType.Buy, stra.Offset, t1.AskPrice, stra.Volume1, pType: OrderType.Market, pCustom: stra.StraID);

                            if (instField2.ExchangeID == "SHFE")
                                _t.ReqOrderInsert(stra.Instrument2, DirectionType.Sell, stra.Offset, t2.LowerLimitPrice, stra.Volume2, pCustom: stra.StraID);
                            else
                                _t.ReqOrderInsert(stra.Instrument2, DirectionType.Sell, stra.Offset, t2.BidPrice, stra.Volume2, pType: OrderType.Market, pCustom: stra.StraID);
                        }
                        else //挂价
                        {
                            _t.ReqOrderInsert(stra.Instrument1, DirectionType.Buy, stra.Offset, t1.BidPrice, stra.Volume1, pCustom: stra.StraID);
                            _t.ReqOrderInsert(stra.Instrument2, DirectionType.Sell, stra.Offset, t2.AskPrice, stra.Volume2, pCustom: stra.StraID);
                        }
                    }
                }
                else if (stra.Direction == DirectionType.Sell)
                {
                    //if (ask >= stra.Price)
                    if ((stra.IsMarket ? bid : ask) >= stra.Price)
                    {
                        stra.Status = ArbStatus.Normal;
                        _queueModifiedStra.Enqueue(new Tuple<Stra, string>(stra, "Status")); //用于刷新

                        if (stra.IsMarket)// && bid >= stra.Price)
                        {
                            if (instField1.ExchangeID == "SHFE")
                                _t.ReqOrderInsert(stra.Instrument1, DirectionType.Sell, stra.Offset, t1.LowerLimitPrice, stra.Volume1, pCustom: stra.StraID);
                            else
                                _t.ReqOrderInsert(stra.Instrument1, DirectionType.Sell, stra.Offset, t1.BidPrice, stra.Volume1, pType: OrderType.Market, pCustom: stra.StraID);

                            if (instField2.ExchangeID == "SHFE")
                                _t.ReqOrderInsert(stra.Instrument2, DirectionType.Buy, stra.Offset, t2.UpperLimitPrice, stra.Volume2, pCustom: stra.StraID);
                            else
                                _t.ReqOrderInsert(stra.Instrument2, DirectionType.Buy, stra.Offset, t2.AskPrice, stra.Volume2, pType: OrderType.Market, pCustom: stra.StraID);
                        }
                        else
                        {
                            _t.ReqOrderInsert(stra.Instrument1, DirectionType.Sell, stra.Offset, t1.AskPrice, stra.Volume1, pCustom: stra.StraID);
                            _t.ReqOrderInsert(stra.Instrument2, DirectionType.Buy, stra.Offset, t2.BidPrice, stra.Volume2, pCustom: stra.StraID);
                        }
                    }
                }
            }
        }