Back

Momentum Strategy with Market Cap and EV/EBITDA

This momentum universe selection strategy is converted from Johnny Wu's EV/EBITDA Value, then momentum. 

In coarse universe selection, the algorithm picked the stocks with fundamental data and excluded the stocks with low price. In fine universe selection, filtered the stocks with the positive EV/EBITDA ratio and high market cap. Here market cap was calculated by using the BasicAverageShares in earnings reports. price per share was extracted from history request. In fine universe selection, self.Porfolio[symbol].Price cannot be used to get the current price as the price data are not subscribed. 

Then the universe was ranked by EV/EBITDA and top 100 stocks were selected. The resulting screen was sorted according to the return of past 200 days. The algorithm picked the top  10 high momentum stocks. The universe is rescreened and rebalanced every month.

The strategy also added a moving average(SPY) market exit towards long-term treasuries TLT to control the downside risk. If the current SPY price is lower than last N-day moving average price, then liquidate all the open positions and buy TLT.

Update Backtest






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 Jing,

    The examples you posted are very helpful. Since I am more interested in intraday trading, could you post some intraday trading example. Such as the one here (by  Chuck Reilly  Dec 4, 2017)

 

 

Many thanks.

Mike

0

Hi Mike,

We don't support sentimental data and the optimization module now. The conversion of this intraday algorithm would be quite different. If there are any other intraday algorithms which can be converted welcome to post it and we'll try to add more examples.

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 found it's hard to calculate MarketCap. Can Quantconnect just provide a simpler way like 'Fundamentals.market_cap.latest' as Quantopian does? MarketCap can be found on Morningstar's valuation section. Using BasicAverageShares in 3 months is onerous and rough.

0

Awesome! Thank you for sharing, just started with QC and Python and this example is very helpful. Love the monthly frequency too, easier to manage and replicate in real life. I'm going to try this with GLD instead of TLT and GLD/TLT 50/50 to see how those alternatives perform.

Looks like the liquidation to the safe haven fund occurs at the beginning of each month, at first I thought it would need to be monitored daily for the exit strategy, but it looks like that's monthly too. Pretty amazing results, almost to good to be true.

0

Ran it with GLD, not as good as TLT.

0


Is there a reason the resolution for SPY is at minute rather than daily here?:

self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
 
0

Hi Ton80, "SPY" here is used as the benchmark to fire schedule event functions. Here I request the minute data of SPY as time rules in two functions are specific time within a day.

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.


Hi Jing,

Since your time rules are set to the top of the hour, can the SPY resolution be "hour" then?  Also,  is there a performance difference if that line is set to hour versus minute?

Thanks!

0

Hi Jon Quant, "hour" resolution for SPY also works here. There is no performance difference between hour and minute here.

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 Jung Wu

Just a quick question: How did you calculate the market cap?

float(x.EarningReports.BasicAverageShares.ThreeMonths) * hist.loc[str(x.Symbol)]['close'][0] > 2e9]

Is the above it? Can you expalin about this? (what is 2e9?) Thanks.

0

Hi hanbyul_p

Yes this is how I calculate the market cap. Here price per share was extracted from the one-day history request. The price is yesterday's close

yesterday_close = hist.loc[str(x.Symbol)]['close'][0]

In fine universe selection, you cannot use self.Porfolio[symbol].Price as the price data is not subscribed. 2e9 is exponential notation which means I filter stocks with the market cap being greater than 2000000000.

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.


Jing Wu, Thanks !

0

Hi Jing,

Tried running this yesterday afternoon through 3/1/2018 and received this error:

0

Hi Jung Wu ,

Nevermind, I had enddate set to 3/2/2018 and I think that caused the error since I was running it on the night of 3/1. To get the trade signals from this strategy do we have to wait until a certain time on the first or the second trading day of the month?

Thanks again!

0

Hi Ton80, this is a monthly rebalance algorithm. The rebalance() function fires at the start of each month. You need to wait until the start of the next month to get the trade signals.  

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.


We explore a new method to approximate the market capitalization. The formula is 
market cap = Shares Outstanding * Price = Shares Outstanding * (Earnings Per Share * PE ratio)

