Quant Strategy: Pairs Trading Algorithm (Mean Reversion)

  Рет қаралды 55,988

Spencer Pao

Spencer Pao

Күн бұрын

===== Likes: 790 👍: Dislikes: 14 👎: 98.259% : Updated on 01-21-2023 11:57:17 EST =====
Interested in the Stock Market? Especially when it comes to technical analysis? The Pairs Trading Strategy is a MUST KNOW! So, look no further! Sit back, relax, and enjoy the demo.
Please take a look at this Article. A lot of inspiration came from this article.
kidquant.com/project/pairs-tr...
Monte Carlo Simulation Video
• Monte Carlo Simulation...
Github:
github.com/SpencerPao/Quantit...
0:00 - What is Pairs Trading?
1:20 - Visualizing and Understanding Pairs Trading
4:12 - Correlation, Cointegration, Stationarity tests, Spreads, Ratios
8:53 - Identifying Price Signals
11:05 - Creating a Model (Moving Average)
12:43 - Potential Buy and Sell opportunities
DISCLOSURE: As of 4/25/2021, I do have long positions mentioned in this video (MSFT/BRK-B)
DISCLAIMER: All information and data on my KZbin Channel, blog, email newsletters, white papers, Excel files, and other materials is solely for informational purposes. I make no representations as to the accuracy, completeness, suitability or validity of any information. I will not be liable for any errors, omissions, losses, injuries or damages arising from its display or use. All information is provided AS IS with no warranties, and confers no rights. I will not be responsible for the accuracy of material that is linked on this site. Because the information herein is based on my personal opinion and experience, it should not be considered professional financial investment advice or tax advice or broker dealer advice. The ideas and strategies that I provide should never be used without first assessing your own personal/financial situation, or without consulting a financial and/or tax professional. My thoughts and opinions may also change from time to time as I acquire more knowledge. These are, as discussed above, solely my thoughts and opinions. I reserve the right to delete any comments for any reason (abusive in nature, contain profanity, etc.). Your continued reading/use of my KZbin Channel, blog, email newsletters, whitepapers, Excel files, and other materials constitutes your agreement with and acceptance of this disclaimer.

Пікірлер: 101
@Mezmathics
@Mezmathics 2 жыл бұрын
A rather complicated concept made concise and very comprehensive. Excellent!!
@Goran78
@Goran78 Жыл бұрын
Hi Spencer, this is pure gold. Thank you for sharing so much knowledge with us for free :)
@michaelladen7049
@michaelladen7049 2 жыл бұрын
Great video, and with Python code too!! Thank you for making this clear explanation, and for making it available to your viewers
@leonardogarces7563
@leonardogarces7563 8 ай бұрын
Very well explained thanks!
@nikolipham5952
@nikolipham5952 2 жыл бұрын
great work man! great work!
@rataash
@rataash 2 жыл бұрын
i think i found a rare gem, thanks for the video, keep up the good work
@mikkaruru
@mikkaruru 9 ай бұрын
Brilliant!
@jessejin2027
@jessejin2027 2 ай бұрын
i love this video Spencer!
@Maximus18.6
@Maximus18.6 3 ай бұрын
Awesome video l. Thanks
@felipemiraflores2984
@felipemiraflores2984 Ай бұрын
Amazing Video!!!
@thelightings
@thelightings 3 ай бұрын
thank you alot
@ludmilasazonova2268
@ludmilasazonova2268 11 ай бұрын
Michael, your tactics are incredible Which one are you recommend for the beginners?
@tchintchie
@tchintchie 3 жыл бұрын
Highly underrated channel!
@SpencerPaoHere
@SpencerPaoHere 3 жыл бұрын
I love the support :')
@botsprofit
@botsprofit 10 ай бұрын
very good video
@cengizali5361
@cengizali5361 2 жыл бұрын
I would also look at cash indices, i quite like DAX vs Nasdaq. Especially as a spreadbet in which u are not exposed to CCY flutuations.
@durchnet
@durchnet Жыл бұрын
Hey Spencer, thankyou so much for the video!! This is my first video on pairstrading and I finally understood the idea. However I have a few doubts especially at 8:55 , when you calculate the mean and then find your price signals. When you do that does that not cause a look ahead bias for older signals? Like we really never knew it was the mean at the beginning of the datapoint right? I'm sorry for the dumb question, but if you can clarify it, that would help me a lot! Thankyou!
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
There are no dumb questions! :) There are inherent assumptions when you go and utilize the pair trading algorithm. (i.e what time horizon you want to choose) Bias is an inherent factor with most if not all time-series particular model. There are some mays to mitigate that bias: 1) you can have normalized models in relation to the market over differing economic trends (recession, depression, growth etc..) and see how that compares. 2) Have additional features that is more representative of the data you are seeing now 3) Data augmentation etc. Hopefully that answered your question?
@leilaan9344
@leilaan9344 3 жыл бұрын
Hi Spencer, this is a fantastic channel! Your explanations are some of the clearest I've seen. When you have the buying and selling signals, that's on the specific asset you declared you were going to be long or short one, correct? I would love a video on how to implement the actual buying/selling part of the strategy and backtesting if you're taking recommendations! Thanks again Spencer
@SpencerPaoHere
@SpencerPaoHere 3 жыл бұрын
To answer your first question -- Yes indeed! I'm glad that you enjoyed this video. :) I'll dabble in the back testing / actual implementation of the algorithm via a brokerage. and see if I can make a video quality walkthrough. Note that the back testing is an art form; many of the testing algorithms are in house. I'm sure that there are some open source backtesting algo's to use.
@leilaan9344
@leilaan9344 3 жыл бұрын
@@SpencerPaoHere that would be fantastic, thank you! Thanks again for the great content - really appreciate all the effort
@jawadj4798
@jawadj4798 11 ай бұрын
Should note: you can have a cointegrated pair with zero correlation. Correlation does not imply cointegration or rather an underlying relationship which influences the process of each time series in a distinct way. The relationship is what we are trading, or in other more specific terms, the statistical deviation from a cointegrated spread.
@mdaplaton
@mdaplaton 2 жыл бұрын
Thx for the content! Do you plan by any means to make a video of pair trading but with the use of kalman filter instead of moving averages?
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
Ah LQE? Few folks are aware of that haha. Estimating position(s) is actually incredibly interesting based on uncertain features. That would be an entirely different video altogether (and not necessarily related to Pairs Trading) I have thought about using Kalman filter on an ARIMA model, and after reading a paper, the filter does provide an incremental improvement. I might do a video on the topic but not in the near future.
@xelhaku
@xelhaku Жыл бұрын
You could use the returns instead of price. And then the ratio between retturns of both assets
@sayonsomchanda6867
@sayonsomchanda6867 6 ай бұрын
Yes, people do use returns for calculating hedging ratios in real life.. but I guess the reason he used "Price" is it is easier to create visualizations using Pricing data.
@lemeslabs
@lemeslabs 2 жыл бұрын
Goodnight ! Do you have any video on your channel about the Pair Trading strategy but instead of long x short, does it work in the form long & long or short & short? Note: congratulations for the channel.
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
Hello! I do not have a video on that topic. However, what you are stating is quite similar to the pair trading algorithm I discussed. Just switch the logic. I.e if asset A and asset B were to decrease (high correlation), you can spread your bets between the 2 asset classes.
@GenoPendula
@GenoPendula 2 жыл бұрын
You are making it sound sooo easy hahah
@catjedi-mindtraining4794
@catjedi-mindtraining4794 7 ай бұрын
are these trading algorithms "a nice to have" or something that you would ned to properly lidentify pairs?
@almelnik
@almelnik 10 ай бұрын
What is your rationale for doing KZbin instead of developing a trading model? Do you view career or KZbin as a more predictable way to increase earnings or do you doubt that it's possible to develop such model? Ignore question confines if needed, any thoughts appreciated.
@SpencerPaoHere
@SpencerPaoHere 10 ай бұрын
An excellent question -- I actually never really put that much thought into creating quant models; I am more into Long only trades with longer time horizons. It may be an interesting venue to explore. It essentially boils down to time -- I only have so many hours in the day, and I recently became very, very busy. There's a lot more to that story but those are the high level points.
@oliveroz2705
@oliveroz2705 3 жыл бұрын
Rich in content
@LagartaGCB
@LagartaGCB Жыл бұрын
Any thoughts on rolling z-scores or z-z-scores (z-scores of z-scores when distributions may be not as mean reverting) and testing for effective lookback periods without data mining and information creep?
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
You most definitley can do a rolling z-score of z scores approach. (In fact that approach can be an extension of what I did in this video.) In terms of back testing, that is definitley more of an artform that I am not 100% super familiar with. Many companies have their own built in backtesting. But, you can look at it from the point of view of specific time horizons in the stock exchange to judge how well your model will fare in economic upturns or dowturns. You don't have to have real time artificially generated data that represents a specific trend. At the end of the day, if you get good predictions, then by all means go for it! :)
@navketan1965
@navketan1965 6 ай бұрын
Sir, In pars trading can one trade spread between SP500 & Nasdq 100 based just on market sentiment of broad market/major indices.When sentiment is bullish,Nasdq100 would outperform SP 500 index.And in bearish scene vice versa.Same may be applied for pairs trading SP500 & Dow 30.In weak market Dow 30 would hold better & go down less compared to SP500/nasdq100.We seek your wisdom.Thank you.
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
NOTE: at 4:20, pandas data reader has been deprecated!! So, the logic when obtaining your data will change. Thus, I have updated the Jupyter notebook here: github.com/SpencerPao/Pairs_Trading At 9:40 2 standard deviations are 95%. 1.25 standard deviations is ~90%
@aravindhann2032
@aravindhann2032 8 ай бұрын
Awesome Video! How did you gain the knowledge that you have? What's the best way for an aspiring Quant Trader to gain all these knowledge from basics?
@SpencerPaoHere
@SpencerPaoHere 4 ай бұрын
I talk with others who are in the field. Honestly, the 'best' way to become a quant trader is to do it yourself. And, if you want to join a huge fund, you will have an edge over everyone else because you were an actual practioner. You learn by failing over and over.
@abhishekgadekar5618
@abhishekgadekar5618 7 ай бұрын
Hey great video Spencer. I have a doubt at the end when we get the buy and sell signals, so say the buy signal is the trigger we got, now do we buy BRK or buy MSFT? how do we decide? Also how is it a pair strategy, we are only buying or selling only one stock at a time?
@SpencerPaoHere
@SpencerPaoHere 4 ай бұрын
Mathematically and theoretically, you can go long on X and short Y. The direction of either one depends on what the model suggests. Also, from the pairs strategy (X,Y), you would typically standardize the pair i.e X would be buy and Y would be sell
@SiMonPhamphaisailam
@SiMonPhamphaisailam Жыл бұрын
I tried to run it but sadly, the signal is not correct. As you can see on the graph u presented, the signal is false as well. Any other way to detect peak and bottom of a graph ?
@mathispaumier1670
@mathispaumier1670 Жыл бұрын
Hello, just wanted to know how you calculate the spread mine is all weird. Great video
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
The spread (which I believe your are mentioning at 6:17) is calculated by BRK_B - MSFT which is the difference between adjusted closing prices.
@xploit811
@xploit811 2 жыл бұрын
Thanks. Any good tool or means to check stock co-integration?
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
It depends on your use case. If speed is not an issue, you can resort to scraping data from Yahoo Finance with Python/R. Those languages should suffice.
@FaizanKhan-gg4kx
@FaizanKhan-gg4kx 6 күн бұрын
Sir can you help me with adftest for pair trading
@navketan1965
@navketan1965 Жыл бұрын
Sir, In pair trading correlated currency pairs(say AUD/USD & NZD/USD) do you compare RSI value & sell overpriced pair & buy the weaker? OR do you compare CCI of each pair? Do you suggest using correlated cross currencies as pair trades(eg EUR/JPY & GBP/JPY)? On 4H chart sum up RSI 3, RSI 7,RSI 14, & compare added value on each pair & go. Thank you.
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
You can try any of those -- whichever has the best signals, go for it. Regardless of which relationships you seek, the pairs trading principle applies to any.
@ronaknathwani5142
@ronaknathwani5142 Жыл бұрын
Hi Spencer what tools do I need to run the similar test as above , I’m trading ES NQ and YM correlated pairs and like to see the stats for divergence?
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
@@ronaknathwani5142 Hmm. Those are ticker symbols I assume? You can run any tool that you are comfortable with. (by tools do you mean software?) Check out code in C. Those are typically the fastest language in the quant world. If you mean which theories to use, you should test whichever one in your overall strategy to get your edge. Perhaps even use a combination of them or some other strategy that I am unfamiliar with.
@navketan1965
@navketan1965 Жыл бұрын
@@ronaknathwani5142 Sir, On one chart plot RSI 3, RSI 7, RSI 14 ( say using MT4) & write values of each & add the numbers for ES NQ & YM--and compare.Please sell overpriced & buy the underpriced. System Programs use RSI 14, but as you know RSI 3 is the best guide to price & RSI 7 is more recent than RSI 14--so we have better picture of strength/weakness mtf.You need no program--just paper & pencil.
@navketan1965
@navketan1965 10 ай бұрын
Sir, Have you tried pair trading forex using rsi7 ,rsi14,rsi30 (add them up for comparison) say on hourly chart & selling strong pair & buying weak pair--pairs have to be highly correlated(eg aud/usd and nzd/usd OR dow30 & sp500).One can do this on any correlating underlying stocks/commodities/futures/crypto/bonds. Trading on hourly charts there would be tons of opportunities all year around.
@SpencerPaoHere
@SpencerPaoHere 10 ай бұрын
I have not! You can definitely find many correlations -- the trick perhaps is to find causailty.
@Mehdi_Maherzi
@Mehdi_Maherzi Жыл бұрын
Hi Spencer thanks for the video. why did you choose 1.25 sd ? 1 sd from the mean covers 68% of the data I agree, but If you want to cover 95% of the data, you should choose 2 sd, considering a normal distribution. Thnaks
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
Hi! Yeah me choosing 1.25 sd was arbitrary. It does make a lot of sense to do 2 sd.
@Mehdi_Maherzi
@Mehdi_Maherzi Жыл бұрын
@@SpencerPaoHere ok :) and a z-score of 1.25 is at the ~90th percentile right, not 95th, as you mentioned. Thats why I got confused.
@SuperJg007
@SuperJg007 6 ай бұрын
I am curious about something, aren't we having a lookahead bias here when doing the tests to determine the pairs to trade, like say in one 1, we wont have info from the future right to tell us if this pair is cointegrated. How do you think we can tackle this?
@SpencerPaoHere
@SpencerPaoHere 4 ай бұрын
Bias is huge. And, I don't talk about penalization. One way to do this is through continuous backtesting and revalidating the model (reweighting and reupdating the model) OR even having a different model altogether.
@muntedme203
@muntedme203 Жыл бұрын
Are trading past if you have not rolled Z scores. You may have false signal with heteroskedasticity even in a pair of two non homoskedastic series.
@marekdziubinski850
@marekdziubinski850 Жыл бұрын
Spencer, very interesting video. Please note that there is an “overfitting” problem in your concept. You are using a prior knowledge regarding the correlation of these two instruments. In other words, the fact that these two stocks are correlated in the period of time that you are analyzing is not necessarily going to be true in the future (and you are analyzing the performance of your method only for the past signal… that was used to determine if the stocks are correlated in the first place). Take care
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
You're absolutley correct! This is an assumption that there is an inherent dependency in this method and your positions will inherently become more risky the longer you hold onto a specific position.
@laureanoralon3880
@laureanoralon3880 2 жыл бұрын
Do you think this can be done using AUDUSD vs. EURAUD?
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
Hi! Most definitely! Although, there are a lot of other features that are associated with forex, Pairs Trading can apply to any sort of asset classes!
@jackyliaw2763
@jackyliaw2763 Жыл бұрын
Any chance to have the same written for crypto In Binance API?
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
Most likely. If you are able to query data from the Binance APi without much limit, you can do the pairs trading algorithm. It's just a matter of latency at that point.
@sebastianwozniczka7403
@sebastianwozniczka7403 2 жыл бұрын
Hi ! I tried to use your code however there is an error. From what I understand, it can't access the data from yahoo finance correctly. Are you experiencing the same issue ?
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
Yes indeed! Thank you for letting me know! Yahoo finance has changed their backend so the functions I go over in this video are deprecated. However, I have updated my notebook to adjust for this. You can check it out on my Github here: github.com/SpencerPao/Pairs_Trading
@sebastianwozniczka7403
@sebastianwozniczka7403 2 жыл бұрын
@@SpencerPaoHere that’s great, thank you !!!
@arghyaghosh13
@arghyaghosh13 10 ай бұрын
Conceptually wrong. You should check if the pairs are cointegrated or not. Correlation does not imply cointegration.
@loveyougames8910
@loveyougames8910 2 ай бұрын
Can you explain it more
@ricktrader3946
@ricktrader3946 2 жыл бұрын
Good explanation ... but you use In Sample data? Really more complex to apply in real world trading when you need to use Out Sample data.
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
Yes! I was essentially using the ratio between two different assets based on the historical data. And, based on the base model: rolling averages, I depend on the historical data to have a prediction in the future. (In essence, using in sample data for predictions) It is a basic model where the typical machine learning principles are not at play.
@poissongirrl
@poissongirrl Жыл бұрын
Hey, thanks for the amazing content, great video. I have one python-related technical question: by using pandas 1.2, the statsmodels doesn't work. I get this error when trying to import statsmodels.tsa.stattools: "module 'pandas' has no attribute 'Panel'". It seems the panel has been removed from Pandas module 0.25.0 onwards. How did you overcome this problem? Thanks in advance. Keep up the good work!
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
Yeah. I'd recommend setting up a separate environment for that specific version of pandas and other dependencies for this specific overview.
@poissongirrl
@poissongirrl Жыл бұрын
Update: with the latest anaconda version (released in May 2022), everything works perfectly, there was no need to install a specific pandas version.
@robnotaro8584
@robnotaro8584 Жыл бұрын
Wouldnt this work better with forex pairs?
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
Not sure. I haven't tried. Though, I bet a lot of folks may use a version of pairs trading in any type of financial market.
@amriksingh5003
@amriksingh5003 2 жыл бұрын
Hi do you have the algo system for pair trading I can use. How much it costs ?
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
Algo system? Not really. For deployments for trading algorithms, you can look into using interactive brokers via their api. Plug in your model and stream their data. They are quite hefty in costs though.
@amriksingh5003
@amriksingh5003 2 жыл бұрын
@@SpencerPaoHere thanks for your prompt response. I m not programming guy.
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
@@amriksingh5003 Hmm. Well, unfortunatley everything has some elment of code. Though, you could just run my notebook locally from github and plug in your pair of public companies (for example) manually purchase/sell equity on some exchange.
@jlee-mp4
@jlee-mp4 Жыл бұрын
At 9:40, doesn't 95% of the data lie between 2 standard deviations, not 1.25?
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
Yes. You are correct. I've updated my pinned comment and it was also asked in an earlier comment as well.
@bijothomas710
@bijothomas710 3 жыл бұрын
can get this code?
@SpencerPaoHere
@SpencerPaoHere 3 жыл бұрын
I uploaded the notebook here: github.com/SpencerPao/Pairs_Trading
@karim03347
@karim03347 Жыл бұрын
Buying what ? And selling what ??
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
Any equities !
@user-pb7qd2fo5z
@user-pb7qd2fo5z 2 жыл бұрын
hi tell me how to do it for the cryptocurrency market?
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
You could think of it in terms of replacing the assets with coins. Same process.
@user-pb7qd2fo5z
@user-pb7qd2fo5z 2 жыл бұрын
@@SpencerPaoHere I can’t load data market from binance
@user-pb7qd2fo5z
@user-pb7qd2fo5z 2 жыл бұрын
it doesn't work there
@SpencerPaoHere
@SpencerPaoHere 2 жыл бұрын
​@@user-pb7qd2fo5z ​ ooh. I think you're referring to scraping financial asset data? I have not attempted to scrape from binance, so I am not familiar with the tooling, but you can set up price scraping mechanisms on Yahoo Finance's API. I did a similar video on this topic: kzbin.info/www/bejne/fXjKf4BtjMahgZo Or this video (includes Yahoo finance data): kzbin.info/www/bejne/rZ3EfaCDlLGpjck
@user-pb7qd2fo5z
@user-pb7qd2fo5z 2 жыл бұрын
@@SpencerPaoHere oh thanks, I think I found it
@sinan_islam
@sinan_islam Жыл бұрын
Pairs Trading belongs to Mean Reversion or Statistical Arbitrage?!!!
@SpencerPaoHere
@SpencerPaoHere Жыл бұрын
Mean reversion. This is more in line with stat arb. kzbin.info/www/bejne/in-tf3aAmp6Zers
@nihitsaxena16
@nihitsaxena16 2 ай бұрын
audio quality is not good
@GohOnLeeds
@GohOnLeeds 7 ай бұрын
dude, you gotta get rid of this line in your code "Cointegration test: A technique used to find a potential correlation in a time series (long term)". This is *absolutely* not what it is. But you have mistaken correlation for causality the entire way through this video so I'm not surprised. Cointegration actually establishes a causal link from one time series to another" Peace bro. But please don't say the purpose of cointegration is to find correlations. Nothing is further from the truth :-)
@28jery
@28jery 2 жыл бұрын
Why so complicated ? Make money is easy .its simpl .
@user-cn6gx5nd1n
@user-cn6gx5nd1n 10 ай бұрын
There's a reason you make KZbin videos instead of working in the industry. Your implementation and programming is garbarge.
Extracting Text from Images | Optical Character Recognition | OCR
21:20
Quant Strategy: Arbitrage Trading Algorithm (Cross-Exchange)
15:37
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00
БОЛЬШОЙ ПЕТУШОК #shorts
00:21
Паша Осадчий
Рет қаралды 7 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Mean Reversion Trading | Lessons From a Fund | By Dr Ernest Chan
51:00
QuantInsti Quantitative Learning
Рет қаралды 38 М.
The Math Equation That Beat Wall Street | Jim Simons vs. EMH
14:11
Chaos Theory Institute
Рет қаралды 230 М.
Mean Reversion Trading Strategy Using Python
39:03
Computer Science
Рет қаралды 16 М.
An Overview of Pairs Trading Strategies
41:45
Hudson & Thames
Рет қаралды 16 М.
1$ vs 500$ ВИРТУАЛЬНАЯ РЕАЛЬНОСТЬ !
23:20
GoldenBurst
Рет қаралды 1,4 МЛН