Skip to content

Instantly share code, notes, and snippets.

@alphazwest
Last active August 26, 2022 12:29
Show Gist options
  • Save alphazwest/72a4c00a413c2296d9e4489cfed58f35 to your computer and use it in GitHub Desktop.
Save alphazwest/72a4c00a413c2296d9e4489cfed58f35 to your computer and use it in GitHub Desktop.
Date Open Adj Close Predicted Close Trade Made Gain
2020-10-22 00:00:00 441.920013 425.790009 408.98989935265433 False 0
2020-10-23 00:00:00 421.839996 420.630005 404.07720184792413 False 0
2020-10-26 00:00:00 411.630005 420.279999 403.74397078786285 False 0
2020-10-27 00:00:00 423.76001 424.679993 407.93308373130776 False 0
2020-10-28 00:00:00 416.480011 406.019989 390.16740853240196 False 0
2020-10-29 00:00:00 409.959991 410.829987 394.7468749770709 False 0
2020-10-30 00:00:00 406.899994 388.040009 373.0491654300521 False 0
2020-11-02 00:00:00 394.0 400.51001 384.92150948185565 False 0
2020-11-03 00:00:00 409.730011 423.899994 407.19046819438336 False 0
2020-11-04 00:00:00 430.619995 420.980011 404.4104329079854 False 0
2020-11-05 00:00:00 428.299988 438.089996 420.7003777637394 False 0
2020-11-06 00:00:00 436.100006 429.950012 412.95052348352124 False 0
2020-11-09 00:00:00 439.5 421.26001 404.67701223401446 False 0
2020-11-10 00:00:00 420.089996 410.359985 394.29939903468437 False 0
2020-11-11 00:00:00 416.450012 417.130005 400.7449483716564 False 0
2020-11-12 00:00:00 415.049988 411.76001 395.63232422700196 False 0
2020-11-13 00:00:00 410.850006 408.5 392.52855861123976 False 0
2020-11-16 00:00:00 408.929993 408.089996 392.13820511001586 False 0
2020-11-17 00:00:00 460.170013 441.609985 424.0516622156464 False 0
2020-11-18 00:00:00 448.350006 486.640015 466.9235119310582 True 38.29
2020-11-19 00:00:00 492.0 499.269989 478.9481618643929 False 0
2020-11-20 00:00:00 497.98999 489.609985 469.7511384616041 False 0
2020-11-23 00:00:00 503.5 521.849976 500.44594477148723 False 0
2020-11-24 00:00:00 540.400024 555.380005 532.3689606842327 False 0
2020-11-25 00:00:00 550.059998 574.0 550.096544417615 True 23.94
2020-11-27 00:00:00 581.159973 585.76001 561.2929256185988 False 0
2020-11-30 00:00:00 602.210022 567.599976 544.0032580684158 False 0
2020-12-01 00:00:00 597.590027 584.76001 560.340853196808 False 0
2020-12-02 00:00:00 556.440002 568.820007 545.1648159372456 False 0
2020-12-03 00:00:00 590.02002 593.380005 568.5477127122825 False 0
2020-12-04 00:00:00 591.01001 599.039978 573.936416913663 False 0
2020-12-07 00:00:00 604.919983 641.76001 614.6089812388828 True 36.84
2020-12-08 00:00:00 625.51001 649.880005 622.3398045434619 False 0
2020-12-09 00:00:00 653.690002 604.47998 579.1156927923497 False 0
2020-12-10 00:00:00 574.369995 627.070007 600.623034506559 True 52.7
2020-12-11 00:00:00 615.01001 609.98999 584.3616213571412 False 0
2020-12-14 00:00:00 619.0 639.830017 612.7714881293336 False 0
2020-12-15 00:00:00 643.280029 633.25 606.5068354087191 False 0
2020-12-16 00:00:00 628.22998 622.77002 596.5291354698 False 0
2020-12-17 00:00:00 628.190002 655.900024 628.0712986120185 False 0
2020-12-18 00:00:00 668.900024 695.0 665.2973074543002 False 0
2020-12-21 00:00:00 666.23999 649.859985 622.3207440535778 False 0
2020-12-22 00:00:00 648.0 640.340027 613.2570545851711 False 0
2020-12-23 00:00:00 632.200012 645.97998 618.6266982966673 False 0
2020-12-24 00:00:00 642.98999 661.77002 633.6599599196408 False 0
2020-12-28 00:00:00 674.51001 663.690002 635.4879218321755 False 0
2020-12-29 00:00:00 661.0 665.98999 637.6776769774252 False 0
2020-12-30 00:00:00 672.0 694.780029 665.0878791316064 False 0
2020-12-31 00:00:00 699.98999 705.669983 675.4559040095767 False 0
@rw417
Copy link

rw417 commented Nov 19, 2021

Hey Zack, I came across your article about using linear regression to model closing price with EMA10. It seems that you were using EMA10 calculated using price on the day of to predict closing price of the same day. Did I understand that correctly?
I know this is a toy example, but wanted to make sure I understood your method! Thanks!

@alphazwest
Copy link
Author

That's correct. IMO, there's a strong case for shifting data in some cases though for this article I felt the EMA--given it accounts for the previous n-many periods (10, in this case)--didn't need such a shift.

Implementing a function to find an optimal lag period to maximize correlation could 100% be a better approach in some cases. Maybe even the EMA case--I didn't run a comparison test.

If one were to use a feature that was an observed value for a single period like "open price greater than 30% above the 52-week low," finding a shift might be useful.

Here's a link to an article that does an OK job offering an example of such a case: https://wtm695450085.medium.com/example-of-the-use-of-shift-for-linear-regression-in-python-how-to-find-optimal-correlation-shift-d86980e82148

@rw417
Copy link

rw417 commented Nov 19, 2021

The article you linked to about correlation shift is an interesting read!

My concern with using EMA at time t to predict the closing price at time t is that when you get EMA at time t, the actual closing price would have become available at time t. This essentially turns the problem into correlational inference instead of prediction. Trading based on predicted price at time t when the actual price of time t wouldn't provide us an edge.

@alphazwest
Copy link
Author

I agree with your concern here. The article was casual in nature but this is definitely a fundamental enough issue it should be addressed. I will definitely plan on updating the article. Out of curiosity, have you done any experimentation using a 1-period shift for such cases?

@carovici
Copy link

Hi. Coud you please share the code for generating this table and the one for the previous graph, of the linear regression with the the predicted values, real values and residuals?
Thanks!

@mathewm3
Copy link

can you please share the source code for generating the table and graphs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment