Exemple #1
0
 def validate_date(self, day):
     if day == "TODAY":
         local_date_now = self.get_local_date()
         return local_date_now if self.quick_dict[local_date_now] == 1 else last_tddate()
     if day == "LASTCLOSEDTRADEDAY":
         local_date_now = self.get_local_date()
         local_time_now = self.get_local_time()
         return local_date_now if self.quick_dict[local_date_now] == 1 and local_time_now >= '15:05:00' \
             else last_tddate()
     else:
         return day  # FIXME
Exemple #2
0
def get_tops(date=None, retry_count=3, pause=0.001):
    """
    获取每日龙虎榜列表
    Parameters
    --------
    date:string
                明细数据日期 format:YYYY-MM-DD 默认为空''
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
        code:代码
        name :名称
        pchange:涨跌幅     
        amount:龙虎榜成交额(万)
        buy:买入额(万)
        bratio:占总成交比例
        sell:卖出额(万)
        sratio :占总成交比例
        reason:上榜原因
        date  :日期
    """
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
    else:
        if du.is_holiday(date):
            return None
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(rv.LHB_URL % (ct.P_TYPE["http"], ct.DOMAINS["em"], date))
            text = urlopen(request, timeout=10).read()
            text = text.decode("GBK")
            html = lxml.html.parse(StringIO(text))
            res = html.xpath('//table[@id="dt_1"]')
            if ct.PY3:
                sarr = [etree.tostring(node).decode("utf-8") for node in res]
            else:
                sarr = [etree.tostring(node) for node in res]
            sarr = "".join(sarr)
            df = pd.read_html(sarr)[0]
            df.columns = [i for i in range(1, 12)]
            df = df.apply(_f_rows, axis=1)
            df = df.fillna(method="ffill")
            df = df.drop([1, 4], axis=1)
            df.columns = rv.LHB_COLS
            df = df.drop_duplicates()
            df["code"] = df["code"].astype(int)
            df["code"] = df["code"].map(lambda x: str(x).zfill(6))
            df["date"] = date
        except:
            pass
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Exemple #3
0
def get_tops(date = None):
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate() 
    else:
        if(du.is_holiday(date)):
            return None
    request = Request(rv.LHB_URL%(ct.P_TYPE['http'], ct.DOMAINS['em'], date))
    text = urlopen(request, timeout=10).read()
    text = text.decode('GBK')
    html = lxml.html.parse(StringIO(text))
    res = html.xpath("//table[@id=\"dt_1\"]")
    if ct.PY3:
        sarr = [etree.tostring(node).decode('utf-8') for node in res]
    else:
        sarr = [etree.tostring(node) for node in res]
    sarr = ''.join(sarr)
    df = pd.read_html(sarr)[0]
    df.columns = [i for i in range(1,12)]
    df = df.apply(_f_rows, axis=1)
    df = df.fillna(method='ffill')
    df = df.drop([1, 4], axis=1)
    df.columns = rv.LHB_COLS
    df = df.drop_duplicates()
    df['code'] = df['code'].astype(int)
    df['code'] = df['code'].map(lambda x: str(x).zfill(6))
    df['date'] = date
    return df
