Back

Trying to Replicate Algorithm from Liquid Value Stocks bootcamp

Hello, the algorithm created in the Liquid Value Stocks bootcamp doesn't include any market order code, so I tried adding it myself, but when I try to backtest it, I encounter this error:

 

Runtime Error: Value cannot be null.
Parameter name: source (Open Stacktrace)

I attached the code with the post; any help would be great, thanks!

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.


I'm sorry, it seems I wasn't able to attach the code, must be because the backtest resulted in an error so i can't attach the code, but this is it:

 

from datetime import timedelta
from QuantConnect.Data.UniverseSelection import *
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class NadionUncoupledPrism(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        self.SetEndDate(2020,1 ,1)
        self.SetCash(100000)
        self.AddUniverseSelection(LiquidValueUniverseSelectionModel())
        self.UniverseSettings.Resolution = Resolution.Daily
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())

class LiquidValueUniverseSelectionModel(FundamentalUniverseSelectionModel):

    def __init__(self):
        super().__init__(True, None, None)

    def SelectCoarse(self,algorithm, coarse):
        sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData], \
        key=lambda x: x.DollarVolume, reverse=True)
        
    def SelectFine(self, algorithm, fine):
        sortedByYields = sorted(fine, key=lambda f: f.ValuationRatios.EarningYield, reverse=True)
        self.universe = sortedByYields[:10]
        return [f.Symbol for f in self.universe]
        
    def OnSecuritiesChanged(self, changes):
        self.changes = changes
        
        for security in self.changes.RemovedSecurities:
            if security.Invested:
                self.Liquidate(security.Symbol)
                
        for security in self.changes.AddedSecurities:
            if not security.Invested:
                self.SetHoldings(security.Symbol, .10)

0

Okay, I apparently forgot to return the new list under def SelectCoarse(), I fixed that by adding:

return [x.Symbol for x in sortedByDollarVolume[:100]]

 

but now, it returns:

 

Backtest Handled Error: No data loaded for MET RTMI3UYBZ0IT because there were no tradeable dates for this security.
No data loaded for CAT R735QTJ8XC9X because there were no tradeable dates for this security.
No data loaded for IBM R735QTJ8XC9X because there were no tradeable dates for this security.
No data loaded for TWTR VLE97YS7S57P because there were no tradeable dates for this security.

 

I'm just trying to add the code for orders, any tips? Sorry for these very basic questions; this time I was able to attach the code!

0


Hi Vncne,

The way this algorithm handles universe selection follows Framework Algorithm design, but the algorithm lacks the other essential components of a Framework Algorithm (Alpha Creation, Portfolio Construction, and Execution). To fix this issue, the algorithm must either commit fully to the Classic Algorithm design, or commit fully to the Framework Algorithm design.

See the attached backtest for an example of a complete Framework Algorithm. In that backtest, the following changes are made to turn the algorithm into a complete Framework Algorithm:

1) In the Initialize method, the following three lines of code are added to set an alpha, set a portfolio construction model, and set an execution model:
        self.AddAlpha(ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(days=1), None, None))
        self.SetExecution(ImmediateExecutionModel())
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())

2) In class LiquidValueUniverseSelectionModel, the OnSecuritiesChanged method is removed. It does not have an impact on the outcome of the backtest.

See the Demonstration Algorithms section of the Universes page for examples of universe selection in Classic Algorithms.

Best,
Gahl Goziker

https://www.quantconnect.com/terminal/processCache/?request=embedded_backtest_81442532bd7537614e475aa9d864383c.html

0

Hi Gahl, thanks for the reply!

So before I saw your post, what I tried to do was learn the algorithm framework and try(very badly at that) to create a very bare bones framework.. The goal I had in mind was to create a liquid universe, but for the algorithm to only allocate to that liquid universe if a criteria has been met(e.g. SPY> 20-SMA), and to allocate to BND if the criteria had not been met. It has been, to say the least.. A huge mess... 

In my mind, if I wanted to incorporate a liquid universe into my algorithm, I also had to incorporate an entire algorithm framework because thats what the bootcamps seemed to imply. So how I approached it was to start with creating the Liquid Universe class, and then add a basic framework, and then somehow add a way for the algorithm to allocate to BND if the rules for allocating to the liquid universe had not been met. It's not working(duh..) But I'm not sure why; I attached the code and backtest to this post, I'm still checking out those links you sent, but so far I still can't figure out how to turn this idea into code, any tips? 

Thanks again!

0


Hi Vncne,

See the attached backtest, where the trading logic has been partially implemented in an Alpha Model.

Please note that the trading logic has been moved from the
OnSecuritiesChanged

method to the

Update

method. In Alpha Models, OnSecuritiesChanged gets called when securities are added/removed from the data feed. Update, on the other hand, is called whenever data comes in from any of those subscribed securities — this is the place to put trading logic.

Best,
Gahl Goziker

0


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