def todayTicks(self, retry=3, pause=0.001):
        """
        获取当日分笔明细数据
        Parameters
        ------
            retry : int, 默认 3
                      如遇网络等问题重复执行的次数
            pause : int, 默认 0
                     重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
         return
         -------
            DataFrame or list: [{'time':, 'price':, ...}, ...]      
		    time:时间
		    price:当前价格
		    pchange:涨跌幅
		    change:价格变动
		    volume:成交手
		    amount:成交金额(元)
		    type:买卖类型【买盘、卖盘、中性盘】

        """
        self._data = pd.DataFrame()
        
        if self.__code is None or len(self.__code)!=6 :
            return None
        
        if not Utility.isTradeDay():
            return None
        
        # 不到交易时间
        openTime = time.mktime(time.strptime(Utility.getToday() + ' 09:25:00', '%Y-%m-%d %H:%M:%S'))
        now = time.time()
        if now < openTime:
            return None
        
        symbol = Utility.symbol(self.__code)
        date = Utility.getToday()
        
        for _ in range(retry):
            time.sleep(pause)
            
            try:
                # http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_Transactions.getAllPageTime?date=2018-12-26&symbol=sh600000
                request = self._session.get( cf.TODAY_TICKS_PAGE_URL % (date, symbol), timeout=10 )
                request.encoding = 'gbk'
                text = request.text[1:-1]
                data_dict = Utility.str2Dict(text)
                pages = len(data_dict['detailPages'])
                
                self._writeHead()
                for pNo in range(1, pages+1):
                    # http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradedetail.php?symbol=sh600000&date=2018-12-26&page=1
                    url = cf.TODAY_TICKS_URL % (symbol, date, pNo)
                    self._data = self._data.append(self.__handleTicks(url, cf.TODAY_TICK_COLUMNS, retry, pause), ignore_index=True)
            except Exception as e:
                print(str(e))
            else:
                return self._result()
            
        raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #2
0
 def restrictedLift(self, year=None, month=None, retry=3, pause=0.001):
     """
     获取限售股解禁数据
     Parameters
     --------
     year:年份,默认为当前年
     month:解禁月份,默认为当前月
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     
     Return
     ------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
     code:股票代码
     name:名称
     date:解禁日期
     count:解禁数量(万股)
     ratio:占总盘比率
     """
     self._data = pd.DataFrame()
     
     year = Utility.getYear() if year is None else year
     month = Utility.getMonth() if month is None else month
     
     for _ in range(retry):
         time.sleep(pause)
         
         try:
             # http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=FD&sty=BST&st=3&sr=true&fd=2019&stat=1
             request = self._session.get( cf.RL_URL % (year, month), timeout = 10 )
             if self._PY3:
                 request.encoding = 'utf-8'
             lines = request.text
         except Exception as e:
             print(e)
         else:
             da = lines[3:len(lines)-3]
             list =  []
             for row in da.split('","'):
                 list.append([data for data in row.split(',')])
             self._data = pd.DataFrame(list)
             self._data = self._data[[1, 3, 4, 5, 6]]
             for col in [5, 6]:
                 self._data[col] = self._data[col].astype(float)
             self._data[5] = self._data[5]/10000
             self._data[6] = self._data[6]*100
             self._data[5] = self._data[5].map(cf.FORMAT)
             self._data[6] = self._data[6].map(cf.FORMAT)
             self._data.columns = cf.RL_COLS
             
             return self._result()
         
     raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #3
0
 def cashFlow(self, year, quarter, retry=3, pause=0.001):
     """
     获取现金流量数据
     Parameters
     --------
     year:int 年度 e.g:2014
     quarter:int 季度 :1、2、3、4,只能输入这4个季度
        说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0.001
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题   
     Return
     --------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
         code,代码
         name,名称
         cf_sales,经营现金净流量对销售收入比率
         rateofreturn,资产的经营现金流量回报率
         cf_nm,经营现金净流量与净利润的比率
         cf_liabilities,经营现金净流量对负债比率
         cashflowratio,现金流量比率
     """
     self._data = pd.DataFrame()
     
     if Utility.checkQuarter(year, quarter) is True:
         self._writeHead()
         
         # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/cashflow/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60
         self._data = self.__parsePage(cf.CASHFLOW_URL, year, quarter, 1, cf.CASHFLOW_COLS, pd.DataFrame(), retry, pause)
         if self._data is not None:
             self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6))
             
         return self._result()
Beispiel #4
0
 def operation(self, year, quarter, retry=3, pause=0.001):
     """
     获取营运能力数据
     Parameters
     --------
     year:int 年度 e.g:2014
     quarter:int 季度 :1、2、3、4,只能输入这4个季度
        说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0.001
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题   
     Return
     --------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
         code,代码
         name,名称
         arturnover,应收账款周转率(次)
         arturndays,应收账款周转天数(天)
         inventory_turnover,存货周转率(次)
         inventory_days,存货周转天数(天)
         currentasset_turnover,流动资产周转率(次)
         currentasset_days,流动资产周转天数(天)
     """
     self._data = pd.DataFrame()
     
     if Utility.checkQuarter(year, quarter) is True:
         self._writeHead()
         
         # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/operation/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60
         self._data = self.__parsePage(cf.OPERATION_URL, year, quarter, 1, cf.OPERATION_COLS, pd.DataFrame(), retry, pause)
         if self._data is not None:
             self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6))
             
         return self._result()
Beispiel #5
0
 def __handleMarginTotal(self, dataArr, page, randInt, retry, pause):
     self._writeConsole()
     
     for _ in range(retry):
         time.sleep(pause)
         
         try:
             request = self._session.get(cf.MAR_TOTAL_URL % (page, randInt), timeout=10)
             text = request.text.split('=')[1]
             text = text.replace('{pages:', '{"pages":').replace(',data:', ',"data":').replace('T00:00:00', '').replace('"-"', '0')
             dataDict = Utility.str2Dict(text)
             data = dataDict['data']
             df = pd.DataFrame(data, columns=cf.MAR_TOTAL_COLS)
             
             df['close'] = df['close'].map(cf.FORMAT)
             df['rzyezb'] = df['rzyezb'].astype(float)
             
             dataArr = dataArr.append(df, ignore_index=True)
             if page < dataDict['pages']:
                 dataArr = self.__handleMarginTotal(dataArr, page+1, randInt, retry, pause)
         except Exception as e:
             print(e)
         else:
             return dataArr
         
     raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #6
0
 def forecast(self, year, quarter, retry=3, pause=0.001):
     """
     获取业绩预告数据
     Parameters
     --------
     year:int 年度 e.g:2014
     quarter:int 季度 :1、2、3、4,只能输入这4个季度
        说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0.001
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题   
     Return
     --------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
         code,代码
         name,名称
         type,业绩变动类型【预增、预亏等】
         report_date,发布日期
         pre_eps,上年同期每股收益
         range,业绩变动范围
     """
     self._data = pd.DataFrame()
     
     if Utility.checkQuarter(year, quarter) is True:
         self._writeHead()
         self._data =  self.__handleForecast(year, quarter, 1, pd.DataFrame(), retry, pause)
         self._data = pd.DataFrame(self._data, columns=cf.FORECAST_COLS)
         self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6))
         
         return self._result()
Beispiel #7
0
 def growth(self, year, quarter, retry=3, pause=0.001):
     """
     获取成长能力数据
     Parameters
     --------
     year:int 年度 e.g:2014
     quarter:int 季度 :1、2、3、4,只能输入这4个季度
        说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0.001
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题   
     Return
     --------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
         code,代码
         name,名称
         mbrg,主营业务收入增长率(%)
         nprg,净利润增长率(%)
         nav,净资产增长率
         targ,总资产增长率
         epsg,每股收益增长率
         seg,股东权益增长率
     """
     self._data = pd.DataFrame()
     
     if Utility.checkQuarter(year, quarter) is True:
         self._writeHead()
         
         # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/grow/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60
         self._data = self.__parsePage(cf.GROWTH_URL, year, quarter, 1, cf.GROWTH_COLS, pd.DataFrame(), retry, pause)
         if self._data is not None:
             self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6))
              
         return self._result()
Beispiel #8
0
 def stockProfiles(self, retry=3, pause=0.001):
     """
     获取上市公司基本情况
     Parameters
     --------
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
                 
     Return
     --------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
                code,代码
                name,名称
                city,所在城市
                staff,员工人数
                date,上市日期
                industry,行业分类
                pro_type,产品类型
                main,主营业务
     """
     self._data = pd.DataFrame()
     
     self._writeHead()
     
     date = '%s-12-31' % Utility.getYear()
     self._data = pd.DataFrame()
     
     self._data = self.__handleStockProfiles(self._data, date, 1, retry, pause)
     
     return self._result()
Beispiel #9
0
 def debtPaying(self, year, quarter, retry=3, pause=0.001):
     """
     获取偿债能力数据
     Parameters
     --------
     year:int 年度 e.g:2014
     quarter:int 季度 :1、2、3、4,只能输入这4个季度
        说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0.001
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题   
     Return
     --------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
         code,代码
         name,名称
         currentratio,流动比率
         quickratio,速动比率
         cashratio,现金比率
         icratio,利息支付倍数
         sheqratio,股东权益比率
         adratio,股东权益增长率
     """
     self._data = pd.DataFrame()
     
     if Utility.checkQuarter(year, quarter) is True:
         self._writeHead()
         
         # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/debtpaying/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60
         self._data = self.__parsePage(cf.DEBTPAYING_URL, year, quarter, 1, cf.DEBTPAYING_COLS, pd.DataFrame(), retry, pause)
         if self._data is not None:
             self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6))
             
         return self._result()
Beispiel #10
0
    def historyTicks(self, date=None, retry=3, pause=0.001):
        """
        获取历史分笔明细数据
        Parameters
        ------
            date : string
                    日期 format:YYYY-MM-DD, 默认为前一个交易日
            retry : int, 默认 3
                    如遇网络等问题重复执行的次数
            pause : int, 默认 0
                    重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
         return
         -------
            DataFrame or list: [{'time':, 'price':, ...}, ...]
		time:时间
		price:成交价格
		change:价格变动
		volume:成交手
		amount:成交金额(元)
		type:买卖类型【买盘、卖盘、中性盘】
        """
        self._data = pd.DataFrame()
        
        symbol = Utility.symbol(self.__code)
        date = Utility.lastTradeDate() if date is None else date
            
        try:
            self._writeHead()
            
            page = 1
            while(True):
                # http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradehistory.php?symbol=sh600000&date=2018-12-26&page=1
                # http://market.finance.sina.com.cn/transHis.php?date=2019-01-25&symbol=sh600000&page=1
                url = cf.HISTORY_TICKS_URL % (date, symbol, page)
                tick_data = self.__handleTicks(url, cf.HISTORY_TICK_COLUMNS, retry, pause)
                if tick_data is not None:
                    self._data = self._data.append(tick_data, ignore_index=True)
                    page = page + 1
                else:
                    break
        except Exception as er:
            print(str(er))
        else:
            return self._result()
            
        raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #11
0
 def xrxd(self, date='', retry=3, pause=0.001):
     """
     获取股票除权除息信息
     Parameters
     ------
         date: string
             format:YYYY-MM-DD
         retry: int, 默认 3
             如遇网络等问题重复执行的次数 
         pause : int, 默认 0
             重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
             
     return
     ------
         Dict or None
             nd, 对应年份
             fh_sh, 分红数额
             djr, 股权登记日
             cqr, 除权日
             FHcontent, 除权除息信息
     """
     data = None
     
     if not date:
         date = Utility.getToday()
         
     symbol = Utility.symbol(self.__code)
     
     for _ in range(retry):
         time.sleep(pause)
         
         url = cf.HISTORY_URL % ('fq', 'qfq', symbol, 'day', date, date, 'qfq', Utility.random(17))
         try:
             request = self._session.get(url, timeout=10)
             pattern = re.compile(r'({"nd".+?})')
             result = re.search(pattern, request.text)
             if result:
                 data = eval(result.group(1))
         except Exception as e:
             print(str(e))
             
         return data
             
     raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #12
0
    def bigDeal(self, date=None, vol=400, retry=3, pause=0.001):
        """
        获取大单数据
        Parameters
        ------
            date:string
                      日期 format:YYYY-MM-DD
            retry : int, 默认 3
                      如遇网络等问题重复执行的次数
            pause : int, 默认 0
                     重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
         return
         -------
            DataFrame or list: [{'code':, 'name', ...}, ...]
		    code:代码
		    name:名称
		    time:时间
		    price:当前价格
		    volume:成交手
		    preprice :上一笔价格
		    type:买卖类型【买盘、卖盘、中性盘】
        """
        self._data = pd.DataFrame()
        
        if self.__code is None or len(self.__code) != 6 or date is None:
            return None
        
        symbol = Utility.symbol(self.__code)
        vol = vol*100
        
        for _ in range(retry):
            time.sleep(pause)
            
            try:
                # http://vip.stock.finance.sina.com.cn/quotes_service/view/cn_bill_download.php?symbol=sh600000&num=60&page=1&sort=ticktime&asc=0&volume=40000&amount=0&type=0&day=2018-12-26
                request = self._session.get( cf.SINA_DD % (symbol, vol, date), timeout=10 )
                request.encoding = 'gbk'
                lines = request.text
                if len(lines) < 100:
                    return None
                self._data = pd.read_csv(StringIO(lines), names=cf.SINA_DD_COLS, skiprows=[0])    
                if self._data is not None:
                    self._data['code'] = self._data['code'].map(lambda x: x[2:])
            except Exception as e:
                print(e)
            else:
                return self._result()
            
        raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #13
0
    def __parseExcel(self, year, datatype, lab, column):
        year = Utility.getYear() if year is None else year
        lab = lab.encode('utf-8') if self._PY3 else lab

        try:
            df = pd.read_excel(cf.SHIBOR_DATA_URL %
                               (datatype, year, lab, year),
                               skiprows=[0])
            df.columns = column
            df['date'] = df['date'].map(lambda x: x.date())
            df['date'] = df['date'].astype('datetime64[ns]')
        except Exception as e:
            print(e)
        else:
            return df
Beispiel #14
0
    def countTops(self, days=5, retry=3, pause=0.001):
        """
        获取个股上榜统计数据
        Parameters
        --------
            days:int
                      天数,统计n天以来上榜次数,默认为5天,其余是10、30、60
            retry : int, 默认 3
                         如遇网络等问题重复执行的次数 
            pause : int, 默认 0
                        重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
        Return
        ------
        DataFrame or List: [{'code':, 'name':, ...}, ...]
            code:代码
            name:名称
            count:上榜次数
            bamount:累积购买额(万)     
            samount:累积卖出额(万)
            net:净额(万)
            bcount:买入席位数
            scount:卖出席位数
        """
        self._data = pd.DataFrame()

        if Utility.checkLhbInput(days) is True:
            self._writeHead()

            # http://vip.stock.finance.sina.com.cn/q/go.php/vLHBData/kind/ggtj/index.phtml?last=5&p=1
            self._data = self.__parsePage(kind=cf.LHB_KINDS[0],
                                          last=days,
                                          column=cf.LHB_GGTJ_COLS,
                                          dataArr=pd.DataFrame(),
                                          pageNo=1,
                                          retry=retry,
                                          pause=pause)
            self._data['code'] = self._data['code'].map(
                lambda x: str(x).zfill(6))
            if self._data is not None:
                self._data = self._data.drop_duplicates('code')

            return self._result()
Beispiel #15
0
    def instTops(self, days=5, retry=3, pause=0.001):
        """
        获取机构席位追踪统计数据
        Parameters
        --------
        days:int
                  天数,统计n天以来上榜次数,默认为5天,其余是10、30、60
        retry : int, 默认 3
                     如遇网络等问题重复执行的次数 
        pause : int, 默认 0
                    重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
                    
        Return
        --------
        DataFrame or List: [{'code':, 'name':, ...}, ...]
            code:代码
            name:名称
            bamount:累积买入额(万)
            bcount:买入次数
            samount:累积卖出额(万)
            scount:卖出次数
            net:净额(万)
        """
        self._data = pd.DataFrame()

        if Utility.checkLhbInput(days) is True:
            self._writeHead()

            # http://vip.stock.finance.sina.com.cn/q/go.php/vLHBData/kind/jgzz/index.phtml?last=5&p=1
            self._data = self.__parsePage(kind=cf.LHB_KINDS[2],
                                          last=days,
                                          column=cf.LHB_JGZZ_COLS,
                                          dataArr=pd.DataFrame(),
                                          pageNo=1,
                                          retry=retry,
                                          pause=pause,
                                          drop_column=[2, 3])
            self._data['code'] = self._data['code'].map(
                lambda x: str(x).zfill(6))

            return self._result()
Beispiel #16
0
    def __parsePage(self, cate='', event=0, num=0, retry=3, pause=0.001):
        for _ in range(retry):
            time.sleep(pause)

            try:
                rdInt = Utility.random()
                request = self._session.get(cf.MACRO_URL %
                                            (rdInt, cate, event, num, rdInt),
                                            timeout=10)
                if self._PY3:
                    request.encoding = 'gbk'

                regSym = re.compile(r'\,count:(.*?)\}')
                datastr = regSym.findall(request.text)
                datastr = datastr[0]
                datastr = datastr.split('data:')[1]
            except Exception as e:
                print(e)
            else:
                return datastr

        raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #17
0
 def marginDetailsAllByDate(self, date, retry=3, pause=0.001):
     """
     按日期获取两市融资融券明细列表
     Parameters
     --------
     date  : string
                  选择日期 format:YYYY-MM-DD
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     
     Return
     ------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
         code: 股票代码
         name: 名称
         rzye: 当日融资余额(元)
         rzyezb: 当日融资余额占比(%)
         rzmre: 当日融资买入额(元)
         rzche: 当日融资偿还额(元)
         rzjmre: 当日融资净买入额(元)
         rqye: 当日融券余额(元)
         rqyl: 当日融券余量(股)
         rqmcl: 当日融券卖出量(股)
         rqchl: 当日融券偿还量(股)
         rqjmcl: 当日融券净卖出量(股)
         rzrqye: 当日融资融券余额(元)
         rzrqyecz: 当日融资融券余额差值(元)
     """
     self._data = pd.DataFrame()
     
     self._writeHead()
     
     self._data = self.__handleMarginDetailsAllByDate(self._data, date, 1, Utility.random(8), retry, pause)
     self._data.rename(columns={'scode':'code', 'sname':'name'}, inplace=True)
     
     return self._result()
Beispiel #18
0
 def report(self, year, quarter, retry=3, pause=0.001):
     """
     获取业绩报表数据
     Parameters
     --------
     year:int 年度 e.g:2014
     quarter:int 季度 :1、2、3、4,只能输入这4个季度
        说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0.001
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题   
     Return
     --------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
         code,代码
         name,名称
         eps,每股收益
         eps_yoy,每股收益同比(%)
         bvps,每股净资产
         roe,净资产收益率(%)
         epcf,每股现金流量(元)
         net_profits,净利润(万元)
         profits_yoy,净利润同比(%)
         distrib,分配方案
         report_date,发布日期
     """
     self._data = pd.DataFrame()
     
     if Utility.checkQuarter(year, quarter) is True:
         self._writeHead()
         
         # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/mainindex/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60
         self._data = self.__parsePage(cf.REPORT_URL, year, quarter, 1, cf.REPORT_COLS, pd.DataFrame(), retry, pause, 11)
         if self._data is not None:
             self._data['code'] = self._data['code'].map(lambda x:str(x).zfill(6))
             
         return self._result()
Beispiel #19
0
 def marginTotal(self, retry=3, pause=0.001):
     """
     两市合计融资融券历史数据
     Parameters
     --------
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     
     Return
     ------
     DataFrame or List: [{'date':, 'close':, ...}, ...]
         date: 日期
         close: 沪深300收盘点数
         zdf: 沪深300收盘涨跌幅(%)
         rzye: 融资余额(元)
         rzyezb: 融资余额占比(%)
         rzmre: 融资买入额(元)
         rzche: 融资偿还额(元)
         rzjmre: 融资净买入额(元)
         rqye: 融券余额(元)
         rqyl: 融券余量(股)
         rqmcl: 融券卖出量(股)
         rqchl: 融券偿还量(股)
         rqjmcl: 融券净卖出量(股)
         rzrqye: 融资融券余额(元)
         rzrqyecz: 融资融券余额差值(元)
     """
     self._data = pd.DataFrame()
     
     self._writeHead()
     
     self._data = self.__handleMarginTotal(self._data, 1, Utility.random(8), retry, pause)
     self._data.rename(columns={'tdate':'date'}, inplace=True)
     
     return self._result()
