This is a pairs trading strategy based on the copula method. Please look into tutorial page for details. In order to compare the performance of copula pairs trading technique, I also implemented with cointegration method for comparison. Leave a comment if anyone has questions or suggestion about my implementation.
HanByul P
Jingw, Thank you for your explanation. Regarding the quantity of each leg, I think it's understandable. But I still don't understand why trading from year 2017 (see backtest right above.) looks so disappointing. Compared to the backtest starting 2016, two results are not giving me any level of confidence. If I deployed this algo in Jan. 2017, I would've gotten so bad result. However, if I did in Jan. 2016, it would've been totally a different story. What do you think? Is there any other way to improve regarding this aspect? How will this algo go in live trading? Thanks :)
Jing Wu
HanByul, It is reasonable. As in algorithm, we just choose copula once and use this copula in the following days. The only thing that changes is the parameter of that copula. Your backtest starting from 2016 and starting from 2017 use the different history data to choose the copula, they might choose the different copula and the algorithm will use this copula for the following backtesting. You can control the copula you use by ignoring the copula selection process to reach the same result.
HanByul P
Jingw, Thanks for all the answers and explanation. I will dig into more about 'copula' method and see if we can implement for multi-pairs instead of picking one pair (If you've already done for multi-pairs, please let us know. I appreciate it.). Again, thank you for your great work and the opportunity to explore the 'copula' method. :)
Roku niju
your universe is: tick_syl = [["SPY","AGG","XME","TNA","FAS","XLF","EWC","QLD"],
["DIA","JNK","EWG","TLT","FAZ","XLU","EWA","QID"]]
as a non-programmer, I'm wondering if I can replace names here and run backtest with my names? I've tried and gotten errors
Dennis Stander
If for a Gumbel copula we get a combination of (u,v) of (1,1) -> the pdf of your algorithm returns an infinity value.
Jing Wu
Hi Dennis,
When (u, v) -> (1, 1), log(u) and log(v) are zero so the Gumbel pdf will be "inf". u and v are constructed with the empirical distribution function from the log return series, there must be a 1 in u or v. Therefore, in the original algorithm I try to convert the infinity value to the finite number with
# Replace nan with zero and inf with finite numbers in lpdf list
lpdf = np.nan_to_num(lpdf)
The finite numbers are still huge so the sum would be inf. A better solution would be converting the 1 in u or v to a number less than 1 for example 0.9999.
Nagendra
Hi,
I cloned the file and tried to run a backtest but its giving me an error
During the algorithm initialization, the following exception has occurred: TypeError : 'numpy.float64' object is not callable
at Initialize in main.py:line 61
at <listcomp> in main.py:line 61
at _lpdf_copula in main.py:line 180
TypeError : 'numpy.float64' object is not callable
Please let me know how to fix this error.
Here's the log
Nagendra
I recloned main.py and now its giving me divide by zero
Jared Broad
# compute today's log return of 2 stocks if len(self.price_list[self.syl[0]]) < 3 or len(self.price_list[self.syl[1]]) < 3: return else: return_x = np.log(float(self.price_list[self.syl[0]][-1]/self.price_list[self.syl[0]][-2])) return_y = np.log(float(self.price_list[self.syl[1]][-1]/self.price_list[self.syl[1]][-2]))
Tweaked it to check for at least 3 days. The -2 position wasn't set yet.
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.
Nagendra
Thanks Jared! worked like a charm
Nagendra
One more question, I am trying to use similar strategy for currencies, is it possible to change the ticker to use currency?
2 currency pairs: EUR/USD and NZD/USD
test for cointegration of prices between both assets
Dennis Stander
Two Questions:
1. How is the Quantity calculated? (i.e. where can I find the function CalculateOrderQuantity() )
2. 1200 days to determine which copula and 250 days to determine the parameters? So 1200 days are used to decide Frank/Gumbel/etc. and the parameter theta is optimized using the 250 days before each trade? Or how many times and when is this parameter updated?
Thanks in advance,
Dennis
Thomas Choi
Hi Jing Wu,
I have tried the pair selection as you did by selecting the highest Kendall’s tau of the pair log return. I ran though the data of entire NYSE stock list and pick the highest 30 stock pair of Kendall's tau each year from 2001 to 2017. Then I backtested the pair on the next year historical data. I find that the high Kendall's tau doesn't has a direct relationship with good performance. P.S. the backtesting is using the stratigy that you posted out.
Is there a better way to pick a good pair?
Hugo Acuna
Hello,
How do I implement this strategy for forex pairs? Thank you
Andrew Czeizler
Hi All!
Sincere apologies for the above for some reason the page did not load for me.
I am really fasciantated with the above and would like to build on it.
I have a couple of quesions -
If i want to try another type of copula in a python package, how can i import the package
My first impression is that i could only do it locally.
So i would like to set up a local backetest enviroment preferably in jypyter notbeook.
What would be the best way to acheive this?
Many thanks all,
Best,
Andrew
Jing Wu
For the request to add more python libraries, please submit an issue on Github
We'll review the request and try to add new libraries to LEAN.
To use LEAN locally. the easiest is with a plugin we've made for Visual Studio. It automatically takes the code you've written and sends it to the cloud for backtesting.
You can see the plugin documentation here; we would love to have support for other IDE's but currently just support Visual Studio.
Ted Yap
quantity = self.CalculateOrderQuantity(self.syl[1],0.4)
I am new to the platform, but can someone explain this to me and why is it 0.4?
Alexandre Catarino
Hi Ted Yap, you can look for the QCAlgorithm classes, method, and properties in the Algorithm Lab:
Baichen
what is the exit point?
Petras petraitis
I checked the copula code from the tutorial (one with 427 trades, 353% Net Profit). If you look at the trade log, it seems that it opens a long position in XLK on 2010-03-03 and the short QQQ gets rejected because of insufficient capital. Another pair trade is opened on 2010-03-26 in opposite direction and half the size. Then the two ~2k shares long positions in both instruments are held for 8+ years, producing most of the total net profit. Am I reading this right?
Â
Â
Jing Wu
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!