# How to code prior close in financial data?

**URL:** <https://community.wolfram.com/t/how-to-code-prior-close-in-financial-data/18873>\
**Category:** Questions\
**Tags:** finance, curated-data\
**Created:** [September 20, 2021, 11:08pm UTC](https://community.wolfram.com/t/how-to-code-prior-close-in-financial-data/18873 "2021-09-20T23:08:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raymondlow](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@raymondlow](https://community.wolfram.com/u/raymondlow)\
**Post date:** [September 20, 2021, 11:08pm UTC](https://community.wolfram.com/t/how-to-code-prior-close-in-financial-data/18873/1 "2021-09-20T23:08:41Z")

</div>

I would like to compute the range based on the high compared to the prior close. My data:

```
upstP = FinancialData["UPST", {2021}];
upstH = FinancialData["UPST", "High", {2021}];
upstL = FinancialData["UPST", "Low", {2021}];
upstC = FinancialData["UPST", "Close", {2021}];

```

I tried DatePlus[] without success

```
DatePlus[upstC, -1]
upstC[[1, 1]] // Normal // Short
DatePlus[upstC[[All, 1]], -1, "Day"]

```

I tried TimeSeriesShift[] without success

```
TimeSeriesThread[{1, -1} . # &, {upstC, TimeSeriesShift[upstC, -1]}] //
   Normal // Short

```

and some other variations that I can no longer remember. How would I subtract yesterday’s close from today’s high – continuous???

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [September 21, 2021, 9:35am UTC](https://community.wolfram.com/t/how-to-code-prior-close-in-financial-data/18873/2 "2021-09-21T09:35:06Z")

</div>

> … high compared to the prior close…

I am not sure what you mean and I do not know anything about stock market (and do feel a very strong aversion to this stuff!), but for the sake of the syntax:

```
DateListPlot[upstH/TimeSeriesShift[upstC, Quantity[1, "Days"]], 
 ImageSize -> Large, Filling -> Bottom, PlotRange -> All]

```

 ![tssh.png](https://global.discourse-cdn.com/wolfram/original/3X/f/3/f38402bf642d1eff8f40620db0924ee6607adf3e.png)

---

<div class="post-metadata">

**Author:** ![moblin88](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/moblin88/32/8951_2.png) [@moblin88](https://community.wolfram.com/u/moblin88)\
**Post date:** [September 21, 2021, 8:50pm UTC](https://community.wolfram.com/t/how-to-code-prior-close-in-financial-data/18873/3 "2021-09-21T20:50:31Z")

</div>

Because these are irregular time series, meaning that the times of our data points can have gaps between them, TimeSeriesShift will produce output that won’t align with the following business day.

I think an easier approach is to get everything in a single time series and then to use MovingMap to take the difference.

```
data=FinancialData["UPST", "OHLC", {2021}];
diffs=MovingMap[#Values[[-1,2]]-#Values[[1,4]]&,data,Quantity[2,"Events"]];
DateListLogPlot[diffs]

```

 ![D40E4752-49FF-4685-82CA-6D3A96CBFFAF.png](https://global.discourse-cdn.com/wolfram/original/2X/2/2ac46741ae0c13fe0a5f11cd8ea517332197401a.png)

Here I use `#Values[[-1,2]]` to get the high of the end of the sliding window and `#Values[[1,4]]` to get the close of the start of the sliding window, where the window size is 2 events in the time series. You can use a similar approach to compute rolling means, crossovers, etc. Also note that the date series is preserved so you don’t need to worry about weekends, holidays, or days where there may not be any data to market disruptions, quality issues, etc.

---

<div class="post-metadata">

**Author:** ![raymondlow](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@raymondlow](https://community.wolfram.com/u/raymondlow)\
**Post date:** [September 24, 2021, 1:17am UTC](https://community.wolfram.com/t/how-to-code-prior-close-in-financial-data/18873/4 "2021-09-24T01:17:49Z")

</div>

Thank you Nicholas,

I tried working with your code and got closer to my target. I managed a 50 day moving average, which was interesting on upst, and questionable, seems like there is a mistake here?? Unless one or two very large jumps are moving through the average causing the distortion??

```
data = FinancialData["UPST", "OHLC", {2021}];

diff50HL = 
  MovingAverage[
   MovingMap[#Values[[-1, 2]] - #Values[[1, 3]] &, data, 
    Quantity[2, "Events"]], 50];
DateListLogPlot[diff50HL]

```

 ![5280Capture.PNG](https://global.discourse-cdn.com/wolfram/original/3X/4/8/48d2d63199389018e2bf335c50eaf7d30dc5191b.png)

I got the differences between the high & low, the high & close, and the low & close –

```
diffHL = MovingMap[#Values[[-1, 2]] - #Values[[1, 3]] &, data, 
   Quantity[2, "Events"]];
diffHC = MovingMap[#Values[[-1, 2]] - #Values[[1, 4]] &, data, 
   Quantity[2, "Events"]];
diffLC = MovingMap[#Values[[-1, 3]] - #Values[[1, 4]] &, data, 
   Quantity[2, "Events"]];

```

Threaded the three differences together to determine the maximum difference –

```
diffT = Transpose[{Flatten@diffHL["Values"], 
    Flatten@Abs[diffHC["Values"]], Flatten@Abs[diffLC["Values"]]}] ;
diffMax = Max[#] & /@ diffT ;

```

And then I got stuck, try as I am, hours later, I still was not able to compute a moving percentage of the maximum difference and 50 day moving average of the high-low (range). The only result I got was diffidently not what I wanted

```
mm = MovingAverage[diffMax/diff50HL, 1];
DateListPlot[mm]

```

 ![6867Capture.PNG](https://global.discourse-cdn.com/wolfram/original/3X/7/4/74b0fc1167b8908d7c87d9522046e63da7b67fe2.png)

Everything else I tried crashed and burned. Thank you for any help that you can provide.
