I try to create pipeline with ADX, which doesn't work. Any help would be appreciable.
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.algorithm import order_optimal_portfolio
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
from quantopian.pipeline.filters import Q1500US
from quantopian.pipeline.filters import Q500US
import numpy as np
import math
import pandas as pd
import quantopian.optimize as opt
class MultipleOutputs(CustomFactor):
inputs=[USEquityPricing.high, USEquityPricing.low, USEquityPricing.close]
outputs = ['ADX','nDI','pDI']
window_length = 30
def compute(self, today, assets, out, high, low, close):
import talib
out.ADX[:]=talib.ADX(high,low,close,15)[-1]
out.pDI[:] = talib.PLUS_DI(high,low,close,15)[-1]
out.nDI[:] = talib.MINUS_DI(high,low,close,15)[-1]
ADX,pDI,nDI = MultipleOutputs()
multiple_outputs = MultipleOutputs()
ADX = multiple_outputs.ADX
nDI = multiple_outputs.nDI
pDI = multiple_outputs.pDI
def initialize(context):
# Schedule our rebalance function to run at the start of
# each week, when the market opens.
schedule_function(
my_rebalance,
date_rules.week_start(),
time_rules.market_open()
)
# Record variables at the end of each day.
schedule_function(
my_record_vars,
date_rules.every_day(),
time_rules.market_close()
)
# Create our pipeline and attach it to our algorithm.
my_pipe = make_pipeline()
attach_pipeline(my_pipe, 'my_pipeline')
def make_pipeline():
"""
Create our pipeline.
"""
# Base universe set to the Q1500US.
base_universe = Q1500US()
# Filter to select securities to short.
shorts = ADX<20 and nDI< pDI
# Filter to select securities to long.
longs = ADX>20 and nDI> pDI
# Filter for all securities that we want to trade.
securities_to_trade = (shorts | longs)
return Pipeline(
columns={
'longs': longs,
'shorts': shorts
},
screen=(securities_to_trade),
)
def compute_target_weights(context, data):
"""
Compute ordering weights.
"""
# Initialize empty target weights dictionary.
# This will map securities to their target weight.
weights = {}
# If there are securities in our longs and shorts lists,
# compute even target weights for each security.
if context.longs and context.shorts:
long_weight = 0.5 / len(context.longs)
short_weight = -0.5 / len(context.shorts)
else:
return weights
# Exit positions in our portfolio if they are not
# in our longs or shorts lists.
for security in context.portfolio.positions:
if security not in context.longs and security not in context.shorts and data.can_trade(security):
weights[security] = 0
for security in context.longs:
weights[security] = long_weight
for security in context.shorts:
weights[security] = short_weight
return weights
def before_trading_start(context, data):
"""
Get pipeline results.
"""
# Gets our pipeline output every day.
pipe_results = pipeline_output('my_pipeline')
# Go long in securities for which the 'longs' value is True,
# and check if they can be traded.
context.longs = []
for sec in pipe_results[pipe_results['longs']].index.tolist():
if data.can_trade(sec):
context.longs.append(sec)
# Go short in securities for which the 'shorts' value is True,
# and check if they can be traded.
context.shorts = []
for sec in pipe_results[pipe_results['shorts']].index.tolist():
if data.can_trade(sec):
context.shorts.append(sec)
def my_rebalance(context, data):
"""
Rebalance weekly.
"""
# Calculate target weights to rebalance
target_weights = compute_target_weights(context, data)
# If we have target weights, rebalance our portfolio
if target_weights:
order_optimal_portfolio(
objective=opt.TargetWeights(target_weights),
constraints=[],
)
def my_record_vars(context, data):
"""
Record variables at the end of each day.
"""
longs = shorts = 0
for position in context.portfolio.positions.itervalues():
if position.amount > 0:
longs += 1
elif position.amount < 0:
shorts += 1
# Record our variables.
record(
leverage=context.account.leverage,
long_count=longs,
short_count=shorts
)
print context.portfolio.positions[context.aapl].amount