From ||y@@k|pn|@ @end|ng |rom gm@||@com Tue Jul 8 17:40:43 2025 From: ||y@@k|pn|@ @end|ng |rom gm@||@com (Ilya Kipnis) Date: Tue, 8 Jul 2025 11:40:43 -0400 Subject: [R-SIG-Finance] Is the adjustOHLC function working correctly? Message-ID: So, I'm not sure the adjustOHLC function is working correctly. MRE: [image: image.png] That is, after using adjustOHLC, the adjusted close price for AAPL (4.56e-08) very much does NOT match the actual adjusted closing price column (.261). I'm not sure if this function is a completely kosher fix because it simply works off of the ratio between close and adjusted columns, but I'd think the most important thing is to make sure that the adjusted closing price matches, and that the same ratio should be used daily, no? So, here's my function: adjust <- function(x){ ratio <- Ad(x)/Cl(x) x[,1] <- Op(x) * ratio x[,2] <- Hi(x) * ratio x[,3] <- Lo(x) * ratio x[,4] <- Cl(x) * ratio return(x) } Thanks. -Ilya -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image.png Type: image/png Size: 38037 bytes Desc: not available URL: From jo@h@m@u|r|ch @end|ng |rom gm@||@com Tue Jul 8 18:18:10 2025 From: jo@h@m@u|r|ch @end|ng |rom gm@||@com (Joshua Ulrich) Date: Tue, 8 Jul 2025 11:18:10 -0500 Subject: [R-SIG-Finance] Is the adjustOHLC function working correctly? In-Reply-To: References: Message-ID: Hi Ilya, On Tue, Jul 8, 2025 at 10:41?AM Ilya Kipnis wrote: > > So, I'm not sure the adjustOHLC function is working correctly. > > MRE: > Thanks for the example. Here's the code and output from your image. getSymbols("AAPL", src = "yahoo", from = "1990-01-01") [1] "AAPL" head(AAPL) AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted 1990-01-02 0.314732 0.334821 0.312500 0.332589 183198400 0.2614986 1990-01-03 0.339286 0.339286 0.334821 0.334821 207995200 0.2632536 1990-01-04 0.341518 0.345982 0.332589 0.335938 221513600 0.2641318 1990-01-05 0.337054 0.341518 0.330357 0.337054 123312000 0.2650093 1990-01-08 0.334821 0.339286 0.330357 0.339286 101572800 0.2667642 1990-01-09 0.339286 0.339286 0.330357 0.335938 86139200 0.2641318 head(aapl <- adjustOHLC(AAPL)) AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted 1990-01-02 4.321185e-08 4.597001e-08 4.290540e-08 4.566357e-08 183198400 0.2614986 1990-01-03 4.658305e-08 4.658305e-08 4.597001e-08 4.597001e-08 207995200 0.2632536 1990-01-04 4.688950e-08 4.750239e-08 4.566357e-08 4.612338e-08 221513600 0.2641318 1990-01-05 4.627660e-08 4.688950e-08 4.535712e-08 4.627660e-08 123312000 0.2650093 1990-01-08 4.597001e-08 4.658305e-08 4.535712e-08 4.658305e-08 101572800 0.2667642 1990-01-09 4.658305e-08 4.658305e-08 4.535712e-08 4.612338e-08 86139200 0.2641318 > > That is, after using adjustOHLC, the adjusted close price for AAPL (4.56e-08) very much does NOT match the actual adjusted closing price column (.261). > > I'm not sure if this function is a completely kosher fix because it simply works off of the ratio between close and adjusted columns, but I'd think the most important thing is to make sure that the adjusted closing price matches, and that the same ratio should be used daily, no? > As is often the case, the issue is with the Yahoo data. I assure you, AAPL was not actually trading at $0.33 at the beginning of 1990. I have no idea what that number represents. It could be just a dividend OR split adjusted price. I don't care enough to try and figure it out. Just switch to a better data source. I use Tiingo [1]. getSymbols("AAPL", src = "tiingo", from = "1990-01-01") [1] "AAPL" head(AAPL) AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted 1990-01-02 35.25 37.50 35.00 37.25 1635700 0.2614729 1990-01-03 38.00 38.00 37.50 37.50 1857100 0.2632278 1990-01-04 38.25 38.75 37.25 37.63 1977800 0.2641403 1990-01-05 37.75 38.25 37.00 37.75 1101000 0.2649826 1990-01-08 37.50 38.00 37.00 38.00 906900 0.2667375 1990-01-09 38.00 38.00 37.00 37.63 769100 0.2641403 head(aapl <- adjustOHLC(AAPL)) AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted 1990-01-02 0.2474586 0.2632538 0.2457036 0.2614988 1635700 0.2614729 1990-01-03 0.2667639 0.2667639 0.2632538 0.2632538 1857100 0.2632278 1990-01-04 0.2685189 0.2720290 0.2614988 0.2641665 1977800 0.2641403 1990-01-05 0.2650089 0.2685189 0.2597438 0.2650089 1101000 0.2649826 1990-01-08 0.2632538 0.2667639 0.2597438 0.2667639 906900 0.2667375 1990-01-09 0.2667639 0.2667639 0.2597438 0.2641665 769100 0.2641403 You can see that adjustOHLC() is working correctly when you give it correct inputs. The differences between the "AAPL.Close" from adjustOHLC() and the "AAPL.Adjusted" from Tiingo are due to floating point round-off errors, or lack of precision in the split / dividend data that adjustOHLC() gets from Yahoo (at least that data appears correct). Best, Josh [1] https://www.tiingo.com/ > So, here's my function: > > adjust <- function(x){ > ratio <- Ad(x)/Cl(x) > x[,1] <- Op(x) * ratio > x[,2] <- Hi(x) * ratio > x[,3] <- Lo(x) * ratio > x[,4] <- Cl(x) * ratio > return(x) > } > > Thanks. > > -Ilya > > > _______________________________________________ > R-SIG-Finance at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance > -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions should go. -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com