每月调仓投资策略--python

# 可以自己import我们平台支持的第三方python模块,比如pandas、numpy等。
def query_fundamental(context, bar_dict):
    # 查询revenue前十名的公司的股票并且他们的pe_ratio在53和67之间。打fundamentals的时候会有auto-complete方便写查询代码。
    fundamental_df = get_fundamentals(
        query(
            fundamentals.income_statement.revenue, fundamentals.eod_derivative_indicator.pe_ratio
        ).filter(
            fundamentals.eod_derivative_indicator.pe_ratio > 53
        ).filter(
            fundamentals.eod_derivative_indicator.pe_ratio < 67
        ).order_by(
            fundamentals.income_statement.revenue.desc()
        ).limit(
            10
        )
    )

    # 将查询结果dataframe的fundamental_df存放在context里面以备后面只需:
    context.fundamental_df = fundamental_df

    # 实时打印日志看下查询结果,会有我们精心处理的数据表格显示:
    logger.info(context.fundamental_df)

    # 对于每一个股票按照平均现金买入:
    context.stocks = context.fundamental_df.columns.values
    update_universe(context.stocks)

    stocksNumber = len(context.stocks)
    context.average_percent = 0.99 / stocksNumber
    logger.info("Calculated average percent for each stock is: %f" % context.average_percent)

    # 先查一下选出来的股票是否在已有的portfolio里面:
    # 这样做并不是最好的,只是代码比较简单
    # 先清仓然后再买入这一个月新的符合条件的股票
    logger.info("Clearing all the current positions.")
    for holding_stock in context.portfolio.positions.keys():
        if context.portfolio.positions[holding_stock].quantity != 0:
            order_target_percent(holding_stock, 0)

    logger.info("Building new positions for portfolio.")
    for stock in context.stocks:
        order_target_percent(stock, context.average_percent)
        logger.info("Buying: " + str(context.average_percent) + " % for stock: " + str(stock))


# 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。
def init(context):
    scheduler.run_monthly(query_fundamental, monthday=1)


# 你选择的证券的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新
def handle_bar(context, bar_dict):
    # 开始编写你的主要的算法逻辑

    # bar_dict[order_book_id] 可以拿到某个证券的bar信息
    # context.portfolio 可以拿到现在的投资组合状态信息

    # 使用order_shares(id_or_ins, amount)方法进行落单

    # TODO: 开始编写你的算法吧!
    pass

每月调仓投资策略--python_第1张图片

你可能感兴趣的:(大数据,量化投资,Python,python,量化投资)