【问题标题】:AttributeError: 'bool' object has no attribute 'ndim'AttributeError: 'bool' 对象没有属性 'ndim'
【发布时间】:2016-12-25 07:55:59
【问题描述】:
from quantopian.pipeline import Pipeline
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.filters.morningstar import Q1500US
from quantopian.pipeline.factors import AnnualizedVolatility
from quantopian.pipeline.factors.morningstar import MarketCap
from quantopian.pipeline import factors, filters, classifiers

Market_Cap=(MarketCap > 1000000000)

def lowvolport():
    return filters.make_us_equity_universe(
    target_size=50,
    rankby=factors.AnnualizedVolatility(window_length=90),
    mask=Market_Cap,
    )

def initialize(context):
    # Schedule our rebalance function to run at the start of each week.
    schedule_function(my_rebalance, date_rules.week_start(),             time_rules.market_open(hours=1))

    # Record variables at the end of each day.
    schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())

    # Create our pipeline and attach it to our algorithm.
    my_pipe = make_pipeline()
    attach_pipeline(my_pipe, 'my_pipeline')

def make_pipeline():
    """
    Create our pipeline.
    """

    # Base universe set to the Q1500US.
    base_universe = Q1500US()

    Market_Cap = (MarketCap > 1000000000)
    # Filter to select securities to long.
    volatility_bottom = AnnualizedVolatility(inputs=[USEquityPricing.close], window_length=90, mask=base_universe)

    volatility_bottom_50=volatility_bottom.bottom(50)

    # Filter for all securities that we want to trade.
    securities_to_trade = (Market_Cap & volatility_bottom_50)

    return Pipeline(
    columns={
        'Market_Cap': Market_Cap
    },
    screen=(securities_to_trade),
    )

def my_compute_weights(context):
    """
    Compute ordering weights.
    """
    # Compute even target weights for our long positions and short positions.
    long_weight = 0.5 / len(context.longs)
    short_weight = -0.5 / len(context.shorts)

    return long_weight, short_weight

def before_trading_start(context, data):
    # Gets our pipeline output every day.
    context.output = pipeline_output('my_pipeline')

    # Go long in securities for which the 'longs' value is True.
    context.longs = context.output[context.output['longs']].index.tolist()

    # Go short in securities for which the 'shorts' value is True.
    context.shorts = context.output[context.output['shorts']].index.tolist()

    context.long_weight, context.short_weight = my_compute_weights(context)

def my_rebalance(context, data):
    """
    Rebalance weekly.
    """
    for security in context.portfolio.positions:
    if security not in context.longs and security not in context.shorts and data.can_trade(security):
        order_target_percent(security, 0)

    for security in context.longs:
    if data.can_trade(security):
        order_target_percent(security, context.long_weight)

    for security in context.shorts:
    if data.can_trade(security):
        order_target_percent(security, context.short_weight)

def my_record_vars(context, data):
    """
    Record variables at the end of each day.
    """
    longs = shorts = 0
    for position in context.portfolio.positions.itervalues():
        if position.amount > 0:
            longs += 1
        elif position.amount < 0:
            shorts += 1

    # Record our variables.
    record(leverage=context.account.leverage, long_count=longs, short_count=shorts)

大家好,我是 python 新手,有一些 Matlab 经验。该代码是我最近在 Quantopian 中所做的。错误信息是

    AttributeError: 'bool' object has no attribute 'ndim'
    There was a runtime error on line 27.

第 27 行是

        my_pipe = make_pipeline()

以上是我的第一个问题。 我的第二个问题是,基于现有算法,如何使用公式每三个月执行一次 VAR 模型

    Yt = a0 + a1Yt-1 + ..... + apYt-p + b1Xt-1 + ..... + bpXt-p + ut

Yt 是超过 90 天的回报,而 Xt-1,...,Xt-p 是波动滞后?

提前致谢!如果需要指定任何细节,请告诉我。

【问题讨论】:

  • 请正确缩进您的代码并发布您收到的错误消息的完整堆栈跟踪。
  • 只是更正缩进,但在 Quantopian IDE 中,我没有看到错误消息的“完整堆栈跟踪”。如果我错过了什么,请提出建议。
  • 您的错误信息是否仅包含这两行?
  • 在“运行时错误”窗口下,多了一行说“出了点问题。很抱歉给您带来不便。尝试使用内置调试器分析您的代码。如果您需要帮助,给我们发一封电子邮件。”
  • 谁能帮忙?谁能猜一猜?

标签: python model var attributeerror volatility


【解决方案1】:

在初始化 MarketCap 因子时,您在第 38 行缺少括号:

Market_Cap = (MarketCap() > 1000000000)

之后,您将在第 69 行收到 KeyError,因为您没有在管道的输出中添加“longs”(“shorts”也是如此)。

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 1970-01-01
    • 2018-07-07
    • 2021-07-06
    • 2022-07-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多