当前位置: 首页>>代码示例>>Python>>正文


Python Snapshot.latest_position方法代码示例

本文整理汇总了Python中snapshot.Snapshot.latest_position方法的典型用法代码示例。如果您正苦于以下问题:Python Snapshot.latest_position方法的具体用法?Python Snapshot.latest_position怎么用?Python Snapshot.latest_position使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在snapshot.Snapshot的用法示例。


在下文中一共展示了Snapshot.latest_position方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Terminal

# 需要导入模块: from snapshot import Snapshot [as 别名]
# 或者: from snapshot.Snapshot import latest_position [as 别名]
class Terminal(object):
    """
    用于和前端交互的数据终端,可用于交互的方法和属性有:
    - update_position(pos_string): 接收前端的持仓字符串,刷新position和snapshot,仅在交易日09:30-15:00有效
    - save(): 保存最新的仓位,仅在交易日15:00后有效
    - reload_snapshot: 刷新snapshot,返回刷新后的overall_info和detail_info
    - snapshot_overall_info: 最新的overall_info
    - snapshot_detail_info: 最新的detail_info
    - latest_position_string: 最新的position的简单字符串形式,以供修改
    """

    def __init__(self):
        self._benchmark = None
        self._benchmark_history = None
        self._benchmark_return = None
        self._portfolio_history = None
        self._portfolio_return = None
        self._trading_days = None

        self._positions = Positions()
        self._auto_fill_positions()
        self._load_benchmark()
        if not os.path.isfile(HISTORY_IMG_FILE):
            self._draw_history_timeline()

        self._snapshot = Snapshot(self._benchmark, self._positions.get_current_position())

    def _auto_fill_positions(self):
        now = datetime.now()
        today = now.strftime("%Y-%m-%d")
        yesterday = get_trading_days_relatively(today, -1)[0]

        trading_days_raw = self._positions._trading_days
        latest_valid_date = trading_days_raw[-1]
        latest_position = self._positions.get_position(latest_valid_date)

        trading_days_new = get_trading_days(latest_valid_date, yesterday)[1:]
        self._trading_days = trading_days_raw + trading_days_new
        if today in TRADING_DAYS_DICT:
            self._positions.set_current_info(today, deepcopy(latest_position))
        else:
            self._positions.set_current_info(yesterday, deepcopy(latest_position))

        if trading_days_new:
            logger.info("Filling positions data of {} through scraper, referring position at {}...".format(trading_days_new, latest_valid_date))

            for date in trading_days_new:
                temp_pos = deepcopy(latest_position)
                universe = temp_pos['securities'].keys()
                data = load_crossectional_close_prices(universe, date)

                value = temp_pos['cash']
                for sec, p in data.iteritems():
                    temp_pos['securities'][sec]['price'] = p
                    value += p * temp_pos['securities'][sec]['amount']
                temp_pos['value'] = value
                self._positions.set_info(date, temp_pos, is_new_date=True)
            logger.info("Successfully filled positions data!")

            self._positions.save_all()
            logger.info("Latest positions data saved at /lib/posdb/.")

        self._portfolio_history = self._positions.history_values
        v0 = self._portfolio_history[0]
        self._portfolio_return = [v/v0 - 1 for v in self._portfolio_history]

    def _load_benchmark(self):
        logger.info("Loading benchmark data file...")

        config = json.load(file(CONFIG_FILE))
        benchmark = config["benchmark"]
        start = self._trading_days[0]
        end = self._trading_days[-1]
        redraw = False

        try:
            benchmark_info = json.load(file(BENCHMARK_CACHE_FILE))
            assert benchmark_info['sec_id'] == benchmark
            assert benchmark_info['start'] == start
            assert benchmark_info['end'] == end
            logger.info("Successfully loaded benchmark data file!")
        except:
            redraw = True
            logger.info("Benchmark data file is outdated. Downloading benchmark data ({}) from {} to {} through scraper...".format(benchmark, start, end))

            data = load_daily_close_prices(universe=[benchmark],
                                           start=start, end=end)
            assert len(data[benchmark]) == len(self._trading_days)
            logger.info("Successfully downloaded benchmark data!")

            benchmark_info = {
                "sec_id": benchmark,
                "start": start,
                "end": end,
                "data": data[benchmark],
            }
            f = open(BENCHMARK_CACHE_FILE, 'w')
            f.write(json.dumps(benchmark_info, sort_keys=True, indent=4))
            f.close()
            logger.info("Benchmark data saved at ./static/temp/benchmark.json.")
#.........这里部分代码省略.........
开发者ID:EdwardBetts,项目名称:PortfolioMonitor,代码行数:103,代码来源:terminal.py


注:本文中的snapshot.Snapshot.latest_position方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。