示例#1
0
    static void Main(string[] args)
    {
        // TO DO: Add your code here

        GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];

        provider.Connect(10000);
        List <GMSDK.TradeDate> tradeDates = provider.MdApi.GetCalendar("SHSE", "2016-01-01", "2018-12-31");
        List <DateTime>        dates      = new List <DateTime>();
        DateTime startTimeUTC             = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));

        foreach (GMSDK.TradeDate tradeDate in tradeDates)
        {
            dates.Add(startTimeUTC.AddSeconds(tradeDate.utc_time));
        }
        MongoClient   client   = new MongoClient("mongodb://localhost:27017");
        MongoServer   server   = client.GetServer();
        MongoDatabase database = server.GetDatabase("FinanceLast");
        MongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("TradeDates");

        collection.RemoveAll();
        foreach (DateTime date in dates)
        {
            BsonDocument insert = new BsonDocument(new BsonElement("Date", date.ToString("yyyy-MM-dd")));
            collection.Insert(insert);
        }
    }
示例#2
0
 protected override bool doJob()
 {
     Console.WriteLine("正在下载Daily数据...");
     try
     {
         GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
         provider.Connect(10000);
         if (provider.IsConnected)
         {
             string endString = this.curDate.ToString("yyyy-MM-dd");
             foreach (Instrument inst in InstrumentManager.Instruments)
             {
                 string symbol = inst.Symbol;
                 List <GMSDK.DailyBar> gmsdkDailys = provider.MdApi.GetDailyBars(symbol, endString, endString);
                 List <ISeriesObject>  gmDailys    = GSKToGM.ConvertDailys(gmsdkDailys);
                 Console.WriteLine("证券:{0}有{1}笔新日线.", symbol, gmDailys.Count);
                 if (gmDailys.Count > 0)
                 {
                     inst.Add((Daily)(gmDailys[0]));
                 }
             }
             return(true);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     return(false);
 }
示例#3
0
 public DQNTradeJob(string name, DateTime jobDate, Strategy strategy, Job[] needJobs) : base(name, needJobs)
 {
     this.dealDate = jobDate;
     this.dealTime = this.dealDate.AddMinutes(Const.DealTimeMins);
     this.strategy = strategy;
     this.provider = (GMRealTimeProvider)this.strategy.MarketDataProvider;
 }
    protected override bool doJob()
    {
        Console.WriteLine("正在检查证券定义...");

        /*Console.WriteLine("测试作业.........");
         * return true;*/
        try {
            GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
            provider.Connect(10000);
            if (provider.IsConnected)
            {
                List <GMSDK.Instrument> gskInsts = provider.MdApi.GetInstruments("SHSE", 1, 0);
                gskInsts.AddRange(provider.MdApi.GetInstruments("SZSE", 1, 0));
                foreach (GMSDK.Instrument gskInst in gskInsts)
                {
                    if (InstrumentManager.Instruments[gskInst.symbol] == null)
                    {
                        Instrument newInst = new Instrument(gskInst.symbol, "CS");
                        string[]   ss      = gskInst.symbol.Split('.');
                        newInst.SecurityExchange = ss[0];                  //市场
                        newInst.SecurityID       = ss[1];                  //代码
                        newInst.SecurityIDSource = "8";
                        newInst.Save();
                        Console.WriteLine("新证券:{0}已添加.", newInst.Symbol);
                    }
                }
                return(true);
            }
        }catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
        return(false);
    }
 public static void Init(GMRealTimeProvider provider, DateTime dealTime)
 {
     BarFeeder.provider = provider;
     BarFeeder.dealTime = dealTime;
     dealDate           = dealTime.Date;
     dealDateString     = Utils.FormatDate(dealDate);
     dealTimeString     = Utils.FormatTime(dealTime);
 }
示例#6
0
 public RealPlateMonitor(IProvider provider, string eastMoneyPath, PlateMonitorForm monitorForm)
     : base(eastMoneyPath, monitorForm)
 {
     if (provider is GMRealTimeProvider)
     {
         this.gmProvider = (GMRealTimeProvider)provider;
     }
 }
示例#7
0
    protected List <DateTime> GetTradeDates(string market, string beginDate, string endDate)
    {
        GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];

        provider.Connect(10000);
        List <GMSDK.TradeDate> tradeDates = provider.MdApi.GetCalendar(market, beginDate, endDate);
        List <DateTime>        dates      = new List <DateTime>();
        DateTime startTimeUTC             = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));

        foreach (GMSDK.TradeDate tradeDate in tradeDates)
        {
            dates.Add(startTimeUTC.AddSeconds(tradeDate.utc_time));
        }
        return(dates);
    }
