Exemplo n.º 1
0
def st():
    db_client = MongoDBClient(
        config.get("db").get("mongodb"),
        config.get("db").get("database"))

    st_df = pd.read_csv(config.get("files").get("st"), header=0)
    st_df['ticker'] = st_df['ticker'].map(lambda x: str(x).zfill(6))

    for index, row in st_df.iterrows():
        _date = str(row["tradeDate"]).replace('-', '')
        _market = 0
        if str(row["exchangeCD"]) == "XSHG":
            _market = 1

        db_client.upsert_one(_filter={
            "code": str(row["ticker"]),
            "market": _market,
            "date": _date
        },
                             _value={
                                 "st": 1,
                                 "name": "%s" % row['tradeAbbrName']
                             },
                             _upsert=False)
        print("更新记录: %d" % index)
Exemplo n.º 2
0
def plot1(years, money):
    tax = 0.00025  # 买卖按照万分之2.5计算

    for item in config.get("indexes"):
        _market = item[0]
        _idx_code = item[1]
        _money_per = money
        click.echo("开始计算 %d - %s 指数的回测策略" % (_market, _idx_code))
        _start_date = date.datetime_to_str(date.years_ago(years))
        last_day_list = date.last_days(int(_start_date), 1,
                                       True)  # 每月最后一个交易日集合

        data = []
        db_client = MongoDBClient(
            config.get("db").get("mongodb"),
            config.get("db").get("database"))

        _idx_list = db_client.find_stock_list(_filter={
            "code": _idx_code,
            "market": _market
        },
                                              _sort=[("date",
                                                      pymongo.ASCENDING)],
                                              _fields={
                                                  "date": 1,
                                                  "close": 1
                                              })

        item_df = pd.DataFrame(list(_idx_list), columns=["date", "close"])
        item_df = item_df.dropna(0)

        total_count = int(0)  # 累计数量

        for idx, item_date in enumerate(last_day_list):
            last_df = item_df[item_df["date"] == str(item_date)]
            if last_df is None or len(last_df) <= 0:
                continue

            item_point = last_df.ix[last_df.index.values[0]]["close"]
            item_count = _money_per * (1 - tax) / item_point
            total_count += item_count
            data.append([item_date, "买入", item_point, item_count, total_count])

        item = data.pop(-1)
        item_count = total_count * item[2] * (1 - tax) / item[2]
        data.append([item[0], "卖出", item[2], item_count, 0])
        result_df = pd.DataFrame(data,
                                 columns=["日期", "操作", "单价", "数量", "累计数量"])

        result_df.to_csv(config.get("files").get("plots") %
                         (PROJECT_ROOT, "plot1", _market, _idx_code),
                         index=False,
                         mode="w",
                         header=True,
                         encoding='gb18030',
                         float_format="%.3f")
Exemplo n.º 3
0
def _indexes(idx_market, idx_code, start_date, calendar_df):
    db_client = MongoDBClient(
        config.get("db").get("mongodb"),
        config.get("db").get("database"))

    today = int("".join(time.strftime('%Y%m%d', time.localtime(time.time()))))
    item_last = db_client.find_stock_item(_filter={
        "code": idx_code,
        "market": idx_market,
        "dr": {
            "$exists": True
        }
    },
                                          _sort=[("date", pymongo.DESCENDING)])

    if item_last is not None and len(item_last) > 0:
        calendar_df = calendar_df[
            calendar_df["calendarDate"] > int(item_last.get("date"))]

    click.echo("\t开始 %s 指数的估值信息..." % idx_code)
    for _, row in calendar_df.iterrows():
        cal_date = int(row["calendarDate"])
        if row['isOpen'] == 0 or cal_date < start_date or cal_date > today:
            continue

        click.echo("\t\t计算%d-%s - %d ..." % (idx_market, idx_code, cal_date))
        _result = index.index_val(cal_date, idx_code, db_client)

        if _result is not None:
            click.echo("\t\t\t code:%s, pe_ttm:%s, pb:%s" %
                       (idx_code, _result[0], _result[1]))
            db_client.upsert_one(_filter={
                "code": str(idx_code),
                "market": idx_market,
                "date": str(cal_date)
            },
                                 _value={
                                     "pe_ttm": _result[0],
                                     "pb": _result[1],
                                     "roe": _result[2],
                                     "dr": _result[3]
                                 })
    click.echo("\t%s 指数的估值信息计算完毕" % idx_code)
Exemplo n.º 4
0
def main():
    # 日志配置
    log_file = os.getcwd() + r'\resources\log\{}.log'.format(time.strftime("%Y-%m-%d", time.localtime()))
    format_str = '%(asctime)s %(levelname)s %(module)s %(filename)s[line:%(lineno)d]: %(message)s'
    logging.basicConfig(filename=log_file, level=config.get('logging', 'level'), format=format_str)
    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    console.setFormatter(logging.Formatter(format_str))
    logging.getLogger('').addHandler(console)
    logging.info('Application start!')
    # 启动程序
    Index(config)
Exemplo n.º 5
0
def indexes(code):
    click.echo("开始计算各指数的估值信息...")
    idx_list = config.get("indexes")

    try:
        calendar_df = pd.read_csv(config.get("files").get("calendar"),
                                  header=0)

        if code is not None:
            for item in idx_list:
                if item[1] != code:
                    continue
                _indexes(item[0], item[1], item[2], calendar_df)
                return

        for item in idx_list:
            _indexes(item[0], item[1], item[2], calendar_df)

    except FileNotFoundError:
        pass

    click.echo("各指数的估值信息计算完毕...")
Exemplo n.º 6
0
def report_with_plan(_date: int, _str_code=None):
    """根据财报的标准时间获取财报信息"""
    global REPORT_PUBLISH_BUFFER

    if len(REPORT_PUBLISH_BUFFER) <= 0:
        REPORT_PUBLISH_BUFFER = pd.read_csv(config.get("files").get("reports"),
                                            header=0)

    report_date = date.report_date_with(_date)
    filter_df = REPORT_PUBLISH_BUFFER[
        (REPORT_PUBLISH_BUFFER["date"] == int(report_date))
        & (REPORT_PUBLISH_BUFFER["code"] == int(_str_code))]
    if filter_df is None or len(filter_df) <= 0:
        return None

    return filter_df.loc[filter_df.index.values[0]]
Exemplo n.º 7
0
def bonus_with(_code=None):
    """ 获取某只股票的高送转信息"""
    global BONUS_BUFFER

    if len(BONUS_BUFFER) <= 0:
        BONUS_BUFFER = pd.read_csv(config.get("files").get("bonus"),
                                   header=0,
                                   encoding="utf8")
        BONUS_BUFFER['code'] = BONUS_BUFFER['code'].map(
            lambda x: str(x).zfill(6))

    if _code is not None:
        bonus_df = BONUS_BUFFER[BONUS_BUFFER["code"] == _code]
        return bonus_df

    return BONUS_BUFFER
Exemplo n.º 8
0
    def on_base(self, header: ResponseHeader, body: Buffer):
        """更新市场股票基础信息列表"""
        idx = 0
        current_df_list = []

        market_type = 0 if header.cmd_id == 0x6D else 1  # 0表示深市,1表示沪市
        stocks_count = body.read_format("<H")[0]
        while idx < stocks_count:
            obj_stock_base = ResponseStockBase.parse(body)

            stock_item = [
                obj_stock_base.code, obj_stock_base.name, market_type,
                obj_stock_base.unknown1, obj_stock_base.unknown2,
                obj_stock_base.unknown3, obj_stock_base.price,
                obj_stock_base.bonus1, obj_stock_base.bonus2
            ]

            current_df_list.append(stock_item)
            idx += 1

        data_stocks = pd.DataFrame(current_df_list,
                                   columns=[
                                       'code', 'name', 'market', 'unknown1',
                                       'unknown2', 'unknown3', 'price',
                                       'bonus1', 'bonus2'
                                   ])
        self.data_stocks = pd.concat([self.data_stocks, data_stocks],
                                     ignore_index=True)

        if len(self.data_stocks) >= (self.proxy.stock_sz_count +
                                     self.proxy.stock_sh_count):
            # 保存数据信息
            print("保存基础数据...")
            self.data_stocks.to_csv(config.get("files").get("stock_list"),
                                    index=False,
                                    mode="w",
                                    encoding='utf8',
                                    float_format="%.3f")

            # 解绑事件处理器
            self.proxy.del_handler(header.event_id)

            # 开始更新股票权息数据
            self.update_bonus()
            return

        self.proxy.send(next(self.data_gen))
Exemplo n.º 9
0
    def on_stock_bonus(self, header: ResponseHeader, body: Buffer):
        """处理权息封包"""
        idx = 0
        current_df_list = []
        stocks_count = body.read_format("<H")[0]
        self.bonus_idx += stocks_count

        while idx < stocks_count:
            idx += 1
            bonus_idx = 0
            body.read_bytes(7)  # 跳过股票代码和市场的字节
            bonus_count = body.read_format("<H")[0]
            while bonus_idx < bonus_count:
                stock_bonus = ResponseStockBonus.parse(body)

                bonus_item = [
                    stock_bonus.code, stock_bonus.date, stock_bonus.market,
                    stock_bonus.type, stock_bonus.money, stock_bonus.price,
                    stock_bonus.count, stock_bonus.rate
                ]
                current_df_list.append(bonus_item)
                bonus_idx += 1

        data_bonus = pd.DataFrame(current_df_list,
                                  columns=[
                                      "code", "date", "market", "type",
                                      "money", "price", "count", "rate"
                                  ])
        self.data_bonus = pd.concat([self.data_bonus, data_bonus],
                                    ignore_index=True)

        print("收到 %d 只股票的高送转信息." % stocks_count)
        if header.idx == 0x1100:
            print("保存权息数据")
            self.data_bonus.to_csv(config.get("files").get("bonus"),
                                   index=False,
                                   mode="w",
                                   encoding='utf8',
                                   float_format="%.6f")
            # 数据更新完毕, 解除事件绑定
            self.bonus_idx = 0
            self.proxy.del_handler(header.event_id)
            print("权息数据更新完毕")
            self.proxy.close()
        else:
            self.update_bonus()