In terms of fundamental variables in LEAN, 

  1. Shares outstanding is EarningReports.BasicAverageShares.ThreeMonths.
  2. Earnings Per Share is EarningReports.BasicEPS.TwelveMonths.
  3. PE Ratio is ValuationRatios.PERatio.

Instead of requesting the history close to calculate the Market cap, this method uses the fundamental data directly and could save the memory.

The updated algorithm is attached for this new method.

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 @Jing Wu, I clone your algorithm and do the backtest.
However, I got errors all the time about the order quantity can't be calculated.
How to solve this? Thank you a lot.
 

159 | 05:59:23: Backtest Handled Error: The order quantity for LMT cannot be calculated: Reason: .160 | 05:59:23: Your algorithm messaging has been rate limited to prevent browser flooding.161 | 06:00:48: Backtest Handled Error: The order quantity for LMT cannot be calculated: Reason: .
0

Hi Xi Liu, this error comes from self.Setholdings(symbol, 0). Setting the holding weight to 0 would cause the calculation of order quantity to be zero if there are no holdings for that symbol in your portfolio. This could generate the error message. It is better to use self.Liquidate(symbols) to avoid the error message. These two statements are the same here and will liquidate all holdings and cancel open orders of the specified symbol.

# instead of self.SetHoldings(symbols, 0)
self.Liquidate(symbol)

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 Jing Wu, can I think as:

self.SetHoldings(symbols, 0) & self.Liquidate(symbol) both works, even 'SetHoldings' may send error message?
Thank you!
0

Yes, they both work. We already fixed this problem please see the PR here

https://github.com/QuantConnect/Lean/pull/1665
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 Jing Wu, Just a quick question: Why is this algo waiting for a month and starting to run from a second month? Is it for warming up? Can we modify this algorithm to run from the first month? Just remove the flags? Please let me know. Thank you. 

0

Thank Jing.

market cap = Shares Outstanding * Price = Shares Outstanding * (Earnings Per Share * PE ratio)

That's what I was waiting for. It's a huge improvement for my algo.

0

Hi Jing Wu,

1. I ran the Algo, and found the exit function(when current SPY price is lower than last N-day moving average price, then liquidate all the open positions and buy TLT.) doesn't work all the time. The exit function doesn't work, making it performs bad on crisis.

The momentum strategy has relatively higher volitility. I think one of the drawback of momentum strategy is failing to act quickly to close a bad position. I'm considering to add a Stop Market Order of the portfolio composite seperately. However, I'm new to do an alogo with python, after reading the Documentation and browing the internet, I only learned how to add StopMarket order while making market order, like this:

            self.MarketOrder(self.baba, 300)
            self._stopMarketTicket = self.StopMarketOrder(self.baba, -300, slice['BABA'].Close * Decimal(0.95), "stop market")

This algo's code seems complex to me, I can't find the place to get the price and deploy the StopMarket order.

Can you give me some instructions on this problem?

 

2. I want to test the algo with leverage, for example, 1.5 leverage then I modify LINE 96:

        weight = 0.99/len(chosen_df)
         for symbol in chosen_df.index:
            self.AddEquity(symbol)
            self.SetHoldings(symbol, weight)  

I change 0.99 to 1.5, but it doesn't work. Do you know how to set the weight here?


Thank you a lot.
0

I have sprinkled a bunch of debug lines in the code just to analyze the behavior and I have noticed that if I set the following dates:

SetStartDate(2004, 1, 1);
SetEndDate(2004, 4, 1);

...the value for the "symbols" variable in "FineSelectionFunction" gets assigned on 01/03/2004 only to be used by the "rebalance" function a month later, 02/02/2004.  This tells me that the rebalance is operating on a fine "symbols" list that is a month old.  Is this intentional?

One other behavior that I have noticed is that the "CoarseSelectionFunction" and "FineSelectionFunction" never gets called on the "MonthStart" day.  In my example date window, it never gets called on 01/02/2004.  Is the "MonthStart" schedule somehow interfering with the Coarse and Fine events?

Thanks!

0

Hi Xi Liu,

You can use self.StopMarketOrder() everywhere in your algortihm. For the stop price, you can use

"self.Securities["BABA"].Close" * Decimal(0.95) instead of "slice['BABA'].Close * Decimal(0.95)"  because you can only get the slice data in OnData() method.

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 Jing Wu, I cloned your new method (for Market Cap) algo and ran for 2015 to March 1st of 2018. I found that the leverage went up to 500% (5x) in the early 2016 and came down to 300% (3x) in the end of this backtest period. You set the maximum weight to 0.99x as below. I guess something should be fixed. Can you take a look and fix the leverage control (e.g. maximum 2.0x)? Thank you.

weight = 0.99/len(chosen_df)
0


Hi Jing Wu,

I ran this today 4/2/2018 and was looking for the trade signals to occur by 10am PST since that seems to be the time the trades occur in the backtesting. Do we need to wait until end of first day of the month or should the signals be there by 10am PST, or another time?

When setting this up for trading live what types of alterations might need to be made? Is there a page/link that describes this or is this something support helps with after creating a live account?

Thank you!

0

Jing Wu,

Also seeing errors like this:

Backtest Handled Error: The order quantity for TLT cannot be calculated: the price of the security is zero. 

Doesn't look like April 2018 TLT trade is getting executed because of it. Could it be that the algo is looking at the first of the month for the price but isn't seeing it because the first falls on a weekend?

0

Hi, Jing, I am new to QuantConnect so a little confused on your code. 

it seems that you do everything on Intialize() and pass on  OnData(), does it mean Initialize() will run many times instead of just setting the initialial condition?

0

nope only once

everything?? settings holdings are in rebalance ....

check the bootcamp(-tab) in the algorithm lab

0

I'm a little bit confused by the code snippet below. We check the moving average exit gate, and if it shuts, we loop through each position, if it's not TLT (bonds), then we exit the position. That makes sense.

if self.Securities[self.spy].Price < spy_hist.mean():
for symbol in self.Portfolio.Keys:
if symbol.Value != "TLT":
self.Liquidate()

But, when we use self.Liquidate() and don't specify a security, doesn't it just liquidate every single position in the portfolio? Why bother to loop through the Portfolio keys at all, if we are just going to liquidate everything anyway? In my head it seems that self.Liquidate(symbol) or self.Liquidate(symbol.Value) would make more sense. Can someone tell me what I am missing?

0

 

Wei Chian Ong, what the code Liquidate() will do is possibly more tricky than you imagined. I suggest you check the code inside ideally debug carefully by using VS.

0

Thanks for the reply. I've had a look at your previous post regarding the self.Liquidate() method, and my question probably doesn't run as deep as the intricacies of dark pools and slippage in actual trading. It's more of a question regarding the coding itself. The documentation and this forum post and reply seem to indicate that using self.Liquidate() without a security specified, will liquidate all positions, and I was having trouble understanding why we bother looping through each symbol in the portfolio and checking if it's not treasury bond ETF, if we go ahead and liquidate everything anyway, would it not make more sense to use self.Liquidate() with the actual position we are currently looping through?

0

I was not meaning dark pools. I was just saying we should look inside the detail codes of Liquidate() in VS if there is something confused.

0

I'm not familiar with how to use visual studio (I assume that's what VS stands for), so I'll have to try it using log or debug calls.

0

I cloned this algorithm and received an error "Invalid Token" on the self.SetStartDate function, so am unable to run or backtest.  Is anyone receiving this same error?  Is there an easy fix?

0

I think I kept getting the same Nick and I wasn't sure why either. I deleted the code for the self.SetStartDate and self.SetEndDate and then re-typed them manually in case it was a whitespace issue or something (probably a very inefficient way to do that anyway). Same problem, but I repeated that several times and eventually it worked. Not a very scientific response to your question .......

0

The invalid token is from python 2.7 to python 3.6 upgrade. Since python 3.6 numbers can't be prefixed with 0 -- e.g. "01"

If you cloned an old backtest it can have dates with 2001, 01, 01 etc. 

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.


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