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)