Exemplo n.º 10
0
def fixed():
    db_client = MongoDBClient(
        config.get("db").get("mongodb"),
        config.get("db").get("database"))

    base_df = stocks.stock_a_list()
    bonus_df = bonus.bonus_with(None)

    for index, row in base_df.iterrows():
        try:
            prev_close = 0
            prev_fixed_close = 0

            print("计算 %d-%s 的后复权数据" % (row["market"], row["code"]))

            stock_day_list = db_client.find_stock_list(_filter={
                "code": row['code'],
                "market": row["market"]
            },
                                                       _sort=[
                                                           ("date",
                                                            pymongo.ASCENDING)
                                                       ],
                                                       _fields={
                                                           "date": 1,
                                                           "close": 1
                                                       })
            if len(stock_day_list) <= 0:
                # 股票本身没有交易量无需复权
                continue

            stock_day_df = pd.DataFrame(stock_day_list,
                                        columns=['date', 'close'])

            try:
                item_last = db_client.find_stock_item(_filter={
                    "code": row['code'],
                    "market": row["market"],
                    "fixed": {
                        "$exists": True
                    }
                },
                                                      _sort=[
                                                          ("date",
                                                           pymongo.DESCENDING)
                                                      ])

                if item_last is not None and len(item_last) > 0:
                    # 如果之前计算过后复权价,则无需重头计算
                    last_day_df = stock_day_df[stock_day_df["date"] ==
                                               item_last['date']]

                    prev_close = last_day_df.ix[
                        last_day_df.index.values[0]]["close"]
                    prev_fixed_close = item_last["fixed"]

                    stock_day_df = stock_day_df[
                        stock_day_df.index > last_day_df.index.values[0]]

            except FileNotFoundError:
                # 如果从来没计算过后复权价则不管
                pass

            filter_df = bonus_df[(bonus_df["type"] == 1)
                                 & (bonus_df["code"] == row["code"])]
            filter_df = filter_df.sort_values(['date'], ascending=True)
            for idx, item in stock_day_df.iterrows():
                money = 0  # 分红
                count = 0  # 送股数

                item_df = filter_df[filter_df["date"] == int(item["date"])]
                if len(item_df) > 0:
                    money = item_df.ix[item_df.index.values[0]]["money"] / 10
                    count = item_df.ix[item_df.index.values[0]]["count"] / 10

                # 除息除权日当天复权后的涨幅 =(当天不复权收盘价 *(1 + 每股送股数量)+每股分红金额) / 上一个交易日的不复权收盘价
                # 复权收盘价 = 上一个交易日的复权收盘价 *(1 + 复权涨幅)
                if prev_close > 0:
                    daily_rate_close = (item["close"] *
                                        (1 + count) + money) / prev_close
                    prev_fixed_close = prev_fixed_close * daily_rate_close

                else:
                    prev_fixed_close = item["close"]

                prev_close = item["close"]
                db_client.upsert_one(_filter={
                    "code": str(row["code"]),
                    "market": row["market"],
                    "date": str(item["date"])
                },
                                     _value={"fixed": prev_fixed_close})

        except FileNotFoundError:
            continue
Exemplo n.º 11
0
def a():
    """计算A股PE中值"""
    click.echo("开始计算整个A股的估值信息...")
    first = True
    result_list = []
    db_client = MongoDBClient(
        config.get("db").get("mongodb"),
        config.get("db").get("database"))

    calendar_df = pd.read_csv(config.get("files").get("calendar"), header=0)
    today = int("".join(time.strftime('%Y%m%d', time.localtime(time.time()))))

    try:
        stock_df = pd.read_csv(config.get("files").get("stock_a"),
                               header=0,
                               encoding="utf8")
        if len(stock_df) > 0:
            first = False
            item_last = stock_df.loc[stock_df.index[-1]]

            calendar_df = calendar_df[
                calendar_df["calendarDate"] > int(item_last["date"])]

    except FileNotFoundError:
        pass

    for _, row in calendar_df.iterrows():
        cal_date = int(row["calendarDate"])
        if row['isOpen'] == 0 or 20020328 > cal_date or cal_date > today:
            continue

        stock_day_list = db_client.find_stock_list(_filter={
            "date": str(cal_date),
            "fixed": {
                "$exists": True
            }
        },
                                                   _sort=[("code",
                                                           pymongo.ASCENDING)],
                                                   _fields={
                                                       "market": 1,
                                                       "code": 1,
                                                       "close": 1,
                                                       "pe_ttm": 1,
                                                       "pb": 1,
                                                       "roe": 1,
                                                       "dr": 1
                                                   })

        stock_df = pd.DataFrame(
            stock_day_list,
            columns=['market', 'code', 'close', 'pe_ttm', 'pb', 'roe', 'dr'])
        stock_df = stock_df.fillna(0)

        pe_list = []
        pb_list = []
        roe_list = []
        dr_list = []
        for _, r in stock_df.iterrows():
            pe_list.append(r['pe_ttm'])
            pb_list.append(r['pb'])
            roe_list.append(r['roe'])
            dr_list.append(r['dr'])

        mid_value_pe = number.get_median(pe_list)
        mid_value_pb = number.get_median(pb_list)
        avg_value_roe = number.get_average(roe_list)
        avg_value_dr = number.get_average(dr_list)
        click.echo("计算 %s 日的数据中值, pe=%f, pb=%f, roe=%f, dr=%f ... " %
                   (cal_date, mid_value_pe, mid_value_pb, avg_value_roe,
                    avg_value_dr))

        result_list.append([
            cal_date, mid_value_pe, mid_value_pb, avg_value_roe, avg_value_dr
        ])

        if len(result_list) % 50 == 0:
            if first:
                first = False

                result_df = pd.DataFrame(
                    result_list, columns=['date', 'pe', 'pb', 'roe', 'dr'])
                result_df.to_csv(config.get("files").get("stock_a"),
                                 index=False,
                                 mode="w",
                                 header=True,
                                 encoding='utf8',
                                 float_format="%.6f")
            else:
                result_df = pd.DataFrame(
                    result_list, columns=['date', 'pe', 'pb', 'roe', 'dr'])
                result_df.to_csv(config.get("files").get("stock_a"),
                                 index=False,
                                 mode="a+",
                                 header=False,
                                 encoding='utf8',
                                 float_format="%.6f")

            result_list = []

    result_df = pd.DataFrame(result_list,
                             columns=['date', 'pe', 'pb', 'roe', 'dr'])
    result_df.to_csv(config.get("files").get("stock_a"),
                     index=False,
                     mode="a+",
                     header=False,
                     encoding='utf8',
                     float_format="%.6f")
    click.echo("整个A股的估值信息计算完毕...")
import re
import pprint
#from netaddr import IPNetwork
import logging
import argparse
import json
import sys
from nmsnetlib.parsers.carrierethernet import ERSParser, SAOSParser
from nmsnetlib.parsers.parsercollection import ParserCollection
from operator import itemgetter, attrgetter
import time
from configure import config as AppConfig
from datetime import datetime

## SETUP LOGGING
myconfig = AppConfig.get(os.environ.get("ENV").lower(), "prod")
logger = myconfig.main_logger
today = datetime.now().strftime("%Y-%m-%d")

# required for ascii table parsing:
#import docutils.statemachine
#import docutils.parsers.rst.tableparser

## GLOBAL VARIABLES
VERSION = "1.0"
VERSION_HISTORY = '''\
-- HISTORY --
1.0 - 20170804 - initial version
1.2 - 20200401 - add logging
                 update saos definition: PORTS section was changed after upgrades and therefore
                    LAG ports were not detected if they had any traffic-services
Exemplo n.º 13
0
def mv():
    """
        总市值=当前股价×总股本
    """
    click.echo("计算总股本、流通股本、流通市值、总市值、股息率...")

    db_client = MongoDBClient(
        config.get("db").get("mongodb"),
        config.get("db").get("database"))

    base_df = stocks.stock_a_list()
    bonus_df = bonus.bonus_with(None)

    report_df = pd.read_csv(config.get("files").get("reports"),
                            header=0,
                            encoding="utf8")
    report_df['code'] = report_df['code'].map(lambda x: str(x).zfill(6))

    for index, row in base_df.iterrows():
        try:
            print("计算 %d-%s 的股本、市值、股息率" % (row["market"], row["code"]))

            ccs = 0  # 流通股
            tcs = 0  # 总股本

            stock_day_list = db_client.find_stock_list(_filter={
                "code": row['code'],
                "market": row["market"],
                "close": {
                    "$exists": True
                }
            },
                                                       _sort=[
                                                           ("date",
                                                            pymongo.ASCENDING)
                                                       ],
                                                       _fields={
                                                           "date": 1,
                                                           "close": 1
                                                       })
            if len(stock_day_list) <= 0:
                # 股票本身没有交易量
                continue

            stock_day_df = pd.DataFrame(stock_day_list,
                                        columns=['date', 'close'])

            try:
                item_last = db_client.find_stock_item(_filter={
                    "code": row['code'],
                    "market": row["market"],
                    "dr": {
                        "$exists": True
                    }
                },
                                                      _sort=[
                                                          ("date",
                                                           pymongo.DESCENDING)
                                                      ])

                if item_last is not None and len(item_last) > 0:
                    # 如果之前计算过,则无需重头计算
                    last_day_df = stock_day_df[stock_day_df["date"] ==
                                               item_last['date']]
                    stock_day_df = stock_day_df[
                        stock_day_df.index > last_day_df.index.values[0]]

                    ccs = item_last["cmv"] // item_last["close"]  # 流通股
                    tcs = item_last["tmv"] // item_last["close"]  # 总股本

            except FileNotFoundError:
                pass

            filter_bonus_df = bonus_df[(bonus_df["code"] == row["code"])
                                       & (bonus_df["type"] != 6) &
                                       ((bonus_df["type"] > 1) &
                                        (bonus_df["type"] < 13))]
            filter_bonus_df = filter_bonus_df.sort_values(['date'],
                                                          ascending=True)

            filter_report_df = report_df[report_df["code"] == row["code"]]
            filter_report_df = filter_report_df.sort_values(['date'],
                                                            ascending=True)

            for idx, item in stock_day_df.iterrows():
                item_df = filter_bonus_df[filter_bonus_df["date"] == int(
                    item["date"])]
                if len(item_df) > 0:
                    # 高送转中记录的数据单位都到万
                    ccs = item_df.ix[item_df.index.values[0]]["count"] * 10000
                    tcs = item_df.ix[item_df.index.values[0]]["rate"] * 10000

                item_df = filter_report_df[filter_report_df["publish"] == int(
                    item["date"])]
                if len(item_df) > 0:
                    ccs = item_df.ix[item_df.index.values[0]]["ccs"]
                    tcs = item_df.ix[item_df.index.values[0]]["tcs"]

                ccs_mv = item["close"] * ccs
                tcs_mv = item["close"] * tcs
                # 3年的股息率
                dr = bonus.dividend_rate_with(int(item["date"]), row["code"],
                                              item["close"])

                db_client.upsert_one(_filter={
                    "code": str(row["code"]),
                    "market": row["market"],
                    "date": str(item["date"])
                },
                                     _value={
                                         "cmv": ccs_mv,
                                         "tmv": tcs_mv,
                                         "dr": dr
                                     })

        except FileNotFoundError:
            continue

    click.echo("总股本、流通股本、流通市值、总市值、股息率 计算完毕...")
Exemplo n.º 14
0
def pe_pb_roe():
    """

    转换PE、PB、ROE、股息率 四个指标需要的信息
    1.基本每股收益、4.每股净资产、96.归属于母公司所有者的净利润、238.总股本、239.已上市流通A股
        PE=股价/每股收益
        PB=股价/每股净资产
        ROE=利润/每股净资产=PB/PE : 财报中已有静态的净资产收益率数据, 这里通过TTM计算一个大概的ROE作为参考
    :return:
    """
    db_client = MongoDBClient(
        config.get("db").get("mongodb"),
        config.get("db").get("database"))

    base_df = stocks.stock_a_list()

    for index, row in base_df.iterrows():
        try:
            print("计算 %d-%s 的PE、PB、ROE数据..." % (row["market"], row["code"]))

            stock_day_list = db_client.find_stock_list(_filter={
                "code": row['code'],
                "market": row["market"],
                "close": {
                    "$exists": True
                }
            },
                                                       _sort=[
                                                           ("date",
                                                            pymongo.ASCENDING)
                                                       ],
                                                       _fields={
                                                           "date": 1,
                                                           "close": 1
                                                       })
            if len(stock_day_list) <= 0:
                # 股票本身没有交易量
                continue

            stock_day_df = pd.DataFrame(stock_day_list,
                                        columns=['date', 'close'])

            item_last = db_client.find_stock_item(_filter={
                "code": row['code'],
                "market": row["market"],
                "pb": {
                    "$exists": True
                }
            },
                                                  _sort=[("date",
                                                          pymongo.DESCENDING)])

            if item_last is not None and len(item_last) > 0:
                # 如果之前计算过,则无需重头计算
                last_day_df = stock_day_df[stock_day_df["date"] ==
                                           item_last['date']]
                stock_day_df = stock_day_df[
                    stock_day_df.index > last_day_df.index.values[0]]

            for idx, item in stock_day_df.iterrows():
                lyr_value = rt.lyr_with(int(item["date"]), row['code'],
                                        item["close"])
                pe_value = rt.ttm_with(int(item["date"]), row['code'],
                                       item["close"])
                pb_value = rt.pb_with(int(item["date"]), row['code'],
                                      item["close"])
                roe_value = 0.0
                if pe_value != 0.0:
                    roe_value = pb_value / pe_value
                db_client.upsert_one(_filter={
                    "code": str(row["code"]),
                    "market": row["market"],
                    "date": str(item["date"])
                },
                                     _value={
                                         "lyr": lyr_value,
                                         "pe_ttm": pe_value,
                                         "pb": pb_value,
                                         "roe": roe_value
                                     })

        except FileNotFoundError:
            continue
Exemplo n.º 15
0
    def find_stock_list(self, _filter=None, _sort=None, _fields=None):
        """查询股票列表 todo: 待完善 暂时没有分页需求, 后补"""
        return list(self.__stock.find(_filter, sort=_sort, projection=_fields))

    def insert_json(self, _dataframe: pd.DataFrame):
        if _dataframe.empty:
            return
        self.__stock.insert_many(
            json.loads(_dataframe.to_json(orient='records')))

    def upsert_one(self, _value, _filter=None, _upsert=True):
        """
        有则更新,无则插入
        :param _filter:
        :param _value:
        :param _upsert: 如果要更新的数据不存在是否插入
        :return:
        """
        try:
            self.__stock.update_one(_filter, {'$set': _value}, upsert=_upsert)
        except Exception as ex:
            print(str(ex))


if __name__ == "__main__":
    db_client = MongoDBClient(
        config.get("db").get("mongodb"),
        config.get("db").get("database"))
    date = db_client.find_stock_item({"code": "399001", "market": 0})
    print("hello world!")
Exemplo n.º 16
0
def indexes_json():
    """显示指数估值信息"""
    # '上证50', '沪深300', '中证500', '中证1000', '上证红利', '中证红利','中小板指', '创业扳指'
    # '食品饮料'
    idx_list = config.get("indexes")
    stock_model = StockModel()
    result = []

    for idx, item in enumerate(idx_list):
        index_obj = {"idx": idx + 1}
        _market = 'SZ' if item[0] == 0 else 'SH'

        item_detail = stock_model.stock_item(_filter={"code": item[1], "market": item[0]},
                                             _sort=[("date", pymongo.DESCENDING)])

        index_obj["name"] = "<a href='/index/%s%s' title='%s的估值信息'> %s </a>" % \
                            (_market, item_detail["code"], item_detail["name"], item_detail["name"])
        index_obj["interest"] = "%.2f%%" % (1 / item_detail["pe_ttm"] * 100)
        index_obj["dr"] = "%.2f%%" % (item_detail["dr"] * 100)
        index_obj["roe"] = "%.2f%%" % (item_detail["roe"] * 100)
        index_obj["PE"] = "%.2f" % item_detail["pe_ttm"]
        index_obj["PB"] = "%.2f" % item_detail["pb"]

        _filter = {"code": item[1], "market": item[0], "date": {"$gte": date.datetime_to_str(date.years_ago(10))}}
        item_list = stock_model.stock_list(_filter=_filter, _sort=[("date", pymongo.ASCENDING)],
                                           _fields={"pe_ttm": 1, "pb": 1})

        item_df = pd.DataFrame(list(item_list), columns=["pe_ttm", "pb"])
        item_df = item_df.dropna(0)

        _pe_series = 1 / item_df["pe_ttm"]
        pe_ranks = _pe_series.rank(pct=True)

        _pb_series = 1 / item_df["pb"]
        pb_ranks = _pb_series.rank(pct=True)

        index_obj["PE_RATE"] = "%.2f%%" % ((1 - pe_ranks.values[-1]) * 100)
        index_obj["PB_RATE"] = "%.2f%%" % ((1 - pb_ranks.values[-1]) * 100)

        # 本周以来的涨跌幅
        now = datetime.today()
        _tmp_date = (now - timedelta(days=now.weekday())).strftime('%Y%m%d')
        item_last_week = stock_model.stock_item(_filter={"code": item[1], "market": item[0],
                                                         "date": _tmp_date if date.is_work_day(int(_tmp_date))
                                                         else str(date.next_day(int(_tmp_date)))})
        if item_last_week is None:
            item_last_week = item_detail

        # 本月以来
        _tmp_date = datetime(now.year, now.month, 1).strftime('%Y%m%d')
        item_last_month = stock_model.stock_item(_filter={"code": item[1], "market": item[0],
                                                          "date": _tmp_date if date.is_work_day(int(_tmp_date))
                                                          else str(date.next_day(int(_tmp_date)))})
        if item_last_month is None:
            item_last_month = item_detail

        # 本季度以来
        month = (now.month - 1) - (now.month - 1) % 3 + 1
        _tmp_date = datetime(now.year, month, 1).strftime('%Y%m%d')
        item_last_quarter = stock_model.stock_item(_filter={"code": item[1], "market": item[0],
                                                            "date": _tmp_date if date.is_work_day(int(_tmp_date))
                                                            else str(date.next_day(int(_tmp_date)))})

        if item_last_quarter is None:
            item_last_quarter = item_detail

        # 本年度以来
        _tmp_date = datetime(now.year, 1, 1).strftime('%Y%m%d')
        item_last_year = stock_model.stock_item(_filter={"code": item[1], "market": item[0],
                                                         "date": _tmp_date if date.is_work_day(int(int(_tmp_date)))
                                                         else str(date.next_day(int(_tmp_date)))})

        if item_last_year is None:
            item_last_year = item_detail

        index_obj["last_week"] = "%.2f%%" % (
                    (item_detail["close"] - item_last_week["close"]) / item_last_week["close"] * 100)
        index_obj["last_month"] = "%.2f%%" % (
                    (item_detail["close"] - item_last_month["close"]) / item_last_month["close"] * 100)
        index_obj["last_quarter"] = "%.2f%%" % (
                    (item_detail["close"] - item_last_quarter["close"]) / item_last_quarter["close"] * 100)
        index_obj["last_year"] = "%.2f%%" % (
                    (item_detail["close"] - item_last_year["close"]) / item_last_year["close"] * 100)
        result.append(index_obj)

    # 追加A股市场的估值信息
    stock_a_df = pd.read_csv(config.get("files").get("stock_a"), header=0, encoding="utf8")
    stock_a_df = stock_a_df.dropna(0)
    if len(stock_a_df) > 0:
        item_last = stock_a_df.loc[stock_a_df.index[-1]]

        pe_array = 1 / stock_a_df["pe"]
        pb_array = 1 / stock_a_df["pb"]
        pe_ranks = pe_array.rank(pct=True)
        pb_ranks = pb_array.rank(pct=True)

        stock_a_item = {"idx": len(idx_list) + 1, "name": "A股市场", "interest": "%.2f%%" % (1 / item_last["pe"] * 100),
                        "dr": "%.2f%%" % (item_last["dr"] * 100), "roe": "%.2f%%" % (item_last["roe"] * 100),
                        "PE": "%.2f" % item_last["pe"], "PB": "%.2f" % item_last["pb"],
                        "PE_RATE": "%.2f%%" % ((1 - pe_ranks.values[-1]) * 100),
                        "PB_RATE": "%.2f%%" % ((1 - pb_ranks.values[-1]) * 100)}

        result.append(stock_a_item)

    # 追加国债收益供参考
    bond_df = pd.read_csv(config.get("files").get("bond"), header=0, encoding="utf8")
    if len(bond_df) > 0:
        item_last = bond_df.loc[bond_df.index[-1]]
        bond_item = {"idx": len(idx_list) + 2,
                     "name": "<a href='http://www.pbc.gov.cn/rmyh/108976/index.html#Container' "
                             "title='%s的历史利率' target='_blank'> 十年国债利率 </a>",
                     "interest": "%.2f%%" % item_last["10year"]}
        result.append(bond_item)

    return json.dumps(result)
Exemplo n.º 17
0
def report():
    """
    转换PE、PB、ROE、股息率 四个指标需要的信息
    1.基本每股收益、4.每股净资产、96.归属于母公司所有者的净利润、238.总股本、239.已上市流通A股
        PE=股价/利润
        PB=股价/每股净资产
        ROE=利润/每股净资产
        因此:PB=PE*ROE
        总市值=当前股价×总股本
    """
    click.echo("转换股票财报数据的指标...")

    all_date_list = []
    file_list = comm.file_list_in_path("%s/data/reports/" % PROJECT_ROOT)

    for file_name in file_list:
        if file_name.startswith(".") or file_name.startswith("rt"):
            continue

        file_name = file_name.split('.')[0]
        ymd_str = file_name[4:]
        all_date_list.append(ymd_str)

    all_date_list.sort()

    is_first = True
    for date_item in all_date_list:

        click.echo("\t开始转换 %s 的财报数据..." % date_item)
        report_list = []
        report_df = stocks.report_list(_date=date_item)

        for idx, row in report_df.iterrows():
            market = stocks.market_with_code(row['code'])
            act = stocks.report_publish_time(row['date'], row['code'])
            report_list.append([
                row['date'], market, row['code'], row[1], row[2], row[4],
                row[6], row[72], row[96], row[238], row[239], act
            ])

        # date, market, code, 1.基本每股收益、2.扣非每股收益、4.每股净资产、6.净资产收益率(roa)、72.所有者权益(或股东权益)合计、96.归属于母公司所有者的净利润、
        # 238.总股本、239.已上市流通A股、披露时间
        result_df = pd.DataFrame(report_list,
                                 columns=[
                                     'date', 'market', 'code', 'eps', 'neps',
                                     'bps', 'roe', 'na', 'np', 'tcs', 'ccs',
                                     "publish"
                                 ])

        result_df = result_df.sort_values(['code'], ascending=1)
        if is_first is True:
            result_df.to_csv(config.get("files").get("reports"),
                             index=False,
                             header=True,
                             mode='w',
                             encoding='utf8')
            is_first = False
        else:
            result_df.to_csv(config.get("files").get("reports"),
                             index=False,
                             header=False,
                             mode='a+',
                             encoding='utf8')
    click.echo("股票财报数据转换完毕...")
Exemplo n.º 18
0
def plot2(years, money):
    """
    红利指数定投进阶方案
    80% <= pe百分位: 全卖
    60% <= pe百分位 < 80%: 定赎回
    40% <= pe百分位 < 60%: 正常定投
    20% <= pe百分位 < 40%: 1.5倍定投
    pe百分位 < 20%: 2倍定投

    :return:
    """
    tax = 0.00025  # 买卖按照万分之2.5计算

    for item in config.get("indexes"):
        data = []
        _market = item[0]
        _idx_code = item[1]
        _money_per = money

        click.echo("开始计算 %d - %s 指数的回测策略" % (_market, _idx_code))
        _start_date = date.datetime_to_str(date.years_ago(years))
        last_day_list = date.last_days(int(_start_date), 1,
                                       True)  # 每月最后一个交易日集合

        db_client = MongoDBClient(
            config.get("db").get("mongodb"),
            config.get("db").get("database"))

        _idx_list = db_client.find_stock_list(_filter={
            "code": _idx_code,
            "market": _market
        },
                                              _sort=[("date",
                                                      pymongo.ASCENDING)],
                                              _fields={
                                                  "date": 1,
                                                  "pe_ttm": 1,
                                                  "close": 1
                                              })

        item_df = pd.DataFrame(list(_idx_list),
                               columns=["date", "pe_ttm", "close"])
        item_df = item_df.dropna(0)

        _pe_series = 1 / item_df["pe_ttm"]
        item_df.insert(0, 'rank', _pe_series.rank(pct=True))
        item_df = item_df.reset_index()

        total_count = 0  # 累计数量

        for idx, item_date in enumerate(last_day_list):
            last_df = item_df[item_df["date"] == str(item_date)]
            if last_df is None or len(last_df) <= 0:
                continue

            _tmp_rank = last_df.ix[last_df.index.values[0]]["rank"]
            item_point = last_df.ix[last_df.index.values[0]]["close"]
            item_rank = float("%.2f" % ((1 - _tmp_rank) * 100))

            if item_rank >= 80 and total_count > 0:
                # 全部卖出, 卖出后要扣税费
                item_count = total_count * item_point * (1 - tax) / item_point
                data.append(
                    [item_date, item_rank, "卖出", -item_point, item_count, 0])
                total_count = 0

            elif 60 <= item_rank < 80:
                # 卖出1份
                _op = "卖出"
                # 卖出的这一份应该包含税费,所以要多卖出一部分税费的数量
                item_count = _money_per * (1 + tax) / item_point
                if total_count >= item_count:
                    total_count -= item_count
                elif total_count > 0:
                    item_count = total_count
                    total_count = 0
                else:
                    _op = "/"
                    item_count = 0
                    total_count = 0

                data.append([
                    item_date, item_rank, _op, -item_point, item_count,
                    total_count
                ])

            elif 40 <= item_rank < 60:
                # 买入1份
                item_count = _money_per * (1 - tax) / item_point
                total_count += item_count
                data.append([
                    item_date, item_rank, "买入", item_point, item_count,
                    total_count
                ])

            elif 20 <= item_rank < 40:
                # 买入1.5份
                item_count = (_money_per * 1.5) * (1 - tax) / item_point
                total_count += item_count
                data.append([
                    item_date, item_rank, "买入", item_point, item_count,
                    total_count
                ])
            elif 0 <= item_rank < 20:
                # 买入2份
                item_count = (_money_per * 2) * (1 - tax) / item_point
                total_count += item_count
                data.append([
                    item_date, item_rank, "买入", item_point, item_count,
                    total_count
                ])
            else:
                data.append(
                    [item_date, item_rank, "/", item_point, 0, total_count])

        result_df = pd.DataFrame(
            data, columns=["日期", "百分位", "操作", "单价", "数量", "累计数量"])

        result_df.to_csv(config.get("files").get("plots") %
                         (PROJECT_ROOT, "plot2", _market, _idx_code),
                         index=False,
                         mode="w",
                         header=True,
                         encoding='gb18030',
                         float_format="%.3f")
Exemplo n.º 19
0
def index_val(_date: int, _idx_code: str, _mongo_client=None):
    """
    计算市场及指数在某一天的估值信息
    :param int _date: 某一天
    :param str _idx_code: 指数代码
    :param _mongo_client: mongodb 连接实例
    :return:
    """
    global CUR_IDX_CODE
    global INDEX_MEMBER_DF
    # 有复权价代表是股票
    stock_day_list = _mongo_client.find_stock_list(_filter={
        "date": str(_date),
        "fixed": {
            "$exists": True
        }
    },
                                                   _sort=[("code",
                                                           pymongo.ASCENDING)],
                                                   _fields={
                                                       "market": 1,
                                                       "code": 1,
                                                       "close": 1,
                                                       "pe_ttm": 1,
                                                       "pb": 1,
                                                       "roe": 1,
                                                       "dr": 1
                                                   })

    if CUR_IDX_CODE != _idx_code:
        # 如果不是同一个指数,则清空缓存重新处理
        INDEX_MEMBER_DF = pd.DataFrame()
        today = int("".join(
            time.strftime('%Y%m%d', time.localtime(time.time()))))
        INDEX_MEMBER_DF = pd.read_csv(config.get("files").get("index") %
                                      (PROJECT_ROOT, _idx_code),
                                      header=0)
        INDEX_MEMBER_DF = INDEX_MEMBER_DF.fillna(today)
        INDEX_MEMBER_DF = INDEX_MEMBER_DF.drop_duplicates()
        INDEX_MEMBER_DF['outDate'] = INDEX_MEMBER_DF['outDate'].astype('int')
        INDEX_MEMBER_DF['consTickerSymbol'] = INDEX_MEMBER_DF[
            'consTickerSymbol'].map(lambda x: str(x).zfill(6))

    filter_member_df = INDEX_MEMBER_DF[(INDEX_MEMBER_DF['intoDate'] <= _date)
                                       & (INDEX_MEMBER_DF['outDate'] >= _date)]

    if len(stock_day_list) <= 0:
        # 这一天没有股票交易
        return None

    stock_df = pd.DataFrame(
        stock_day_list,
        columns=['market', 'code', 'close', 'pe_ttm', 'pb', 'roe', 'dr'])
    stock_df = stock_df.fillna(0)

    pe_list = []
    pb_list = []
    roe_list = []
    dr_list = []

    for idx, r in filter_member_df.iterrows():
        _market = 0 if r["consExchangeCD"] == 'XSHE' else 1
        _item = stock_df[(stock_df["market"] == _market)
                         & (stock_df["code"] == r["consTickerSymbol"])]

        if len(_item) <= 0:
            print("\t\t没有找到股票: %s" % r["consTickerSymbol"])
            continue

        pe_list.append(_item.values[0][3])
        pb_list.append(_item.values[0][4])
        roe_list.append(_item.values[0][5])
        dr_list.append(_item.values[0][6])

    mid_value_pe = number.get_median(pe_list)
    mid_value_pb = number.get_median(pb_list)
    avg_value_roe = number.get_average(roe_list)
    avg_value_dr = number.get_average(dr_list)

    # 日期、指数代码、pe-ttm中位数、pb中位数、roe中位数、股息率中位数
    # todo 1周涨幅、一月涨幅、一季涨幅、今年至今、一年涨幅、两年涨幅、三年涨幅、五年涨幅
    return [mid_value_pe, mid_value_pb, avg_value_roe, avg_value_dr]