Back

Copula pairs trading minute data backtest

Hi,

I am trying to convert the copula pairs trading algorithm to higher frequency data.

But i am getting errors and i am not sure why.

Best,

Andrew

Update Backtest








Your algo is above my realm of intelligence but what if you just changed all your Resolution.Minutes to Resolution.Second? 

0

There's no severe error in the algorithm. Only thing needs to be noticed is the leverage when calculating the order quantity. The default leverage is 2X. When the buying power is not sufficient, the computed order quantity is less than 1. You should avoid this condition using "if quantity > 1", then place the order. 

Also merely converting the algorithm resolution from daily to the minute is invalid for this pairs trading algorithm. The lookback period is 250 days in this original algorithm. The regression coefficient is calculated from this period and used in the algorithm to calculated the order quantity. If you use the minute resolution, you should think about what period to use. Besides, the original algorithm assumes this long-term relationship persists in the future based on the daily data. However, for minute data, they are correlated initially (based on the correlation for the 250-minute history price series) does not mean a long-term relationship between two asset prices. This assumption is too strong for minute resolution. Therefore, for high-frequency pairs trading, the selection of pairs should be a dynamic process (while in this example, we only select one pair to trade based on the daily history price). 

For intraday data, we have this pairs trading example for your reference.

https://www.quantconnect.com/tutorials/strategy-library/intraday-dynamic-pairs-trading-using-correlation-and-cointegration-approach#Intraday-Dynamic-Pairs-Trading-using-Correlation-and-Cointegration-Approach-References
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!

Thank you
0

Hi Jing!

Thank you ! So just for my understanding, the copula is used to produce the trading rules?

It is not used to calculate the portfolio weights?

Many thanks,

Best,

Andrew

0

Hi Jing!

I tried changing the backtest with the quantity >1 command and got the below.

I am not sure if I am doing it correctly?

Best,

Andrew

0


Hi Jing!

I tried the HFT algorithm and received the below -

44 | 21:37:50:
Successfully sent backtest request for 'Virtual Yellow Mosquito', (Compile Id: d2b2c93bd4e7cb5e9cb5112b71dbb001-bee5e07fd0e3c04931c7fc84322e681f)
45 | 21:38:06:
Launching analysis for f311d56076740fd6d37672a6d686232e with LEAN Engine v2.4.0.0.4780
46 | 21:38:08:
Backtest deployed in 17.988 seconds
47 | 21:38:53:
Backtest Handled Error: Order Error: id: 13, Insufficient buying power to complete order (Value:26193.4587), Reason: Id: 13, Initial Margin: 13098.38936, Free Margin: 1580.7060944490959
48 | 21:38:53:
Your algorithm messaging has been rate limited to prevent browser flooding.
49 | 21:38:54:
Backtest Handled Error: Order Error: id: 21, Insufficient buying power to complete order (Value:26445.7789), Reason: Id: 21, Initial Margin: 13226.114451, Free Margin: 2059.7054548121541
50 | 21:38:56:
Backtest Handled Error: Order Error: id: 29, Insufficient buying power to complete order (Value:26591.58), Reason: Id: 29, Initial Margin: 13300.155, Free Margin: 927.29162836811602
51 | 21:39:06:
Backtest Handled Error: Order Error: id: 39, Insufficient buying power to complete order (Value:27255.3085), Reason: Id: 39, Initial Margin: 13629.32424668, Free Margin: 98.0862031113761
52 | 21:39:06:
Your algorithm messaging has been rate limited to prevent browser flooding.
53 | 21:39:14:
Backtest Handled Error: Order Error: id: 45, Insufficient buying power to complete order (Value:27737.5673), Reason: Id: 45, Initial Margin: 13870.5836742, Free Margin: 1186.68173745946598
54 | 21:39:14:
Your algorithm messaging has been rate limited to prevent browser flooding.
55 | 21:39:17:
Backtest Handled Error: Order Error: id: 53, Insufficient buying power to complete order (Value:27887.5944), Reason: Id: 53, Initial Margin: 13946.85717525, Free Margin: 1048.98778979946598
56 | 21:39:18:
Backtest Handled Error: Order Error: id: 57, Insufficient buying power to complete order (Value:27733.2660), Reason: Id: 57, Initial Margin: 13872.64799616, Free Margin: 686.81796977946598
57 | 21:39:21:
Backtest Handled Error: Order Error: id: 61, Insufficient buying power to complete order (Value:27636.4857), Reason: Id: 61, Initial Margin: 13824.0278306, Free Margin: 487.67327927946598
58 | 21:39:21:
Your algorithm messaging has been rate limited to prevent browser flooding.
59 | 21:39:25:
Backtest Handled Error: Order Error: id: 63, Insufficient buying power to complete order (Value:27946.3445), Reason: Id: 63, Initial Margin: 13988.37724675, Free Margin: 1096.84605863946598
60 | 21:39:28:
Backtest Handled Error: Order Error: id: 67, Insufficient buying power to complete order (Value:27849.4785), Reason: Id: 67, Initial Margin: 13927.76425975, Free Margin: 955.15874405946598
61 | 21:39:28:
Your algorithm messaging has been rate limited to prevent browser flooding.
62 | 21:39:30:
Backtest Handled Error: Order Error: id: 69, Insufficient buying power to complete order (Value:28063.9452), Reason: Id: 69, Initial Margin: 14033.7675814, Free Margin: 1465.51120463946598
63 | 21:39:32:
Backtest Handled Error: Order Error: id: 79, Insufficient buying power to complete order (Value:28251.2289), Reason: Id: 79, Initial Margin: 14126.80446464, Free Margin: 109.86934575446598
64 | 21:39:36:
Backtest Handled Error: Order Error: id: 81, Insufficient buying power to complete order (Value:47227.12), Reason: Id: 81, Initial Margin: 23621.5, Free Margin: 119.93682329446598
65 | 21:39:36:
Your algorithm messaging has been rate limited to prevent browser flooding.
66 | 21:39:37:
Backtest Handled Error: Unable to compute order quantity of KB. Reason: The portfolio does not have enough margin available.. Returning null.
67 | 21:39:41:
Backtest Handled Error: Unable to compute order quantity of ASB. Reason: The portfolio does not have enough margin available.. Returning null.
68 | 21:39:44:
Backtest Handled Error: Order Error: id: 86, Insufficient buying power to complete order (Value:-46319.44), Reason: Id: 86, Initial Margin: -23172.65921956, Free Margin: 4148.55860764446598
69 | 21:39:48:
Backtest Handled Error: Order Error: id: 89, Insufficient buying power to complete order (Value:-48011.79), Reason: Id: 89, Initial Margin: -24018.87724016, Free Margin: 5326.84107436446598
70 | 21:39:49:
Backtest Handled Error: Order Error: id: 90, Insufficient buying power to complete order (Value:47789.1975), Reason: Id: 90, Initial Margin: 23915.6837371525, Free Margin: 5068.36516036446598
71 | 21:39:49:
Your algorithm messaging has been rate limited to prevent browser flooding.
72 | 21:39:56:
Backtest Handled Error: Order Error: id: 93, Insufficient buying power to complete order (Value:55070.0667), Reason: Id: 93, Initial Margin: 27546.07837424, Free Margin: 26794.70057824446598
73 | 21:39:57:
Your algorithm messaging has been rate limited to prevent browser flooding.
74 | 21:39:58:
Runtime Error: KeyNotFoundException : 'OFG' wasn't found in the Slice object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("OFG")
at QuantConnect.Data.Slice.get_Item (QuantConnect.Symbol symbol) [0x0002d] in <4a6b97fa62574a3b86fb1a3916b84166>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0
at OnData in main.py:line 160
KeyNotFoundException : 'OFG' wasn't found in the Slice object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("OFG")
at QuantConnect.Data.Slice.get_Item (QuantConnect.Symbol symbol) [0x0002d] in <4a6b97fa62574a3b86fb1a3916b84166>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0 (Open Stacktrace)
75 | 21:39:58:
Algorithm Id:(f311d56076740fd6d37672a6d686232e) completed in 112.44 seconds at 42k data points per second. Processing total of 4,702,162 data points.
76 | 21:40:01:
Your log was successfully created and can be retrieved from: https://www.quantconnect.com/backtest/11574/1888185/f311d56076740fd6d37672a6d686232e-log.txt

0

1) You should add if quantity > 1 before you use self.Sell() and self.Buy(). For example

if self.Portfolio[self.syl[0]].Quantity < 0 and self.Portfolio[self.syl[1]].Quantity > 0:
self.Liquidate()
quantity = float(self.CalculateOrderQuantity(self.syl[1],0.4))
if quantity > 1 and self.coef * quantity > 1:
self.Sell(self.syl[1], 1 * quantity )
self.Buy(self.syl[0], self.coef * quantity)

2) The default leverage is 2X. The algorithm set the sum of buying power targets to be 5. This causes the warning message "Insufficient buying power to complete orders". You can use a smaller target weight like 0.5 avoid the insufficient buying power error.

self.SetHoldings(i.b, 0.5/(len(self.selected_pair)))
self.SetHoldings(i.a, -0.5/(len(self.selected_pair)))

3) For the error, you need to check if the slice data contains the key before retrieving the slice price.

for i in self.selected_pair:
if data.ContainsKey(i.a) and data.ContainsKey(i.b):
price_a = float(data[i.a].Close)
price_b = float(data[i.b].Close)

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!

I have update quantity in the attached backtest.

But i dont know which line to update


 
  1. self.SetHoldings(i.b, 0.5/(len(self.selected_pair)))
  2. self.SetHoldings(i.a, -0.5/(len(self.selected_pair)))
and 
 
  • for i in self.selected_pair:
  • if data.ContainsKey(i.a) and data.ContainsKey(i.b):
  • price_a = float(data[i.a].Close)
  • price_b = float(data[i.b].Close)
 Many thanks,Best,Andrew
0


My attempt at updating the code.

I think i did something wrong as the backtest does not finish?

Many thanks for your help Jing!

Best,

Andrew

0


 

The suggestion 2) and 3) are for this high-frequency pairs trading algorithm 

https://www.quantconnect.com/tutorials/strategy-library/intraday-dynamic-pairs-trading-using-correlation-and-cointegration-approach#Intraday-Dynamic-Pairs-Trading-using-Correlation-and-Cointegration-Approach-References

The copula pairs trading algorithm works fine without any errors like you've attached the first time. The warning message comes from the buying power target setting in  "self.CalculateOrderQuantity(symbol, weight)". To avoid insufficient buying power warning, you can use a small weight here and check if the quantity is greater than 1 before placing orders. 

quantity = self.CalculateOrderQuantity(symbol, weight)
if quantity > 1:
# place orders


 

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.


have a better equity curve, but it does not seem to be making any trades.

i am trying to get it to reopen at the beggining of each day.

Best,

Andrew

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