コード例 #1
0
ファイル: MetaStrategy.cs プロジェクト: ForTrade/CSharp
		internal override void OnTrade_(Trade trade)
		{
			foreach (Strategy current in this.strategiesByInstrument[trade.instrumentId])
			{
				current.OnTrade_(trade);
			}
			base.OnTrade_(trade);
		}
コード例 #2
0
ファイル: DemoTabControl.cs プロジェクト: xxmmxx/DemoDock
 private void OnTrade(object sender,Trade trade)
 {
     TabViewer viewer;
     int key = trade.InstrumentId;
     if (!this.viewers.TryGetValue(key, out viewer))
     {
         viewer = new TabViewer(){Dock = DockStyle.Fill};
         this.viewers.Add(key,viewer);
         TabPage page = new TabPage();
         var instrument = base.framework.InstrumentManager.GetById(key);
         page.Text = instrument.ToString();
         page.Controls.Add(viewer);
         this.tabViewers.TabPages.Add(page);
     }
     viewer.OnTrade(sender,trade);
 }
コード例 #3
0
        public void OnTrade(Trade trade)
        {
            if (this.orders[trade.instrumentId] == null)
            {
                return;
            }
            if (this.fillOnTrade)
            {
                int i = 0;
                while (i < this.orders[trade.instrumentId].Count)
                {
                    Order order = this.orders[trade.instrumentId][i];
                    while (true)
                    {
                        switch (order.type)
                        {
                        case OrderType.Market:
                            goto IL_60;

                        case OrderType.Stop:
                            switch (order.side)
                            {
                            case OrderSide.Buy:
                                if (trade.price >= order.stopPx)
                                {
                                    order.type = OrderType.Market;
                                    continue;
                                }
                                break;

                            case OrderSide.Sell:
                                if (trade.price <= order.stopPx)
                                {
                                    order.type = OrderType.Market;
                                    continue;
                                }
                                break;
                            }
                            break;

                        case OrderType.Limit:
                            goto IL_78;

                        case OrderType.StopLimit:
                            switch (order.side)
                            {
                            case OrderSide.Buy:
                                if (trade.price >= order.stopPx)
                                {
                                    order.type = OrderType.Limit;
                                    continue;
                                }
                                break;

                            case OrderSide.Sell:
                                if (trade.price <= order.stopPx)
                                {
                                    order.type = OrderType.Limit;
                                    continue;
                                }
                                break;
                            }
                            break;
                        }
                        break;
                    }
IL_1C1:
                    i++;
                    continue;
                    goto IL_1C1;
IL_60:
                    this.Fill(order, trade.price, trade.size);
                    goto IL_1C1;
IL_78:
                    switch (order.side)
                    {
                    case OrderSide.Buy:
                        if (trade.price <= order.price)
                        {
                            if (this.fillAtLimitPrice)
                            {
                                this.Fill(order, order.price, trade.size);
                            }
                            else
                            {
                                this.Fill(order, trade.price, trade.size);
                            }
                        }
                        break;

                    case OrderSide.Sell:
                        if (trade.price >= order.price)
                        {
                            if (this.fillAtLimitPrice)
                            {
                                this.Fill(order, order.price, trade.size);
                            }
                            else
                            {
                                this.Fill(order, trade.price, trade.size);
                            }
                        }
                        break;
                    }
                    goto IL_1C1;
                }
            }
        }
コード例 #4
0
        private void OnRtnDepthMarketData(IntPtr pApi, ref CThostFtdcDepthMarketDataField pDepthMarketData)
        {
            DataRecord record;
            if (!_dictAltSymbol2Instrument.TryGetValue(pDepthMarketData.InstrumentID, out record))
            {
                mdlog.Warn("合约{0}不在订阅列表中却收到了数据", pDepthMarketData.InstrumentID);
                return;
            }

            Instrument instrument = record.Instrument;

            CThostFtdcDepthMarketDataField DepthMarket;
            _dictDepthMarketData.TryGetValue(pDepthMarketData.InstrumentID, out DepthMarket);

            //将更新字典的功能提前,因为如果一开始就OnTrade中下单,涨跌停没有更新
            _dictDepthMarketData[pDepthMarketData.InstrumentID] = pDepthMarketData;

            if (TimeMode.LocalTime == _TimeMode)
            {
                //为了生成正确的Bar,使用本地时间
                _dateTime = Clock.Now;
            }
            else
            {
                //直接按HH:mm:ss来解析,测试过这种方法目前是效率比较高的方法
                try
                {
                    // 只有使用交易所行情时才需要处理跨天的问题
                    ChangeTradingDay(pDepthMarketData.TradingDay);

                    int HH = int.Parse(pDepthMarketData.UpdateTime.Substring(0, 2));
                    int mm = int.Parse(pDepthMarketData.UpdateTime.Substring(3, 2));
                    int ss = int.Parse(pDepthMarketData.UpdateTime.Substring(6, 2));

                    _dateTime = new DateTime(_yyyy, _MM, _dd, HH, mm, ss, pDepthMarketData.UpdateMillisec);
                }
                catch (Exception)
                {
                    _dateTime = Clock.Now;
                }
            }

            if (record.TradeRequested)
            {
                //通过测试,发现IB的Trade与Quote在行情过来时数量是不同的,在这也做到不同
                if (DepthMarket.LastPrice == pDepthMarketData.LastPrice
                    && DepthMarket.Volume == pDepthMarketData.Volume)
                { }
                else
                {
                    //行情过来时是今天累计成交量,得转换成每个tick中成交量之差
                    int volume = pDepthMarketData.Volume - DepthMarket.Volume;
                    if (0 == DepthMarket.Volume)
                    {
                        //没有接收到最开始的一条,所以这计算每个Bar的数据时肯定超大,强行设置为0
                        volume = 0;
                    }
                    else if (volume < 0)
                    {
                        //如果隔夜运行,会出现今早成交量0-昨收盘成交量,出现负数,所以当发现为负时要修改
                        volume = pDepthMarketData.Volume;
                    }

                    Trade trade = new Trade(_dateTime,
                        pDepthMarketData.LastPrice == double.MaxValue ? 0 : pDepthMarketData.LastPrice,
                        volume);

                    EmitNewTradeEvent(instrument, trade);
                }
            }

            if (record.QuoteRequested)
            {
                //if (
                //DepthMarket.BidVolume1 == pDepthMarketData.BidVolume1
                //&& DepthMarket.AskVolume1 == pDepthMarketData.AskVolume1
                //&& DepthMarket.BidPrice1 == pDepthMarketData.BidPrice1
                //&& DepthMarket.AskPrice1 == pDepthMarketData.AskPrice1
                //)
                //{ }
                //else
                {
                    Quote quote = new Quote(_dateTime,
                        pDepthMarketData.BidPrice1 == double.MaxValue ? 0 : pDepthMarketData.BidPrice1,
                        pDepthMarketData.BidVolume1,
                        pDepthMarketData.AskPrice1 == double.MaxValue ? 0 : pDepthMarketData.AskPrice1,
                        pDepthMarketData.AskVolume1
                    );

                    EmitNewQuoteEvent(instrument, quote);
                }
            }

            if (record.MarketDepthRequested)
            {
                EmitNewMarketDepth(instrument, _dateTime, 0, MDSide.Ask, pDepthMarketData.AskPrice1, pDepthMarketData.AskVolume1);
                EmitNewMarketDepth(instrument, _dateTime, 0, MDSide.Bid, pDepthMarketData.BidPrice1, pDepthMarketData.BidVolume1);

                //EmitNewMarketDepth(instrument, _dateTime, 1, MDSide.Ask, pDepthMarketData.AskPrice2, pDepthMarketData.AskVolume2);
                //EmitNewMarketDepth(instrument, _dateTime, 1, MDSide.Bid, pDepthMarketData.BidPrice2, pDepthMarketData.BidVolume2);

                //EmitNewMarketDepth(instrument, _dateTime, 2, MDSide.Ask, pDepthMarketData.AskPrice3, pDepthMarketData.AskVolume3);
                //EmitNewMarketDepth(instrument, _dateTime, 2, MDSide.Bid, pDepthMarketData.BidPrice3, pDepthMarketData.BidVolume3);

                //EmitNewMarketDepth(instrument, _dateTime, 3, MDSide.Ask, pDepthMarketData.AskPrice4, pDepthMarketData.AskVolume4);
                //EmitNewMarketDepth(instrument, _dateTime, 3, MDSide.Bid, pDepthMarketData.BidPrice4, pDepthMarketData.BidVolume4);

                //EmitNewMarketDepth(instrument, _dateTime, 4, MDSide.Ask, pDepthMarketData.AskPrice5, pDepthMarketData.AskVolume5);
                //EmitNewMarketDepth(instrument, _dateTime, 4, MDSide.Bid, pDepthMarketData.BidPrice5, pDepthMarketData.BidVolume5);
            }

            // 价差生成功能
            do
            {
                if (null == CTPAPI.GetInstance().SpreadMarketData)
                    break;

                ISpreadMarketData SpreadMarketData = CTPAPI.GetInstance().SpreadMarketData;
                var ticks = SpreadMarketData.CalculateSpread(pDepthMarketData);
                if (null == ticks)
                    break;

                foreach (var tick in ticks)
                {
                    Instrument inst = InstrumentManager.Instruments[tick.Symbol];
                    if (null == inst)
                        continue;

                    if (!double.IsNaN(tick.Price))
                    {
                        Trade trade = new Trade(_dateTime, tick.Price, tick.Size);
                        trade.ProviderId = tick.ProviderId;

                        EmitNewTradeEvent(inst, trade);
                    }
                    if (!double.IsNaN(tick.Ask) && !double.IsNaN(tick.Bid))
                    {
                        Quote quote = new Quote(_dateTime,
                            tick.Bid, tick.BidSize,
                            tick.Ask, tick.AskSize);
                        quote.ProviderId = tick.ProviderId;

                        EmitNewQuoteEvent(inst, quote);
                    }
                }
            } while (false);

            // 直接回报CTP的行情信息
            if (EmitOnRtnDepthMarketData)
            {
                CTPAPI.GetInstance().FireOnRtnDepthMarketData(pDepthMarketData);
            }
        }
コード例 #5
0
ファイル: DataImport.cs プロジェクト: ForTrade/CSharp
		public void Import(string fileName, string symbol, int type)
		{
			Console.WriteLine("Starting export: " + fileName + " " + symbol);
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			TextReader textReader = File.OpenText(fileName);
			Instrument instrument = this.framework.InstrumentManager.Get(symbol);
			if (instrument == null)
			{
				instrument = new Instrument(InstrumentType.Stock, symbol, "", 1);
				this.framework.InstrumentManager.Add(instrument, true);
			}
			int num4 = 0;
			double num5 = -1.0;
			int num6 = -1;
			double num7 = -1.0;
			int num8 = -1;
			textReader.ReadLine();
			while (true)
			{
				string text = textReader.ReadLine();
				if (text == null)
				{
					break;
				}
				string[] array = text.Split(new char[]
				{
					','
				});
				CultureInfo invariantCulture = CultureInfo.InvariantCulture;
				switch (type)
				{
				case 4:
				{
					DateTime dateTime = DateTime.ParseExact(array[0], "yyyy-MM-dd HH:mm:ss.fff", invariantCulture);
					double num9 = double.Parse(array[1], invariantCulture);
					int num10 = int.Parse(array[2]);
					if (num9 > 0.0 && num10 > 0)
					{
						Trade obj = new Trade(dateTime, 1, instrument.Id, num9, num10);
						this.framework.DataManager.Save(instrument, obj);
						num3++;
					}
					break;
				}
				case 5:
				{
					DateTime dateTime2 = DateTime.ParseExact(array[0], "yyyy-MM-dd HH:mm:ss.fff", invariantCulture);
					double num11 = double.Parse(array[1], invariantCulture);
					int num12 = int.Parse(array[2]);
					double num13 = double.Parse(array[3], invariantCulture);
					int num14 = int.Parse(array[4]);
					if (num11 > 0.0 && num12 > 0 && (num5 != num11 || num6 != num12))
					{
						Bid obj2 = new Bid(dateTime2, 1, instrument.Id, num11, num12);
						this.framework.DataManager.Save(instrument, obj2);
						num5 = num11;
						num6 = num12;
						num2++;
					}
					if (num13 > 0.0 && num14 > 0 && (num7 != num13 || num8 != num14))
					{
						Ask obj3 = new Ask(dateTime2, 1, instrument.Id, num13, num14);
						this.framework.DataManager.Save(instrument, obj3);
						num7 = num13;
						num8 = num14;
						num++;
					}
					break;
				}
				}
				if (num4 % 100000 == 0)
				{
					Console.WriteLine(num4);
				}
				num4++;
			}
			Console.WriteLine(string.Concat(new object[]
			{
				"Lines = ",
				num4,
				" bids: ",
				num2,
				" asks: ",
				num,
				" trades: ",
				num3
			}));
			textReader.Close();
		}
コード例 #6
0
ファイル: ExecutionSimulator.cs プロジェクト: ForTrade/CSharp
		public void OnTrade(Trade trade)
		{
			if (this.orders[trade.instrumentId] == null)
			{
				return;
			}
			if (this.fillOnTrade)
			{
				int i = 0;
				while (i < this.orders[trade.instrumentId].Count)
				{
					Order order = this.orders[trade.instrumentId][i];
					while (true)
					{
						switch (order.type)
						{
						case OrderType.Market:
							goto IL_60;
						case OrderType.Stop:
							switch (order.side)
							{
							case OrderSide.Buy:
								if (trade.price >= order.stopPx)
								{
									order.type = OrderType.Market;
									continue;
								}
								break;
							case OrderSide.Sell:
								if (trade.price <= order.stopPx)
								{
									order.type = OrderType.Market;
									continue;
								}
								break;
							}
							break;
						case OrderType.Limit:
							goto IL_78;
						case OrderType.StopLimit:
							switch (order.side)
							{
							case OrderSide.Buy:
								if (trade.price >= order.stopPx)
								{
									order.type = OrderType.Limit;
									continue;
								}
								break;
							case OrderSide.Sell:
								if (trade.price <= order.stopPx)
								{
									order.type = OrderType.Limit;
									continue;
								}
								break;
							}
							break;
						}
						break;
					}
					IL_1C1:
					i++;
					continue;
					goto IL_1C1;
					IL_60:
					this.Fill(order, trade.price, trade.size);
					goto IL_1C1;
					IL_78:
					switch (order.side)
					{
					case OrderSide.Buy:
						if (trade.price <= order.price)
						{
							if (this.fillAtLimitPrice)
							{
								this.Fill(order, order.price, trade.size);
							}
							else
							{
								this.Fill(order, trade.price, trade.size);
							}
						}
						break;
					case OrderSide.Sell:
						if (trade.price >= order.price)
						{
							if (this.fillAtLimitPrice)
							{
								this.Fill(order, order.price, trade.size);
							}
							else
							{
								this.Fill(order, trade.price, trade.size);
							}
						}
						break;
					}
					goto IL_1C1;
				}
			}
		}
コード例 #7
0
ファイル: AlphaComponent.cs プロジェクト: ForTrade/CSharp
		public virtual void OnTrade(Trade trade)
		{
		}
コード例 #8
0
ファイル: Strategy.cs プロジェクト: ForTrade/CSharp
 internal virtual void OnTrade_(Trade trade)
 {
     if (this.raiseEvents && this.instruments.Contains(trade.instrumentId))
     {
         this.OnTrade(this.framework.InstrumentManager.GetById(trade.instrumentId), trade);
     }
     List<Stop> list = this.stopsByInstrument[trade.instrumentId];
     if (list != null)
     {
         for (int i = 0; i < list.Count; i++)
         {
             list[i].OnTrade(trade);
         }
     }
     LinkedList<Strategy> linkedList = this.strategiesByInstrument[trade.instrumentId];
     if (linkedList != null)
     {
         for (LinkedListNode<Strategy> linkedListNode = linkedList.First; linkedListNode != null; linkedListNode = linkedListNode.Next)
         {
             linkedListNode.Data.OnTrade_(trade);
         }
     }
 }
コード例 #9
0
        public void OutputSeries(out IDataSeries trades, out IDataSeries bids, out IDataSeries asks)
        {
            trades = new TickSeries();
            bids = new TickSeries();
            asks = new TickSeries();

            PbTickCodec codec = new PbTickCodec();
            int TradingDay = -1;
            int _lastTradeSize = 0;
            foreach (var s in Series)
            {
                if(TradingDay != s.TradingDay)
                {
                    _lastTradeSize = 0;
                    TradingDay = s.TradingDay;
                }
                var dateTime = codec.GetDateTime(s.ActionDay == 0 ? s.TradingDay : s.ActionDay).Add(codec.GetUpdateTime(s));
                var tick = PbTick2DepthMarketDataNClass(codec, s);

                if(SubscribeExternData)
                {
                    var trade = new TradeEx(dateTime, 0, _InstrumentId, tick.LastPrice, (int)tick.Volume);
                    trade.Size -= _lastTradeSize;
                    trade.DepthMarketData = tick;
                    trades.Add(trade);
                }
                else
                {
                    var trade = new Trade(dateTime, 0, _InstrumentId, tick.LastPrice, (int)tick.Volume);
                    trade.Size -= _lastTradeSize;
                    trades.Add(trade);
                }


                if (tick.Bids != null && tick.Bids.Length > 0)
                {
                    var bid = new Bid(dateTime, 0, _InstrumentId, tick.Bids[0].Price, tick.Bids[0].Size);
                    bids.Add(bid);
                }
                if (tick.Asks != null && tick.Asks.Length > 0)
                {
                    var ask = new Ask(dateTime, 0, _InstrumentId, tick.Asks[0].Price, tick.Asks[0].Size);
                    asks.Add(ask);
                }

                _lastTradeSize = (int)tick.Volume;
            }
        }
コード例 #10
0
ファイル: MatchingEngine.cs プロジェクト: ForTrade/CSharp
		public void OnTrade(Trade trade)
		{
			if (this.orders[trade.instrumentId] == null)
			{
				return;
			}
			for (int i = 0; i < this.orders[trade.instrumentId].Count; i++)
			{
				Order order = this.orders[trade.instrumentId][i];
				if (!order.isOutPrice)
				{
					this.Process(order, trade);
				}
				if (this.addQueueToOrderText)
				{
					order.Text = order.queueSize.ToString();
				}
			}
		}
コード例 #11
0
ファイル: MatchingEngine.cs プロジェクト: ForTrade/CSharp
		private void Process(Order order, Trade trade)
		{
			if (!order.isQueueCalculated)
			{
				return;
			}
			if (order.type == OrderType.Limit && order.price == trade.price)
			{
				order.queueSize -= (double)trade.size;
				if (order.queueSize < order.leavesQty)
				{
					if (order.queueSize > 0.0)
					{
						double num = order.leavesQty - order.queueSize;
						this.Fill(order, order.price, (int)num);
						return;
					}
					if (order.queueSize < 0.0)
					{
						order.queueSize = 0.0;
						this.Fill(order, order.price, (int)order.leavesQty);
					}
				}
			}
		}
コード例 #12
0
        private void OnRtnDepthMarketData(IntPtr pApi, ref CZQThostFtdcDepthMarketDataField pDepthMarketData)
        {
            CZQThostFtdcDepthMarketDataField DepthMarket;
            if (_dictDepthMarketData.TryGetValue(pDepthMarketData.InstrumentID, out DepthMarket))
            {
                //将更新字典的功能提前,因为如果一开始就OnTrade中下单,涨跌停没有更新
                _dictDepthMarketData[pDepthMarketData.InstrumentID] = pDepthMarketData;

                if (TimeMode.LocalTime == _TimeMode)
                {
                    //为了生成正确的Bar,使用本地时间
                    _dateTime = Clock.Now;
                }
                else
                {
                    //直接按HH:mm:ss来解析,测试过这种方法目前是效率比较高的方法
                    int HH = int.Parse(pDepthMarketData.UpdateTime.Substring(0, 2));
                    int mm = int.Parse(pDepthMarketData.UpdateTime.Substring(3, 2));
                    int ss = int.Parse(pDepthMarketData.UpdateTime.Substring(6, 2));

                    _dateTime = new DateTime(_yyyy, _MM, _dd, HH, mm, ss, pDepthMarketData.UpdateMillisec);
                }

                Instrument instrument = _dictAltSymbol2Instrument[pDepthMarketData.InstrumentID];

                //通过测试,发现IB的Trade与Quote在行情过来时数量是不同的,在这也做到不同
                if (DepthMarket.LastPrice == pDepthMarketData.LastPrice
                    && DepthMarket.Volume == pDepthMarketData.Volume)
                { }
                else
                {
                    //行情过来时是今天累计成交量,得转换成每个tick中成交量之差
                    int volume = pDepthMarketData.Volume - DepthMarket.Volume;
                    if (0 == DepthMarket.Volume)
                    {
                        //没有接收到最开始的一条,所以这计算每个Bar的数据时肯定超大,强行设置为0
                        volume = 0;
                    }
                    else if (volume<0)
                    {
                        //如果隔夜运行,会出现今早成交量0-昨收盘成交量,出现负数,所以当发现为负时要修改
                        volume = pDepthMarketData.Volume;
                    }

                    Trade trade = new Trade(_dateTime,
                        pDepthMarketData.LastPrice == double.MaxValue ? 0 : pDepthMarketData.LastPrice,
                        volume);

                    if (null != marketDataFilter)
                    {
                        //comment by fouvy, for openquant 2.9
                        //Trade t = marketDataFilter.FilterTrade(trade, instrument.Symbol);
                        //if (null != t)
                        //{
                        //    EmitNewTradeEvent(instrument, t);
                        //}
                    }
                    else
                    {
                        EmitNewTradeEvent(instrument, trade);
                    }
                }

                if (
                    DepthMarket.BidVolume1 == pDepthMarketData.BidVolume1
                    && DepthMarket.AskVolume1 == pDepthMarketData.AskVolume1
                    && DepthMarket.BidPrice1 == pDepthMarketData.BidPrice1
                    && DepthMarket.AskPrice1 == pDepthMarketData.AskPrice1
                    )
                { }
                else
                {
                    Quote quote = new Quote(_dateTime,
                        pDepthMarketData.BidPrice1 == double.MaxValue ? 0 : pDepthMarketData.BidPrice1,
                        pDepthMarketData.BidVolume1,
                        pDepthMarketData.AskPrice1 == double.MaxValue ? 0 : pDepthMarketData.AskPrice1,
                        pDepthMarketData.AskVolume1
                    );

                    if (null != marketDataFilter)
                    {
                        //comment by fouvy change for openquant 2.9
                        //Quote q = marketDataFilter.FilterQuote(quote, instrument.Symbol);
                        //if (null != q)
                        //{
                        //    EmitNewQuoteEvent(instrument, q);
                        //}
                    }
                    else
                    {
                        EmitNewQuoteEvent(instrument, quote);
                    }
                }
            }
        }
コード例 #13
0
ファイル: ComponentStrategy.cs プロジェクト: ForTrade/CSharp
		protected internal override void OnTrade(Instrument instrument, Trade trade)
		{
			this.alphaComponent.OnTrade(trade);
			this.positionComponent.OnTrade(trade);
		}
コード例 #14
0
 protected internal override void OnTrade(Instrument instrument, Trade trade)
 {
     this.alphaComponent.OnTrade(trade);
     this.positionComponent.OnTrade(trade);
 }
コード例 #15
0
 protected internal override void OnStrategyStart()
 {
     Trade data = new Trade();
     this.watch.Start();
     (base.DataProvider as PerformanceProvider).EmitData(data);
 }
コード例 #16
0
 protected internal override void OnTrade(Instrument instrument, Trade trade)
 {
     this.watch.Stop();
 }
コード例 #17
0
ファイル: Stop.cs プロジェクト: ForTrade/CSharp
 internal void OnTrade(Trade trade)
 {
     if (this.traceOnTrade)
     {
         this.currPrice = trade.Price;
         this.fillPrice = trade.Price;
         this.trailPrice = trade.Price;
         this.CheckStop();
     }
 }
コード例 #18
0
ファイル: Strategy.cs プロジェクト: ForTrade/CSharp
 protected internal virtual void OnTrade(Instrument instrument, Trade trade)
 {
 }
コード例 #19
0
ファイル: SellSideStrategy.cs プロジェクト: ForTrade/CSharp
 public void EmitTrade(Trade trade)
 {
     this.framework.eventManager.OnEvent(trade);
 }
コード例 #20
0
ファイル: Trade.cs プロジェクト: ForTrade/CSharp
 public Trade(Trade trade)
     : base(trade)
 {
 }
コード例 #21
0
        private void OnRtnDepthMarketData(IntPtr pApi, ref CThostFtdcDepthMarketDataField pDepthMarketData)
        {
            DataRecord record;
            if (!_dictAltSymbol2Instrument.TryGetValue(pDepthMarketData.InstrumentID, out record))
            {
                mdlog.Warn("合约{0}不在订阅列表中却收到了数据", pDepthMarketData.InstrumentID);
                return;
            }

            Instrument instrument = record.Instrument;

            CThostFtdcDepthMarketDataField DepthMarket;
            _dictDepthMarketData.TryGetValue(pDepthMarketData.InstrumentID, out DepthMarket);

            //将更新字典的功能提前,因为如果一开始就OnTrade中下单,涨跌停没有更新
            _dictDepthMarketData[pDepthMarketData.InstrumentID] = pDepthMarketData;

            if (TimeMode.LocalTime == _TimeMode)
            {
                //为了生成正确的Bar,使用本地时间
                _dateTime = Clock.Now;
            }
            else
            {
                //直接按HH:mm:ss来解析,测试过这种方法目前是效率比较高的方法
                int HH = int.Parse(pDepthMarketData.UpdateTime.Substring(0, 2));
                int mm = int.Parse(pDepthMarketData.UpdateTime.Substring(3, 2));
                int ss = int.Parse(pDepthMarketData.UpdateTime.Substring(6, 2));

                _dateTime = new DateTime(_yyyy, _MM, _dd, HH, mm, ss, pDepthMarketData.UpdateMillisec);
            }

            if (record.TradeRequested)
            {
                //通过测试,发现IB的Trade与Quote在行情过来时数量是不同的,在这也做到不同
                if (DepthMarket.LastPrice == pDepthMarketData.LastPrice
                    && DepthMarket.Volume == pDepthMarketData.Volume)
                { }
                else
                {
                    //行情过来时是今天累计成交量,得转换成每个tick中成交量之差
                    int volume = pDepthMarketData.Volume - DepthMarket.Volume;
                    if (0 == DepthMarket.Volume)
                    {
                        //没有接收到最开始的一条,所以这计算每个Bar的数据时肯定超大,强行设置为0
                        volume = 0;
                    }
                    else if (volume < 0)
                    {
                        //如果隔夜运行,会出现今早成交量0-昨收盘成交量,出现负数,所以当发现为负时要修改
                        volume = pDepthMarketData.Volume;
                    }

                    Trade trade = new Trade(_dateTime,
                        pDepthMarketData.LastPrice == double.MaxValue ? 0 : pDepthMarketData.LastPrice,
                        volume);

                    if (null != MarketDataFilter)
                    {
                        Trade t = MarketDataFilter.FilterTrade(trade, instrument.Symbol);
                        if (null != t)
                        {
                            EmitNewTradeEvent(instrument, t);
                        }
                    }
                    else
                    {
                        EmitNewTradeEvent(instrument, trade);
                    }
                }
            }

            if (record.QuoteRequested)
            {
                //if (
                //DepthMarket.BidVolume1 == pDepthMarketData.BidVolume1
                //&& DepthMarket.AskVolume1 == pDepthMarketData.AskVolume1
                //&& DepthMarket.BidPrice1 == pDepthMarketData.BidPrice1
                //&& DepthMarket.AskPrice1 == pDepthMarketData.AskPrice1
                //)
                //{ }
                //else
                {
                    Quote quote = new Quote(_dateTime,
                        pDepthMarketData.BidPrice1 == double.MaxValue ? 0 : pDepthMarketData.BidPrice1,
                        pDepthMarketData.BidVolume1,
                        pDepthMarketData.AskPrice1 == double.MaxValue ? 0 : pDepthMarketData.AskPrice1,
                        pDepthMarketData.AskVolume1
                    );

                    if (null != MarketDataFilter)
                    {
                        Quote q = MarketDataFilter.FilterQuote(quote, instrument.Symbol);
                        if (null != q)
                        {
                            EmitNewQuoteEvent(instrument, q);
                        }
                    }
                    else
                    {
                        EmitNewQuoteEvent(instrument, quote);
                    }
                }
            }

            if (record.MarketDepthRequested)
            {
                EmitNewMarketDepth(instrument, _dateTime, 0, MDSide.Ask, pDepthMarketData.AskPrice1, pDepthMarketData.AskVolume1);
                EmitNewMarketDepth(instrument, _dateTime, 0, MDSide.Bid, pDepthMarketData.BidPrice1, pDepthMarketData.BidVolume1);

                //EmitNewMarketDepth(instrument, _dateTime, 1, MDSide.Ask, pDepthMarketData.AskPrice2, pDepthMarketData.AskVolume2);
                //EmitNewMarketDepth(instrument, _dateTime, 1, MDSide.Bid, pDepthMarketData.BidPrice2, pDepthMarketData.BidVolume2);

                //EmitNewMarketDepth(instrument, _dateTime, 2, MDSide.Ask, pDepthMarketData.AskPrice3, pDepthMarketData.AskVolume3);
                //EmitNewMarketDepth(instrument, _dateTime, 2, MDSide.Bid, pDepthMarketData.BidPrice3, pDepthMarketData.BidVolume3);

                //EmitNewMarketDepth(instrument, _dateTime, 3, MDSide.Ask, pDepthMarketData.AskPrice4, pDepthMarketData.AskVolume4);
                //EmitNewMarketDepth(instrument, _dateTime, 3, MDSide.Bid, pDepthMarketData.BidPrice4, pDepthMarketData.BidVolume4);

                //EmitNewMarketDepth(instrument, _dateTime, 4, MDSide.Ask, pDepthMarketData.AskPrice5, pDepthMarketData.AskVolume5);
                //EmitNewMarketDepth(instrument, _dateTime, 4, MDSide.Bid, pDepthMarketData.BidPrice5, pDepthMarketData.BidVolume5);
            }
        }
コード例 #22
0
ファイル: Instrument.cs プロジェクト: ForTrade/CSharp
		public Instrument(Instrument instrument) : this()
		{
			this.id = instrument.id;
			this.type = instrument.type;
			this.symbol = instrument.symbol;
			this.description = instrument.description;
			this.exchange = instrument.exchange;
			this.currencyId = instrument.currencyId;
			this.tickSize = instrument.tickSize;
			this.putcall = instrument.putcall;
			this.factor = instrument.factor;
			this.strike = instrument.strike;
			this.maturity = instrument.maturity;
			this.margin = instrument.margin;
			foreach (Leg current in instrument.Legs)
			{
				this.Legs.Add(new Leg(current.Instrument, current.Weight));
			}
			this.trade = instrument.Trade;
			this.bid = instrument.Bid;
			this.ask = instrument.Ask;
		}
コード例 #23
0
ファイル: DataManager.cs プロジェクト: ForTrade/CSharp
		internal void OnTrade(Trade trade)
		{
			this.trade[trade.instrumentId] = trade;
		}
コード例 #24
0
ファイル: TabViewer.cs プロジェクト: ctguxp/DemoDock
 public void OnTrade(object sender, Trade trade)
 {
     this.label1.Text = trade.ToString();
 }
コード例 #25
0
ファイル: ReportItem.cs プロジェクト: hack1t/SmartQuant.dll
 protected virtual void OnTrade(Trade trade)
 {
 }
コード例 #26
0
ファイル: StrategyManager.cs プロジェクト: ForTrade/CSharp
 internal void OnTrade(Trade trade)
 {
     if (this.strategy != null && this.strategy.status == StrategyStatus.Running)
     {
         this.strategy.OnTrade_(trade);
     }
 }
コード例 #27
0
 void Dispatcher_Trade(object sender, SmartQuant.Trade trade)
 {
     base.InvokeAction(() => { OnTrade(sender, trade); });
 }
