I am trying to create a slow stochastic model from scratch using the pipeline for learning purposes (I understand that talib offers a library function for such a computation), however I am running into the following error at line 48 (labeled below) --> TypeError: 'lazyval' object is not iterable. Below is the algorithm I have constructed thus far.
Right now, my intention is to plot both K and D using the record() function to ensure that I am computing the values properly. If anyone could provide some insight, I would really appreciate it, thanks!
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.filters import StaticSids
import numpy as np
class K(CustomFactor):
window_safe = True
inputs = [USEquityPricing.high, USEquityPricing.low, USEquityPricing.close]
def compute(self, today, asset_ids, out, high, low, close):
h14 = np.max(high, axis=0)
l14 = np.min(low, axis=0)
c = close[-1]
out[:] = 100*(c - l14)/(h14 - l14)
def initialize(context):
context.stock = sid(19920)
set_benchmark(context.stock)
# Only trade longs, not shorts
set_long_only()
# Create our dynamic stock selector.
attach_pipeline(make_pipeline(context), 'my_pipeline')
def make_pipeline(context):
in_security_list = StaticSids([context.stock])
# the current market rate for the currency pair
k = K(window_length=14)
# 3-period moving average of k
d = SimpleMovingAverage(inputs=[K], window_length=3)
pipe = Pipeline(
screen = (in_security_list),
columns = {
'K': k,
'D': d,
}
)
return pipe
def before_trading_start(context, data):
# Pipeline output after applying the screen
context.output = pipeline_output('my_pipeline')
# These are the securities that we are interested in trading each day.
context.security_list = context.output.index # LINE 48
k_val = context.output.get_value(context.stock, 'K')
d_val = context.output.get_value(context.stock, 'D')
# plot stochastic indicator
record(K=k_val,D=d_val,mid=50,top=80,btm=20)
def handle_data(context, data):
pass