예제 #1
0
    //获取某个证券某天以前的N条日线数据
    public static ISeriesObject[] GetNDailysBeforeDate(Instrument inst, DateTime date, int n)
    {
        FileSeries dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily);

        if (dailySeries == null || dailySeries.Count <= 0)
        {
            return new ISeriesObject[] {}
        }
        ;
        DateTime lastDate = date;
        int      i;

        do
        {
            lastDate = lastDate.AddDays(-1);

            i = dailySeries.IndexOf(lastDate);
        }while(i < 0 && lastDate > dailySeries.FirstDateTime);
        if (i < 0)
        {
            return new ISeriesObject[] {}
        }
        ;
        int j = i - n + 1 > 0?i - n + 1:0;

        return(dailySeries.GetArray(j, i));
    }
예제 #2
0
        public List <Bar> GetLastNBars(string symbol, int barSize, int n, DateTime lastTime)
        {
            Instrument inst = InstrumentManager.Instruments[symbol];
            List <Bar> bars = new List <Bar>();

            if (inst != null)
            {
                string     seriesName = "Bar.Time." + barSize.ToString();
                FileSeries series     = (FileSeries)inst.GetDataSeries(seriesName);
                if (series != null)
                {
                    int last = series.IndexOf(lastTime, SearchOption.Prev);
                    if (last >= 0)
                    {
                        int first = last - n + 1;
                        first = first < 0 ? 0 : first;
                        ISeriesObject[] seriesObjects = series.GetArray(first, last);
                        foreach (ISeriesObject seriesObject in seriesObjects)
                        {
                            bars.Add((Bar)seriesObject);
                        }
                    }
                }
            }
            return(bars);
        }
예제 #3
0
    /*获取某个证券日线的个数*/
    public static int GetDailyCountBeforeDate(Instrument inst, DateTime date)
    {
        FileSeries dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily);

        if (dailySeries == null || dailySeries.Count <= 0)
        {
            return(0);
        }
        DateTime lastDate = date;
        int      i;

        do
        {
            lastDate = lastDate.AddDays(-1);
            i        = dailySeries.IndexOf(lastDate);
        }while(i < 0 && lastDate > dailySeries.FirstDateTime);
        if (i < 0)
        {
            return(0);
        }
        else
        {
            return(i - 0 + 1);
        }
    }
예제 #4
0
        public List <Daily> GetLastNDailys(string symbol, int n, DateTime lastDate)
        {
            Instrument   inst   = InstrumentManager.Instruments[symbol];
            List <Daily> dailys = new List <Daily>();

            if (inst != null)
            {
                FileSeries series = (FileSeries)inst.GetDataSeries("Daily");
                if (series != null)
                {
                    int last = series.IndexOf(lastDate, SearchOption.Prev);
                    if (last >= 0)
                    {
                        int first = last - n + 1;
                        first = first < 0 ? 0 : first;
                        ISeriesObject[] seriesObjects = series.GetArray(first, last);
                        foreach (ISeriesObject seriesObject in seriesObjects)
                        {
                            dailys.Add((Daily)seriesObject);
                        }
                    }
                }
            }
            return(dailys);
        }
예제 #5
0
    public override void OnInit()
    {
        DateTime curDate = Clock.Now.Date;

        ISeriesObject[] dailyBars = Util.GetNDailiesBeforeDate(this.instrument, curDate, 60);
        foreach (Daily dBar in dailyBars)
        {
            this.Bar.Add(dBar);
        }
        bool       overWrite = false;
        FileSeries series    = (FileSeries)this.instrument.GetDataSeries("RiskLevel");

        if (series == null)
        {
            series = (FileSeries)this.instrument.AddDataSeries("RiskLevel");
        }
        float prevRiskLevel = 1.0F;

        if (series.Count > 0)
        {
            DateTime lastDate = curDate;
            int      i;
            do
            {
                lastDate = lastDate.AddDays(-1);
                i        = series.IndexOf(lastDate);
            }while(i < 0 && lastDate > series.FirstDateTime);
            if (i >= 0)
            {
                ISeriesObject[] riskArray = series.GetArray(i, i);
                GMData          gmData    = (GMData)riskArray[0];
                prevRiskLevel = gmData.DataValue;
            }
        }
        GMDaily gmLastDaily = (GMDaily)this.DailyBar.Last;
        double  inc         = (gmLastDaily.Close / gmLastDaily.LastClose - 1) * 100;
        double  abs         = Math.Abs(inc);
        double  riskLevel   = prevRiskLevel + inc / abs * (int)(abs / 0.5) * 0.1;

        riskLevel = riskLevel < 0.5?0.5:(riskLevel > 1.2?1.2:riskLevel);
        GMData gmData1 = new GMData(curDate, (float)riskLevel);

        if (overWrite)
        {
            series.Remove(curDate);
            series.Add(gmData1);
        }
        else if (!series.Contains(curDate))
        {
            series.Add(gmData1);
        }
        //this.PositionLevel=(float)riskLevel;
        Console.WriteLine("昨日涨跌幅:{0},今日风险水平为:{1}", inc, riskLevel);
    }