示例#8
0
    static void Main(string[] args)
    {
        // TO DO: Add your code here
        bool liveMode = true;
        RandomTradeStrategy myStrategy = new RandomTradeStrategy();

        if (liveMode)
        {
            GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
            myStrategy.StrategyMode         = StrategyMode.Live;
            myStrategy.MarketDataProvider   = provider;
            myStrategy.ExecutionProvider    = ProviderManager.ExecutionSimulator;
            myStrategy.ResetPortfolio       = false;
            myStrategy.SaveOrders           = true; //是否保存委托订单
            myStrategy.Portfolio.Persistent = true; //是否保存投资组合
        }
        else
        {
            myStrategy.ResetPortfolio      = false;     //每次运行是否重置投资组合
            myStrategy.CheckBuyPower       = true;
            myStrategy.SimulationEntryDate = new DateTime(2017, 1, 1);
            myStrategy.SimulationExitDate  = new DateTime(2017, 1, 31);
        }
        myStrategy.SimulationCash = 100000;

        Thread aThread = new Thread(new ThreadStart(myStrategy.Start));

        if (aThread.ThreadState == ThreadState.Unstarted)
        {
            aThread.Start();
        }
        MessageBox.Show("Press OK to stop ATS", "SmartQuant Automation", MessageBoxButtons.OK, MessageBoxIcon.Information);
        myStrategy.Stop();
        aThread.Abort();
        aThread.Join();
    }
示例#9
0
    protected override bool doJob()
    {
        Console.WriteLine("正在下载Daily数据...");
        bool flag = true;

        try {
            GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
            provider.Connect(10000);
            if (provider.IsConnected)
            {
                string endString      = this.curDate.ToString("yyyy-MM-dd");
                string lastTimeString = this.curDate.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
                foreach (Instrument inst in InstrumentManager.Instruments)
                {
                    string symbol = inst.Symbol;
                    if (this.hasDailySymbols.Contains(symbol))
                    {
                        continue;
                    }
                    List <Daily> gmDailys = new List <Daily>();
                    int          i        = 0;
                    bool         hasTrade = true;
                    do
                    {
                        gmDailys = provider.GetDailys(symbol, endString, endString);
                        i++;
                        if (gmDailys.Count > 0)
                        {
                            inst.Add(gmDailys[0]);
                            this.hasDailySymbols.Add(symbol);
                        }
                        else
                        {
                            List <Trade> gmTrades = null;
                            gmTrades = provider.GetLastNTrades(symbol, lastTimeString, 1);
                            if (gmTrades.Count <= 0 || gmTrades[0].DateTime < this.curDate || ((GMTrade)gmTrades[0]).Open <= 0.0)
                            {
                                Console.WriteLine("证券{0}:当天没有交易", symbol);
                                hasTrade = false;
                            }
                            else
                            {
                                Console.WriteLine("证券{0}:当天有交易,但没有读取到日线", symbol);
                            }
                        }
                    }while(gmDailys.Count <= 0 && i < 5 && hasTrade);
                    if (gmDailys.Count <= 0 && hasTrade)
                    {
                        Console.WriteLine("尝试多次({0})获取{1}的最新日线失败", i, symbol);
                        flag = false;
                    }
                }
            }
            else
            {
                Console.WriteLine("GMRealTimeProvider is not connected.");
                flag = false;
            }
        }catch (Exception ex) {
            Console.WriteLine(ex.Message);
            flag = false;
        }
        return(flag);
    }
示例#10
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);
    }
示例#11
0
    protected override bool doJob()
    {
        Console.WriteLine("正在检查Tick数据的完整性...");

        /*Console.WriteLine("测试作业.........");
         * for(int i=0;i<1000;i++) Console.WriteLine("tick{0}",i);
         * return false;*/
        try {
            GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
            provider.Connect(10000);
            if (!provider.IsConnected)
            {
                return(false);
            }
            bool   overwrite = false;
            string path      = this.dataPath + "/" + this.curDate.Year.ToString() + "/" + this.curDate.Month.ToString();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            DateTime beginTime   = this.curDate.Add(new TimeSpan(9, 0, 0));
            DateTime endTime     = this.curDate.Add(new TimeSpan(24, 0, 0));
            string   beginString = beginTime.ToString("yyyy-MM-dd HH:mm:ss");
            string   endString   = endTime.ToString("yyyy-MM-dd HH:mm:ss");
            Console.WriteLine("时间段:" + beginString + "----" + endString);

            DataFile file = DataFile.Open(path);
            try {
                //获取股票代码列表
                foreach (Instrument inst in InstrumentManager.Instruments)
                {
                    //获取当天的日线数据
                    Daily       dailyBar    = null;
                    DailySeries dailySeries = inst.GetDailySeries(this.curDate, this.curDate);
                    if (dailySeries.Count > 0)
                    {
                        dailyBar = dailySeries[0];
                    }
                    else
                    {
                        continue;
                    }

                    string name = inst.Symbol + ".Trade";
                    if (!file.Series.Contains(name))
                    {
                        file.Series.Add(name);
                    }
                    FileSeries      series      = file.Series[name];
                    ISeriesObject[] has         = series.GetArray(beginTime, endTime);
                    bool            needRebuild = false;
                    //检查是否丢失
                    if (has.Length <= 0)
                    {
                        Console.WriteLine("证券:{0},在日期:{1}时丢失Tick数据", inst.Symbol, this.curDate);
                        needRebuild = true;
                    }
                    else
                    {
                        //检查是否不完整
                        GMTrade lastTrade = (GMTrade)has[has.Length - 1];
                        if (lastTrade.TotalSize < dailyBar.Volume)
                        {
                            Console.WriteLine("证券:{0},在日期:{1}时Tick数据不全", inst.Symbol, this.curDate);
                            needRebuild = true;
                        }
                    }
                    if (needRebuild)
                    {
                        List <GMSDK.Tick> gskTicksCache = provider.MdApi.GetTicks(inst.Symbol, beginString, endString);
                        Console.WriteLine(inst.Symbol + "有" + gskTicksCache.Count.ToString() + "笔数据。");
                        if (gskTicksCache.Count > 0)
                        {
                            //添加trades
                            Console.WriteLine("存储Trade数据...");
                            List <ISeriesObject> trades = GSKToGM.ConvertTrades(gskTicksCache);
                            string name1 = inst.Symbol + ".Trade";
                            if (!file.Series.Contains(name1))
                            {
                                file.Series.Add(name1);
                            }
                            FileSeries      series1   = file.Series[name1];
                            ISeriesObject[] hasTrades = series1.GetArray(beginTime, endTime);
                            if (overwrite || hasTrades.Length != trades.Count)
                            {
                                foreach (ISeriesObject aTrade in hasTrades)
                                {
                                    series1.Remove(aTrade.DateTime);
                                }
                                foreach (ISeriesObject trade in trades)
                                {
                                    series1.Add(trade);
                                }
                            }
                            series1.Reindex(Indexer.Daily);
                            //添加quotes
                            if (inst.SecurityType == "IDX")
                            {
                                continue;                                                      //指数没有报价数据
                            }
                            Console.WriteLine("存储Quote数据...");
                            List <ISeriesObject> quotes = GSKToGM.ConvertQuotes(gskTicksCache);
                            string name2 = inst.Symbol + ".Quote";
                            if (!file.Series.Contains(name2))
                            {
                                file.Series.Add(name2);
                            }
                            FileSeries      series2   = file.Series[name2];
                            ISeriesObject[] hasQuotes = series2.GetArray(beginTime, endTime);
                            if (overwrite || hasQuotes.Length != quotes.Count)
                            {
                                foreach (ISeriesObject aQuote in hasQuotes)
                                {
                                    series2.Remove(aQuote.DateTime);
                                }
                                foreach (ISeriesObject quote in quotes)
                                {
                                    series2.Add(quote);
                                }
                            }
                            series2.Reindex(Indexer.Daily);
                        }
                    }
                }
                return(true);
            }catch (Exception ex) {
                throw ex;
            }finally {
                file.Close();
            }
        }catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
        return(false);
    }
 public SaveRandomTradeRecordsJob(string name, DateTime jobDate, Strategy strategy, Job[] needJobs) : base(name, needJobs)
 {
     this.dealDate = jobDate;
     this.strategy = strategy;
     this.provider = (GMRealTimeProvider)this.strategy.MarketDataProvider;
 }
示例#13
0
    static void Main(string[] args)
    {
        // TO DO: Add your code here
        bool                  liveMode    = true;
        PlateMonitorForm      monitorForm = null;
        HotPlateTradeStrategy myStrategy  = null;

        if (liveMode)
        {
            monitorForm = new PlateMonitorForm();
            GMRealTimeProvider provider     = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
            RealPlateMonitor   plateMonitor = new RealPlateMonitor(provider, @"D:\eastmoney\swc8", monitorForm);
            myStrategy = new HotPlateTradeStrategy(plateMonitor);
            myStrategy.StrategyMode       = StrategyMode.Live;
            myStrategy.MarketDataProvider = provider;
            myStrategy.ExecutionProvider  = ProviderManager.ExecutionSimulator;
            myStrategy.ResetPortfolio     = false;
            //myStrategy.SaveOrders=true;//是否保存委托订单
            myStrategy.Portfolio.Persistent = true;          //是否保存投资组合
        }
        else
        {
            myStrategy = new HotPlateTradeStrategy(null);
            myStrategy.ResetPortfolio      = false;     //每次运行是否重置投资组合
            myStrategy.CheckBuyPower       = true;
            myStrategy.SimulationEntryDate = new DateTime(2017, 1, 1);
            myStrategy.SimulationExitDate  = new DateTime(2017, 1, 31);
        }
        myStrategy.SimulationCash = 100000;

        Thread aThread = new Thread(new ThreadStart(myStrategy.Start));

        if (aThread.ThreadState == ThreadState.Unstarted)
        {
            aThread.Start();
        }
        if (monitorForm != null)
        {
            Application.Run(monitorForm);
        }
        else
        {
            MessageBox.Show("Press OK to stop ATS", "SmartQuant Automation", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        myStrategy.Stop();
        aThread.Abort();
        aThread.Join();

        /*GraphShowForm showForm=new GraphShowForm();
         * DateTime curDate=Clock.Now.Date;
         * string symbol="SZSE.300503";
         * Instrument inst=InstrumentManager.Instruments[symbol];
         * ISeriesObject[] dailyBars=Util.GetNDailiesBeforeDate(inst,curDate,60);
         * if (dailyBars.Length>0){
         * Util.AdjustDailys(dailyBars);//向前复权
         * double[] inputs=new double[60];
         * double[] outputs=new double[60];
         * double basePrice=((GMDaily)dailyBars[0]).Amount/((GMDaily)dailyBars[0]).Volume;
         * for(int i=0;i<dailyBars.Length;i++){
         * inputs[i]=i;
         * outputs[i]=((GMDaily)dailyBars[i]).Amount/((GMDaily)dailyBars[i]).Volume;
         * outputs[i]=(outputs[i]/basePrice-1)*100;
         * }
         * PatternRecognition pr=new PatternRecognition(showForm);
         * RecognitionState ret=pr.Recognition(inputs,outputs);
         * Console.WriteLine("slope={0},shape={1},speed={2}",ret.Slope,ret.Shape,ret.Speed);
         * Application.Run(showForm);
         * }else {
         * Console.WriteLine("no data.");
         * }*/
        Console.WriteLine("Strategy Stopped!");
    }
示例#14
0
 public RandomTradeJob(string name, Strategy strategy, Job[] needJobs) : base(name, needJobs)
 {
     this.strategy = strategy;
     this.provider = (GMRealTimeProvider)this.strategy.MarketDataProvider;
 }
        protected override bool doJob()
        {
            Console.WriteLine("正在下载Tick数据...");
            try
            {
                GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"];
                provider.Connect(10000);
                if (!provider.IsConnected)
                {
                    return(false);
                }
                bool   overwrite = false;
                string path      = this.dataPath + "/" + this.curDate.Year.ToString() + "/" + this.curDate.Month.ToString();
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                DateTime beginTime   = this.curDate.Add(new TimeSpan(9, 0, 0));
                DateTime endTime     = this.curDate.Add(new TimeSpan(24, 0, 0));
                string   beginString = beginTime.ToString("yyyy-MM-dd HH:mm:ss");
                string   endString   = endTime.ToString("yyyy-MM-dd HH:mm:ss");
                Console.WriteLine("时间段:" + beginString + "----" + endString);

                DataFile file = DataFile.Open(path);
                try
                {
                    //获取股票代码列表
                    foreach (Instrument inst in InstrumentManager.Instruments)
                    {
                        string symbol = inst.Symbol;
                        Console.WriteLine("正在处理证券" + symbol + "的Tick数据...");
                        List <GMSDK.Tick> gskTicksCache = provider.MdApi.GetTicks(symbol, beginString, endString);
                        Console.WriteLine(symbol + "有" + gskTicksCache.Count.ToString() + "笔数据。");
                        if (gskTicksCache.Count > 0)
                        {
                            //添加trades
                            Console.WriteLine("存储Trade数据...");
                            List <ISeriesObject> trades = GSKToGM.ConvertTrades(gskTicksCache);
                            string name1 = symbol + ".Trade";
                            if (!file.Series.Contains(name1))
                            {
                                file.Series.Add(name1);
                            }
                            FileSeries      series1   = file.Series[name1];
                            ISeriesObject[] hasTrades = series1.GetArray(beginTime, endTime);
                            if (overwrite || hasTrades.Length != trades.Count)
                            {
                                foreach (ISeriesObject aTrade in hasTrades)
                                {
                                    series1.Remove(aTrade.DateTime);
                                }
                                foreach (ISeriesObject trade in trades)
                                {
                                    series1.Add(trade);
                                }
                            }
                            series1.Reindex(Indexer.Daily);
                            //添加quotes
                            if (inst.SecurityType == "IDX")
                            {
                                continue;                            //指数没有报价数据
                            }
                            Console.WriteLine("存储Quote数据...");
                            List <ISeriesObject> quotes = GSKToGM.ConvertQuotes(gskTicksCache);
                            string name2 = symbol + ".Quote";
                            if (!file.Series.Contains(name2))
                            {
                                file.Series.Add(name2);
                            }
                            FileSeries      series2   = file.Series[name2];
                            ISeriesObject[] hasQuotes = series2.GetArray(beginTime, endTime);
                            if (overwrite || hasQuotes.Length != quotes.Count)
                            {
                                foreach (ISeriesObject aQuote in hasQuotes)
                                {
                                    series2.Remove(aQuote.DateTime);
                                }
                                foreach (ISeriesObject quote in quotes)
                                {
                                    series2.Add(quote);
                                }
                            }
                            series2.Reindex(Indexer.Daily);
                            Console.WriteLine("证券:{0}的Tick数据存储完毕.", symbol);
                        }
                    }
                    return(true);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    file.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(false);
        }
示例#16
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);
        }
示例#17
0
 public RandomBehavior(Instrument instrument, Strategy strategy) : base(instrument, strategy)
 {
     this.provider = (GMRealTimeProvider)this.strategy.MarketDataProvider;
 }