Ejemplo n.º 1
0
def download_dd_data(start=None):
    '''
    获取大单数据
    '''
    conn = db.get_dd_data_db()
    start = start
    if start is None:
        start = utils.today_last_year(1)
    for code in get_all_stock_code():

        end = datetime.today().date()
        while start < end:
            date = end.strftime('%Y-%m-%d')
            df = ts.get_sina_dd(code=code, date=date, vol=500)
            if df is not None:
                df.insert(0, 'code', code)
                try:
                    sql_df = df.loc[:, :]
                    sql.to_sql(sql_df,
                               name='dd_data',
                               con=conn,
                               index=True,
                               if_exists='append')
                    log.info('%s,%s dd data download ok.' % (code, start))
                except Exception as e:
                    log.error('download error:%s,%s' % (code, date))
                pass
            start = start + timedelta(days=1)
Ejemplo n.º 2
0
def download_dd_data(start=None):
    '''
    获取大单数据
    '''
    conn = db.get_dd_data_db()
    start = start
    if start is None:
        start = utils.today_last_year(1)
    for code in get_all_stock_code():

        end = datetime.today().date()
        while start < end:
            date = end.strftime('%Y-%m-%d')
            df = ts.get_sina_dd(code=code, date=date, vol=500)
            if df is not None:
                df.insert(0, 'code', code)
                try:
                    sql_df = df.loc[:, :]
                    sql.to_sql(sql_df, name='dd_data', con=conn,
                               index=True, if_exists='append')
                    log.info('%s,%s dd data download ok.' % (code, start))
                except Exception as e:
                    log.error('download error:%s,%s' % (code, date))
                pass
            start = start + timedelta(days=1)
Ejemplo n.º 3
0
    def run(self):
        '''
        strategy goes here
        Returns:
        '''
        # dataFrames = self.datas.values()
        for tick in self.btData.ticks:
            try:
                self.curr_tick = tick
                self.curr_tick_pos += 1
                for df in self.btData.datas.values():
                    row = df.ix[tick]
                    if len(row) > 3 and tick in df.index:
                        order = self.handle_tick(tick=tick, data=df.ix[:tick], row=row)
                        self.acount.update_price_of_position(code=row.code, price=row.close, date=tick)
                        # if self.begin is None:
                        #     self.begin = tick
                        self.end = tick
            except Exception as e:

                if isinstance(e, KeyError):
                    pass
                else:
                    logger.info('error %s' % e)
                    traceback.print_exc()
                # ignore
                pass
Ejemplo n.º 4
0
def download_money_flow_data(num=1000):
    '''
    get money flow from sina finance
    :param num:
    :return:
    '''
    conn = db.get_money_flow_db()
    cur_time = datetime.now()
    for code in get_all_stock_code():
        code = append_stock_perfix(code)
        cost = datetime.now()
        for i in range(retry):
            try:
                url = sina_money_flow_api % (num, code)
                text = request.urlopen(url, timeout=10).read()
                text = text.decode('GBK')
                log.info('url=%s,size=%d, try=%d' % (url, len(text), i))
                if len(text) < 10:
                    continue
                # j = demjson.decode(text, 'utf-8') #json很大的时候效率非常查
                text = text[2:-2]
                j = text.replace('"', '').split('},{')
                head = ['date', 'close', 'chg_r', 'turnover', 'netamount', 'ratio', 'zl_netamount', 'zl_ratio',
                        'cat_ratio']
                # 日期	收盘价	涨跌幅	换手率	净流入   	净流入率	主力净流入	    主力净流入率	行业净流入率
                data = []

                for x in j:
                    m = {}
                    for s in x.split(','):
                        k, v = s.split(':')
                        if '-' == v or 'null' == v:
                            v = '0.0'
                        m[k] = v
                    date = m['opendate']
                    close = float(m['trade'])
                    chg_r = float(m['changeratio'])
                    turnover = float(m['turnover']) / 10000
                    netamount = float(m['netamount']) / 10000
                    ratio = float(m['ratioamount'])
                    zl_netamount = float(m['r0_net']) / 10000
                    zl_ratio = float(m['r0_ratio'])
                    cat_ratio = float(m['cate_ra'])
                    data.append([date, close, chg_r, turnover, netamount, ratio, zl_netamount, zl_ratio, cat_ratio])

                df = pd.DataFrame(data, columns=head)
                log.info('data ok')
                if not df.empty:
                    df.insert(1, 'code', code)
                    sql_df = df.loc[:, :]
                    sql.to_sql(sql_df, name='money_flow', con=conn, index=False, if_exists='append')
                    log.info('%s,%s,%d money flow data download ok.' % (code, str(start), len(sql_df)))
                    break
            except Exception as e:
                log.error('error:code=%s,start=%s,msg=%s' % (code, start, e))
        log.debug('%s,costs:%d s' % (code, (datetime.now() - cost).seconds))
    conn.close()
    log.info('money flow data download complete. cost %d s' % (datetime.now() - cur_time).seconds)

    pass
Ejemplo n.º 5
0
def bench_mark(codes, all_data, strategy=None, **kwargs):
    import matplotlib.pyplot as plt
    import pandas as pd
    import matplotlib
    matplotlib.style.use('ggplot')
    start = kwargs.get('start', '2016-01-01')
    end = kwargs.get('end', '2016-08-01')
    for code in codes:
        try:
            a = AbstrictStrategy(stock_pool=[code], start=start, end=end, data=all_data)
            t = pd.DataFrame(index=a.btData.datas[code].index)
            # print a.btData.datas[code].head()
            # t['c'] = a.btData.datas[code].close
            # t['ma5'] = a.btData.datas[code].ma5
            # t['ma10'] = a.btData.datas[code].ma10
            t['beta'] = a.btData.datas[code].beta
            bs = []
            a.run()
            t['bs'] = 0
            t_times = 0
            if a.acount.history_orders.get_history(code):
                print len(a.acount.history_orders.get_history(code)), 'trade'
                t_times = len(a.acount.history_orders.get_history(code))
                for oh in a.acount.history_orders.get_history(code):
                    logger.info('order#%s' % oh)
                    if oh.date in t.index:
                        if 'buy' == oh.cmd:
                            # t.ix[oh.date]['bs'] = 5
                            t['bs'][oh.date] = 1
                        elif 'sell' == oh.cmd:
                            t['bs'][oh.date] = -1
                        # t.ix[oh.date]['bs']= oh.cmd

                # t.ix['2015-08-07']['bs'] = 10
                # print t.ix['2015-08-07']['bs']
                # print '############################# history assets #####################'
                # for x in a.acount.history_assets:
                #     print x
                # print 'total_profit=', a.acount.history_orders.get_total_profit(code)
                # print a.acount.cash, a.acount.get_market_value(), a.acount.get_assets(), a.acount.get_position_profit()

                logger.info( '~ '+ code +'\t'+ str(a.baseline()) +'\t'+ str((a.acount.get_assets() - a.acount.initial_cash)/a.acount.initial_cash))
                X.append((code, a.baseline(), (a.acount.get_assets() - a.acount.initial_cash)/a.acount.initial_cash))
                # print t.describe()
                # t.plot()
                # plt.show()
        except Exception, e:
            traceback.print_exc()
            pass
Ejemplo n.º 6
0
 def __init__(self, stock_pool=['000001'], **kwargs):
     self.hd = HistoryData()
     self.stock_pool = stock_pool
     start_date = kwargs.get('start')
     end_date = kwargs.get('end')
     if start_date is None or end_date is None:
         raise Exception('no start,end time set')
     logger.info('load data...')
     self.btData = BtData(stock_pool=stock_pool, **kwargs)
     self.btData.indicator()
     logger.info('load data completed')
     self.curr_tick = 0
     self.curr_tick_pos = 0
     self.begin = None
     self.end = None
     if 'acount' in kwargs:
         self.acount = kwargs.get('acount')
     else:
         self.acount = Account(max_position=1., min_position=0., max_position_per_stock=1.)
Ejemplo n.º 7
0
def download_history_data_fq(autype='qfq', startTime=None):
    '''
    获取前复权的历史k线数据
    '''

    conn = db.get_history_data_db('D')
    start = startTime
    if startTime is None:
        start = utils.today_last_year(6)

    for code in get_all_stock_code():
        df = ts.get_h_data(code, start=start, drop_factor=False)
        if df is not None:
            try:
                df.insert(0, 'code', code)
                sql_df = df.loc[:, :]
                sql.to_sql(sql_df, name='history_data_%s' %
                                        autype, con=conn, index=True, if_exists='append')
                log.info('%s,%s history qfq data download ok.' % (code, start))
            except Exception as e:
                log.error('error:code:%s,start:%s' % (code, start))
Ejemplo n.º 8
0
def updata_all_stock_basics():
    '''
    更新所有股票的基本数据
    return
    DataFrame
           code,代码
           name,名称
           industry,细分行业
           area,地区
           pe,市盈率
           outstanding,流通股本
           totals,总股本(万)
           totalAssets,总资产(万)
           liquidAssets,流动资产
           fixedAssets,固定资产
           reserved,公积金
           reservedPerShare,每股公积金
           eps,每股收益
           bvps,每股净资
           pb,市净率
           timeToMarket,上市日期
    '''
    conn = db.get_history_data_db()
    conn.text_factory = str
    retry = 3
    for i in range(retry):
        try:
            df = ts.get_stock_basics()
            if not df.empty:
                sql_df = df.loc[:, :]
                sql.to_sql(sql_df,
                           name='stock_basics',
                           con=conn,
                           index=True,
                           if_exists='replace')
                log.info('all stock basics updated, total size=%d' % len(df))
                break
        except Exception as e:
            log.error(e)
    conn.close()
Ejemplo n.º 9
0
def updata_all_stock_basics():
    '''
    更新所有股票的基本数据
    return
    DataFrame
           code,代码
           name,名称
           industry,细分行业
           area,地区
           pe,市盈率
           outstanding,流通股本
           totals,总股本(万)
           totalAssets,总资产(万)
           liquidAssets,流动资产
           fixedAssets,固定资产
           reserved,公积金
           reservedPerShare,每股公积金
           eps,每股收益
           bvps,每股净资
           pb,市净率
           timeToMarket,上市日期
    '''
    conn = db.get_history_data_db()
    conn.text_factory = str
    retry = 3
    for i in range(retry):
        try:
            df = ts.get_stock_basics()
            if not df.empty:
                sql_df = df.loc[:, :]
                sql.to_sql(sql_df, name='stock_basics', con=conn,
                           index=True, if_exists='replace')
                log.info('all stock basics updated, total size=%d' % len(df))
                break
        except Exception as e:
            log.error(e)
    conn.close()
Ejemplo n.º 10
0
def download_history_data_fq(autype='qfq', startTime=None):
    '''
    获取前复权的历史k线数据
    '''

    conn = db.get_history_data_db('D')
    start = startTime
    if startTime is None:
        start = utils.today_last_year(6)

    for code in get_all_stock_code():
        df = ts.get_h_data(code, start=start, drop_factor=False)
        if df is not None:
            try:
                df.insert(0, 'code', code)
                sql_df = df.loc[:, :]
                sql.to_sql(sql_df,
                           name='history_data_%s' % autype,
                           con=conn,
                           index=True,
                           if_exists='append')
                log.info('%s,%s history qfq data download ok.' % (code, start))
            except Exception as e:
                log.error('error:code:%s,start:%s' % (code, start))
Ejemplo n.º 11
0
 def baseline(self, sync=False):
     '''
     等权买入并持有
     '''
     b = 0.
     e = 0.
     for data in self.btData.datas.values():
         if sync:
             b += data.ix[self.begin].close
             e += data.ix[self.end].close
             logger.info('#SYNC\t%s=>%f,%s=>%f' % (self.begin, data.ix[self.begin].close, self.end, data.ix[self.end].close))
         else:
             b += data.ix[0].close
             e += data.ix[-1].close
             logger.info('#!SYNC\t%s=>%f,%s=>%f' % (data.ix[0].date, data.ix[0].close, data.ix[-1].date, data.ix[-1].close))
     n = len(self.btData.datas)
     logger.info('avg_b=%f, avg_e=%f' % (b/n, e/n))
     return (e/n - b/n) / b/n
Ejemplo n.º 12
0
def download_money_flow_data(num=1000):
    '''
    get money flow from sina finance
    :param num:
    :return:
    '''
    conn = db.get_money_flow_db()
    cur_time = datetime.now()
    for code in get_all_stock_code():
        code = append_stock_perfix(code)
        cost = datetime.now()
        for i in range(retry):
            try:
                url = sina_money_flow_api % (num, code)
                text = request.urlopen(url, timeout=10).read()
                text = text.decode('GBK')
                log.info('url=%s,size=%d, try=%d' % (url, len(text), i))
                if len(text) < 10:
                    continue
                # j = demjson.decode(text, 'utf-8') #json很大的时候效率非常查
                text = text[2:-2]
                j = text.replace('"', '').split('},{')
                head = [
                    'date', 'close', 'chg_r', 'turnover', 'netamount', 'ratio',
                    'zl_netamount', 'zl_ratio', 'cat_ratio'
                ]
                # 日期	收盘价	涨跌幅	换手率	净流入   	净流入率	主力净流入	    主力净流入率	行业净流入率
                data = []

                for x in j:
                    m = {}
                    for s in x.split(','):
                        k, v = s.split(':')
                        if '-' == v or 'null' == v:
                            v = '0.0'
                        m[k] = v
                    date = m['opendate']
                    close = float(m['trade'])
                    chg_r = float(m['changeratio'])
                    turnover = float(m['turnover']) / 10000
                    netamount = float(m['netamount']) / 10000
                    ratio = float(m['ratioamount'])
                    zl_netamount = float(m['r0_net']) / 10000
                    zl_ratio = float(m['r0_ratio'])
                    cat_ratio = float(m['cate_ra'])
                    data.append([
                        date, close, chg_r, turnover, netamount, ratio,
                        zl_netamount, zl_ratio, cat_ratio
                    ])

                df = pd.DataFrame(data, columns=head)
                log.info('data ok')
                if not df.empty:
                    df.insert(1, 'code', code)
                    sql_df = df.loc[:, :]
                    sql.to_sql(sql_df,
                               name='money_flow',
                               con=conn,
                               index=False,
                               if_exists='append')
                    log.info('%s,%s,%d money flow data download ok.' %
                             (code, str(start), len(sql_df)))
                    break
            except Exception as e:
                log.error('error:code=%s,start=%s,msg=%s' % (code, start, e))
        log.debug('%s,costs:%d s' % (code, (datetime.now() - cost).seconds))
    conn.close()
    log.info('money flow data download complete. cost %d s' %
             (datetime.now() - cur_time).seconds)

    pass
