pythonzipline

Where is the python code for the methods used for zipline?


I'm quite a beginner to Python but in java, you normally have these imports showing exactly where the objects are coming from. For example, the import statement below tells me the SpringBootApplication object comes directly from the Spring Boot class and I can dive into the class to read all its method code:

import org.springframework.boot.autoconfigure.SpringBootApplication;

Now I'm looking at the zipline library in python:

https://github.com/quantopian/zipline

This is from their sample code on the main page of their github repo:

from zipline.api import (
    history,
    order_target,
    record,
    symbol,
)

So I have a look at the zipline folder to see whether there is an api file from which the methods history, order_target, record, symbol is imported as I want to read the underlying code that is driving these methods.

The code doesn't tell me much (https://github.com/quantopian/zipline/blob/master/zipline/api.py):

from .finance.asset_restrictions import (
    Restriction,
    StaticRestrictions,
    HistoricalRestrictions,
    RESTRICTION_STATES,
)
from .finance import commission, execution, slippage, cancel_policy
from .finance.cancel_policy import (
    NeverCancel,
    EODCancel
)
from .finance.slippage import (
    FixedSlippage,
    VolumeShareSlippage,
)
from .utils import math_utils, events
from .utils.events import (
    date_rules,
    time_rules
)

__all__ = [
    'EODCancel',
    'FixedSlippage',
    'NeverCancel',
    'VolumeShareSlippage',
    'Restriction',
    'StaticRestrictions',
    'HistoricalRestrictions',
    'RESTRICTION_STATES',
    'cancel_policy',
    'commission',
    'date_rules',
    'events',
    'execution',
    'math_utils',
    'slippage',
    'time_rules'
]

However, there is a file called api.pyi that seems to contain some text on what the methods I'm interested in does (https://github.com/quantopian/zipline/blob/master/zipline/api.pyi). For example, with the method record, it says:

def record(*args, **kwargs):
    """Track and record values each day.

    Parameters
    ----------
    **kwargs
        The names and values to record.

    Notes
    -----
    These values will appear in the performance packets and the performance
    dataframe passed to ``analyze`` and returned from
    :func:`~zipline.run_algorithm`.
    """

I thought maybe the code was sitting within zipline.run_algorithm and also had a look for the zipline/run_algorithm file but couldn't find it in the repo.

Where is the code saved for these methods in python? I just want to read the code to get a better understanding of how it works.


Solution

  • zipline is using a somewhat complex and unusual import structure. The clue is in this comment in api.py:

    # Note that part of the API is implemented in TradingAlgorithm as
    # methods (e.g. order). These are added to this namespace via the
    # decorator ``api_method`` inside of algorithm.py.
    

    If you look in algorithm.py you can see these record and the rest of these methods defined with the @api_method decorator. (And if you look in zipline/utils/api_support.py you can see the code for the decorator itself, which adds these methods to zipline.api by using setattr.)