Exemplo n.º 1
0
def calPEHistory(ID, startDate, endDate=None):
    """
    计算某一指数的TTMPE
    :param ID: 长格式的指数代码,如: 000010.SH
    :param startDate:
    :param endDate:
    :return:
    """
    if startDate is None:
        startDate = datetime.strptime('19900101', '%Y%m%d').date()
    assert len(ID) == 9, '指数代码错误, 正确格式:000010.SH'
    # assert len(ID) == 6, '指数代码错误, 正确格式:000010'
    ID = ID.upper()
    if endDate is None:
        endDate = dt.datetime.today().date()
    session = Session()
    for tradeDate in dateStrList(startDate, endDate):
        sql = f'call calchengfenpe("{ID}", "{tradeDate}");'
        print(sql)
        # result = engine.execute(sql)
        result = session.execute(sql)
        session.commit()
        # result = result.fetchall()
        # print(result)
        # session.execute(sql)
    session.close()
Exemplo n.º 2
0
def updateDailybasic():
    """更新每日指标
    """
    sql = 'select max(trade_date) from daily_basic'
    lastdate = engine.execute(sql).fetchone()[0]
    lastdate += dt.timedelta(days=1)
    startDate = lastdate.strftime('%Y%m%d')
    endDate = dt.datetime.today().date() - dt.timedelta(days=1)
    endDate = endDate.strftime('%Y%m%d')
    dates = dateStrList(startDate, endDate)
    for d in dates:
        downDailyBasic(tradeDate=d)
Exemplo n.º 3
0
def calAllPEHistory(startDate, endDate=None):
    """
    计算全市场TTMPE
    :param startDate:
    :param endDate:
    :return:
    """
    # startDate = datetime.strptime('2010-01-01', '%Y-%m-%d').date()
    endDate = dt.datetime.today().date()
    session = Session()
    for tradeDate in dateStrList(startDate, endDate):
        sql = 'call calallpe("%(tradeDate)s");' % locals()
        print(sql)
        session.execute(sql)
        session.commit()
    session.close()
Exemplo n.º 4
0
def scatter(startDate, endDate):
    dateList = dateStrList(startDate, endDate)
    for date in dateList:
        print(date)
        sql = ('select pe, lirunincrease from pelirunincrease '
               f'where date="{date}";')
        result = engine.execute(sql)
        peList = []
        incrateList = []
        for pe, lirunincrease in result.fetchall():
            if pe is not None and lirunincrease is not None:
                peList.append(pe)
                incrateList.append(lirunincrease)
        if not peList:
            continue
        plt.scatter(incrateList, peList)
        plt.axes().set_xlim((-200, 200))
        plt.axes().set_ylim((-200, 200))
        filename = './data/plot/%(date)s.png' % locals()
        plt.savefig(filename)
        plt.clf()
Exemplo n.º 5
0
def downDaily(trade_date=None):
    """下载日K线数据

    :param trade_date:
    :return:
    """
    pro = ts.pro_api()
    dates = []
    if trade_date is None:
        sql = 'select max(trade_date) from daily'
        startDate = engine.execute(sql).fetchone()[0]
        assert isinstance(startDate, dt.date), 'startDate应为date类型'
        startDate += dt.timedelta(days=1)
        endDate = dt.datetime.now().date()
        dates = dateStrList(startDate.strftime('%Y%m%d'),
                            endDate.strftime('%Y%m%d'))
    else:
        dates.append(trade_date)
    for d in dates:
        logging.debug(f'下载日线:{d}')
        df = pro.daily(trade_date=d)
        writeSQL(df, 'daily')