Hey guys, I'm a Python/Quantopian noob, trying to figure out how to create a factor for IPOs. My idea is to take a 2 day window of closing price, and if the sum of the closing prices for those two days is NaN but the closing price for today IS a number (>0), then set the IPO factor to 1 for that stock.
Im having trouble figuring out how to do this programmatically without using a FOR loop to compare every value in the SUM array to every value in the TODAY array.
An IF statement in compute() fails because I don't understand how compute() iterates over stocks and how to pass the IF statement each INDIVIDUAL value of an array to compare, versus just the entire array, which throws the "truth value of an array is ambiguous" error.
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
import numpy as np
class isIPO(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 2
def compute(self, today, assets, out, EquityClose):
sumForBothDays = np.sum(EquityClose, axis=0)
today = EquityClose[0,:]
outVal = 0
if np.logical_and(np.isnan(sumForBothDays[:]) is True, today[:] > 0):
outVal = 1
out[:] = outVal
def initialize(context):
set_slippage(slippage.VolumeShareSlippage(volume_limit=0.0, price_impact=0.05))
set_commission(commission.PerTrade(0))
pipe = attach_pipeline(Pipeline(), name='pipe1')
IPOFactor = isIPO()
pipe.add(IPOFactor, name = 'IPOFactor')
pipe.set_screen(IPOFactor > 0)
def before_trading_start(context, data):
result = pipeline_output('pipe1')
context.result = result
print result.head(1000)