예제 #6
0
        //当前时间的下一笔交易,交易时间必须是当前时间之后
        public Trade GetNextTrade(string symbol, DateTime lastTime)
        {
            Trade trade = null;

            if (this.seriesNamesToPartitionStore.Contains("Trade"))
            {
                this.changeStoreFileForTime(lastTime);
                if ((this.lastFile != null))
                {
                    string seriesName = symbol + ".Trade";
                    if (this.lastFile.Series.Contains(seriesName))
                    {
                        FileSeries series = this.lastFile.Series[seriesName];
                        int        index  = series.IndexOf(lastTime, SearchOption.Next);
                        if (index >= 0)
                        {
                            ISeriesObject seriesObj = series[index];
                            if (seriesObj.DateTime == lastTime)
                            {
                                seriesObj = series[index + 1];
                            }
                            trade = (Trade)seriesObj;
                        }
                    }
                }
            }
            else
            {
                Instrument inst = InstrumentManager.Instruments[symbol];
                if (inst != null)
                {
                    FileSeries series = (FileSeries)inst.GetDataSeries("Trade");
                    if (series != null)
                    {
                        int index = series.IndexOf(lastTime, SearchOption.Next);
                        if (index >= 0)
                        {
                            ISeriesObject seriesObj = series[index];
                            if (seriesObj.DateTime == lastTime)
                            {
                                seriesObj = series[index + 1];
                            }
                            trade = (Trade)seriesObj;
                        }
                    }
                }
            }
            return(trade);
        }
예제 #7
0
    public static List <Daily> GetLastNDailys(Instrument inst, DateTime lastDate, int n)
    {
        List <Daily> ret         = new List <Daily>();
        FileSeries   dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily);

        if (dailySeries == null || dailySeries.Count <= 0)
        {
            return(ret);
        }
        int i = dailySeries.IndexOf(lastDate, SearchOption.Prev);

        if (i < 0)
        {
            return(ret);
        }
        int j = i - n + 1 > 0?i - n + 1:0;

        foreach (ISeriesObject isObj in dailySeries.GetArray(j, i))
        {
            ret.Add((Daily)isObj);
        }
        return(ret);
    }