Beispiel #20
0
    def brokerTops(self, days=5, retry=3, pause=0.001):
        """
        获取营业部上榜统计数据
        Parameters
        --------
        days:int
                  天数,统计n天以来上榜次数,默认为5天,其余是10、30、60
        retry : int, 默认 3
                     如遇网络等问题重复执行的次数 
        pause : int, 默认 0
                    重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
        Return
        ---------
        DataFrame or List: [{'broker':, 'count':, ...}, ...]
            broker:营业部名称
            count:上榜次数
            bamount:累积购买额(万)
            bcount:买入席位数
            samount:累积卖出额(万)
            scount:卖出席位数
            top3:买入前三股票
        """
        self._data = pd.DataFrame()

        if Utility.checkLhbInput(days) is True:
            self._writeHead()

            # http://vip.stock.finance.sina.com.cn/q/go.php/vLHBData/kind/yytj/index.phtml?last=5&p=1
            self._data = self.__parsePage(kind=cf.LHB_KINDS[1],
                                          last=days,
                                          column=cf.LHB_YYTJ_COLS,
                                          dataArr=pd.DataFrame(),
                                          pageNo=1,
                                          retry=retry,
                                          pause=pause)

            return self._result()
Beispiel #21
0
 def profit(self, year, quarter, retry=3, pause=0.001):
     """
     获取盈利能力数据
     Parameters
     --------
     year:int 年度 e.g:2014
     quarter:int 季度 :1、2、3、4,只能输入这4个季度
        说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度
     retry : int, 默认 3
                  如遇网络等问题重复执行的次数 
     pause : int, 默认 0.001
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题   
     Return
     --------
     DataFrame or List: [{'code':, 'name':, ...}, ...]
         code,代码
         name,名称
         roe,净资产收益率(%)
         net_profit_ratio,净利率(%)
         gross_profit_rate,毛利率(%)
         net_profits,净利润(万元)
         eps,每股收益
         business_income,营业收入(百万元)
         bips,每股主营业务收入(元)
     """
     self._data = pd.DataFrame()
     
     if Utility.checkQuarter(year, quarter) is True:
         self._writeHead()
         
         # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/profit/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60
         self._data = self.__parsePage(cf.PROFIT_URL, year, quarter, 1, cf.PROFIT_COLS, pd.DataFrame(), retry, pause)
         if self._data is not None:
             self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6))
             
         return self._result()
Beispiel #22
0
    def topList(self, date=None, retry=3, pause=0.001):
        """
        获取每日龙虎榜列表
        Parameters
        --------
        date:string
                    明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
        retry : int, 默认 3
                     如遇网络等问题重复执行的次数 
        pause : int, 默认 0
                    重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
        
        Return
        ------
        DataFrame or List: [{'code':, 'name':, ...}, ...]
            code:代码
            name :名称
            pchange:涨跌幅     
            amount:龙虎榜成交额(万)
            buy:买入额(万)
            bratio:占总成交比例
            sell:卖出额(万)
            sratio :占总成交比例
            reason:上榜原因
            unscramble: 解读
            date  :日期
        """
        self._data = pd.DataFrame()

        if date is None:
            if Utility.getHour() < 18:
                date = Utility.lastTradeDate()
            else:
                date = Utility.getToday()
        else:
            if not Utility.isTradeDay(date):
                return None

        for _ in range(retry):
            time.sleep(pause)

            try:
                # http://data.eastmoney.com/DataCenter_V3/stock2016/TradeDetail/pagesize=200,page=1,sortRule=-1,sortType=,startDate=2019-01-10,endDate=2019-01-10,gpfw=0,js=vardata_tab_1.html
                request = self._session.get(cf.LHB_URL % (date, date),
                                            timeout=10)
                request.encoding = 'gbk'
                text = request.text.split('_1=')[1]
                dataDict = Utility.str2Dict(text)

                self._data = pd.DataFrame(dataDict['data'],
                                          columns=cf.LHB_TMP_COLS)
                self._data.columns = cf.LHB_COLS
                self._data['buy'] = self._data['buy'].astype(float)
                self._data['sell'] = self._data['sell'].astype(float)
                self._data['amount'] = self._data['amount'].astype(float)
                self._data['Turnover'] = self._data['Turnover'].astype(float)
                self._data[
                    'bratio'] = self._data['buy'] / self._data['Turnover']
                self._data[
                    'sratio'] = self._data['sell'] / self._data['Turnover']
                self._data['bratio'] = self._data['bratio'].map(cf.FORMAT)
                self._data['sratio'] = self._data['sratio'].map(cf.FORMAT)
                self._data['date'] = date
                for col in ['amount', 'buy', 'sell']:
                    self._data[col] = self._data[col].astype(float)
                    self._data[col] = self._data[col] / 10000
                    self._data[col] = self._data[col].map(cf.FORMAT)
                self._data = self._data.drop('Turnover', axis=1)
            except:
                pass
            else:
                return self._result()

        raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #23