Exemple #4
0
def top_detail(code, date=None, retry_count= 3, pause= 0.001):
    """
    根据指定日期,指定股票的龙虎榜交易细节
    Parameters
    --------
	code: string
			指定股票代码
	date: string
			指定日期
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题

    Return
    --------
    code:代码
    name:名称
    bamount:累积买入额(万)
    bcount:买入次数
    samount:累积卖出额(万)
    scount:卖出次数
    net:净额(万)
    """
    if code is None or code == "":
        return None
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today()
    else:
        if(du.is_holiday(date)):
            return None
        time.sleep(pause)
        try:
            request = Request(rv.LHB_SINA_URL2%(ct.P_TYPE['http'], ct.DOMAINS['vsf'], code, date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            text = text.split('details=')[1][2:-2]
            text = eval(text, type('Dummy', (dict,),
                                           dict(__getitem__ = lambda s, n:n))())
            text = json.dumps(text)
            text = json.loads(text)
            buy_df = pd.DataFrame(text['buy'], columns=rv.LHB_TMP_DETAIL_COLS)
            buy_df['type'] = 'buy'
            sell_df = pd.DataFrame(text['sell'], columns=rv.LHB_TMP_DETAIL_COLS)
            sell_df['type'] = 'sell'
            #LHB_DETAIL_COLS = ['code', 'type', 'insCode', 'insName', 'bamount', 'samount', 'net']
            df = sell_df.append(buy_df)
            df.columns = rv.LHB_DETAIL_COLS
            df['bamount'] = df['bamount'].astype(float)
            df['samount'] = df['samount'].astype(float)
            df['net'] = df['net'].astype(float)
            df['date'] = date
        except Exception as e:
            print e
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Exemple #5
0
def get_day_all(date=None):
    """
    获取每日收盘行情
    Parameters:
    -------------
    date:交易日期,格式:YYYY-MM-DD
    
    Return:
    -------------
    DataFrame
    code 代码, name 名称, p_change 涨幅%,
    price 现价, change 涨跌, open 今开, high 最高,
    low 最低, preprice 昨收, pe 市盈(动),
    volratio 量比, turnover 换手%, range 振幅%%,
    volume 总量, selling 内盘, buying 外盘,
    amount 总金额, totals 总股本(万), industry 细分行业,
    area 地区, floats 流通股本(万), fvalues 流通市值,
    abvalues AB股总市值, avgprice 均价, strength 强弱度%,
    activity 活跃度, avgturnover 笔换手, attack 攻击波%,
    interval3 近3月涨幅 ,interval 近6月涨幅
    """
    wdate = du.last_tddate() if date is None else date
    wdate = wdate.replace('-', '')
    if wdate < '20170614':
        return None
    datepre = '' if date is None else wdate[0:4] + wdate[4:6] + '/'
    df = pd.read_csv(ct.ALL_DAY_FILE%(datepre, \
                                      'hq' if date is None else wdate), \
                                      dtype={'code':'object'})
    return df
Exemple #6
0
def get_day_all(date=None):
    """
    获取每日收盘行情
    Parameters:
    -------------
    date:交易日期,格式:YYYY-MM-DD
    
    Return:
    -------------
    DataFrame
    code 代码, name 名称, p_change 涨幅%,
    price 现价, change 涨跌, open 今开, high 最高,
    low 最低, preprice 昨收, pe 市盈(动),
    volratio 量比, turnover 换手%, range 振幅%%,
    volume 总量, selling 内盘, buying 外盘,
    amount 总金额, totals 总股本(万), industry 细分行业,
    area 地区, floats 流通股本(万), fvalues 流通市值,
    abvalues AB股总市值, avgprice 均价, strength 强弱度%,
    activity 活跃度, avgturnover 笔换手, attack 攻击波%,
    interval3 近3月涨幅 ,interval 近6月涨幅
    """
    wdate = du.last_tddate() if date is None else date
    wdate = wdate.replace('-', '')
    if wdate < '20170614':
        return None
    datepre = '' if date is None else wdate[0:4] + wdate[4:6] + '/'
    df = pd.read_csv(ct.ALL_DAY_FILE%(datepre, \
                                      'hq' if date is None else wdate), \
                                      dtype={'code':'object'})
    return df
Exemple #7
0
def get_stock_basics(date=None):
    """
        获取沪深上市公司基本情况
    Parameters
    date:日期YYYY-MM-DD,默认为上一个交易日,目前只能提供2016-08-09之后的历史数据

    Return
    --------
    DataFrame
               code,代码
               name,名称
               industry,细分行业
               area,地区
               pe,市盈率
               outstanding,流通股本
               totals,总股本(万)
               totalAssets,总资产(万)
               liquidAssets,流动资产
               fixedAssets,固定资产
               reserved,公积金
               reservedPerShare,每股公积金
               eps,每股收益
               bvps,每股净资
               pb,市净率
               timeToMarket,上市日期
    """
    wdate = du.last_tddate() if date is None else date
    wdate = wdate.replace('-', '')
    if wdate < '20160809':
        return None
    datepre = '' if date is None else wdate[0:4] + wdate[4:6] + '/'
    request = Request(ct.ALL_STOCK_BASICS_FILE %
                      (datepre, '' if date is None else wdate),
                      headers=ua.get_ua())
    text = urlopen(request, timeout=10).read()
    text = text.decode('GBK')
    text = text.replace('--', '')
    df = pd.read_csv(StringIO(text), dtype={'code': 'object'})
    df = df.set_index('code')
    return df
Exemple #8
0
def get_stock_basics(date=None):
    """
        获取沪深上市公司基本情况
    Parameters
    date:日期YYYY-MM-DD,默认为上一个交易日,目前只能提供2016-08-09之后的历史数据

    Return
    --------
    DataFrame
               code,代码
               name,名称
               industry,细分行业
               area,地区
               pe,市盈率
               outstanding,流通股本
               totals,总股本(万)
               totalAssets,总资产(万)
               liquidAssets,流动资产
               fixedAssets,固定资产
               reserved,公积金
               reservedPerShare,每股公积金
               eps,每股收益
               bvps,每股净资
               pb,市净率
               timeToMarket,上市日期
    """
    wdate = du.last_tddate() if date is None else date
    wdate = wdate.replace('-', '')
    if wdate < '20160809':
        return None
    datepre = '' if date is None else wdate[0:4] + wdate[4:6] + '/'
    request = Request(ct.ALL_STOCK_BASICS_FILE%(datepre, '' if date is None else wdate))
    text = urlopen(request, timeout=10).read()
    text = text.decode('GBK')
    text = text.replace('--', '')
    df = pd.read_csv(StringIO(text), dtype={'code':'object'})
    df = df.set_index('code')
    return df
Exemple #9
0
def top_list(date=None, retry_count=3, pause=0.001):
    """
    获取每日龙虎榜列表
    Parameters
    --------
    date:string
                明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
        code:代码
        name :名称
        pchange:涨跌幅     
        amount:龙虎榜成交额(万)
        buy:买入额(万)
        bratio:占总成交比例
        sell:卖出额(万)
        sratio :占总成交比例
        reason:上榜原因
        date  :日期
    """
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today()
    else:
        if (du.is_holiday(date)):
            return None
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(
                rv.LHB_URL % (ct.P_TYPE['http'], ct.DOMAINS['em'], date, date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            text = text.split('_1=')[1]
            text = eval(
                text,
                type('Dummy', (dict, ), dict(__getitem__=lambda s, n: n))())
            text = json.dumps(text)
            text = json.loads(text)
            df = pd.DataFrame(text['data'], columns=rv.LHB_TMP_COLS)
            df.columns = rv.LHB_COLS
            df['buy'] = df['buy'].astype(float)
            df['sell'] = df['sell'].astype(float)
            df['amount'] = df['amount'].astype(float)
            df['Turnover'] = df['Turnover'].astype(float)
            df['bratio'] = df['buy'] / df['Turnover']
            df['sratio'] = df['sell'] / df['Turnover']
            df['bratio'] = df['bratio'].map(ct.FORMAT)
            df['sratio'] = df['sratio'].map(ct.FORMAT)
            df['date'] = date
            for col in ['amount', 'buy', 'sell']:
                df[col] = df[col].astype(float)
                df[col] = df[col] / 10000
                df[col] = df[col].map(ct.FORMAT)
            df = df.drop('Turnover', axis=1)
        except:
            pass
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Exemple #10
0
def top_list(date = None, retry_count=3, pause=0.001):
    """
    获取每日龙虎榜列表
    Parameters
    --------
    date:string
                明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
        code:代码
        name :名称
        pchange:涨跌幅     
        amount:龙虎榜成交额(万)
        buy:买入额(万)
        bratio:占总成交比例
        sell:卖出额(万)
        sratio :占总成交比例
        reason:上榜原因
        date  :日期
    """
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today() 
    else:
        if(du.is_holiday(date)):
            return None
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(rv.LHB_URL%(ct.P_TYPE['http'], ct.DOMAINS['em'], date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            html = lxml.html.parse(StringIO(text))
            res = html.xpath("//table[@id=\"dt_1\"]")
            if ct.PY3:
                sarr = [etree.tostring(node).decode('utf-8') for node in res]
            else:
                sarr = [etree.tostring(node) for node in res]
            sarr = ''.join(sarr)
            df = pd.read_html(sarr)[0]
            df.columns = [i for i in range(1,12)]
            df = df.apply(_f_rows, axis=1)
            df = df.fillna(method='ffill')
            df = df.drop([1, 4], axis=1)
            df.columns = rv.LHB_COLS
            df = df.drop_duplicates()
            df['code'] = df['code'].astype(int)
            df['code'] = df['code'].map(lambda x: str(x).zfill(6))
            df['date'] = date
        except:
            pass
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Exemple #11
0
def top_list(date = None, retry_count=3, pause=0.001):
    """
    获取每日龙虎榜列表
    Parameters
    --------
    date:string
                明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
        code:代码
        name :名称
        pchange:涨跌幅     
        amount:龙虎榜成交额(万)
        buy:买入额(万)
        bratio:占总成交比例
        sell:卖出额(万)
        sratio :占总成交比例
        reason:上榜原因
        date  :日期
    """
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today() 
    else:
        if(du.is_holiday(date)):
            return None
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(rv.LHB_URL%(ct.P_TYPE['http'], ct.DOMAINS['em'], date, date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            text = text.split('_1=')[1]
            text = eval(text, type('Dummy', (dict,), 
                                           dict(__getitem__ = lambda s, n:n))())
            text = json.dumps(text)
            text = json.loads(text)
            df = pd.DataFrame(text['data'], columns=rv.LHB_TMP_COLS)
            df.columns = rv.LHB_COLS
            df['buy'] = df['buy'].astype(float)
            df['sell'] = df['sell'].astype(float)
            df['amount'] = df['amount'].astype(float)
            df['Turnover'] = df['Turnover'].astype(float)
            df['bratio'] = df['buy'] / df['Turnover']
            df['sratio'] = df['sell'] /df['Turnover']
            df['bratio'] = df['bratio'].map(ct.FORMAT)
            df['sratio'] = df['sratio'].map(ct.FORMAT)
            df['date'] = date
            for col in ['amount', 'buy', 'sell']:
                df[col] = df[col].astype(float)
                df[col] = df[col] / 10000
                df[col] = df[col].map(ct.FORMAT)
            df = df.drop('Turnover', axis=1)
        except:
            pass
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Exemple #12
0
import tushare as ts
import TsExt as te
from datetime import date
from sqlalchemy import create_engine
from tushare.util import dateu as du
import pandas as pd

if __name__ == '__main__':
    engine = create_engine(
        'mysql+pymysql://root:[email protected]/tushare?charset=utf8')
    if date.today() == ts.util.dateu.last_tddate():
        for code in ts.get_stock_basics().index:
            tt = te.stk_get_today_ticks(code)
            print(code + tt)

    df = te.stk_get_today_all(engine=engine)
    pd.io.sql.execute(
        'delete from stk_today_all where date = \'' + du.last_tddate() + '\'',
        engine)
    df.to_sql('stk_today_all', engine, if_exists='append')
Exemple #13
0
 def gen_date_list(self, goal, t='TODAY', in_mkt=False):
     # TODO
     if goal == "T" and t == 'TODAY':
         return [self.get_local_date()] if not in_mkt else [last_tddate()]
Exemple #14
0
from sqlalchemy import create_engine
from tushare.util import dateu as du
import threading
import time
import TsExt as te
from util.util import last_tddate_delta


if __name__ == '__main__':


    engine = create_engine('mysql+pymysql://root:[email protected]/tushare?charset=utf8')

    td = du.today()
    if(du.is_holiday(td) | int(du.get_hour()) < 16):
        dt = du.last_tddate()
    else:
        dt = td
    print(dt)


    """数据表
    stk_lhb_broker_detail   龙虎榜股票营业部买入详细情况
    stk_k_line_data         股票K线数据
    """

    """
    获取股票基本信息
    """

    df = te.stk_get_basics(engine)
Exemple #15
0
from datetime import date, timedelta

# df = ts.get_hist_data('002024')
# df2 = df.sort_index()
# ma50 = ta.MA(df2['close'],timeperiod=50,matype=0)
# v_ma50 = ta.MA(df2['volume'],timeperiod=50,matype=0)
# df2['ma50'] = round(ma50,2)
# df2['vma50'] = round(v_ma50,2)
# df2 = df2.fillna(0)
# print(df2)

tod = date.today()
yed = tod + timedelta(days=-1)

df = ts.get_today_all()
df.set_index(['code'], inplace=True)
df['last_vol'] = 'NaN'
# print(df.dtype)

for stkcode in ts.get_stock_basics().index:
    try:
        print("############" + stkcode + "###################")
        df2 = ts.get_hist_data(stkcode, start=str(yed), end=str(yed))
        df.at[stkcode, 'last_vol'] = df2.at[du.last_tddate(), 'volume']
        # print(df.at[stkcode,'volume'],df.at[stkcode,'last_vol'])
        if ((df.at[stkcode, 'volume'] / df.at[stkcode, 'last_vol']) > 2
                and df.at[stkcode, 'changepercent'] > 0):
            print(df.loc[stkcode])
    except Exception as e:
        print(e)
Exemple #16
0
def stk_get_today_all(engine):
    df = ts.get_today_all()
    df['date'] = du.last_tddate()
    return df
Exemple #17
0
def last_tddate_delta(t):
    tt = du.last_tddate()
    tt2 = datetime.datetime.strptime(tt, '%Y-%m-%d').date()
    tt_delta = tt2 + datetime.timedelta(days=t)
    return tt_delta
Exemple #18
0
def lhb_details(retry_count=3, pause=0.001, code=None, date=None):
    """
	获取最近一个交易日龙虎榜上榜个股的交易详情. 包括买1-5和卖1-5的买入及卖出量,营业部名称.

	可选参数:
	证券代码(code): 指定代码用于只获取该证券的交易详情.省略的话会返回当日所有上榜证券的交易详情
	日期(date): 获取指定日期的交易详情.默认为最近一个交易日

	返回值:
	----------
	code:股票代码
	name:股票名称     
	date:交易日期     
	买1-5营业部名称及买入和卖出额及净额(万)    
	卖1-5营业部名称及买入和卖出额净额(万)    

	"""
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today()
    else:
        if (du.is_holiday(date)):
            return None

    if code is None:
        print('请输入股票代码,比如: lhb_details(000001)')
        raise SystemExit(5)

    lhb_details_url = 'http://vip.stock.finance.sina.com.cn/q/api/jsonp.php/var%20details=/InvestConsultService.getLHBComBSData?symbol=%s&tradedate=%s&type=01'
    text = urlopen(lhb_details_url % (code, date), timeout=10).read()
    text = text.decode('GBK')
    text = text.split('details=')[1]
    text = eval(text,
                type('Dummy', (dict, ), dict(__getitem__=lambda s, n: n))())
    text = json.dumps(text)
    text = json.loads(text)
    ## text['buy'] = list of dicts
    ## text['sell'] = list of dicts

    #for i in range(len(text['buy'])):
    #	for key in text['buy'][i].keys():
    #		print(text['buy'][i][key],end=' ')
    #	print()
    #SYMBOL	sellAmount	buyAmount	comCode	type	netAmount	comName
    #000973 7.6802		2101.4900	80291363 01	2093.8098 浙商证券股份有限公司临安万马路证券营业部
    #000973 0.0000		1777.2700	80348499 01 1777.27 华泰证券股份有限公司浙江分公司
    #000973 317.3780	2069.5400	80127954 01 1752.162 安信证券股份有限公司南昌胜利路证券营业部
    #000973 0.9050		1389.3300	80564863 01 1388.425 华鑫证券有限责任公司郑州普惠路证券营业部
    #000973 39.0354		1239.5100	80034811 01 1200.4746 财通证券股份有限公司绍兴人民中路证券营业部

    print('%4s %6s %10s %10s %10s %s' %
          ('买入榜', '股票代码', '卖出额(万)', '买入额(万)', '买入净额(万)', '营业部名称'))
    for i in range(len(text['buy'])):
        print('%-4d %-8s %-10s %-10s %-12s %s' %(i+1,text['buy'][i]['SYMBOL'],text['buy'][i]['sellAmount'],\
         text['buy'][i]['buyAmount'],text['buy'][i]['netAmount'],text['buy'][i]['comName']))
    #1    000973   7.6802   2101.4900  2093.8098    浙商证券股份有限公司临安万马路证券营业部
    #2    000973   0.0000   1777.2700  1777.27      华泰证券股份有限公司浙江分公司
    #3    000973   317.3780 2069.5400  1752.162     安信证券股份有限公司南昌胜利路证券营业部
    #4    000973   0.9050   1389.3300  1388.425     华鑫证券有限责任公司郑州普惠路证券营业部
    #5    000973   39.0354  1239.5100  1200.4746    财通证券股份有限公司绍兴人民中路证券营业部

    print('%4s %6s %10s %10s %10s %s' %
          ('卖出榜', '股票代码', '卖出额(万)', '买入额(万)', '买入净额(万)', '营业部名称'))
    for i in range(len(text['sell'])):
        print('%-4d %-8s %-10s %-10s %-12s %s' %(i+1,text['sell'][i]['SYMBOL'],text['sell'][i]['sellAmount'],\
         text['sell'][i]['buyAmount'],text['sell'][i]['netAmount'],text['sell'][i]['comName']))