Not sure what to do about the key errors
:D
Not sure what to do about the key errors
:D
Hope you slept well. Here is the code that works. I ran a build on it for the same time range as you did and got no exceptions. the key error is due to the fact that we have no data for that symbol during that bar. The key line of code to include is before you order check to see if security in data.
import pandas as pd
import numpy as np
import talib
def initialize(context):
schedule_function(rebalance,date_rule=date_rules.month_start(),time_rule=time_rules.market_open())
schedule_function(close_orders,date_rule=date_rules.week_end(),time_rule=time_rules.market_close())
def before_trading_start(context):
fundamental_df = get_fundamentals(
query(
fundamentals.valuation.market_cap,
fundamentals.share_class_reference.is_primary_share,
)
.filter(fundamentals.asset_classification.morningstar_sector_code != 311)
.filter(fundamentals.asset_classification.morningstar_sector_code != 104)
.filter(fundamentals.asset_classification.morningstar_sector_code != 103)
.filter(fundamentals.share_class_reference.is_depositary_receipt == False)
.filter(fundamentals.share_class_reference.is_primary_share == True)
.filter(fundamentals.company_reference.primary_exchange_id != "OTCPK")
.filter(fundamentals.company_reference.country_id == "USA")
.filter(fundamentals.valuation.market_cap != None)
.filter(fundamentals.valuation.shares_outstanding != None)
.filter(fundamentals.operation_ratios.long_term_debt_equity_ratio > 0.5)
.filter(fundamentals.operation_ratios.days_in_payment < fundamentals.operation_ratios.days_in_sales)
.filter(fundamentals.cash_flow_statement.cash_flow_from_continuing_operating_activities < 0)
.filter(fundamentals.balance_sheet.goodwill_and_other_intangible_assets > fundamentals.balance_sheet.current_assets)
.filter(fundamentals.cash_flow_statement.free_cash_flow < 0)
.filter(fundamentals.valuation.shares_outstanding != None)
.filter(fundamentals.valuation.market_cap >= 100000000)
.filter(fundamentals.operation_ratios.roic < 0)
.filter(fundamentals.operation_ratios.roe < 0)
.order_by(fundamentals.valuation_ratios.ev_to_ebitda.desc())
.limit(20)
)
context.stocks = [stock for stock in fundamental_df]
context.fundamental_df = fundamental_df
update_universe(context.fundamental_df.columns.values)
def rebalance(context, data):
for stock in context.portfolio.positions:
if stock not in context.fundamental_df and stock in data:
order_target_percent(stock, 0)
weight = create_weights(context, context.stocks)
for stock in context.fundamental_df:
if weight != 0 and stock in data:
order_target_percent(stock, -weight)
record(Leverage = context.account.leverage)
def close_orders(context, data):
orders = get_open_orders()
if orders:
for o in orders:
cancel_order(o)
log.info("Canceling orders")
def create_weights(context, stocks):
if len(stocks) == 0:
return 0
else:
weight = 0.99/len(stocks)
return weight
def handle_data(context, data):
pass
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.