Hi Jingw, I backtested your pair trading algo using 'cointegration' method. I changed yours to: 1). Weekly trading (trade every Monday at 10 am), 2) Shorten lookback periods. Please see the attached backtest below. I found that there's no trading between 04.10.2014 and 10.04.2014 (for about six months) (Please see below 'Trade' logs.). I checked if the algo generated any signals, and yes it did during the period (Please see the below 'Logs'.). And later 10.04.2014, trades resumed and stopped again on 10.23.2014. Why is this happening? The only reason I could guess is that it should reached to some sort of capped (max) leverage and then the system automatically stopped trading for new signals. Once the max leverage gets resolved for new trading, it resumed trading. Is this right? Can you give me some clear explanations on ths? Thanks :)
# Trade Logs
2014-04-08T04:00:00Z QQQ 82.54548728 167 4 Filled 13785.09637
2014-04-08T04:00:00Z XLK 33.51219591 -174 4 Filled -5831.122089
2014-04-09T04:00:00Z QQQ 83.52851585 337 4 Filled 28149.10984
2014-04-09T04:00:00Z XLK 33.85942393 -352 4 Filled -11918.51722
2014-04-10T04:00:00Z QQQ 84.68502005 677 4 Filled 57331.75857
2014-04-10T04:00:00Z XLK 0 -707 4 0 0
2014-04-11T04:00:00Z QQQ 0 1358 4 Invalid 0
2014-04-11T04:00:00Z XLK 0 -1419 4 Invalid 0
2014-04-12T04:00:00Z QQQ 0 1359 4 Invalid 0
2014-04-12T04:00:00Z XLK 0 -1420 4 Invalid 0
2014-04-15T04:00:00Z QQQ 0 1359 4 Invalid 0
2014-04-15T04:00:00Z XLK 0 -1391 4 Invalid 0
2014-04-16T04:00:00Z QQQ 0 1359 4 Invalid 0
2014-04-16T04:00:00Z XLK 0 -1391 4 Invalid 0
2014-04-17T04:00:00Z QQQ 0 1357 4 Invalid 0
2014-04-17T04:00:00Z XLK 0 -1389 4 Invalid 0
2014-04-18T04:00:00Z QQQ 0 1357 4 Invalid 0
2014-04-18T04:00:00Z XLK 0 -1389 4 Invalid 0
2014-04-22T04:00:00Z QQQ 0 1356 4 Invalid 0
2014-04-22T04:00:00Z XLK 0 -1359 4 Invalid 0
2014-05-09T04:00:00Z QQQ 0 1358 4 Invalid 0
2014-05-09T04:00:00Z XLK 0 -1249 4 Invalid 0
2014-10-04T04:00:00Z QQQ 95.38213835 -1342 4 Filled -128002.8297
2014-10-04T04:00:00Z XLK 37.67476284 1679 4 Filled 63255.92681
2014-10-11T04:00:00Z QQQ 91.33321646 -7 4 Filled -639.3325152
2014-10-11T04:00:00Z XLK 35.988964 8 4 Filled 287.911712
2014-10-14T04:00:00Z QQQ 90.66485376 -18 4 Filled -1631.967368
2014-10-14T04:00:00Z XLK 35.93213932 22 4 Filled 790.507065
2014-10-15T04:00:00Z QQQ 88.67913848 -36 4 Filled -3192.448985
2014-10-15T04:00:00Z XLK 35.1839477 45 4 Filled 1583.277647
2014-10-16T04:00:00Z QQQ 87.59425989 -73 4 Filled -6394.380972
2014-10-16T04:00:00Z XLK 34.87141196 92 4 Filled 3208.1699
2014-10-17T04:00:00Z QQQ 90.07398238 -147 4 Filled -13240.87541
2014-10-17T04:00:00Z XLK 35.59119124 186 4 Filled 6619.961571
2014-10-18T04:00:00Z QQQ 90.04492314 -291 4 Filled -26203.07263
2014-10-18T04:00:00Z XLK 35.45860032 368 4 Filled 13048.76492
2014-10-21T04:00:00Z QQQ 92.54401846 -576 4 Filled -53305.35464
2014-10-21T04:00:00Z XLK 36.16890882 732 4 Filled 26475.64126
2014-10-22T04:00:00Z QQQ 0 -1140 4 Invalid 0
2014-10-22T04:00:00Z XLK 36.73715562 1448 4 Filled 53195.40134
2014-10-23T04:00:00Z QQQ 0 -1140 4 Invalid 0
2014-10-23T04:00:00Z XLK 0 1448 4 Invalid 0
2014-10-24T04:00:00Z QQQ 0 -1135 4 Invalid 0
2014-10-24T04:00:00Z XLK 0 1442 4 Invalid 0
2014-10-25T04:00:00Z QQQ 0 -1134 4 Invalid 0
2014-10-25T04:00:00Z XLK 0 1441 4 Invalid 0
2014-10-28T04:00:00Z QQQ 0 -1134 4 Invalid 0
2014-10-28T04:00:00Z XLK 0 1446 4 Invalid 0
2014-10-29T04:00:00Z QQQ 0 -1130 4 Invalid 0
2014-10-29T04:00:00Z XLK 0 1441 4 Invalid 0
Logs:2014-04-08 00:00:00 Converted OrderID: 23 into a MarketOnOpen order.
2014-04-08 00:00:00 Converted OrderID: 24 into a MarketOnOpen order.
2014-04-08 00:00:00 Trade II : QQQ , -167.0 , XLK , [-174.59564091]
2014-04-09 00:00:00 Converted OrderID: 25 into a MarketOnOpen order.
2014-04-09 00:00:00 Converted OrderID: 26 into a MarketOnOpen order.
2014-04-09 00:00:00 Trade II : QQQ , -337.0 , XLK , [-352.32773046]
2014-04-10 00:00:00 Converted OrderID: 27 into a MarketOnOpen order.
2014-04-10 00:00:00 Converted OrderID: 28 into a MarketOnOpen order.
2014-04-10 00:00:00 Trade II : QQQ , -677.0 , XLK , [-707.79190956]
2014-04-11 00:00:00 Converted OrderID: 29 into a MarketOnOpen order.
2014-04-11 00:00:00 Converted OrderID: 30 into a MarketOnOpen order.
2014-04-11 00:00:00 Trade II : QQQ , -1358.0 , XLK , [-1419.76575063]
2014-04-12 00:00:00 Converted OrderID: 31 into a MarketOnOpen order.
2014-04-12 00:00:00 Converted OrderID: 32 into a MarketOnOpen order.
2014-04-12 00:00:00 Trade II : QQQ , -1359.0 , XLK , [-1420.81123351]
2014-04-15 00:00:00 Converted OrderID: 33 into a MarketOnOpen order.
2014-04-15 00:00:00 Converted OrderID: 34 into a MarketOnOpen order.
2014-04-15 00:00:00 Trade II : QQQ , -1359.0 , XLK , [-1391.80580346]
2014-04-16 00:00:00 Converted OrderID: 35 into a MarketOnOpen order.
2014-04-16 00:00:00 Converted OrderID: 36 into a MarketOnOpen order.
2014-04-16 00:00:00 Trade II : QQQ , -1359.0 , XLK , [-1391.80580346]
2014-04-17 00:00:00 Converted OrderID: 37 into a MarketOnOpen order.
2014-04-17 00:00:00 Converted OrderID: 38 into a MarketOnOpen order.
2014-04-17 00:00:00 Trade II : QQQ , -1357.0 , XLK , [-1389.75752413]
2014-04-18 00:00:00 Converted OrderID: 39 into a MarketOnOpen order.
2014-04-18 00:00:00 Converted OrderID: 40 into a MarketOnOpen order.
2014-04-18 00:00:00 Trade II : QQQ , -1357.0 , XLK , [-1389.75752413]
2014-04-22 00:00:00 Converted OrderID: 41 into a MarketOnOpen order.
2014-04-22 00:00:00 Converted OrderID: 42 into a MarketOnOpen order.
2014-04-22 00:00:00 Trade II : QQQ , -1356.0 , XLK , [-1359.83141118]
2014-05-09 00:00:00 Converted OrderID: 43 into a MarketOnOpen order.
2014-05-09 00:00:00 Converted OrderID: 44 into a MarketOnOpen order.
2014-05-09 00:00:00 Trade II : QQQ , -1358.0 , XLK , [-1249.52169536]
2014-10-04 00:00:00 Converted OrderID: 45 into a MarketOnOpen order.
2014-10-04 00:00:00 Converted OrderID: 46 into a MarketOnOpen order.
2014-10-04 00:00:00 Trade I : QQQ , -1342.0 , XLK , [-1679.68154203]
2014-10-11 00:00:00 Converted OrderID: 47 into a MarketOnOpen order.
2014-10-11 00:00:00 Converted OrderID: 48 into a MarketOnOpen order.
2014-10-11 00:00:00 Trade I : QQQ , 7.0 , XLK , [ 8.83882976]
2014-10-14 00:00:00 Converted OrderID: 49 into a MarketOnOpen order.
2014-10-14 00:00:00 Converted OrderID: 50 into a MarketOnOpen order.
2014-10-14 00:00:00 Trade I : QQQ , 18.0 , XLK , [ 22.79430496]
2014-10-15 00:00:00 Converted OrderID: 51 into a MarketOnOpen order.
2014-10-15 00:00:00 Converted OrderID: 52 into a MarketOnOpen order.
2014-10-15 00:00:00 Trade I : QQQ , 36.0 , XLK , [ 45.58860991]
2014-10-16 00:00:00 Converted OrderID: 53 into a MarketOnOpen order.
2014-10-16 00:00:00 Converted OrderID: 54 into a MarketOnOpen order.
2014-10-16 00:00:00 Trade I : QQQ , 73.0 , XLK , [ 92.4435701]
2014-10-17 00:00:00 Converted OrderID: 55 into a MarketOnOpen order.
2014-10-17 00:00:00 Converted OrderID: 56 into a MarketOnOpen order.
2014-10-17 00:00:00 Trade I : QQQ , 147.0 , XLK , [ 186.15349048]
2014-10-18 00:00:00 Converted OrderID: 57 into a MarketOnOpen order.
2014-10-18 00:00:00 Converted OrderID: 58 into a MarketOnOpen order.
2014-10-18 00:00:00 Trade I : QQQ , 291.0 , XLK , [ 368.50793013]
2014-10-21 00:00:00 Converted OrderID: 59 into a MarketOnOpen order.
2014-10-21 00:00:00 Converted OrderID: 60 into a MarketOnOpen order.
2014-10-21 00:00:00 Trade I : QQQ , 576.0 , XLK , [ 732.0129035]
2014-10-22 00:00:00 Converted OrderID: 61 into a MarketOnOpen order.
2014-10-22 00:00:00 Converted OrderID: 62 into a MarketOnOpen order.
2014-10-22 00:00:00 Trade I : QQQ , 1140.0 , XLK , [ 1448.77553817]
2014-10-23 00:00:00 Converted OrderID: 63 into a MarketOnOpen order.
2014-10-23 00:00:00 Converted OrderID: 64 into a MarketOnOpen order.
2014-10-23 00:00:00 Trade I : QQQ , 1140.0 , XLK , [ 1448.77553817]
2014-10-24 00:00:00 Converted OrderID: 65 into a MarketOnOpen order.
2014-10-24 00:00:00 Converted OrderID: 66 into a MarketOnOpen order.
2014-10-24 00:00:00 Trade I : QQQ , 1135.0 , XLK , [ 1442.42125949]
2014-10-25 00:00:00 Converted OrderID: 67 into a MarketOnOpen order.
2014-10-25 00:00:00 Converted OrderID: 68 into a MarketOnOpen order.
2014-10-25 00:00:00 Trade I : QQQ , 1134.0 , XLK , [ 1441.15040376]
2014-10-28 00:00:00 Converted OrderID: 69 into a MarketOnOpen order.
2014-10-28 00:00:00 Converted OrderID: 70 into a MarketOnOpen order.
2014-10-28 00:00:00 Trade I : QQQ , 1134.0 , XLK , [ 1446.51044912]
2014-10-29 00:00:00 Converted OrderID: 71 into a MarketOnOpen order.
2014-10-29 00:00:00 Converted OrderID: 72 into a MarketOnOpen order.
2014-10-29 00:00:00 Trade I : QQQ , 1130.0 , XLK , [ 1441.40811949]
Jing Wu
Hi Hanbyul, since here you use schedule Events method, then you can set your equity resolution to minute and move all the code in Ondata to Action function in Schedule Event(see the attached backtest below). As for daily resolution, it will calculate the order quantity at midnight 00:00, it is not accurate since market is not open and there are also margin and cash requirements for short selling. That might be the reason for many invalid orders with huge quantity in your backtest.
HanByul P
Hi Jingw, I see. With 'Minute' mode, the result looks quite different now. Thank you :)
HanByul P
Jingw, Sorry, I have one more last question. I thought that 'Daily' mode in schedule function is based on all the close prices of each day. So the most recent 'close' is yesterday's close price. Then, from what you mentioned, "...As for daily resolution, it will calculate the order quantity at midnight 00:00, it is not accurate since market is not open...", I don't understand why the calculation of 'quantity' is NOT accurate. At midnight, isn' t this based on the all the 'close' of each day? or different ? I guess I am missing something here. Please give me more explanation. Thank you :)
Jing Wu
Hanbyul, when using minute resolution and scheduled event, the quantity is calculated at scheduled time 10:00 am, the order is submitted at the same time. But when using daily resolution and scheduled event together, quantity is calculated at 00:00, the order is submitted at 110:00 am.There is a gap between them.
HanByul P
Hi Jingw, Thanks for your explanation. In addition, I investigated some based on your explanation. I was confused by 'Daily' mode in 'History' function and 'Daily' mode in 'AddSecurity'. When we use 'History' function with 'Daily' resolution, the most recent price (close) is yesterday's close price. So when we use 'AddSecurity("XLK", Resolution.Daily)' or 'AddSecurity("XLK", Resolution.Minute)', we get the same historical prices for our defined lookback periods. In other words, although we use 'AddSecurity' with different resolution modes ('Daily' vs 'Minute'), I think that all the calculations we want are based on the same historical data, meaning we get the same result from the calculations.
However, I missed this part: When we use 'AddSecurity("XLK", Resolution.Daily)', all the orders are filled with the most recent (price) data, which is in our case yesterday's close price. For example, if our orders are generated at 3:55pm everyday, our orders get filled with yesterday's close price (How can we get the yesterday's close price at 3:55pm today? It's impossible and totally non-sense !!!). But if we use 'AddSecurity("XLK", Resolution.Minute)', all the orders are filled with the most current price, which is in our case the price at 10am today (Not Yesterday's price !). This is more realistic ! I think this huge difference between two modes made totally different results.
In conclusion, I think we all should use 'AddSecurity("Symbol", Resolution.Minute)' to get more realistic results. Or we should add most current minute-based data to our historical data (if it's 'Daily' mode.). This is very basic but I think it is very important too. Thank you Jingw.
Jared Broad
Correct Han -- I think if using daily data you should only submit MarketOnOpen orders; as the event is triggered after the trading data is completed and market/market on close orders will both be too late. Submitting a MOO order at midnight will be filled accurately the same day; but you still won't get any information on it until midnight when that daily bar closes (but at least it'll be fill with the expected prices).
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.
HanByul P
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!