Hello everybody, I'm working on a basic pairs trading strategy on the forex market, a little bit different than the one which Quantconnect provides on the bootcamp.
I'm stuck in the phase where I'm trying to select the highest correlated pair among the ones that I'm working with. From the below numpy array how can I select the two currencies based on their correlation value?
import numpy as np
from numpy import corrcoef
class LogicalTanCaribou(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 3, 1) # Set Start Date
#self.SetEndDate(2021, 1, 30)
self.SetCash(1000) # Set Strategy Cash
self.currencies = ['EURUSD', 'USDJPY', 'GBPUSD', 'USDCAD', 'AUDUSD', 'NZDUSD', 'EURJPY']
for ticker in self.currencies:
self.AddForex(ticker, Resolution.Hour)
self.df = self.History([self.Symbol("EURUSD"), self.Symbol("USDJPY"), self.Symbol("GBPUSD"),
self.Symbol("USDCAD"), self.Symbol("AUDUSD"), self.Symbol("NZDUSD"),
self.Symbol("EURJPY")], 100)
if not self.df.empty:
eurusd_quotebars = self.df.loc["EURUSD"]
usdjpy_quotebars = self.df.loc["USDJPY"]
gbpusd_quotebars = self.df.loc["GBPUSD"]
usdcad_quotebars = self.df.loc["USDCAD"]
audusd_quotebars = self.df.loc["AUDUSD"]
nzdusd_quotebars = self.df.loc["NZDUSD"]
eurjpy_quotebars = self.df.loc["EURJPY"]
corr = corrcoef([eurusd_quotebars['close'], usdjpy_quotebars['close'], gbpusd_quotebars['close'],
usdcad_quotebars['close'], audusd_quotebars['close'], nzdusd_quotebars['close'],
eurjpy_quotebars['close']])
Vladimir
SIG_94,
This may help
2021-05-10 06:00:00 : [('GBPUSD 8G', 0.3654976797201165)]
2021-05-10 07:00:00 : [('GBPUSD 8G', 0.35897685884932734)]
2021-05-10 08:00:00 : [('GBPUSD 8G', 0.36123581045601105)]
2021-05-10 09:00:00 : [('GBPUSD 8G', 0.3465360916377733)]
2021-05-10 10:00:00 : [('GBPUSD 8G', 0.3436557853601734)]
2021-05-10 11:00:00 : [('EURUSD 8G', 0.34255027004855626)]
2021-05-10 12:00:00 : [('EURUSD 8G', 0.3469208556437079)]
2021-05-10 13:00:00 : [('EURUSD 8G', 0.34934350806021014)]
2021-05-10 14:00:00 : [('EURUSD 8G', 0.34845877842157563)]
Vladimir
The same for two pairs
2021-05-10 06:00:00 : [('EURUSD 8G', 0.3494472370390506), ('GBPUSD 8G', 0.3654976797201165)]
2021-05-10 07:00:00 : [('EURUSD 8G', 0.34242862457977574), ('GBPUSD 8G', 0.35897685884932734)]
2021-05-10 08:00:00 : [('EURUSD 8G', 0.3406963454557722), ('GBPUSD 8G', 0.36123581045601105)]
2021-05-10 09:00:00 : [('EURUSD 8G', 0.33821427473219995), ('GBPUSD 8G', 0.3465360916377733)]
2021-05-10 10:00:00 : [('EURUSD 8G', 0.3372861353846373), ('GBPUSD 8G', 0.3436557853601734)]
2021-05-10 11:00:00 : [('GBPUSD 8G', 0.3313701680310525), ('EURUSD 8G', 0.34255027004855626)]
Vladimir
Here is updated version.
2021-05-09 23:00:00 : 1.0
2021-05-09 23:00:00 : 0.09234517717837581
2021-05-09 23:00:00 : 0.7397499656103798
2021-05-09 23:00:00 : 0.7129995587690616
2021-05-09 23:00:00 : 0.8933829960158504
2021-05-09 23:00:00 : -0.6479186596769565
2021-05-09 23:00:00 : -0.6523590520691587
2021-05-09 23:00:00 : [('AUDUSD 8G', 1.0), ('NZDUSD 8G', 0.8933829960158504)]
SIG_94
Thank you Vladimir, that was helpful.
SIG_94
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!