0

Need some help in figuring out the name of what this is called in Python.

A finance library I use (called Quantopian) has a pretty cool api. When defining a new financial algorithm, you would create a new file and simply define functions based off of various keywords. This python file then somehow gets passed to an executor which is then calling these functions.

Is there a name for this concept? Basically you seem to have some python code that gets passed a python file, and is able to call functions in this file (if they exist).

Here is an example of what the code would look like:

 from zipline.api import order_target, record, symbol

def initialize(context):
    context.i = 0
    context.asset = symbol('AAPL')


def handle_data(context, data):
    # Skip first 300 days to get full windows
    context.i += 1
    if context.i < 300:
        return

    # Compute averages
    # data.history() has to be called with the same params
    # from above and returns a pandas dataframe.
    short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean()
    long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="1d").mean()

    # Trading logic
    if short_mavg > long_mavg:
        # order_target orders as many shares as needed to
        # achieve the desired number of shares.
        order_target(context.asset, 100)
    elif short_mavg < long_mavg:
        order_target(context.asset, 0)

    # Save values for later inspection
    record(AAPL=data.current(context.asset, 'price'),
           short_mavg=short_mavg,
           long_mavg=long_mavg)
billybob2
  • 681
  • 1
  • 8
  • 17
  • It would help if you linked to the docs for the specific part you are referring to. – roganjosh Aug 22 '19 at 19:46
  • You are probably looking for the words `reflection` and/or `eval`. Reflection lets you inspect your own code at runtime (such as getting a list of functions and calling them), eval lets you execute python at runtime – Gillespie Aug 22 '19 at 19:49
  • For example: https://stackoverflow.com/questions/139180/how-to-list-all-functions-in-a-python-module – Gillespie Aug 22 '19 at 19:49
  • 1
    @Gillespie `eval` is probably not getting used here or any reflection is really being used here. I assume that the library in question simply assumes modules will have various attributes. This is pretty standard python duck typing. – juanpa.arrivillaga Aug 22 '19 at 19:50
  • 1
    The other comments are attempting to give you a technical answer. If you're asking strictly about the concept of this library's architecture, the term is _inversion of control_. It is something that sees a lot more use in enterprise languages like Java, but it is something that already exists in many places in Python on a much smaller scale, such as passing a lambda to `map()`. – JSON Brody Aug 22 '19 at 20:03
  • @roganjosh https://www.zipline.io/ is the documentation. The library is called zipline – jdoej Aug 22 '19 at 20:41
  • 1
    It's sometimes called a "plug-in" software architecture. – martineau Aug 22 '19 at 20:41

0 Answers0