예제 #8
0
        protected override bool doJob()
        {
            Console.WriteLine("正在读取Tick数据生成涨停板数据分析表...");

            /*Console.WriteLine("测试作业.........");
             * return true;*/
            try
            {
                GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
                provider.Connect(10000);
                if (!provider.IsConnected)
                {
                    return(false);
                }

                string   dateString = this.curDate.ToString("yyyy-MM-dd");
                DateTime endTime    = this.curDate.Add(new TimeSpan(24, 0, 0));
                string   endString  = endTime.ToString("yyyy-MM-dd HH:mm:ss");

                List <AnalysisItem> analysisList = new List <AnalysisItem>();

                foreach (Instrument inst in InstrumentManager.Instruments)
                {
                    if (inst.SecurityDesc.IndexOf("B") >= 0)
                    {
                        continue;                                     //除去B股
                    }
                    if (inst.SecurityType == "IDX")
                    {
                        continue;                            //除去指数
                    }
                    FileSeries dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily);
                    if (dailySeries == null || dailySeries.Count <= 0)
                    {
                        continue;
                    }
                    int i = dailySeries.IndexOf(this.curDate);
                    if (i < 0)
                    {
                        continue;                //没有日线,当日不开盘
                    }
                    i = i - 10 > 0 ? i - 10 : 0; //往前推10个交易日
                    DateTime        firstDate = dailySeries[i].DateTime;
                    ISeriesObject[] dailys    = dailySeries.GetArray(firstDate, this.curDate);
                    int             k         = dailys.Length - 1;
                    GMDaily         gmDaily   = (GMDaily)dailys[k];

                    if (gmDaily.Close / gmDaily.LastClose > 1.095)
                    {//今日涨停
                        AnalysisItem ai = new AnalysisItem();
                        ai.Date   = gmDaily.DateTime;
                        ai.Symbol = inst.Symbol;
                        GMSDK.ShareIndex si = null;
                        //先读取当日股本,如果没有,则读取最近股本。一般情况下当日股本与最近股本相同,除非当天正好是除权日
                        List <GMSDK.ShareIndex> shareIndexList = provider.MdApi.GetShareIndex(ai.Symbol, dateString, dateString);
                        if (shareIndexList.Count <= 0)
                        {
                            shareIndexList = provider.MdApi.GetLastShareIndex(ai.Symbol);
                        }
                        if (shareIndexList.Count > 0)
                        {
                            si = shareIndexList[0];
                        }
                        if (si != null)
                        {
                            ai.FlowAShare = si.flow_a_share;
                        }
                        else
                        {
                            ai.FlowAShare = 0.0;
                        }

                        ai.LastClose = gmDaily.LastClose;
                        ai.Open      = gmDaily.Open;
                        ai.High      = gmDaily.High;
                        ai.Low       = gmDaily.Low;
                        ai.Close     = gmDaily.Close;
                        //换手率
                        if (ai.FlowAShare > 0)
                        {
                            ai.TurnoverRate = gmDaily.Volume / ai.FlowAShare * 100;
                        }
                        else
                        {
                            ai.TurnoverRate = 0.0;
                        }
                        //当日量与前5日均量比
                        int    m      = k - 1;
                        double sumVol = 0.0;
                        while (m >= 0 && m >= k - 5)
                        {
                            sumVol += ((Daily)dailys[m]).Volume;
                            m--;
                        }
                        double last5AvgVol = sumVol / (k - m - 1);
                        ai.VolDivLast5AvgVol = gmDaily.Volume / last5AvgVol;
                        //最后一笔判断是否是涨停
                        List <GMSDK.Tick> lastTicks = provider.MdApi.GetLastNTicks(inst.Symbol, 1, endString);
                        if (lastTicks.Count <= 0)
                        {
                            continue;
                        }
                        GMTrade gmLastTrade = (GMTrade)GSKToGM.ConvertTrade(lastTicks[0]);//最后一笔交易数据
                        if (gmLastTrade.Price < gmLastTrade.UpperLimit)
                        {
                            continue;
                        }

                        //封成比

                        GMQuote gmLastQuote = (GMQuote)GSKToGM.ConvertQuote(lastTicks[0]);//最后一刻报价数据
                        ai.BidSizeDivVol = (double)gmLastQuote.BidSize / gmDaily.Volume;

                        if (k > 0)
                        {
                            GMDaily lastGMDaily = (GMDaily)dailys[k - 1];
                            if (lastGMDaily.Close / lastGMDaily.LastClose >= 1.099)
                            {
                                ai.LastUpLimited = true;
                            }
                        }
                        analysisList.Add(ai);
                    }
                }

                Console.WriteLine("今日共有{0}只涨停。", analysisList.Count);
                foreach (AnalysisItem ai in analysisList)
                {
                    Console.WriteLine(@"Date:{0},Symbol:{1},昨收价:{2},昨日是否涨停:{3},开盘价:{4},
					最高价:{5},最低价:{6},收盘价:{7},涨停时间:{8},是否开板:{9},涨停时长:{10},
					成交量与昨日5日均量比:{11},收盘买一量与成交量比:{12},流通A股:{13},换手率:{14}"                    ,
                                      ai.Date, ai.Symbol, ai.LastClose, ai.LastUpLimited, ai.Open, ai.High, ai.Low, ai.Close,
                                      ai.WhenUpLimit, ai.UpLimitBreaked, ai.HowLongUpLimit, ai.VolDivLast5AvgVol, ai.BidSizeDivVol,
                                      ai.FlowAShare, ai.TurnoverRate);
                }

                MongoClient   client   = new MongoClient("mongodb://localhost:27017");
                MongoServer   server   = client.GetServer();
                MongoDatabase database = server.GetDatabase("finance");

                MongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("UpLimitAnalysis");
                foreach (AnalysisItem ai in analysisList)
                {
                    BsonElement[] eleArray = new BsonElement[2];
                    eleArray[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd"));
                    eleArray[1] = new BsonElement("symbol", ai.Symbol);
                    QueryDocument query = new QueryDocument(eleArray);
                    collection.Remove(query);
                    BsonElement[] eleArray1 = new BsonElement[19];
                    eleArray1[0]  = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd"));
                    eleArray1[1]  = new BsonElement("symbol", ai.Symbol);
                    eleArray1[2]  = new BsonElement("flowashare", ai.FlowAShare);
                    eleArray1[3]  = new BsonElement("lastclose", ai.LastClose);
                    eleArray1[4]  = new BsonElement("lastuplimited", ai.LastUpLimited);
                    eleArray1[5]  = new BsonElement("open", ai.Open);
                    eleArray1[6]  = new BsonElement("high", ai.High);
                    eleArray1[7]  = new BsonElement("low", ai.Low);
                    eleArray1[8]  = new BsonElement("close", ai.Close);
                    eleArray1[9]  = new BsonElement("whenuplimit", ai.WhenUpLimit.ToString());
                    eleArray1[10] = new BsonElement("uplimitbreaked", ai.UpLimitBreaked);
                    eleArray1[11] = new BsonElement("howlonguplimit", ai.HowLongUpLimit.ToString());
                    eleArray1[12] = new BsonElement("turnoverrate", ai.TurnoverRate);
                    eleArray1[13] = new BsonElement("voldivlast5avgvol", ai.VolDivLast5AvgVol);
                    eleArray1[14] = new BsonElement("bidsizedivvol", ai.BidSizeDivVol);
                    BsonDocument insert = new BsonDocument(eleArray1);
                    collection.Insert(insert);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(false);
        }
예제 #9
0
    protected override bool doJob()
    {
        Console.WriteLine("正在读取Tick数据生成涨停板数据分析表...");

        /*Console.WriteLine("测试作业.........");
         * return true;*/
        try {
            GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
            provider.Connect(10000);
            if (!provider.IsConnected)
            {
                return(false);
            }
            string path = this.dataPath + "/" + this.curDate.Year.ToString() + "/" + this.curDate.Month.ToString();
            if (!Directory.Exists(path))
            {
                Console.WriteLine("Trade数据目录不存在!");
                return(false);
            }
            string dateString = this.curDate.ToString("yyyy-MM-dd");
            List <AnalysisItem> analysisList = new List <AnalysisItem>();
            DataFile            file         = null;
            try {
                file = DataFile.Open(path);
                foreach (Instrument inst in InstrumentManager.Instruments)
                {
                    if (inst.SecurityDesc.IndexOf("B") >= 0)
                    {
                        continue;                                                       //除去B股
                    }
                    if (inst.SecurityType == "IDX")
                    {
                        continue;                                              //除去指数
                    }
                    FileSeries dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily);
                    if (dailySeries == null || dailySeries.Count <= 0)
                    {
                        continue;
                    }
                    int i = dailySeries.IndexOf(this.curDate);
                    if (i < 0)
                    {
                        continue;                       //没有日线,当日不开盘
                    }
                    i = i - 10 > 0?i - 10:0;            //往前推10个交易日
                    DateTime        firstDate = dailySeries[i].DateTime;
                    ISeriesObject[] dailys    = dailySeries.GetArray(firstDate, this.curDate);
                    int             k         = dailys.Length - 1;
                    GMDaily         gmDaily   = (GMDaily)dailys[k];

                    if (gmDaily.Close / gmDaily.LastClose > 1.095)                  //今日涨停
                    {
                        AnalysisItem ai = new AnalysisItem();
                        ai.Date   = gmDaily.DateTime;
                        ai.Symbol = inst.Symbol;
                        GMSDK.ShareIndex si = null;
                        //先读取当日股本,如果没有,则读取最近股本。一般情况下当日股本与最近股本相同,除非当天正好是除权日
                        List <GMSDK.ShareIndex> shareIndexList = provider.MdApi.GetShareIndex(ai.Symbol, dateString, dateString);
                        if (shareIndexList.Count <= 0)
                        {
                            shareIndexList = provider.MdApi.GetLastShareIndex(ai.Symbol);
                        }
                        if (shareIndexList.Count > 0)
                        {
                            si = shareIndexList[0];
                        }
                        if (si != null)
                        {
                            ai.FlowAShare = si.flow_a_share;
                        }
                        else
                        {
                            ai.FlowAShare = 0.0;
                        }

                        ai.LastClose = gmDaily.LastClose;
                        ai.Open      = gmDaily.Open;
                        ai.High      = gmDaily.High;
                        ai.Low       = gmDaily.Low;
                        ai.Close     = gmDaily.Close;
                        //换手率
                        if (ai.FlowAShare > 0)
                        {
                            ai.TurnoverRate = gmDaily.Volume / ai.FlowAShare * 100;
                        }
                        else
                        {
                            ai.TurnoverRate = 0.0;
                        }
                        //当日量与前5日均量比
                        int    m      = k - 1;
                        double sumVol = 0.0;
                        while (m >= 0 && m >= k - 5)
                        {
                            sumVol += ((Daily)dailys[m]).Volume;
                            m--;
                        }
                        double last5AvgVol = sumVol / (k - m - 1);
                        ai.VolDivLast5AvgVol = gmDaily.Volume / last5AvgVol;
                        //计算封板时间、时长等
                        bool   flag = false;
                        string name = inst.Symbol + ".Trade";
                        if (!file.Series.Contains(name))
                        {
                            continue;
                        }
                        FileSeries      series = file.Series[name];
                        ISeriesObject[] trades = series.GetArray(gmDaily.DateTime, gmDaily.DateTime.AddDays(1));
                        if (trades.Length <= 0)
                        {
                            continue;
                        }
                        GMTrade gmLastTrade = (GMTrade)trades[trades.Length - 1];                    //最后一笔交易数据
                        if (gmLastTrade.Price < gmLastTrade.UpperLimit)
                        {
                            continue;
                        }
                        GMTrade  lastGMTrade = null;
                        DateTime openTimeAM  = gmDaily.Date.Add(new TimeSpan(9, 25, 0));
                        DateTime openTimePM  = gmDaily.Date.Add(new TimeSpan(13, 0, 0));
                        foreach (ISeriesObject aTrade in trades)
                        {
                            GMTrade gmTrade = (GMTrade)aTrade;
                            if (!flag && gmTrade.Price == gmTrade.UpperLimit && gmTrade.DateTime >= openTimeAM)
                            {
                                ai.WhenUpLimit = gmTrade.DateTime - ai.Date;
                                flag           = true;
                            }
                            if (flag && gmTrade.Price < gmTrade.UpperLimit)
                            {
                                ai.UpLimitBreaked = true;
                            }
                            //将下午13:00之后的每笔时间减去90分钟,这样便于计算封涨停的时长
                            if (gmTrade.DateTime >= openTimePM)
                            {
                                gmTrade.DateTime = gmTrade.DateTime.AddMinutes(-90);
                            }
                            if (flag && lastGMTrade != null && gmTrade.Price == gmTrade.UpperLimit && lastGMTrade.Price == gmTrade.UpperLimit)
                            {
                                ai.HowLongUpLimit += gmTrade.DateTime - lastGMTrade.DateTime;
                            }
                            if (flag)
                            {
                                lastGMTrade = gmTrade;
                            }
                        }
                        //封成比
                        name = inst.Symbol + ".Quote";
                        if (!file.Series.Contains(name))
                        {
                            continue;
                        }
                        series = file.Series[name];
                        ISeriesObject[] quotes = series.GetArray(gmDaily.DateTime, gmDaily.DateTime.AddDays(1));
                        if (quotes.Length <= 0)
                        {
                            continue;
                        }
                        GMQuote gmLastQuote = (GMQuote)quotes[quotes.Length - 1];                    //最后一刻报价数据
                        ai.BidSizeDivVol = (double)gmLastQuote.BidSize / gmDaily.Volume;

                        if (k > 0)
                        {
                            GMDaily lastGMDaily = (GMDaily)dailys[k - 1];
                            if (lastGMDaily.Close / lastGMDaily.LastClose >= 1.099)
                            {
                                ai.LastUpLimited = true;
                            }
                        }
                        if (flag)
                        {
                            analysisList.Add(ai);
                        }
                    }
                }
            }catch (Exception ex) {
                throw ex;
            }finally{
                file.Close();
            }
            Console.WriteLine("今日共有{0}只涨停。", analysisList.Count);
            foreach (AnalysisItem ai in analysisList)
            {
                Console.WriteLine(@"Date:{0},Symbol:{1},昨收价:{2},昨日是否涨停:{3},开盘价:{4},
					最高价:{5},最低价:{6},收盘价:{7},涨停时间:{8},是否开板:{9},涨停时长:{10},
					成交量与昨日5日均量比:{11},收盘买一量与成交量比:{12},流通A股:{13},换手率:{14}"                    ,
                                  ai.Date, ai.Symbol, ai.LastClose, ai.LastUpLimited, ai.Open, ai.High, ai.Low, ai.Close,
                                  ai.WhenUpLimit, ai.UpLimitBreaked, ai.HowLongUpLimit, ai.VolDivLast5AvgVol, ai.BidSizeDivVol,
                                  ai.FlowAShare, ai.TurnoverRate);
            }

            MongoClient   client   = new MongoClient("mongodb://localhost:27017");
            MongoServer   server   = client.GetServer();
            MongoDatabase database = server.GetDatabase("finance");

            MongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("UpLimitAnalysis");
            foreach (AnalysisItem ai in analysisList)
            {
                BsonElement[] eleArray = new BsonElement[2];
                eleArray[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd"));
                eleArray[1] = new BsonElement("symbol", ai.Symbol);
                QueryDocument query = new QueryDocument(eleArray);
                collection.Remove(query);
                BsonElement[] eleArray1 = new BsonElement[19];
                eleArray1[0]  = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd"));
                eleArray1[1]  = new BsonElement("symbol", ai.Symbol);
                eleArray1[2]  = new BsonElement("flowashare", ai.FlowAShare);
                eleArray1[3]  = new BsonElement("lastclose", ai.LastClose);
                eleArray1[4]  = new BsonElement("lastuplimited", ai.LastUpLimited);
                eleArray1[5]  = new BsonElement("open", ai.Open);
                eleArray1[6]  = new BsonElement("high", ai.High);
                eleArray1[7]  = new BsonElement("low", ai.Low);
                eleArray1[8]  = new BsonElement("close", ai.Close);
                eleArray1[9]  = new BsonElement("whenuplimit", ai.WhenUpLimit.ToString());
                eleArray1[10] = new BsonElement("uplimitbreaked", ai.UpLimitBreaked);
                eleArray1[11] = new BsonElement("howlonguplimit", ai.HowLongUpLimit.ToString());
                eleArray1[12] = new BsonElement("turnoverrate", ai.TurnoverRate);
                eleArray1[13] = new BsonElement("voldivlast5avgvol", ai.VolDivLast5AvgVol);
                eleArray1[14] = new BsonElement("bidsizedivvol", ai.BidSizeDivVol);
                BsonDocument insert = new BsonDocument(eleArray1);
                collection.Insert(insert);
            }
            return(true);
        }catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
        return(false);
    }
예제 #10
0
    static void Main(string[] args)
    {
        // TO DO: Add your code here
        string username = "******";
        string password = "******";
        int    ret      = _md.Init(username, password, GMSDK.MDMode.MD_MODE_NULL, "", "", "");

        if (ret != 0)
        {
            string msg = _md.StrError(ret);
            Console.WriteLine(msg);
            return;
        }
        string   dataPath        = "e:/QDData";
        DateTime beginDate       = new System.DateTime(2017, 4, 10);
        DateTime endDate         = new System.DateTime(2017, 4, 17);
        string   beginDateString = beginDate.ToString("yyyy-MM-dd");
        string   endDateString   = endDate.ToString("yyyy-MM-dd");
        Dictionary <string, Dictionary <string, double> > shareDict = new Dictionary <string, Dictionary <string, double> >();//存放股本
        List <AnalysisItem> analysisList = new List <AnalysisItem>();

        foreach (Instrument inst in InstrumentManager.Instruments)
        {
            if (inst.SecurityDesc.IndexOf("B") >= 0)
            {
                continue;                                               //除去B股
            }
            FileSeries dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily);
            if (dailySeries == null || dailySeries.Count <= 0)
            {
                continue;
            }
            int      i;
            DateTime firstDate = beginDate;
            do
            {
                i         = dailySeries.IndexOf(firstDate);
                firstDate = firstDate.AddDays(1);
            }while(i < 0 && firstDate < endDate);
            if (i < 0)
            {
                continue;
            }
            i         = i - 5 > 0?i - 5:0;
            firstDate = dailySeries[i].DateTime;
            ISeriesObject[] dailys = dailySeries.GetArray(firstDate, endDate);

            for (int k = 0; k < dailys.Length; k++)
            {
                GMDaily gmDaily = (GMDaily)dailys[k];
                if (gmDaily.DateTime >= beginDate && gmDaily.DateTime < endDate)
                {
                    if (gmDaily.Close / gmDaily.LastClose > 1.095)                  //今日涨停
                    {
                        AnalysisItem ai = new AnalysisItem();
                        ai.Date   = gmDaily.DateTime;
                        ai.Symbol = inst.Symbol;
                        Dictionary <string, double> sd = null;
                        if (!shareDict.TryGetValue(inst.Symbol, out sd))
                        {
                            sd = new Dictionary <string, double>();
                            List <GMSDK.ShareIndex> shareIndexList = _md.GetShareIndex(inst.Symbol, beginDateString, endDateString);
                            foreach (GMSDK.ShareIndex si in shareIndexList)
                            {
                                sd.Add(si.pub_date, si.flow_a_share);
                            }
                            shareDict.Add(inst.Symbol, sd);
                        }
                        sd.TryGetValue(ai.Date.ToString("yyyy-MM-dd"), out ai.FlowAShare);
                        ai.LastClose = gmDaily.LastClose;
                        ai.Open      = gmDaily.Open;
                        ai.High      = gmDaily.High;
                        ai.Low       = gmDaily.Low;
                        ai.Close     = gmDaily.Close;

                        if (ai.FlowAShare > 0)
                        {
                            ai.TurnoverRate = gmDaily.Volume / ai.FlowAShare * 100;
                        }
                        int    m      = k - 1;
                        double sumVol = 0.0;
                        while (m >= 0 && m >= k - 5)
                        {
                            sumVol += ((Daily)dailys[m]).Volume;
                            m--;
                        }
                        double last5AvgVol = sumVol / (k - m - 1);
                        ai.VolDivLast5AvgVol = gmDaily.Volume / last5AvgVol;
                        string path = dataPath + "/" + gmDaily.DateTime.Year.ToString() + "/" + gmDaily.DateTime.Month.ToString();
                        if (!Directory.Exists(path))
                        {
                            Console.WriteLine("Trade数据目录不存在!");
                            return;
                        }
                        DataFile file = null;
                        bool     flag = false;
                        try {
                            file = DataFile.Open(path);
                            string name = inst.Symbol + ".Trade";
                            if (!file.Series.Contains(name))
                            {
                                continue;
                            }
                            FileSeries      series = file.Series[name];
                            ISeriesObject[] trades = series.GetArray(gmDaily.DateTime, gmDaily.DateTime.AddDays(1));
                            if (trades.Length <= 0)
                            {
                                continue;
                            }
                            GMTrade gmLastTrade = (GMTrade)trades[trades.Length - 1];                        //最后一笔交易数据
                            if (gmLastTrade.Price < gmLastTrade.UpperLimit)
                            {
                                continue;
                            }
                            GMTrade  lastGMTrade = null;
                            DateTime openTimeAM  = gmDaily.Date.Add(new TimeSpan(9, 25, 0));
                            DateTime openTimePM  = gmDaily.Date.Add(new TimeSpan(13, 0, 0));
                            foreach (ISeriesObject aTrade in trades)
                            {
                                GMTrade gmTrade = (GMTrade)aTrade;
                                if (!flag && gmTrade.Price == gmTrade.UpperLimit && gmTrade.DateTime >= openTimeAM)
                                {
                                    ai.WhenUpLimit = gmTrade.DateTime - ai.Date;
                                    flag           = true;
                                }
                                if (flag && gmTrade.Price < gmTrade.UpperLimit)
                                {
                                    ai.UpLimitBreaked = true;
                                }
                                //将下午13:00之后的每笔时间减去90分钟,这样便于计算封涨停的时长
                                if (gmTrade.DateTime >= openTimePM)
                                {
                                    gmTrade.DateTime = gmTrade.DateTime.AddMinutes(-90);
                                }
                                if (flag && lastGMTrade != null && gmTrade.Price == gmTrade.UpperLimit && lastGMTrade.Price == gmTrade.UpperLimit)
                                {
                                    ai.HowLongUpLimit += gmTrade.DateTime - lastGMTrade.DateTime;
                                }
                                if (flag)
                                {
                                    lastGMTrade = gmTrade;
                                }
                            }
                            //封成比
                            name = inst.Symbol + ".Quote";
                            if (!file.Series.Contains(name))
                            {
                                continue;
                            }
                            series = file.Series[name];
                            ISeriesObject[] quotes = series.GetArray(gmDaily.DateTime, gmDaily.DateTime.AddDays(1));
                            if (quotes.Length <= 0)
                            {
                                continue;
                            }
                            GMQuote gmLastQuote = (GMQuote)quotes[quotes.Length - 1];                        //最后一笔报价数据
                            ai.BidSizeDivVol = (double)gmLastQuote.BidSize / gmDaily.Volume;
                        }catch (Exception ex) {
                            Console.WriteLine(ex.Message);
                        }finally{
                            file.Close();
                        }
                        if (k > 0)
                        {
                            GMDaily lastGMDaily = (GMDaily)dailys[k - 1];
                            if (lastGMDaily.Close / lastGMDaily.LastClose > 1.098)
                            {
                                ai.LastUpLimited = true;
                            }
                        }
                        if (k < dailys.Length - 1)
                        {
                            GMDaily nextGMDaily = (GMDaily)dailys[k + 1];
                            ai.NextOpen  = nextGMDaily.Open;
                            ai.NextHigh  = nextGMDaily.High;
                            ai.NextLow   = nextGMDaily.Low;
                            ai.NextClose = nextGMDaily.Close;
                        }
                        if (flag)
                        {
                            analysisList.Add(ai);
                        }
                    }
                }
            }
        }
        Console.WriteLine(analysisList.Count);
        analysisList.Sort(delegate(AnalysisItem ai1, AnalysisItem ai2){
            return(ai1.Date.CompareTo(ai2.Date));
        });
        foreach (AnalysisItem ai in analysisList)
        {
            Console.WriteLine(@"Date:{0},Symbol:{1},昨收价:{2},昨日是否涨停:{3},开盘价:{4},
			最高价:{5},最低价:{6},收盘价:{7},涨停时间:{8},是否开板:{9},涨停时长:{10},
			成交量与昨日5日均量比:{11},收盘买一量与成交量比:{12},
				明日开盘价:{13},明日最高价:{14},明日最低价:{15},明日收盘价:{16},流通A股:{17},换手率:{18}"                ,
                              ai.Date, ai.Symbol, ai.LastClose, ai.LastUpLimited, ai.Open, ai.High, ai.Low, ai.Close,
                              ai.WhenUpLimit, ai.UpLimitBreaked, ai.HowLongUpLimit, ai.VolDivLast5AvgVol, ai.BidSizeDivVol,
                              ai.NextOpen, ai.NextHigh, ai.NextLow, ai.NextClose, ai.FlowAShare, ai.TurnoverRate);
        }

        MongoClient   client   = new MongoClient("mongodb://localhost:27017");
        MongoServer   server   = client.GetServer();
        MongoDatabase database = server.GetDatabase("finance");

        MongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("UpLimitAnalysis");

        foreach (AnalysisItem ai in analysisList)
        {
            BsonElement[] eleArray = new BsonElement[2];
            eleArray[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd"));
            eleArray[1] = new BsonElement("symbol", ai.Symbol);
            QueryDocument query = new QueryDocument(eleArray);
            collection.Remove(query);
            BsonElement[] eleArray1 = new BsonElement[19];
            eleArray1[0]  = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd"));
            eleArray1[1]  = new BsonElement("symbol", ai.Symbol);
            eleArray1[2]  = new BsonElement("flowashare", ai.FlowAShare);
            eleArray1[3]  = new BsonElement("lastclose", ai.LastClose);
            eleArray1[4]  = new BsonElement("lastuplimited", ai.LastUpLimited);
            eleArray1[5]  = new BsonElement("open", ai.Open);
            eleArray1[6]  = new BsonElement("high", ai.High);
            eleArray1[7]  = new BsonElement("low", ai.Low);
            eleArray1[8]  = new BsonElement("close", ai.Close);
            eleArray1[9]  = new BsonElement("whenuplimit", ai.WhenUpLimit.ToString());
            eleArray1[10] = new BsonElement("uplimitbreaked", ai.UpLimitBreaked);
            eleArray1[11] = new BsonElement("howlonguplimit", ai.HowLongUpLimit.ToString());
            eleArray1[12] = new BsonElement("turnoverrate", ai.TurnoverRate);
            eleArray1[13] = new BsonElement("voldivlast5avgvol", ai.VolDivLast5AvgVol);
            eleArray1[14] = new BsonElement("bidsizedivvol", ai.BidSizeDivVol);
            eleArray1[15] = new BsonElement("nextopen", ai.NextOpen);
            eleArray1[16] = new BsonElement("nexthigh", ai.NextHigh);
            eleArray1[17] = new BsonElement("nextlow", ai.NextLow);
            eleArray1[18] = new BsonElement("nextclose", ai.NextClose);
            BsonDocument insert = new BsonDocument(eleArray1);
            collection.Insert(insert);
        }
    }