from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage,ExponentialWeightedMovingAverage
from quantopian.pipeline import CustomFactor,CustomFilter
import numpy as np
import pandas as pd
class StdDev(CustomFactor):
def compute(self, today, asset_ids, out, values):
# Calculates the column-wise standard deviation, ignoring NaNs
out[:] = numpy.nanstd(values, axis=0)
class SPY_Close_Price(CustomFactor):
'''
One can use any inputs from any assets and set any factor outputs based upon them.
In this case, simply return the close price of SPY for every asset.
'''
inputs = [USEquityPricing.close]
window_length = 1
def compute(self, today, assets, out, close):
spy_index = np.where(assets == 8554) # 8554 is the SID of SPY
# index -1 gets the latest price row. spy_index gets the SPY column.
out[:] = np.log(close[-1, spy_index])
def make_pipeline():
Asset_Px = (USEquityPricing.open.latest + USEquityPricing.high.latest + USEquityPricing.low.latest + USEquityPricing.close.latest)*0.25
Asset = np.log(Asset_Px)
Underlying = SPY_Close_Price()
Px = Underlying/Asset
#FastMA = SimpleMovingAverage(inputs=[Px], window_length=8)
#SlowMA = SimpleMovingAverage(inputs=[Px], window_length=34)
#Diff = np.abs(FastMA - SlowMA)
#AvgDiff = SimpleMovingAverage(inputs=[Diff], window_length=110)
#Diff_StdDev = StdDev(inputs=[Diff], window_length=110)
#RM_LimitLow= SlowMA + (AvgDiff + Diff_StdDev * 1.618)
#RM_LimitUpp= SlowMA + (AvgDiff + Diff_StdDev * 2.618)
return Pipeline(
columns={
'RM_LimitLow': Px,
#'RM_LimitUpp': FastMA
},
screen=Q500US()
)
result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')
result
def make_pipeline():
Asset_Px = (USEquityPricing.open.latest + USEquityPricing.high.latest + USEquityPricing.low.latest + USEquityPricing.close.latest)*0.25
Asset = np,log(Asset_Px)
Underlying = np.log(symbols('SPY'))
Px = Underlying/Asset
FastMA = ExponentialWeightedMovingAverage(inputs=[Px], window_length=8)
SlowMA = SimpleMovingAverage(inputs=[Px], window_length=34)
Diff = np.abs(FastMA - SlowMA)
AvgDiff = SimpleMovingAverage(inputs=[Diff], window_length=110)
Diff_StdDev = StdDev(inputs=[Diff], window_length=110)
RM_LimitLow= SlowMA + (AvgDiff + Diff_StdDev * 1.618)
RM_LimitUpp= SlowMA + (AvgDiff + Diff_StdDev * 2.618)
return Pipeline(
columns={
'Asset_Px': Asset_Px,
'Asset': Asset
},
screen=Q500US()
)
def make_pipeline():
Asset_Px = (USEquityPricing.open.latest + USEquityPricing.high.latest + USEquityPricing.low.latest + USEquityPricing.close.latest)*0.25
Asset = np.log(Asset_Px)
SPY = symbols('SPY').sid
Underlying = np.log(USEquityPricing[(USEquityPriceing.sid==SPY)].close.latest)
return Pipeline(
columns={
'Asset_Px': Asset_Px,
'Asset': Underlying
},
screen=Q500US()
)
result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')
result
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline import CustomFactor
import numpy as np
class StdDev(CustomFactor):
def compute(self, today, asset_ids, out, values):
# Calculates the column-wise standard deviation, ignoring NaNs
out[:] = numpy.nanstd(values, axis=0)
def make_pipeline():
FastMA = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=8)
SlowMA = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=34)
Diff = np.abs(FastMA - SlowMA)
AvgDiff = SimpleMovingAverage(inputs=[Diff], window_length=110)
Diff_StdDev = StdDev(inputs=[Diff], window_length=110)
return Pipeline(
columns={
'AvgDiff': AvgDiff,
'Diff_StdDev': Diff_StdDev
},
screen=Q500US()
)
class StdDev(CustomFactor):
def compute(self, today, asset_ids, out, values):
# Calculates the column-wise standard deviation, ignoring NaNs
out[:] = numpy.nanstd(values, axis=0)
class SPY_Close_Price(CustomFactor):
'''
One can use any inputs from any assets and set any factor outputs based upon them.
In this case, simply return the close price of SPY for every asset.
'''
inputs = [USEquityPricing.close]
window_length = 1
def compute(self, today, assets, out, close):
spy_index = np.where(assets == 8554) # 8554 is the SID of SPY
# index -1 gets the latest price row. spy_index gets the SPY column.
out[:] = np.log(close[-1, spy_index])
def make_pipeline():
Asset_Px = (USEquityPricing.open.latest + USEquityPricing.high.latest + USEquityPricing.low.latest + USEquityPricing.close.latest)*0.25
Asset = np.log(Asset_Px)
Underlying = SPY_Close_Price()
Px = Underlying/Asset
FastMA = SimpleMovingAverage(inputs=[Px], window_length=8)
SlowMA = SimpleMovingAverage(inputs=[Px], window_length=34)
Diff = np.abs(FastMA - SlowMA)
AvgDiff = SimpleMovingAverage(inputs=[Diff], window_length=110)
Diff_StdDev = StdDev(inputs=[Diff], window_length=110)
RMLow= SlowMA + (AvgDiff + Diff_StdDev)
RMUpp= SlowMA + (AvgDiff + Diff_StdDev)
return Pipeline(
columns={
'RMLow': RMLow,
'RMUpp': RMUpp
},
screen=Q500US()
)