コード例 #28
0
ファイル: QBProvider.CTP.cs プロジェクト: fouvy/OpenQuant
        private void OnRtnDepthMarketData(IntPtr pApi, ref CThostFtdcDepthMarketDataField pDepthMarketData)
        {
            CThostFtdcDepthMarketDataField DepthMarket;
            if (_dictDepthMarketData.TryGetValue(pDepthMarketData.InstrumentID, out DepthMarket))
            {
                if (TimeMode.LocalTime == _TimeMode)
                {
                    //为了生成正确的Bar,使用本地时间
                    _dateTime = Clock.Now;
                }
                else
                {
                    //直接按HH:mm:ss来解析,测试过这种方法目前是效率比较高的方法
                    int HH = int.Parse(pDepthMarketData.UpdateTime.Substring(0, 2));
                    int mm = int.Parse(pDepthMarketData.UpdateTime.Substring(3, 2));
                    int ss = int.Parse(pDepthMarketData.UpdateTime.Substring(6, 2));

                    _dateTime = new DateTime(_yyyy, _MM, _dd, HH, mm, ss, pDepthMarketData.UpdateMillisec);
                }

                Instrument instrument = _dictAltSymbol2Instrument[pDepthMarketData.InstrumentID];

                //通过测试,发现IB的Trade与Quote在行情过来时数量是不同的,在这也做到不同
                if (DepthMarket.LastPrice == pDepthMarketData.LastPrice
                    && DepthMarket.Volume == pDepthMarketData.Volume)
                { }
                else
                {
                    //行情过来时是今天累计成交量,得转换成每个tick中成交量之差
                    int volume = pDepthMarketData.Volume - DepthMarket.Volume;
                    if (0 == DepthMarket.Volume)
                    {
                        //没有接收到最开始的一条,所以这计算每个Bar的数据时肯定超大,强行将设置为0
                        volume = 0;
                    }

                    Trade trade = new Trade(_dateTime,
                        pDepthMarketData.LastPrice == double.MaxValue ? 0 : pDepthMarketData.LastPrice,
                        volume);

                    if (null != marketDataFilter)
                    {
            /*                        Trade t = marketDataFilter.FilterTrade(trade, instrument.Symbol);
                        if (null != t)
                        {
                            EmitNewTradeEvent(instrument, t);
                        }
            */                    }
                    else
                    {
                        EmitNewTradeEvent(instrument, trade);
                    }
                }

                if (
                    DepthMarket.BidVolume1 == pDepthMarketData.BidVolume1
                    && DepthMarket.AskVolume1 == pDepthMarketData.AskVolume1
                    && DepthMarket.BidPrice1 == pDepthMarketData.BidPrice1
                    && DepthMarket.AskPrice1 == pDepthMarketData.AskPrice1
                    )
                { }
                else
                {
                    Quote quote = new Quote(_dateTime,
                        pDepthMarketData.BidPrice1 == double.MaxValue ? 0 : pDepthMarketData.BidPrice1,
                        pDepthMarketData.BidVolume1,
                        pDepthMarketData.AskPrice1 == double.MaxValue ? 0 : pDepthMarketData.AskPrice1,
                        pDepthMarketData.AskVolume1
                    );

                    if (null != marketDataFilter)
                    {
            /*                        Quote q = marketDataFilter.FilterQuote(quote, instrument.Symbol);
                        if (null != q)
                        {
                            EmitNewQuoteEvent(instrument, q);
                        }
            */                    }
                    else
                    {
                        EmitNewQuoteEvent(instrument, quote);
                    }
                }

                _dictDepthMarketData[pDepthMarketData.InstrumentID] = pDepthMarketData;
            }
        }
コード例 #29
0
        private void Send(Order order)
        {
            base.EmitExecutionReport(new ExecutionReport
            {
                dateTime    = this.framework.Clock.DateTime,
                order       = order,
                commandID   = order.id,
                instrument  = order.instrument,
                ordQty      = order.qty,
                timeInForce = order.timeInForce,
                execType    = ExecType.ExecNew,
                ordStatus   = OrderStatus.New,
                currencyId  = order.instrument.currencyId,
                ordType     = order.type,
                side        = order.side,
                cumQty      = 0.0,
                lastQty     = 0.0,
                leavesQty   = 0.0,
                lastPx      = 0.0,
                avgPx       = 0.0,
                text        = order.text
            });
            int id = order.instrument.Id;

            if (this.orders[id] == null)
            {
                this.orders[id] = new List <Order>();
            }
            this.orders[id].Add(order);
            switch (order.type)
            {
            case OrderType.Market:
                if (!this.fillMarketOnNext)
                {
                    Instrument instrument = order.instrument;
                    if (this.fillOnQuote)
                    {
                        switch (order.side)
                        {
                        case OrderSide.Buy:
                        {
                            Ask ask = this.framework.DataManager.GetAsk(instrument);
                            if (ask != null)
                            {
                                this.Fill(order, ask.Price, ask.Size);
                                return;
                            }
                            break;
                        }

                        case OrderSide.Sell:
                        {
                            Bid bid = this.framework.DataManager.GetBid(instrument);
                            if (bid != null)
                            {
                                this.Fill(order, bid.Price, bid.Size);
                                return;
                            }
                            break;
                        }
                        }
                    }
                    if (this.fillOnTrade)
                    {
                        Trade trade = this.framework.DataManager.GetTrade(instrument);
                        if (trade != null)
                        {
                            this.Fill(order, trade.Price, trade.Size);
                        }
                    }
                }
                break;

            case OrderType.Stop:
            case OrderType.Limit:
                break;

            default:
                return;
            }
        }