public static TradeArray GetHistoricalTrades(IHistoricalDataProvider provider, Instrument instrument, DateTime datetime1, DateTime datetime2) { ArrayList arrayList = DataManager.r6ZT8iFUv(provider, instrument, DataManager.EDataSeries.Trade, datetime1, datetime2, -1L); TradeArray tradeArray = new TradeArray(); foreach (Trade trade in arrayList) { tradeArray.Add(trade); } return(tradeArray); }
public Stack stkTrades; // stack the trades so oldest at the bottom based on retrieval from iqfeed public HistoryState( GetHistory gh ) { this.gh = gh; trades = new TradeArray(); quotes = new QuoteArray(); stkTrades = new Stack(2000); dtstk = new DateTime(0); Init(); }
public void Close() { gh = null; instrument = null; bs = null; bars = null; trades = null; quotes = null; stkTrades = null; bufSock.Close(); bufSock = null; }
public TradeArray GetTradeArray(string series, DateTime datetime1, DateTime datetime2) { TradeArray trades = new TradeArray(); if (this.file.Series[series] != null) { foreach (Trade trade in this.file.Series[series].GetArray(datetime1, datetime2)) { trades.Add(trade); } } return(trades); }
public TradeArray this[Instrument instrument] { get { TradeArray tradeArray = this.arrayList[instrument] as TradeArray; if (tradeArray == null) { tradeArray = new TradeArray(); this.arrayList.Add(instrument, tradeArray); } return(tradeArray); } }
public static TradeSeries GetHistoricalTrades(string provider, Instrument instrument, DateTime begin, DateTime end) { TradeSeries tradeSeries = new TradeSeries(); if (SmartQuant.Providers.ProviderManager.HistoricalDataProviders.Contains(provider)) { TradeArray historicalTrades = SmartQuant.Instruments.DataManager.GetHistoricalTrades(SmartQuant.Providers.ProviderManager.HistoricalDataProviders[provider], instrument.instrument, begin, end); foreach (SmartQuant.Data.Trade obj in historicalTrades) { tradeSeries.series.Add(obj); } } return(tradeSeries); }
private static void OnNewTrade(object sender, TradeEventArgs e) { Instrument instrument = e.Instrument as Instrument ?? InstrumentManager.Instruments[e.Instrument.Symbol, e.Provider.Name]; if (instrument == null) { return; } Trade trade = e.Trade; if (DataManager.tradeArrayLength != 0) { TradeArray tradeArray = DataManager.tradeArrayList[instrument]; tradeArray.Add(trade); if (DataManager.tradeArrayLength != -1 && tradeArray.Count > DataManager.tradeArrayLength) { tradeArray.RemoveAt(0); } } instrument.EmitNewTrade(new TradeEventArgs(trade, instrument, e.Provider)); }
public TradeSeries() { this.series = new TradeArray(); }
internal TradeSeries(TradeArray series) { this.series = series; }
public TradeSeriesEnumerator(TradeArray series) { this.series = series; this.enumerator = series.GetEnumerator(); }
public virtual void Save() { //修正持仓周期 if (HasPosition && this.holdingPeriod < 1) { this.holdingPeriod = 1; //当天入场的 } if (this.holdingPeriod < 1) { return; } //提取日线,分线数据,还有大盘的数据 string symbol = this.instrument.Symbol; Instrument indexInst = null; if (symbol.IndexOf("SHSE.") >= 0) { indexInst = InstrumentManager.Instruments["SHSE.000001"]; } else if (symbol.IndexOf("SZSE.3") >= 0) { indexInst = InstrumentManager.Instruments["SZSE.399006"]; } else { indexInst = InstrumentManager.Instruments["SZSE.399001"]; } //指数的日线和分线 BarSeries indexDaily = this.getDailySeries(indexInst); if (indexDaily.Count <= 0) { this.readLastNDailys(indexInst, this.indexDailyPeriod - 1); this.addTodayDaily(indexInst); } BarSeries indexMin5 = this.getMin5Series(indexInst); if (indexMin5.Count <= 0) { this.readLastNMin5s(indexInst, this.indexMin5Period); } //股票的日线和分线 BarSeries stockDaily = this.getDailySeries(this.instrument); if (stockDaily.Count <= 0) { this.readLastNDailys(this.instrument, this.dailyPeriod - 1); } BarSeries stockMin5 = this.getMin5Series(this.instrument); if (stockMin5.Count <= 0) { this.readLastNMin5s(this.instrument, this.min5Period); } if (stockDaily.Count < 1) { return; //没有日线的不记录 } //计算当天的奖赏 double rewardForInside = 0.0; //在场内的奖赏 double rewardForOutside = 0.0; //在场外的奖赏 if (this.holdingPeriod == 1) { rewardForInside = Math.Log(this.lastPrice / Position.EntryPrice); } else if (this.holdingPeriod > 1) { rewardForInside = Math.Log(this.lastPrice / stockDaily.Last.Close); rewardForOutside = Math.Log(this.lastPriceWhenDeal / stockDaily.Last.Close); } //Console.WriteLine("symbol:{0} --- last price is {1} --- entry Price is {2},has {3} ticks", // this.instrument.Symbol,this.lastPrice,Position.EntryPrice,this.Trades[this.instrument].Count); //当天的日线,交易决断前一刻的数据 this.addTodayDaily(this.instrument); //正规化数据 List <NormalizedBar> indexNormalizedDaily = this.NormalizeBars(indexDaily); List <NormalizedBar> indexNormalizedMin5 = this.NormalizeBars(indexMin5); List <NormalizedBar> stockNormalizedDaily = this.NormalizeBars(stockDaily); List <NormalizedBar> stockNormalizedMin5 = this.NormalizeBars(stockMin5); //写入数据库 try{ this.initDB(); string curDateString = Clock.Now.Date.ToString("yyyy-MM-dd"); //周期大于1且当天停盘没有交易的,不插入新记录,但要修改之前记录的下一个交易日期 TradeArray trades = this.Trades[this.instrument]; if (this.holdingPeriod > 1 && (trades.Count <= 0 || trades.LastDateTime < Clock.Now.Date)) { Console.WriteLine("证券{0}:今日没有交易", this.instrument.Symbol); BsonElement[] eleArray2 = new BsonElement[3]; eleArray2[0] = new BsonElement("NextTradeDate", curDateString); eleArray2[1] = new BsonElement("Symbol", this.instrument.Symbol); eleArray2[2] = new BsonElement("Inside", 1); QueryDocument query2 = new QueryDocument(eleArray2); UpdateDocument update = new UpdateDocument(); update.Add(new BsonElement("$set", new QueryDocument(new BsonElement("NextTradeDate", (string)this.strategy.Global["NextTradeDate"])))); this.collection.Update(query2, update); return; } //如果当天已写入记录,先删除 BsonElement[] eleArray = new BsonElement[2]; eleArray[0] = new BsonElement("Date", curDateString); eleArray[1] = new BsonElement("Symbol", this.instrument.Symbol); QueryDocument query = new QueryDocument(eleArray); this.collection.Remove(query); //写入在场外的记录,如果是第一天,则是假设没有买入,如果是第二天及以后,则是假设卖出 //此情况下,下一交易日为空 BsonElement[] eleArray1 = new BsonElement[12]; eleArray1[0] = new BsonElement("Date", curDateString); eleArray1[1] = new BsonElement("Symbol", this.instrument.Symbol); eleArray1[2] = new BsonElement("HoldingPeriod", this.holdingPeriod); eleArray1[3] = new BsonElement("Inside", 0); //当前是否在场内 eleArray1[4] = new BsonElement("IndexDaily", this.GetBsonArrayFromBars(indexNormalizedDaily)); eleArray1[5] = new BsonElement("IndexMin5", this.GetBsonArrayFromBars(indexNormalizedMin5)); eleArray1[6] = new BsonElement("StockDaily", this.GetBsonArrayFromBars(stockNormalizedDaily)); eleArray1[7] = new BsonElement("StockMin5", this.GetBsonArrayFromBars(stockNormalizedMin5)); eleArray1[8] = new BsonElement("Reward", rewardForOutside); eleArray1[9] = new BsonElement("NextTradeDate", ""); BsonDocument insert = new BsonDocument(eleArray1); this.collection.Insert(insert); //写入在场内的记录,如果是第一天,则是买入,如果是第二天及以后,则是继续持有 eleArray1 = new BsonElement[10]; eleArray1[0] = new BsonElement("Date", curDateString); eleArray1[1] = new BsonElement("Symbol", this.instrument.Symbol); eleArray1[2] = new BsonElement("HoldingPeriod", this.holdingPeriod); eleArray1[3] = new BsonElement("Inside", 1); //当前是否在场内 eleArray1[4] = new BsonElement("IndexDaily", this.GetBsonArrayFromBars(indexNormalizedDaily)); eleArray1[5] = new BsonElement("IndexMin5", this.GetBsonArrayFromBars(indexNormalizedMin5)); eleArray1[6] = new BsonElement("StockDaily", this.GetBsonArrayFromBars(stockNormalizedDaily)); eleArray1[7] = new BsonElement("StockMin5", this.GetBsonArrayFromBars(stockNormalizedMin5)); eleArray1[8] = new BsonElement("Reward", rewardForInside); //已到了持仓周期限制,并且确实已卖出的,下一交易日为空 if (this.holdingPeriod >= this.holdingPeriodLimit && (!HasPosition)) { eleArray1[9] = new BsonElement("NextTradeDate", ""); } else { eleArray1[9] = new BsonElement("NextTradeDate", (string)this.strategy.Global["NextTradeDate"]); } insert = new BsonDocument(eleArray1); this.collection.Insert(insert); }catch (Exception ex) { Console.WriteLine(ex.Message); } }