如何在Quantopian / Python中添加条件?

时间:2016-03-12 23:48:03

标签: python-3.x

我试图在Quantopian(使用Python)中构建一个后备测试器,并且在我构建的Quantopian中运行backtest算法时,我收到错误:

"开始你的测试时出现问题。"

因此,我希望测试人员能够寻找市值低于3亿美元,负现金流量,负资产收益率和负净利润率的公司。

我相信我的测试人员可以正确找到这些公司。我认为问题出在"尝试:"阻止,当我告诉我的测试人员购买公司的股票时。我用:

    if context.fundamentals[stock]['market_cap'] < 11 and 
    context.fundamentals[stock]['free_cash_flow'] < 0 and 
    context.fundamentals[stock]['net_margin'] < 0 and 
    context.fundamentals[stock]['roe'] < 0:

将这些条件结合起来的正确方法是什么:&#34;尝试:&#34;方框?

这是整个代码......

def initialize(context):
    context.limit = 10

def before_trading_start(context):
    context.fundamentals = get_fundamentals(
        query(
            fundamentals.valuation.market_cap,
            fundamentals.cash_flow_statement.free_cash_flow,
            fundamentals.operation_ratios.net_margin,
            fundamentals.operation_ratios.roe,
        )
        .filter(
            fundamentals.valuation.market_cap < 300000000
        )
        .filter(
            fundamentals.cash_flow_statement.free_cash_flow < 0
        )
        .filter(
            fundamentals.operation_ratios.net_margin < 0
        )
        .filter(
            fundamentals.operation_ratios.roe < 0
        )
        .order_by(
            fundamentals.valuation.market_cap.desc()
        )
        .limit(context.limit)
    )

    update_universe(context.fundamentals.columns.values)

def_handle_data(context, data):
    cash = context.portfolio.cash
    current_positions = context.portfolio.positions

    for stock in data:
        current_position = context.portfolio.positions[stock].amount
        stock_price = data[stock].price
        plausible_investment = cash / 10.0

        share_amount = int(plausible_investment / stock_price)

        try:
            if stock_price < plausible_investment:
                if current_position == 0:
                    if context.fundamentals[stock]['market_cap'] < 11 and context.fundamentals[stock]['free_cash_flow'] < 0 and context.fundamentals[stock]['net_margin'] < 0 and context.fundamentals[stock]['roe'] < 0:
                        order(stock, share_amount)

        except Exception as E:
            print(str(e))

1 个答案:

答案 0 :(得分:0)

你的代码被缩进了。

def_handle_data(context, data):应为def handle_data