0
我試圖建立一個Quantopian backtester(它使用Python)和運行在一個Quantopian回溯測試的算法,我建的時候,我收到錯誤:「有你的開始回溯測試有問題」如何在Quantopian/Python中添加條件?
因此,我希望測試人員尋找市值不足3億美元的公司,其自由現金流爲負值,股本收益爲負,且淨利潤率爲負值。
我相信我的測試人員可以正確地找到這樣的公司。當我告訴我的測試人員購買這些公司的股票時,我認爲問題出在「try:」區塊。我使用:
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:
什麼是這些條件中的「嘗試:」相結合的正確方法塊?
這裏是整個代碼...
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))
感謝。如果context.fundamentals [stock] ['market_cap'] <11且context.fundamentals [stock] ['free_cash_flow'] <0且context.fundamentals [stock] ['net_margin'] <0且上下文,則該行是: .fundamentals [stock] ['roe'] <0: ...正確嗎?我的意思是,這是在Quantopian中添加上下文的正確方法嗎? – Brandon
其實,我在該行看到一個錯誤。關於市值,「11」應該是「300000000」。但那不是在這裏,也不在那裏。 – Brandon
我想你可以問問你的股票是否在context.fundamentals。如果不是,它已經過濾了。你不需要再問:「如果stock.fundamentals:'應該足夠。 – xvan