from zipline.api import attach_pipeline, pipeline_output
from zipline.api import date_rules, schedule_function, get_datetime
from zipline.pipeline import Pipeline
from zipline.pipeline.factors import CustomFactor
from zipline.pipeline.data import USEquityPricing
class TenDayRange(CustomFactor):
inputs = [USEquityPricing.high, USEquityPricing.low]
window_length = 2
def compute(self, today, assets, out, highs, lows):
#print("---", today, assets, highs, lows) #highs ,lows is nan
from numpy import nanmin, nanmax
highest_highs = nanmax(highs, axis=0)
lowest_lows = nanmin(lows, axis=0)
out[:] = highest_highs - lowest_lows
def make_pipeline():
tdr = TenDayRange()
return Pipeline(
columns={
'longs': tdr.top(2),
'shorts': tdr.bottom(2),
},
)
def initialize(context):
attach_pipeline(make_pipeline(), 'my_pipeline')
schedule_function(rebalance, date_rules.every_day())
def before_trading_start(context, data):
context.pipeline_data = pipeline_output('my_pipeline')
def rebalance(context, data):
# Pipeline data will be a dataframe with boolean columns named 'longs' and
# 'shorts'.
pipeline_data = context.pipeline_data
print get_datetime()
for asset in pipeline_data.index:
print ' ', asset.symbol, data.current(asset, 'price'), pipeline_data['longs'][asset], pipeline_data['shorts'][asset]
You can run it this way:
$ zipline run -f test6.py -s 2014-01-01 -e 2014-01-31 -b yahoo-bundle -o /dev/null
2014-01-02 21:00:00+00:00
AMD 3.95 True True
CERN 55.07 True True
2014-01-03 21:00:00+00:00
AMD 4.0 True True
CERN 54.7 True True
2014-01-06 21:00:00+00:00
AMD 4.13 True True
CERN 54.2 True True
2014-01-07 21:00:00+00:00
AMD 4.18 True True
CERN 55.259 True True
2014-01-08 21:00:00+00:00
AMD 4.18 True True
CERN 54.32 True True
2014-01-09 21:00:00+00:00
AMD 4.09 True True
CERN 55.02 True True
2014-01-10 21:00:00+00:00
AMD 4.17 True True
CERN 55.18 True True
2014-01-13 21:00:00+00:00
AMD 4.13 True True
CERN 53.83 True True
2014-01-14 21:00:00+00:00
AMD 4.3 True True
CERN 55.07 True True
2014-01-15 21:00:00+00:00
AMD 4.47 True True
CERN 55.52 True True
2014-01-16 21:00:00+00:00
AMD 4.38 True True
CERN 55.389 True True
2014-01-17 21:00:00+00:00
AMD 4.18 True True
CERN 55.74 True True
2014-01-21 21:00:00+00:00
AMD 4.17 True True
CERN 55.9 True True
2014-01-22 21:00:00+00:00
AMD 3.67 True True
CERN 57.02 True True
2014-01-23 21:00:00+00:00
AMD 3.62 True True
CERN 56.49 True True
2014-01-24 21:00:00+00:00
AMD 3.47 True True
CERN 54.709 True True
2014-01-27 21:00:00+00:00
AMD 3.41 True True
CERN 54.139 True True
2014-01-28 21:00:00+00:00
AMD 3.54 True True
CERN 54.9 True True
2014-01-29 21:00:00+00:00
AMD 3.48 True True
CERN 54.049 True True
2014-01-30 21:00:00+00:00
AMD 3.48 True True
CERN 56.27 True True
2014-01-31 21:00:00+00:00
AMD 3.43 True True
CERN 56.889 True True
[2017-04-16 19:42:54.778222] INFO: Performance: Simulated 21 trading days out of 21.
[2017-04-16 19:42:54.778380] INFO: Performance: first open: 2014-01-02 14:31:00+00:00
[2017-04-16 19:42:54.778475] INFO: Performance: last close: 2014-01-31 21:00:00+00:00
--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--BR,
Ed
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.