Back

Enhanced Short-Term Mean Reversion Algorithm

This algorithm is converted from Rob Reider Enhancing Short-Term Mean-Reversion Strategies.

Universe selection is implemented each month. In coarse universe selection, stocks with the price lower than 5 or without fundamental will be dropped. Then the top 50 stocks ranked by the dollar are selected. Fine universe selection picks the stocks with the positive EV to EBITDA in those top 50.
Those stocks are sorted into quintiles based on five-day returns(here mean reversion period is 5 days). The long and short stocks are filtered by the extreme momentum. The equally weighted portfolio is rebalanced daily at the close. Stocks were liquidated when they dropped out of the extreme quintile.

There are three enhancements in this algorithm:

  1. skip the most recent day when computing the five-day mean reversion return.(compute returns from five days ago up to the previous day’s close)
  2. Trade the stock with low volatility (volatility is sorted into trisection to filter the low volatility stock)
  3. The velocity of the benchmark is used to control the leverage (The algorithm is long unless SPY has a low enough velocity) - enhancement from Jacob Shrum

The cumulative return over the entire backtesting period from 2006 to present was about 680% and the Sharpe Ratio was 0.674.

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.



 

self.existing_longs = 0
self.existing_shorts = 0

Hello, I have tried using these lines of code in my own algorithm, but they dont do anything when i use them. They are meant to close any longs or shorts right?

0

Hi edvinas_jablonskis, I try to use self.existing_longs and self.existing_shorts here to record the number of existing positions. If you want to close the positions, you could use

self.Liquidate()

This will close all open positions in your portfolio. If you want to distinguish between long and short positions

# long position
if self.Portfolio[symbol].Quantity > 0:
self.Liquidate(symbol)

# short position
elif self.Portfolio[symbol].Quantity < 0:
self.Liquidate(symbol)
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.


That does exactly what i want it to do. Thank you so much!

0

When I clone the notebook and try to run a backtest, it continually seems to be stopping mid 2011, even though I have set self.SetEndDate(2018,1,27). Any ideas why this might be happening?

I actually tried to attach a backtest to show the problem, but unfortunately everytime I attach a backtest, the browser window crashes, sorry about that.

0

Hi Wei Chian Ong,

I cloned the algorithm and it works fine. Please see the algorithm below

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.


Jing Wu 

I am testing the algorithm Live on IB but when it places orders it shows as "Invalid".

I did not change the code, unless I need to add something before running it live?

Time,Symbol,Price,Quantity,Type,Status,Value,Tag
2018-05-31T15:00:01.391282Z,IBM,0,42,Market,7,0,
2018-05-31T15:00:06.156856Z,DIS,0,60,Market,7,0,
2018-05-31T15:00:11.000032Z,JNJ,0,49,Market,7,0,

https://imgur.com/a/FpRUIyw
0

Donkey Trader, please add OnOrderEvent to check the details of the invalid order

def OnOrderEvent(self, orderEvent):
   
if orderEvent.Status == OrderStatus.Invalid:
       
self.Log(str(orderEvent))
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.


Ok, thank you

0

Thanks for your reply Jing. I tried again with the newly posted algorithm and it stopped this time as well, but this time I got a log message saying I had exceeded 10,000 orders and that for unlimited orders I need to upgrade my account, so I have my answer, thank you.

0

I still cannot get the strategy to trade on IB live. Apart from the invalid order in the log, this is the other error I get:

Brokerage Info: System.Net.Sockets.SocketException (0x80004005): Connection refused. at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, System.Int32 port) [0x000e9] in <59be416de143456b88b9988284f43350>:0 . at System.Net.Sockets.TcpClient.Connect (System.String hostname, System.Int32 port) [0x00007] in <59be416de143456b88b9988284f43350>:0 . at System.Net.Sockets.TcpClient..ctor (System.String hostname, System.Int32 port) [0x00006] in <59be416de143456b88b9988284f43350>:0 . at IBApi.EClientSocket.createClientStream (System.String host, System.Int32 port) [0x00001] in <157d4e40593a4bff95e01b0ef064385d>:0 . at IBApi.EClientSocket.eConnect (System.String host, System.Int32 port, System.Int32 clientId, System.Boolean extraAuth) [0x00034] in <157d4e40593a4bff95e01b0ef064385d>:0
1

Jing Wu let me know if I can do anything to get it to work....

0

Jing Wu thanks so much for implementing this algorithm.  Could you explain what this portion does?

        # request the history of benchmark
        pri = self.History(["SPY"], 200, Resolution.Daily)
        pos_one = (pri.loc["SPY"]['close'][-1])
        pos_six = (pri.loc["SPY"]['close'][-75:].mean())
        # calculate velocity of the benchmark 
        velocity_stop = (pos_one - pos_six)/100.0
        SPY_Velocity = velocity_stop
 

Does it compare.... yesterday's close, against the close 75 days ago?  Or the mean of closes over the last 75 days?

0

I just read this. i'm late to the party. but wouldn't you want to rebalance bi weekly or once a month not every day? the transaction fees would eat up the capital. 

0

2018-09-29 12:07:21 Brokerage Info: System.Net.Sockets.SocketException (0x80004005): Connection refused.   at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, System.Int32 port) [0x000e9] in <59be416de143456b88b9988284f43350>:0 .   at System.Net.Sockets.TcpClient.Connect (System.String hostname, System.Int32 port) [0x00007] in <59be416de143456b88b9988284f43350>:0 .   at System.Net.Sockets.TcpClient..ctor (System.String hostname, System.Int32 port) [0x00006] in <59be416de143456b88b9988284f43350>:0 .   at IBApi.EClientSocket.createClientStream (System.String host, System.Int32 port) [0x00001] in <157d4e40593a4bff95e01b0ef064385d>:0 .   at IBApi.EClientSocket.eConnect (System.String host, System.Int32 port, System.Int32 clientId, System.Boolean extraAuth) [0x00034] in <157d4e40593a4bff95e01b0ef064385d>:0 

 

This error is filling my logs every 2 minutes, 5 times each 2 min interavl.  Is this affecting my performance, and if so how do I troubleshoot this.  I've read elsewhere this is intended(?) and if that is the case is there a way for me to clean my logs and allow this operation to happen in the background?  Any help would be greatly appreciated.

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