Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Fundamentals SQLAlchemy Model in zipline

As zipline doesn't support get_fundamentals() I'm trying to implement an own SQLAlchemy Model that totally replicates the original one and then feed it with my data.

Unfortunately I cannot really figure out how to do it and I hope the the Quantopian developer could help.

Actually I defined this model (it's of course not complete):


Base = declarative_base()

class Balance_sheet(Base):  
    __tablename__ = 'balance_sheet'  
    id = Column(Integer, primary_key=True)  
    total_assets = Column(Integer)  
    cash = Column(Integer)  
    # and so on ...  
class Income_statement(Base):  
    __tablename__ = 'income_statement'  
    id = Column(Integer, primary_key=True)  
    ebit = Column(Integer)  
    ebitda = Column(Integer)  
    # and so on ...  
class Fundamentals(Base):  
    __tablename__ = 'fundamentals'  
    id = Column(Integer, primary_key=True)  
    symbol = Column(String)  
    date = Column(Date)  
    balance_sheet_id = Column(Integer, ForeignKey('balance_sheet.id'))  
    balance_sheet = relationship("Balance_sheet", backref="fundamentals", uselist=False)  
    income_statement_id = Column(Integer, ForeignKey('income_statement.id'))  
    income_statement = relationship("Income_statement", backref="fundamentals", uselist=False)  
    def __init__(self, symbol, date):  
        self.symbol = symbol  
        self.date = date

It works to query like this:


q = s.query(Balance_sheet).filter(Balance_sheet.total_assets > 0).options(joinedload('fundamentals'))  
df = pd.read_sql(q.statement,s.bind)  
df.index = df['symbol']

but not like the original Quantopian API, that is i.e.:


s.query(fundamentals.balance_sheet.total_assets).filter(fundamentals.balance_sheet.total_assets> 0)
4 responses

Nobody can help?
For me would be very useful to integrate the get_fundamentals API in zipline and then offer an ingest mechanism similar to the price data.

Well, I implemented this API.
The project is in github: https://github.com/alphaville76/Fundamentals/blob/master/query_data.py
Actually I use the Quandl dataset SF1-Core-US-Fundamentals-Data (https://www.quandl.com/data/SF1-Core-US-Fundamentals-Data/documentation/indicators) but you can add your own Mornistar importer. Take a look at the mapping in https://github.com/alphaville76/Fundamentals/blob/master/insert_data.py

It's of course a first alpha version but it already usable. It's backed by a mysql database.
I'm open to community's feedback, let me know you thougths!

@Costantino: nice job - thank you for sharing!