Hi,
I have some code that loads data from a CSV, that I execute in initialize(context). I also have a custom factor class that I have definitions to compute a signal in. However, I don't know how to get access to the data variable in that custom factor class.
The data from the CSV is accessible by netQty = data.current(data.fetcher_assets, 'netQty'). However, my custom factor class has no access to the variable "data". How do I get it into the class and the def compute method?
My code is below:
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import Q500US, Q1500US
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data import morningstar
import numpy as np
def initialize(context):
# Rebalance at the beginning of the month, 1 hour after market open.
schedule_function(rebalance,date_rules.month_start(),time_rules.market_open(hours=1))
# get csv of insider holdings data (loaded from lh2456's Google Drive)
fetch_csv('https://docs.google.com/spreadsheets/d/e/2PACX-1vQI-J2Ewg_XH6DD9GfTPfO7QQsCmHBujrKUKbdzek6jLU5uWHGmCRPPftvz8l8sM9ukemgUS6SRXYeu/pub?output=csv',
date_column='date',
date_format='%m/%d/%y')
# Create our dynamic stock selector.
attach_pipeline(make_pipeline(), 'pipeline')
# Trading cost module.
set_commission(commission.PerShare(cost=0.0, min_trade_cost=0))
set_slippage(slippage.VolumeShareSlippage(volume_limit=0.50, price_impact=0.0))
class ComputeInsiderSignal(CustomFactor):
# how do I get the CSV into the compute custom factor class?
# Want access to netQty variable here.
inputs = [morningstar.valuation_ratios.pb_ratio]
window_length = 1
def compute(self, today, assets, out, pb_ratio):
out[:] = 1.0/pb_ratio[-1]
def make_pipeline():
# Base universe set to the Q1500US.
base_universe = Q1500US()
# Compute the factor.
InsiderSignal = ComputeInsiderSignal(mask=base_universe)
# Take the top 50 and bottom 50 signals.
longs = InsiderSignal.top(50)
shorts = InsiderSignal.bottom(50)
securities_to_trade = (shorts | longs)
return Pipeline(
columns={
'Longs': longs,
'Shorts': shorts,
'InsiderSignal': InsiderSignal,
},
screen=(securities_to_trade),
)
def before_trading_start(context, data):
# Get our database of signals every day.
pipe_results = pipeline_output('pipeline')
# read the net quantity of insider trades (all) from the fetched data
netQty = data.current(data.fetcher_assets, 'netQty')
# print out data (can access the data here, since we have access to data variable)
print netQty
print type(netQty)
print pipe_results.sort_values('InsiderSignal',ascending = False).head()
print pipe_results.sort_values('InsiderSignal',ascending = False).tail()
# Get a list of securities that we can trade and that we want to buy.
context.longs = []
for sec in pipe_results[pipe_results['Longs']].index.tolist():
if data.can_trade(sec):
context.longs.append(sec)
# Get a list of securities that we can trade and that we want to short.
context.shorts = []
for sec in pipe_results[pipe_results['Shorts']].index.tolist():
if data.can_trade(sec):
context.shorts.append(sec)
def rebalance(context, data):
long_weight = 0.5 / len(context.longs)
short_weight = -0.5 / len(context.shorts)
for security in context.portfolio.positions:
if security not in context.longs and security not in context.shorts and data.can_trade(security):
order_target_percent(security, 0)
for security in context.longs:
order_target_percent(security, long_weight)
for security in context.shorts:
order_target_percent(security, short_weight)
record(NoLongs = len(context.longs), NoShorts = len(context.shorts))