Ejemplo n.º 13
0
def download_index_history_data(start=None, end=None, init_run=False):
    '''
    start:开始时间 yyyyMMdd,第一次调用空则取20100101,之后以数据表中最近时间为准
    end:结束时间 yyyyMMdd,空则取当前日期
    '''
    cur_time = datetime.now()
    conn = db.get_history_data_db()
    if init_run:
        start = default_start_time
    if start is None:
        try:
            onerow = conn.execute(config.sql_last_date_index_all).fetchone()
            if onerow is not None:
                start = onerow[0]
                dt = datetime.strptime(start, '%Y-%m-%d') + timedelta(days=1)
                start = datetime.strftime(dt, '%Y%m%d')
            else:
                start = default_start_time
        except Exception as e:
            start = default_start_time

    if end is None:
        end = datetime.today().date().strftime('%Y%m%d')
    print(start, end)
    if int(end) <= int(start):
        return None
    for code in indices.keys():
        for i in range(retry):
            try:
                url = sohu_history_api % (code, start, end)

                text = request.urlopen(url, timeout=10).read()
                text = text.decode('GBK')
                log.info('url=%s,size=%d, try=%d' % (url, len(text), i))
                if len(text) < 20:
                    continue
                j = demjson.decode(text, 'utf-8')
                head = [
                    'date', 'open', 'close', 'chg', 'chg_r', 'low', 'high',
                    'vibration', 'volume', 'amount'
                ]  # 日期    开盘    收盘    涨跌额    涨跌幅    最低    最高    成交量(手)    成交金额(万)
                # 日期	开盘	收盘	涨跌额	涨跌幅	最低	最高	成交量(手)	成交金额(万)	换手率
                data = []
                for x in j[0].get('hq'):
                    date, open, close, change, _, low, high, valume, amount, _ = x
                    chg_r = '%.4f' % (
                        (float(close) - float(open)) / float(open))
                    vibration = '%.4f' % (float(
                        (float(high) - float(low)) / float(open)))
                    # print date, vibration, str(float(vibration))
                    data.append([
                        date,
                        float(open),
                        float(close),
                        float(change),
                        float(chg_r),
                        float(low),
                        float(high),
                        float(vibration),
                        float(valume),
                        float(amount)
                    ])

                    # sql_str = 'insert OR IGNORE into all_index values(?,?,?,?,?,?,?,?,?,?,?)'
                    # print len(data[0])
                    # conn.executemany(sql_str, data)
                df = pd.DataFrame(data, columns=head)
                if not df.empty:
                    df.insert(1, 'code', code)
                    sql_df = df.loc[:, :]
                    sql.to_sql(sql_df,
                               name='all_index',
                               con=conn,
                               index=False,
                               if_exists='append')
                    log.info('%s,%s index history download ok.' %
                             (code, start))
                    break
            except Exception as e:
                log.error(e)
    conn.close()
    log.info('index history data download complete. cost %d s' %
             (datetime.now() - cur_time).seconds)
Ejemplo n.º 14
0
def download_history_data(ktype='D', start=None, end=None, init_run=False):
    '''
    获取近不复权的历史k线数据
    '''
    if init_run:
        start = default_start_time
    if end == None:
        end = datetime.today().date().strftime('%Y%m%d')

    conn = db.get_history_data_db(ktype)
    cost = 0
    cur_time = datetime.now()

    for code in get_all_stock_code():
        cost = datetime.now()
        if start is None:
            _start = get_laste_update_dt(code)
            if _start is not None:
                dt = datetime.strptime(_start, '%Y-%m-%d') + timedelta(days=1)
                start = datetime.strftime(dt, '%Y%m%d')
            else:
                row = conn.execute(config.sql_last_date_history_data_by_code %
                                   code).fetchone()
                if row is not None:
                    start = row[0]
                    dt = datetime.strptime(start,
                                           '%Y-%m-%d') + timedelta(days=1)
                    start = datetime.strftime(dt, '%Y%m%d')
                else:
                    start = default_start_time
        for i in range(retry):
            try:
                url = sohu_history_api % ('cn_' + code, start, end)
                text = request.urlopen(url, timeout=10).read()
                text = text.decode('GBK')
                log.info('url=%s,size=%d, try=%d' % (url, len(text), i))
                if len(text) < 20:
                    continue
                j = demjson.decode(text, 'utf-8')
                head = [
                    'date', 'open', 'close', 'chg', 'chg_r', 'low', 'high',
                    'vibration', 'volume', 'amount', 'turnover'
                ]  # 日期    开盘    收盘    涨跌额    涨跌幅    最低    最高    成交量(手)    成交金额(万)
                # 日期	开盘	收盘	涨跌额	涨跌幅	最低	最高	成交量(手)	成交金额(万)	换手率
                data = []

                for x in j[0].get('hq'):
                    date, open, close, change, _, low, high, valume, amount, turnover = x
                    if '-' == turnover:
                        turnover = '0.0%'
                    chg_r = '%.4f' % (
                        (float(close) - float(open)) / float(open))
                    vibration = '%.4f' % float(
                        (float(high) - float(low)) / float(open))
                    chg_r = float(chg_r)
                    vibration = float(vibration)
                    data.append([
                        date,
                        float(open),
                        float(close),
                        float(change),
                        float(chg_r),
                        float(low),
                        float(high),
                        float(vibration),
                        float(valume),
                        float(amount),
                        float(turnover[:-1])
                    ])

                df = pd.DataFrame(data, columns=head)
                if not df.empty:
                    df.insert(1, 'code', code)
                    sql_df = df.loc[:, :]
                    sql.to_sql(sql_df,
                               name='history_data',
                               con=conn,
                               index=False,
                               if_exists='append')
                    log.info('%s,%s,%d history data download ok.' %
                             (code, str(start), len(sql_df)))
                    slog.info('%s,%s' % (code, data[0][0]))
                    break
            except Exception as e:
                log.error('error:code=%s,start=%s,msg=%s' % (code, start, e))
                if str(e).find('UNIQUE constraint') > -1:
                    break
        log.debug('%s,costs:%d s' % (code, (datetime.now() - cost).seconds))
    conn.close()
    log.info('history data download complete. cost %d s' %
             (datetime.now() - cur_time).seconds)
