Exemplo n.º 1
0
        public bool getAllSecuritys(ref Dictionary <string, string> dcCodeNameShenzhen, ref Dictionary <string, string> dcCodeNameShanghai)
        {
            StringBuilder sErrInfo            = new StringBuilder(256);
            StringBuilder sResult             = new StringBuilder(1024 * 1024);
            short         MAX_COUNT_PER_TIMES = 1000;
            byte          MARKET_SHENZHEN     = 0;
            byte          MARKET_SHANGHAI     = 1;

            int idConn = ThreadGetDataFromTradeX.connectTDX();

            if (idConn < 0)
            {
                return(false);
            }

            byte[] markets = new byte[] { MARKET_SHENZHEN, MARKET_SHANGHAI };

            foreach (var market in markets)
            {
                short count = 0;
                bool  bRet  = TradeXB.TdxHq_GetSecurityCount(idConn, market, ref count, sErrInfo);
                if (!bRet)
                {
                    return(false);
                }

                int loops = count / MAX_COUNT_PER_TIMES;
                if (count % MAX_COUNT_PER_TIMES > 0)
                {
                    loops++;
                }
                for (short i = 0; i < loops; i++)
                {
                    bRet = TradeXB.TdxHq_GetSecurityList(idConn, market, (short)(i * MAX_COUNT_PER_TIMES), ref count, sResult, sErrInfo);
                    if (bRet)
                    {
                        string[] rows = sResult.ToString().Split(new char[] { '\n' });
                        foreach (var row in rows.Skip(1))
                        {
                            string[] cols = row.Split(new char[] { '\t' });
                            if (cols.Count() >= 8)
                            {
                                if (market == MARKET_SHENZHEN)
                                {
                                    dcCodeNameShenzhen.Add(cols[0], cols[2]);
                                }
                                else if (market == MARKET_SHANGHAI)
                                {
                                    dcCodeNameShanghai.Add(cols[0], cols[2]);
                                }
                            }
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        private void GetTodayXDRFromTradeXB()
        {
            StringBuilder sErrInfo = new StringBuilder(256);
            StringBuilder sResult  = new StringBuilder(1024 * 1024);

            int idConn = 0;

            while (true)
            {
                idConn = ThreadGetDataFromTradeX.connectTDX();
                if (idConn < 0)
                {
                    Console.WriteLine("没有可用的TDX行情服务器!  请检查网络...... 重连......" + DateTime.Now);

                    System.Threading.Thread.Sleep(1000);

                    continue;
                }

                bool   bRet      = true;
                string code      = "";
                short  getCounts = -1;
                while (getCode(ref code, ref getCounts))
                {
                    sResult.Clear();
                    sErrInfo.Clear();

                    byte market = 0;
                    if (code.Substring(0, 1) == "6")
                    {
                        market = 1;
                    }

                    bRet = TradeXB.TdxHq_GetXDXRInfo(idConn, market, code, sResult, sErrInfo);
                    if (bRet)
                    {
                        string[] rows = sResult.ToString().Split(new char[] { '\n' });
                        foreach (var row in rows)
                        {
                            string[] cols = row.Split(new char[] { '\t' });
                            if (cols.Count() >= 8 &&
                                (cols[0] == "0" || cols[0] == "1") &&
                                cols[2] == DateTime.Now.ToString("yyyyMMdd") &&
                                cols[3] == "1")
                            {
                                TodayXDR xdr = new TodayXDR
                                {
                                    presentCash  = Convert.ToDecimal(cols[4]),
                                    presentStock = Convert.ToDecimal(cols[6]),
                                };
                                pushDataTodayXDR(code, xdr);
                            }
                        }
                    }
                    else
                    {
                        Console.Write(0);

                        if (getCounts < 3)
                        {
                            pushCode(code, ++getCounts);
                        }

                        string szErrorInfo = sErrInfo.ToString();
                        Console.WriteLine(szErrorInfo);
                        if (szErrorInfo.Contains("连接断开"))
                        {
                            System.Threading.Thread.Sleep(100);
                            idConn = -1;

                            break;
                        }
                        else
                        {
                            bRet = true;
                        }                //no break
                    }
                }

                if (bRet)
                {
                    if (idConn >= 0)
                    {
                        TradeXB.TdxHq_Disconnect(idConn);
                    }

                    break;
                }
            }

            subtractReferenced();
        }
Exemplo n.º 3
0
        private void Get1MBarsFromTradeXB()
        {
            StringBuilder sErrInfo = new StringBuilder(256);
            StringBuilder sResult  = new StringBuilder(1024 * 1024);

            int idConn = 0;

            while (true)
            {
                idConn = ThreadGetDataFromTradeX.connectTDX();
                if (idConn < 0)
                {
                    Console.WriteLine("没有可用的TDX行情服务器!  请检查网络...... 重连......" + DateTime.Now);

                    System.Threading.Thread.Sleep(1000);

                    continue;
                }

                bool   bRet      = true;
                string code      = "";
                short  getCounts = -1;
                while (getCode(ref code, ref getCounts))
                {
                    sResult.Clear();
                    sErrInfo.Clear();

                    short c      = 240;
                    byte  market = 0;
                    if (code.Substring(0, 1) == "6")
                    {
                        market = 1;
                    }

                    bRet = TradeXB.TdxHq_GetSecurityBars(idConn, 7, market, code, 0, ref c, sResult, sErrInfo);
                    if (bRet)
                    {
                        List <Bar1M> ltTmpData = new List <Bar1M>();
                        DateTime?    lastDay   = null;
                        string[]     rows      = sResult.ToString().Split(new char[] { '\n' });
                        foreach (var row in rows)
                        {
                            string[] cols = row.Split(new char[] { '\t' });
                            if (cols.Count() >= 7 &&
                                cols[0].StartsWith("2"))
                            {
                                Bar1M b1m = new Bar1M()
                                {
                                    dt = Convert.ToDateTime(cols[0]),
                                    O  = Convert.ToDecimal(cols[1]),
                                    C  = Convert.ToDecimal(cols[2]),
                                    H  = Convert.ToDecimal(cols[3]),
                                    L  = Convert.ToDecimal(cols[4]),
                                    V  = Convert.ToDecimal(cols[5]),
                                    vm = Convert.ToDecimal(cols[6]),
                                };
                                lastDay = b1m.dt.Date;
                                ltTmpData.Add(b1m);
                            }
                        }

                        List <Bar1M> ltB1M = new List <Bar1M>();
                        foreach (var b1m in ltTmpData)
                        {
                            if (b1m.dt.Date == lastDay)
                            {
                                ltB1M.Add(b1m);
                            }
                        }
                        if (ltB1M.Count > 0)
                        {
                            pushData1M(code, ltB1M);
                        }
                    }
                    else
                    {
                        Console.Write(0);

                        if (getCounts < 3)
                        {
                            pushCode(code, ++getCounts);
                        }

                        string szErrorInfo = sErrInfo.ToString();
                        Console.WriteLine(szErrorInfo);
                        if (szErrorInfo.Contains("连接断开"))
                        {
                            System.Threading.Thread.Sleep(100);
                            idConn = -1;

                            break;
                        }
                        else
                        {
                            bRet = true;
                        }                //no break
                    }
                }

                if (bRet)
                {
                    if (idConn >= 0)
                    {
                        TradeXB.TdxHq_Disconnect(idConn);
                    }

                    break;
                }
            }

            subtractReferenced();
        }
Exemplo n.º 4
0
        private void GetQuotesFromTradeXB()
        {
            StringBuilder sErrInfo = new StringBuilder(256);
            StringBuilder sResult  = new StringBuilder(1024 * 1024);

            int idConn = -1;

            while (true)
            {
                if (idConn < 0)
                {
                    idConn = ThreadGetDataFromTradeX.connectTDX();
                }
                if (idConn < 0)
                {
                    Console.WriteLine("没有可用的TDX行情服务器!  请检查网络...... 重连......" + DateTime.Now);

                    System.Threading.Thread.Sleep(1000);

                    continue;
                }

                List <string> ltCode      = new List <string>();
                List <byte>   ltMarket    = new List <byte>();
                List <short>  ltGetCounts = new List <short>();

                bool   bRet      = true;
                string code      = "";
                short  getCounts = 0;
                while (getCode(ref code, ref getCounts))
                {
                    ltCode.Add(code);
                    ltMarket.Add(ThreadGetDataFromTradeX.getMarketType(code));
                    ltGetCounts.Add(getCounts);

                    if (ltCode.Count >= ThreadGetDataFromTradeX.MAX_COUNT_PER_TIMES)
                    {
                        break;
                    }
                }

                short count = (short)ltCode.Count;
                if (count > 0)
                {
                    sResult.Clear();
                    sErrInfo.Clear();

                    string[] arryCode   = new string[count];
                    byte[]   arryMarket = new byte[count];
                    ltCode.CopyTo(arryCode);
                    ltMarket.CopyTo(arryMarket);
                    short c = count;
                    bRet = TradeXB.TdxHq_GetSecurityQuotes(idConn, arryMarket, arryCode, ref c, sResult, sErrInfo);
                    if (bRet)
                    {
                        Console.Write(0);

                        string[] rows = sResult.ToString().Split(new char[] { '\n' });
                        foreach (var row in rows)
                        {
                            RealtimeDataCenter.RealtimeData rd = ThreadGetDataFromTradeX.analyseRD(row);
                            if (null != rd)
                            {
                                pushDataQuotes(rd);
                            }
                        }
                    }
                    else
                    {
                        Console.Write(0);

                        for (int i = 0; i < ltCode.Count; i++)
                        {
                            if (ltGetCounts[i] < 3)
                            {
                                pushCode(ltCode[i], ++ltGetCounts[i]);
                            }
                        }

                        string szErrorInfo = sErrInfo.ToString();
                        Console.WriteLine(szErrorInfo);
                        if (szErrorInfo.Contains("连接断开"))
                        {
                            System.Threading.Thread.Sleep(100);
                            idConn = -1;

                            break;
                        }
                        else
                        {
                            bRet = true;
                        }
                    }
                }

                if (bRet && count <= 0)
                {
                    if (idConn >= 0)
                    {
                        TradeXB.TdxHq_Disconnect(idConn);
                    }

                    break;
                }
            }

            subtractReferenced();
        }
Exemplo n.º 5
0
        public void GetData()
        {
            List <RealtimeDataCenter.RealtimeData> ltRd = new List <RealtimeDataCenter.RealtimeData>();
            StringBuilder sErrInfo = new StringBuilder(256);
            StringBuilder sResult  = new StringBuilder(1024 * 1024);

            while (true)
            {
                if (!isTradingTime(DateTime.Now))
                {
                    System.Threading.Thread.Sleep(1000);
                    continue;
                }

                int idConn = ThreadGetDataFromTradeX.connectTDX();
                if (idConn < 0)
                {
                    Console.WriteLine("没有可用的TDX行情服务器!  请检查网络...... 重连......" + DateTime.Now);

                    System.Threading.Thread.Sleep(1000);

                    continue;
                }

                bool bRet = true;
                while (bRet)
                {
                    if (!isTradingTime(DateTime.Now))
                    {
                        bRet = false;
                        TradeXB.TdxHq_Disconnect(idConn);
                        Console.WriteLine("is not trading time");

                        break;
                    }

                    if (this.bChanged)
                    {
                        generateCodeMarketGrp();
                    }

                    foreach (var item in ltCodeMarket)
                    {
                        sResult.Clear();
                        sErrInfo.Clear();
                        short count = item.Item3[0];

                        bRet = TradeXB.TdxHq_GetSecurityQuotes(idConn, item.Item2, item.Item1, ref count, sResult, sErrInfo);
                        if (bRet)
                        {
                            string[] rows = sResult.ToString().Split(new char[] { '\n' });
                            foreach (var row in rows)
                            {
                                RealtimeDataCenter.RealtimeData rd = analyseRD(row);
                                if (null != rd)
                                {
                                    ltRd.Add(rd);
                                }
                            }

                            if (ltRd.Count > 0)
                            {
                                this.rdc.recv(ltRd);
                                ltRd.Clear();
                            }
                        }
                        else
                        {
                            string szErrorInfo = sErrInfo.ToString();
                            Console.WriteLine(szErrorInfo);
                            if (szErrorInfo.Contains("连接断开"))
                            {
                                TradeXB.TdxHq_Disconnect(idConn);

                                break;
                            }
                        }
                    }

                    if (bRet)
                    {
                        System.Threading.Thread.Sleep(800);
                    }
                }

                TradeXB.TdxHq_Disconnect(idConn);
                System.Threading.Thread.Sleep(1000);
            }
        }
Exemplo n.º 6
0
        public RealtimeDataCenter(int thCount = 8, string codes = null, EDataSource ds = EDataSource.sinajs)
        {
#if (!LOCAL_TEST)
            if (thCount <= 0 ||
                thCount > 100)
            {
                throw new Exception("invalid thCount");
            }

            List <string> ltCode = new List <string>();
            if (null == codes)
            {
                /*
                 * StreamReader srCodes = new StreamReader(Program.ramdisk + "\\George\\sql\\objCode.txt", Encoding.Default);
                 * string line;
                 * while (null != (line = srCodes.ReadLine()))
                 * {
                 *  string[] arrCodes = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                 *  foreach (var item in arrCodes)
                 *  { ltCode.Add(item); }
                 * }
                 */
                DBDataContext db = new DBDataContext();
                var           q  = from n in db.tbObjs
                                   select new { n.code };
                foreach (var item in q)
                {
                    ltCode.Add(item.code);
                }
            }
            else
            {
                string[] arrCodes = codes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in arrCodes)
                {
                    ltCode.Add(item);
                }
            }

            if (ltCode.Count <= 0)
            {
                throw new Exception("null code");
            }

            if (ltCode.Count < thCount)
            {
                thCount = ltCode.Count;
            }

            int countPlanPerTh = ltCode.Count / thCount;
            int countCurrentTh = countPlanPerTh;
            for (int i = 0; i < thCount; i++)
            {
                if (i == thCount - 1) //lastTh
                {
                    countCurrentTh = ltCode.Count - countPlanPerTh * (thCount - 1);
                }

                string[] arrCodes = new string[countCurrentTh];
                ltCode.CopyTo(i * countPlanPerTh, arrCodes, 0, countCurrentTh);
                foreach (var item in arrCodes)
                {
                    this.dcCodeIdxThObj.Add(item, i);
                }

                if (EDataSource.sinajs == ds)
                {
                    var to = new ThreadGetDataFromHttp2(arrCodes, this);
                    var th = new Thread(to.GetData)
                    {
                        IsBackground = true
                    };
                    this.ltTh.Add(th);
                    this.ltThObj.Add(to);

                    th.Start();
                }
                else if (EDataSource.tradeX == ds)
                {
                    var to = new ThreadGetDataFromTradeX(arrCodes, this);
                    var th = new Thread(to.GetData)
                    {
                        IsBackground = true
                    };
                    this.ltTh.Add(th);
                    this.ltThObj.Add(to);

                    th.Start();
                }

                System.Threading.Thread.Sleep(10);
            }

            this.thSendRd2Interface = new Thread(this.sendRd2Interface)
            {
                IsBackground = true
            };
            this.thSendRd2Interface.Start();
#endif
        }