0
 def realtime(self):
     """
     获取实时交易数据 getting real time quotes data
     用于跟踪交易情况(本次执行的结果-上一次执行的数据)
     Parameters
     ------
         stockdata(code) code : string, array-like object (list, tuple, Series).
     return
     -------
         DataFrame or list: [{'name':, 'open':, ...}, ...]
         	0:name,股票名字
             1:open,今日开盘价
             2:pre_close,昨日收盘价
             3:price,当前价格
             4:high,今日最高价
             5:low,今日最低价
             6:bid,竞买价,即“买一”报价
             7:ask,竞卖价,即“卖一”报价
             8:volumn,成交量 maybe you need do volumn/100
             9:amount,成交金额(元 CNY)
             10:b1_v,委买一(笔数 bid volume)
             11:b1_p,委买一(价格 bid price)
             12:b2_v,“买二”
             13:b2_p,“买二”
             14:b3_v,“买三”
             15:b3_p,“买三”
             16:b4_v,“买四”
             17:b4_p,“买四”
             18:b5_v,“买五”
             19:b5_p,“买五”
             20:a1_v,委卖一(笔数 ask volume)
             21:a1_p,委卖一(价格 ask price)
             ...
             30:date,日期;
             31:time,时间;
     """
     self._data = pd.DataFrame()
     
     symbols_list = ''
     if isinstance(self.__code, list) or isinstance(self.__code, set) or isinstance(self.__code, tuple) or isinstance(self.__code, pd.Series):
         for code in self.__code:
             symbols_list += Utility.symbol(code) + ','
     else:
         symbols_list = Utility.symbol(self.__code)
     symbols_list = symbols_list[:-1] if len(symbols_list) > 8 else symbols_list
     
     # http://hq.sinajs.cn/rn=4879967949085&list=sh600000,sh600004
     request = self._session.get( cf.LIVE_DATA_URL % (Utility.random(), symbols_list), timeout=10 )
     request.encoding = 'gbk'
     reg = re.compile(r'\="(.*?)\";')
     data = reg.findall(request.text)
     regSym = re.compile(r'(?:sh|sz)(.*?)\=')
     syms = regSym.findall(request.text)
     data_list = []
     syms_list = []
     for index, row in enumerate(data):
         if len(row)>1:
             data_list.append([astr for astr in row.split(',')])
             syms_list.append(syms[index])
     if len(syms_list) == 0:
         return None
     
     self._data = pd.DataFrame(data_list, columns=cf.LIVE_DATA_COLS)
     self._data = self._data.drop('s', axis=1)
     self._data['code'] = syms_list
     ls = [cls for cls in self._data.columns if '_v' in cls]
     for txt in ls:
         self._data[txt] = self._data[txt].map(lambda x : x[:-2])
         
     # 价格、成交量等转换为浮点类型
     for col in ['open', 'pre_close', 'price', 'high', 'low', 'bid', 'ask', 'volume','amount',
                 'b1_v', 'b1_p', 'b2_v', 'b2_p', 'b3_v', 'b3_p', 'b4_v', 'b4_p', 'b5_v', 'b5_p',
                 'a1_v', 'a1_p', 'a2_v', 'a2_p', 'a3_v', 'a3_p', 'a4_v', 'a4_p', 'a5_v', 'a5_p']:
         self._data[col] = self._data[col].astype(float)
         
     return self._result()
