Setting custom lengths for fundamental factors

Back

import numpy as np
import pandas as pd
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import *
from datetime import timedelta
from System.Collections.Generic import List
from QuantConnect.Data.UniverseSelection import *
class QualityMomentumModel(QCAlgorithm):



def Initialize(self):
self.SetStartDate(2010, 1, 1) # Set Start Date
self.SetEndDate(2015, 1, 1)
self.SetCash(1000) # Set Strategy Cash
momentumlookbackdays = 126 #Momentum lookback
momentumskipdays = 10
overalllookback = 136
self.spy = self.AddEquity("SPY", Resolution.Minute) #add SPY to use for trends
#list of bond etfs for when markets down.
self.AddEquity("TLT").Symbol
self.AddEquity("IEF").Symbol
self.BONDS = ['TLT', 'IEF']
# Add bonds

self.stocks_to_hold = []
self.trend_up = 0
self.UniverseSettings.Resolution = Resolution.Minute #update the universe every minute
#adding a universe of stocks


#schedule function for making trades
self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 30), Action(self.trade))
self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.BeforeMarketClose("SPY", 30), Action(self.trade))



#15 Day moving average of SPY
self.spy_ma_fast = self.SMA("SPY", 10)
#100 Day moving average of SPY
self.spy_ma_slow = self.SMA("SPY", 100)

if self.spy_ma_fast >= self.spy_ma_slow:
self.trend_up = 1

self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)


def CoarseSelectionFunction(self, coarse):
#drop any securities that dont have fundamental data and cost less than $5
selected = [x for x in coarse if (x.HasFundamentalData)
and (float(x.Price) > 5)]


filtered = sorted(selected, key=lambda x: x.DollarVolume, reverse=True)
return [x.Symbol for x in filtered[:2000]]


def FineSelectionFunction(self, fine):
#filter with fundamentals
self.quality = sorted(fine,
key = lambda f: f.OperationRatios.ROIC.SixMonths, reverse=True)

self.high_quality = sorted(self.quality,
key = lambda f: f.OperationRatios.LongTermDebtEquityRatio.OneYear + f.ValuationRatios.CashReturn + f.ValuationRatios.FCFYield, reverse=True)

self.momentum = sorted(self.high_quality,
key = lambda f: f.ValuationRatios.CashReturn.overalllookback - f.ValuationRatios.CashReturn.momentumlookbackdays, reverse=True)


self.TQ = [x.Symbol for x in self.momentum[:5]] #search for top 5 equities with the highest ROIC

return self.TopTQ[:5]#take the 5 with the highest ROIC

def OnData(self, data):
pass


def trade(self):

self.stocks_to_hold = []
for i in self.TopTQ:
self.stocks_to_hold.append(i)

if self.trend_up == 1:
for i in self.Portfolio.Values:
if (i.Invested) and (i not in self.BONDS and self.stocks_to_hold):
self.Liquidate(i.Symbol)

for i in self.stocks_to_hold:
self.SetHoldings(i, 0.7)
for i in self.BONDS:
self.SetHoldings(i, 0.3)

In this algo, I want to use the difference in the returns for each equity to determine momentum. In self.momentum, I want to use custom lengths. But I get the error, 

'float' object has no attribute 'overalllookback'
at FineSelectionFunction in main.py:line 75

Is there some way to set a custom length for fundamental factors, rather than "OneYear" or "SixMonths"?

Update Backtest







 
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Jimothy,

Custom lengths for Fundamental Factors is not supported at the moment, however, you could store the data in a RollingWindow and do custom calculations to get the length desired. I provide an example of using RollingWindows with Fundamental data in the attached backtest.

Best,
Shile Wen

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed