Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Creating a talib.ema in a custom class.

I have been reading the custom factors reference in the help section but I am having trouble understanding how to create a talib.ema inside a custom factor so i can then use it in the pipeline to get ema data. I keep getting errors left and right. If someone could show me a example that would be very helpful.

1 response

Here is the code im currently working with.

from quantopian.pipeline import CustomFactor, Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing as USEP
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.research import run_pipeline
import talib
import numpy
import pandas
from matplotlib.pyplot import subplots

class EMA8(CustomFactor):
def compute(self, today, assets, out, *inputs):
talib.ema(data.history(assets, 'price',10, '1d'), 8)

def make_pipeline():

base_universe = QTradableStocksUS()

ema = EMA8(inputs=[USEP.close],window_length=8)  


return Pipeline(  
    columns={  
        'sentiment_score': ema,  
    },  
    screen=base_universe  


)  

my_pipe = make_pipeline()
result = run_pipeline(my_pipe, '2015-05-05', '2015-05-05')
result.head()

Here are the errors

SecurityViolationTraceback (most recent call last)
in ()
29 )
30 my_pipe = make_pipeline()
---> 31 result = run_pipeline(my_pipe, '2015-05-05', '2015-05-05')
32 result.head()

/build/src/qexec_repo/qexec/research/api.py in run_pipeline(pipeline, start_date, end_date, chunksize) 488 chunksize,
489 pipeline_engine,
--> 490 holdout_manager,
491 )
492

/build/src/qexec_repo/qexec/research/_api.pyc in inner_run_pipeline(pipeline, start_date, end_date, chunksize, engine, holdout_manager) 732 adjusted_start_date,
733 adjusted_end_date,
--> 734 chunksize=chunksize,
735 )
736

/build/src/qexec_repo/zipline_repo/zipline/pipeline/engine.pyc in run_chunked_pipeline(self, pipeline, start_date, end_date, chunksize) 345 chunksize,
346 )
--> 347 chunks = [self.run_pipeline(pipeline, s, e) for s, e in ranges]
348
349 if len(chunks) == 1:

/build/src/qexec_repo/zipline_repo/zipline/pipeline/engine.pyc in run_pipeline(self, pipeline, start_date, end_date) 326 )
327
--> 328 results = self.compute_chunk(graph, dates, assets, initial_workspace)
329
330 return self._to_narrow(

/build/src/qexec_repo/zipline_repo/zipline/pipeline/engine.pyc in compute_chunk(self, graph, dates, sids, initial_workspace) 586 mask_dates,
587 sids,
--> 588 mask,
589 )
590 if term.ndim == 2:

/build/src/qexec_repo/zipline_repo/zipline/pipeline/mixins.pyc in _compute(self, windows, dates, assets, mask) 216 inputs = format_inputs(windows, inputs_mask)
217
--> 218 compute(date, masked_assets, out_row, *inputs, **params)
219 out[idx][out_mask] = out_row
220 return out

in compute(self, today, assets, out, *inputs)
10 class EMA8(CustomFactor):
11 def compute(self, today, assets, out, *inputs):
---> 12 talib.ema(data.history(assets, 'price',10, '1d'), 8)
13
14

/build/src/qexec_repo/qexec/algo/safety.py in getattribute(self, attr) 149 mute_greylist=mute_greylist,
150 module=module,
--> 151 module_wrapper=self,
152 )
153 finally:

/build/src/qexec_repo/qexec/algo/safety.py in check_attribute_access(full_path, whitelist, name_blacklist, caller_fr, module, module_wrapper, msg, mute_greylist) 99 violations=[{'message': strip_qexec_module_path(error_message),
100 'lineno': caller_fr.f_lineno,
--> 101 'extra': extra}])
102
103

SecurityViolation: 0002 Security Violation(s): Accessing talib.ema raised an AttributeError. No attributes with a similar name were found.