Beispiel #24
0
 def history(self, start='', end='', ktype='D', autype='qfq', index=False, retry=3, pause=0.001):
     """
     获取股票交易历史数据
     ---------
     Parameters:
       start:string
                   开始日期 format:YYYY-MM-DD 为空时取上市首日
       end:string
                   结束日期 format:YYYY-MM-DD 为空时取最近一个交易日
       autype:string
                   复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
       ktype:string
                   数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
       retry: int, 默认 3
                  如遇网络等问题重复执行的次数 
       pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     return
     -------
       DataFrame or list: [{'date':, 'open':, ...}, ...]
           date 交易日期 (index)
           open 开盘价
           high  最高价
           close 收盘价
           low 最低价
           volume 成交量
           code 股票代码
     """
     self._data = pd.DataFrame()
     
     url = ''
     dataflag = ''
     symbol = cf.INDEX_SYMBOL[self.__code] if index else Utility.symbol(self.__code)
     autype = '' if autype is None else autype
     
     if (start is not None) & (start != ''):
         end = Utility.getToday() if end is None or end == '' else end
         
     if ktype.upper() in cf.K_LABELS:
         fq = autype if autype is not None else ''
         if self.__code[:1] in ('1', '5') or index:
             fq = ''
             
         kline = '' if autype is None else 'fq'
         if ((start is None or start == '') or (end is None or end == '')) or (start == end):
             # http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_dayqfq&param=sh600000,day,,,640,qfq&r=0.73327461855388564
             urls = [cf.HISTORY_URL % (kline, fq, symbol, cf.TT_K_TYPE[ktype.upper()], start, end, fq, Utility.random(17))]
         else:
             years = Utility.ttDates(start, end)
             urls = []
             for year in years:
                 startdate = str(year) + '-01-01'
                 enddate = str(year+1) + '-12-31'
                 # http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_dayqfq2008&param=sh600000,day,2008-01-01,2009-12-31,640,qfq&r=0.73327461855388564
                 url = cf.HISTORY_URL % (kline, fq+str(year), symbol, cf.TT_K_TYPE[ktype.upper()], startdate, enddate, fq, Utility.random(17))
                 urls.append(url)
         dataflag = '%s%s'%(fq, cf.TT_K_TYPE[ktype.upper()])     # qfqday
     elif ktype in cf.K_MIN_LABELS:
         # http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sh600000,m30,,640&_var=m30_today&r=0.5537154641907898
         urls = [cf.HISTORY_MIN_URL % (symbol, ktype, ktype, Utility.random(16))]
         dataflag = 'm%s'%ktype      # m30
     else:
         raise TypeError('ktype input error.')
     
     for url in urls:
         self._data = self._data.append(self.__handleHistory(url, dataflag, symbol, index, ktype, retry, pause), ignore_index=True)
     if ktype not in cf.K_MIN_LABELS:
         if ((start is not None) & (start != '')) & ((end is not None) & (end != '')):
             self._data = self._data[(self._data.date >= start) & (self._data.date <= end)]
     
     return self._result()
 
     raise IOError(cf.NETWORK_URL_ERROR_MSG)
Beispiel #25
0
    def __handleFoundHoldings(self, start, end, pageNo, retry, pause):
        for _ in range(retry):
            time.sleep(pause)
            if pageNo>0:
                    self._writeConsole()
            try:
                # http://quotes.money.163.com/hs/marketdata/service/jjcgph.php?host=/hs/marketdata/service/jjcgph.php&page=0&query=start:2018-06-30;end:2018-09-30&order=desc&count=60&type=query&req=73259
                request = self._session.get( cf.FUND_HOLDS_URL % (pageNo, start, end, Utility.random(5)), timeout=10 )
                if self._PY3:
                    request.encoding = 'utf-8'
                lines = request.text
                lines = lines.replace('--', '0')
                lines = json.loads(lines)
                data = lines['list']
                df = pd.DataFrame(data)
                df = df.drop(['CODE', 'ESYMBOL', 'EXCHANGE', 'NAME', 'RN', 'SHANGQIGUSHU', 'SHANGQISHIZHI', 'SHANGQISHULIANG'], axis=1)
                for col in ['GUSHU', 'GUSHUBIJIAO', 'SHIZHI', 'SCSTC27']:
                    df[col] = df[col].astype(float)
                df['SCSTC27'] = df['SCSTC27']*100
                df['GUSHU'] = df['GUSHU']/10000
                df['GUSHUBIJIAO'] = df['GUSHUBIJIAO']/10000
                df['SHIZHI'] = df['SHIZHI']/10000
                df['GUSHU'] = df['GUSHU'].map(cf.FORMAT)
                df['GUSHUBIJIAO'] = df['GUSHUBIJIAO'].map(cf.FORMAT)
                df['SHIZHI'] = df['SHIZHI'].map(cf.FORMAT)
                df['SCSTC27'] = df['SCSTC27'].map(cf.FORMAT)
                df.columns = cf.FUND_HOLDS_COLS
                df = df[['code', 'name', 'date', 'nums', 'nlast', 'count', 
                            'clast', 'amount', 'ratio']]
            except Exception as e:
                print(e)
            else:
                if pageNo == 0:
                    return df, int(lines['pagecount'])
                else:
                    return df

        raise IOError(cf.NETWORK_URL_ERROR_MSG)