Hello,
I'm new and am running into this wall I can't get over. If someone would be so kind as to help me understand this error more and how to correct it, I'd appreciate it.
I'm watching a video from a Quantopian employee, and simply watching, inputting the code, and seeing how it works. I am watching this video if anyone is interested in the source: https://www.youtube.com/watch?v=j1FX36bO9ds
There are two errors I'm trying to remedy. The first one is an attribute error for line 62 of the code. I simply do not know what is wrong with the code, it does say " AttributeError: 'Latest' object has no attribute 'eq' " I assume that the I've defined something inappropriately, but some guidance would be great.
ERROR 1:
Here's a code snippet:
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
#The fundamental data is from a partnership with MorningStar
from quantopian.pipeline.data import morningstar
def initialize(context):
#Every pipeline must be named so that you can reference it, in the future you can reference multiple pipelines simultaneously
pipe = Pipeline()
attach_pipeline(pipe, 'top500')
# Get the latest revenue value
revenue = morningstar.income_statement.total_revenue.latest
pipe.add(revenue, 'revenue')
# Rank all companies based on revenue , Ascending = False means 1st return will be the largest revenue company.
ranked_rev = revenue.rank(ascending=False)
# in ( ) will be a requirement of what the data will be, and what the name will be. This should be the case everytime.
pipe.add(ranked_rev, 'rev_rank')
# Get the share class using latest to get the most recent value
share_class = morningstar.share_class_reference.is_primary_share.latest
#Filter out any companies without revenue and all non-primary share classes
#Filter out companies with revenues only greater than $1, and share_class.eq(1) which are primary shares being traded,
# I believe leveraged stocks will not be included in this, VXX, UVXY, etc.
#share_class.eq(1) means shares that are tradeable. 1 means securities that are primary shares, 0 means they are not. Value is either 1 or 0.
# 'pipe.set_screen((revenue > 1) & (share_class.eq(1)))'share_class.eq object error for .eq .
# pipe.set_screen(revenue > 1)
pipe.set_screen((revenue > 1) & (share_class.eq(1)))
pipe.set_screen((revenue > 1) & (share_class.eq(1))) is the line that triggers the error response.
ERROR 2:
I'm going to ask the second question here and hope this chain or replies doesn't become clustered. fingers crossed
So I commented the previous error out and ran the code again and it worked and began to backtest for a short period until I ran into this new error: " KeyError: 'my_leverage' "
I don't think my_leverage is ever defined properly, but I again am so new to coding that I honestly can't say with any confidence other than the line where it says 'my_leverage' is the first time it appears in the algo.
Here is the code snippet:
def handle_data(context, data):
# Record and plot the leverae of our portfolio over time.
record(leverage = context.account.leverage)
# This rebalancing is called according to our schedule_function settings.
def rebalance(context, data):
weight = context.my_leverage / float(len(context.top500))
for stock in context.top500.index:
if stock in data:
order_target_percent(stock, weight)
for stock in context.portfolio.positions.iterkeys():
if stock not in context.top500.index:
order_target(stock, 0)
Thanks for any help! I realize this is length, but it is two separate errors please so clarity please refer to them as ERROR 1 and ERROR 2 when responding.