In this post, I demonstrate how to construct the Alpha Model PanicToExuberance() where I apply indicators to a custom data.

The Alpha Model, PanicToExuberance(), uses data from URC containing the number of companies currently reaching new highs in stock price, within the NASDAQ exchange. It is likely that very few companies are reaching new highs when a recession is imminent. Likewise, many companies reach new highs when market participants are exuberant.

The Alpha Model uses smoothed Exponential Moving Average (EMA) to determine how many companies on average are reaching a new high and a custom indicator, Rank which reduces the number of parameters in the alpha. The indicator creates a scaled value representing the distribution of the data. Using this distribution, the Alpha emits signals when the current value is above a percentile threshold. This way is more robust than using a hard-coded absolute number of stocks.

For demonstration purposes, I also add the Max and Min indicators using a one year lookback period to construct the custom indicator Rank, which oscillates between 0 and 1, depending on the relative position of latest value to the high and low. When the value of this custom indicator exceeds a certain threshold the Alpha emits insights on SPY, predicting an increase in value during the prediction interval.

Constructing the Alpha

First, create the class QuandlData(), specifying the column name for the custom data field to be retrieved from Quandl:

class QuandlData(PythonQuandl):
# Custom data Quandl link: https://www.quandl.com/data/URC/NASDAQ_52W_HI-NASDAQ-Number-of-Stocks-Making-52-Week-Highs

def __init__(self):
## Retrieve the data from the the Quandl object, specifying the data field used on Quandl
self.ValueColumnName = "NUMBERS OF STOCKS"

# More URC market data can be found at Quandl
# https://www.quandl.com/data/URC-Unicorn-Research-Corporation

Then, create the Alpha Model class, PanicToExuberance() and set parameters and construct indicators in the method __init__(). Also, retrieve the custom data using algorithm.AddData() in this method:

class PanicToExcuberanceAlphaModel(AlphaModel):

def __init__(self, algorithm):

# Set resolution for EMA Indicator
self.resolution = Resolution.Daily

self.algorithm = algorithm

# Add custom data from Quandl
self.yearlyHighs = algorithm.AddData(QuandlData, 'URC/NASDAQ_52W_HI', Resolution.Daily).Symbol

# Add indicators using custom data
self.ema = algorithm.EMA(self.yearlyHighs, 5, self.resolution)
self.max = algorithm.MAX(self.yearlyHighs, 252, self.resolution)
self.min = algorithm.MIN(self.yearlyHighs, 252, self.resolution)

# Set the prediction period
self.period = timedelta(days=30)

 

Define the algorithm logic in the method Update(). Define the custom indicator, Rank, in this method and add to a custom chart using algorithm.Plot().

If the indicator rank exceeds 0.5, the Alpha emits insights, predicting the SPY to increase in value during the prediction period.

def Update(self, algorithm, data):
insights = []

# Return if no data
if not data.ContainsKey(self.yearlyHighs): return insights

# Return if indicators are not ready
if not (self.min.IsReady or self.max.IsReady): return insights

# Calculate the numerator of the custom indicator
delta = self.max.Current.Value - self.min.Current.Value

# Calculate the denominator of the custom indicator
scaled = self.ema.Current.Value - self.min.Current.Value

# Calculate the value of the custom indicator
rank = scaled / delta

# Plot the custom indicator
self.algorithm.Plot("Rank", "High Rank", rank)

# Emit insight on SPY if the indicator value exceeds 0.5
if rank > 0.50:
insights.append(Insight.Price("SPY", self.period, InsightDirection.Up, 0.001))

return insights