NOTE: This script is for learning purposes only and does not constitute a recommendation for buying or selling any stock mentioned in this script.
SUMMARY: This project aims to construct and test an algorithmic trading model and document the end-to-end steps using a template.
INTRODUCTION: This algorithmic trading model employs a simple strategy using the weekly price and simple moving average (SMA) for the entry and exit signals. For the SMA line, the model will use 25, 50, 75, and 100 days. The model will signal a long position when the weekly price crosses from below the SMA line to above. Conversely, the model will exit the long position when the weekly moves below the SMA line. The model will deploy all of its available capital per transaction to compare strategies.
ANALYSIS: In this modeling iteration, we analyzed thirteen Exchange-Traded Funds (ETF) for five years between January 1, 2017, and December 31, 2021. The models’ performance appeared at the end of the script. The buy-and-hold model generally produced a better return for the tested equities. The algorithmic trading model only comes out ahead with the energy sector ETF (XLE).
Amongst the moving average models, the 50-day model achieved the best results.
Buy-and-Hold: $150,303.80
25-day SMA: $43,853.16
50-day SMA: $54,411.06
75-day SMA: $51,384.59
100-day SMA: $26,551.52
SMA = 25
Buy_Hold_Return BH_Return_PCT BH_MAX_Drawdown Model_Return_VAL Model_Return_PCT Model_MAX_Drawdown
SPY 12877.5546 130.7275 0.0000 4097.0205 40.9702 -15.2550
QQQ 24586.6537 246.2731 0.0000 10218.5834 102.1858 -156.3498
XLB 10058.0173 100.7556 -1341.2387 -36.7190 -0.3672 -2656.6290
XLC 6086.7340 61.1607 -1931.8250 3555.1586 35.5516 -560.4453
XLE -723.7860 -7.2649 -6116.7540 5178.1182 51.7812 -515.5180
XLF 8254.3023 82.6351 -1534.1967 -688.0155 -6.8802 -3886.3622
XLI 8488.9394 85.1742 -1300.3856 1343.5108 13.4351 -1748.8565
XLK 28015.9005 280.4021 0.0000 11079.4514 110.7945 0.0000
XLP 7058.5651 70.7087 -113.8109 1854.8170 18.5482 -38.0145
XLRE 9797.9809 98.1428 -267.4271 386.0426 3.8604 -2354.5343
XLU 7366.0707 73.7749 -22.7373 1055.4053 10.5541 -453.0287
XLV 12094.9515 121.4858 0.0000 941.6611 9.4166 -1037.0731
XLY 16341.9195 163.5815 0.0000 4868.1323 48.6813 -246.8632
SMA = 50
Buy_Hold_Return BH_Return_PCT BH_MAX_Drawdown Model_Return_VAL Model_Return_PCT Model_MAX_Drawdown
SPY 12877.5546 130.7275 0.0000 5308.2378 53.0824 -15.2550
QQQ 24586.6537 246.2731 0.0000 12737.0915 127.3709 0.0000
XLB 10058.0173 100.7556 -1341.2387 -132.1118 -1.3211 -1025.3787
XLC 6086.7340 61.1607 -1931.8250 2947.1054 29.4711 -318.3278
XLE -723.7860 -7.2649 -6116.7540 4316.2861 43.1629 -1302.4730
XLF 8254.3023 82.6351 -1534.1967 642.7439 6.4274 -1780.0172
XLI 8488.9394 85.1742 -1300.3856 1473.5733 14.7357 -1061.1170
XLK 28015.9005 280.4021 0.0000 14795.0697 147.9507 0.0000
XLP 7058.5651 70.7087 -113.8109 2056.0999 20.5610 -168.0443
XLRE 9797.9809 98.1428 -267.4271 1643.2901 16.4329 -1051.2054
XLU 7366.0707 73.7749 -22.7373 -480.1260 -4.8013 -979.0842
XLV 12094.9515 121.4858 0.0000 1627.9453 16.2795 -430.0784
XLY 16341.9195 163.5815 0.0000 7475.8599 74.7586 0.0000
SMA = 75
Buy_Hold_Return BH_Return_PCT BH_MAX_Drawdown Model_Return_VAL Model_Return_PCT Model_MAX_Drawdown
SPY 12877.5546 130.7275 0.0000 5377.6900 53.7769 0.0000
QQQ 24586.6537 246.2731 0.0000 8470.7960 84.7080 -337.9920
XLB 10058.0173 100.7556 -1341.2387 2155.4260 21.5543 -911.9216
XLC 6086.7340 61.1607 -1931.8250 6026.6871 60.2669 -13.4357
XLE -723.7860 -7.2649 -6116.7540 763.1591 7.6316 -2461.8256
XLF 8254.3023 82.6351 -1534.1967 3190.6586 31.9066 -355.4903
XLI 8488.9394 85.1742 -1300.3856 1850.3269 18.5033 -366.5802
XLK 28015.9005 280.4021 0.0000 7299.2418 72.9924 -352.3613
XLP 7058.5651 70.7087 -113.8109 2394.2547 23.9425 -38.0145
XLRE 9797.9809 98.1428 -267.4271 5188.4688 51.8847 -246.4546
XLU 7366.0707 73.7749 -22.7373 1024.6837 10.2468 -502.6518
XLV 12094.9515 121.4858 0.0000 952.9627 9.5296 -1395.3537
XLY 16341.9195 163.5815 0.0000 6690.2355 66.9024 -72.1826
SMA = 100
Buy_Hold_Return BH_Return_PCT BH_MAX_Drawdown Model_Return_VAL Model_Return_PCT Model_MAX_Drawdown
SPY 12877.5546 130.7275 0.0000 411.8029 4.1180 -469.4300
QQQ 24586.6537 246.2731 0.0000 2555.0782 25.5508 -60.6627
XLB 10058.0173 100.7556 -1341.2387 -1105.1112 -11.0511 -1366.3663
XLC 6086.7340 61.1607 -1931.8250 7824.3660 78.2437 -60.7521
XLE -723.7860 -7.2649 -6116.7540 440.9515 4.4095 -2717.9478
XLF 8254.3023 82.6351 -1534.1967 1916.5176 19.1652 -1881.6719
XLI 8488.9394 85.1742 -1300.3856 3611.8204 36.1182 -986.2365
XLK 28015.9005 280.4021 0.0000 5922.2349 59.2223 -132.4859
XLP 7058.5651 70.7087 -113.8109 305.2303 3.0523 -800.5252
XLRE 9797.9809 98.1428 -267.4271 1426.8374 14.2684 -1412.7599
XLU 7366.0707 73.7749 -22.7373 411.9854 4.1199 -744.8940
XLV 12094.9515 121.4858 0.0000 924.9504 9.2495 -1070.3463
XLY 16341.9195 163.5815 0.0000 1904.8643 19.0486 -72.1826
CONCLUSION: For most stocks during the modeling time frame, the customized trading strategy with the Weekly Price and Simple Moving Average did not produce a better return than the buy-and-hold approach. We should consider modeling these stocks further by experimenting with more strategy variations.
Dataset ML Model: Time series analysis with numerical attributes
Dataset Used: Sharadar US Equities and Fund Prices hosted by Quandl/Nasdaq Data Link
The HTML formatted report can be found here on GitHub.