Initially, I wanted to use marketstack.com for the API stock data, but the free connection is only by http, not by https. I updated the Info.plist for this exception, but was not totally sure, if it was working. I then looked into the great solution from @chflorian and now I’m using AlphaVantage, too.
I used the full daily API and extracted the min/max of the last year (last 365 days; the API response will give to you more than 2 years of data). Furthermore I passed the data from the last week and the last month and the last year into arrays, so I can display them in the line chart.
Then, when the values are displayed in the tiles, I truncated them to the decimal point (no rounding) when they were passed as a string, and rounded them when they were passed as decimal.
For the line chart, I added another rectangle so that you can see the values when you slide over the line chart with your finger. The value is only displayed in black and I couldn’t find a parameter to change this.
Currently, I use the Demo Key for the API request, thus you only see fictional IBM data. In the sample below, you see the current value of IBM, the daily min/max and the annual min/max. In the chart, you see the closing values of the last 7 days. You may notice that the current day is on the left side, so I need to reverse the values in the array.
Things to do:
- Reverse the values in the line chart array, so that the most current date is on the righthand side
- Highlight the “week”, “month”, year" buttons, depending on what is seen on the line chart
- Get the “add ticker symbol” and “remove” buttons to work
As this is working quite nicely, I will try to share my solution when it’s finished.
(keep in mind: the colors in the background blend without any boarders in the app, it’s just in the GIF that you can see the pixels)
Edit: Oh, the array reverse was reaaaaly easy.
var oneWeekCloseValues: [Double] {
var first7 = Array(closeValues.prefix(upTo: 7))
first7.reverse()
return first7
}