Hello,
I'm new to quant trading, but I had an idea that I really wanted to test. I have a list of stocks and I want to build an algorithm that buys the ones with the most statistically low value of a custom factor and short the ones with the highest.
I've attached my algorithm so far and I'm having some trouble with pipeline.
I've built a 10 day moving average of this factor and a 30 day moving average. What I want is to calculate the percent difference and attach it to the pipeline and use my custom filter to screen stocks. I get an error saying that there is nothing to graph. I assume that the pieline output is different from what I think it is.
I also noticed that there are some errors with my execution code.
I would greatly appreciate any help
Thank you so much,
Dan
from quantopian.pipeline import Pipeline, CustomFilter, CustomFactor
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.factors import Latest
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
import numpy as np
class SidInList(CustomFilter):
inputs = []
window_length = 1
params = ('sid_list',)
def compute(self, today, assets, out, sid_list):
out[:] = np.in1d(assets, sid_list)
class CustomFactor_10 (CustomFactor):
inputs = [morningstar.balance_sheet.net_assets, morningstar.valuation.market_cap, USEquityPricing.close]
window_length=10
def compute (self, today, assets, out, net, market, close):
out[:] = close[-1]/ (net[-1]/market[-1])
class CustomFactor_30 (CustomFactor):
inputs = [morningstar.balance_sheet.net_assets, morningstar.valuation.market_cap, USEquityPricing.close]
window_length=30
def compute (self, today, assets, out, net, market, close):
out[:] = close[-1]/ (net[-1]/market[-1])
CF_10= CustomFactor_10()
CF_30= CustomFactor_30()
percent_diff = (CF_10-CF_30)/CF_30
def my_pipeline(context):
set_symbol_lookup_date('2016-1-1')
my_sid_filter = SidInList(
sid_list = (
symbol('AFT').sid,
symbol('ARDC').sid,
symbol('ACP').sid,
symbol('BHL').sid,
symbol('FRA').sid,
symbol('BGT').sid,
symbol('BSL').sid,
symbol('BGX').sid,
symbol('BGB').sid,
symbol('EFT').sid,
symbol('EFF').sid,
symbol('EFR').sid,
symbol('EVF').sid,
symbol('FCT').sid,
symbol('VVR').sid,
symbol('VTA').sid,
symbol('JQC').sid,
symbol('JRO').sid,
symbol('JFR').sid,
symbol('NSL').sid,
symbol('JSD').sid,
symbol('OXLC').sid,
symbol('TSLF').sid,
symbol('PPR').sid,
symbol('PHD').sid,
symbol('TLI').sid,
)
)
close = Latest(
inputs=[USEquityPricing.close,percent_diff],
mask = my_sid_filter,
)
pipe = Pipeline(
columns = {'close' : close,
'percent_diff' : percent_diff,
},
screen = my_sid_filter,
)
return pipe
def initialize(context):
attach_pipeline(my_pipeline, 'my_pipeline')
schedule_function(trade, date_rules.every_day(), time_rules.market_open())
def trade (context,data):
results = pipeline_output('my_pipeline')
securities_in_results = results.index
for sec in securities_in_results:
if data.can_trade(sec) & (percent_diff < 0):
order_target_percent(sec, 1.0 / len(securities_in_results))
if data.can_trade(sec) & (percent_diff > 0):
order_target_percent(sec,-1.0 / len (securities_in_results))