Ejemplo n.º 15
0
def download_index_history_data(start=None, end=None, init_run=False):
    '''
    start:开始时间 yyyyMMdd,第一次调用空则取20100101,之后以数据表中最近时间为准
    end:结束时间 yyyyMMdd,空则取当前日期
    '''
    cur_time = datetime.now()
    conn = db.get_history_data_db()
    if init_run:
        start = default_start_time
    if start is None:
        try:
            onerow = conn.execute(config.sql_last_date_index_all).fetchone()
            if onerow is not None:
                start = onerow[0]
                dt = datetime.strptime(start, '%Y-%m-%d') + timedelta(days=1)
                start = datetime.strftime(dt, '%Y%m%d')
            else:
                start = default_start_time
        except Exception as e:
            start = default_start_time

    if end is None:
        end = datetime.today().date().strftime('%Y%m%d')
    print(start, end)
    if int(end) <= int(start):
        return None
    for code in indices.keys():
        for i in range(retry):
            try:
                url = sohu_history_api % (code, start, end)

                text = request.urlopen(url, timeout=10).read()
                text = text.decode('GBK')
                log.info('url=%s,size=%d, try=%d' % (url, len(text), i))
                if len(text) < 20:
                    continue
                j = demjson.decode(text, 'utf-8')
                head = ['date', 'open', 'close', 'chg', 'chg_r', 'low', 'high', 'vibration', 'volume',
                        'amount']  # 日期    开盘    收盘    涨跌额    涨跌幅    最低    最高    成交量(手)    成交金额(万)
                # 日期	开盘	收盘	涨跌额	涨跌幅	最低	最高	成交量(手)	成交金额(万)	换手率
                data = []
                for x in j[0].get('hq'):
                    date, open, close, change, _, low, high, valume, amount, _ = x
                    chg_r = '%.4f' % ((float(close) - float(open)) / float(open))
                    vibration = '%.4f' % (float((float(high) - float(low)) / float(open)))
                    # print date, vibration, str(float(vibration))
                    data.append([date, float(open), float(close), float(change), float(chg_r), float(low), float(high),
                                 float(vibration), float(valume), float(amount)])

                    # sql_str = 'insert OR IGNORE into all_index values(?,?,?,?,?,?,?,?,?,?,?)'
                    # print len(data[0])
                    # conn.executemany(sql_str, data)
                df = pd.DataFrame(data, columns=head)
                if not df.empty:
                    df.insert(1, 'code', code)
                    sql_df = df.loc[:, :]
                    sql.to_sql(sql_df, name='all_index', con=conn, index=False, if_exists='append')
                    log.info('%s,%s index history download ok.' % (code, start))
                    break
            except Exception as e:
                log.error(e)
    conn.close()
    log.info('index history data download complete. cost %d s' % (datetime.now() - cur_time).seconds)
Ejemplo n.º 16
0
        # t['ma10'] = a.btData.datas[code].ma10
        # t['beta'] = a.btData.datas[code].beta
        # t['l_angle'] = a.btData.datas[code].l_angle
        # t['l_intercept'] = a.btData.datas[code].l_intercept
        # t['l_slope'] = a.btData.datas[code].l_slope
        # t['sar'] = a.btData.datas[code].sar
        bs = []
        t['bs'] = 0
        # print t['sar']
        # print t.tail()
        # plt.figure()
        if a.acount.history_orders.get_history(code):
            print len(a.acount.history_orders.get_history(code)), 'trade'
            t_times = len(a.acount.history_orders.get_history(code))
            for oh in a.acount.history_orders.get_history(code):
                logger.info('order#%s' % oh)
                if oh.date in t.index:
                    if 'buy' == oh.cmd:
                        # t.ix[oh.date]['bs'] = 5
                        t['bs'][oh.date] = 1
                    elif 'sell' == oh.cmd:
                        t['bs'][oh.date] = -1
                    # t.ix[oh.date]['bs']= oh.cmd

            # t.ix['2015-08-07']['bs'] = 10
            # print t.ix['2015-08-07']['bs']
            # print '############################# history assets #####################'
            # for x in a.acount.history_assets:
            #     print x
            # print 'total_profit=', a.acount.history_orders.get_total_profit(code)
            # print a.acount.cash, a.acount.get_market_value(), a.acount.get_assets(), a.acount.get_position_profit()
Ejemplo n.º 17
0
def download_history_data(ktype='D', start=None, end=None, init_run=False):
    '''
    获取近不复权的历史k线数据
    '''
    if init_run:
        start = default_start_time
    if end == None:
        end = datetime.today().date().strftime('%Y%m%d')

    conn = db.get_history_data_db(ktype)
    cost = 0
    cur_time = datetime.now()

    for code in get_all_stock_code():
        cost = datetime.now()
        if start is None:
            _start = get_laste_update_dt(code)
            if _start is not None:
                dt = datetime.strptime(_start, '%Y-%m-%d') + timedelta(days=1)
                start = datetime.strftime(dt, '%Y%m%d')
            else:
                row = conn.execute(config.sql_last_date_history_data_by_code % code).fetchone()
                if row is not None:
                    start = row[0]
                    dt = datetime.strptime(start, '%Y-%m-%d') + timedelta(days=1)
                    start = datetime.strftime(dt, '%Y%m%d')
                else:
                    start = default_start_time
        for i in range(retry):
            try:
                url = sohu_history_api % ('cn_' + code, start, end)
                text = request.urlopen(url, timeout=10).read()
                text = text.decode('GBK')
                log.info('url=%s,size=%d, try=%d' % (url, len(text), i))
                if len(text) < 20:
                    continue
                j = demjson.decode(text, 'utf-8')
                head = ['date', 'open', 'close', 'chg', 'chg_r', 'low', 'high', 'vibration', 'volume',
                        'amount', 'turnover']  # 日期    开盘    收盘    涨跌额    涨跌幅    最低    最高    成交量(手)    成交金额(万)
                # 日期	开盘	收盘	涨跌额	涨跌幅	最低	最高	成交量(手)	成交金额(万)	换手率
                data = []

                for x in j[0].get('hq'):
                    date, open, close, change, _, low, high, valume, amount, turnover = x
                    if '-' == turnover:
                        turnover = '0.0%'
                    chg_r = '%.4f' % ((float(close) - float(open)) / float(open))
                    vibration = '%.4f' % float((float(high) - float(low)) / float(open))
                    chg_r = float(chg_r)
                    vibration = float(vibration)
                    data.append(
                        [date, float(open), float(close), float(change), float(chg_r), float(low), float(high),
                         float(vibration), float(valume), float(amount), float(turnover[:-1])])

                df = pd.DataFrame(data, columns=head)
                if not df.empty:
                    df.insert(1, 'code', code)
                    sql_df = df.loc[:, :]
                    sql.to_sql(sql_df, name='history_data', con=conn, index=False, if_exists='append')
                    log.info('%s,%s,%d history data download ok.' % (code, str(start), len(sql_df)))
                    slog.info('%s,%s' % (code, data[0][0]))
                    break
            except Exception as e:
                log.error('error:code=%s,start=%s,msg=%s' % (code, start, e))
                if str(e).find('UNIQUE constraint') > -1:
                    break
        log.debug('%s,costs:%d s' % (code, (datetime.now() - cost).seconds))
    conn.close()
    log.info('history data download complete. cost %d s' % (datetime.now() - cur_time).seconds)