Visualizing Stock Market Data with Google Trends
The purpose of this project is to visualize stock market data with google trends data. The interest in this project comes from the idea of whether more searches of a company corresponds to changes in stock market prices. The data for the stocks market is pulled from yahoo finance and the google trends data straight from google trends. The google trends data is shown as a score from 0 - 100 which represents the amount of traffic the search term received relative to itself during the timeframe. Further layered on top of this is what the rising related queries were during the time span.
Graph
The graph can be configured to be more easily readable depending on what data you want to visualize.
Select the company:
- Apple
- NVIDIA
- Intel
- AMD
- Samsung
Select graph configuration for trends:
- Markers
- Line
- Text
Select the text position for trends:
- Top
- Bottom
RuntimeError: Failed to fetch
Discussion
The graphs show us something interesting in most of the cases. The google trends does actually seem to follow the stock market price fluctuations. This is most likely due to investors betting on wether product cycles will find success or if consumers may not find a large amount of interest in the releasing products. The related queries will sometimes show us what products are being released or announced which gives an interesting insight into stock market fluctuations.
Google Trends
Google trends is the data analysis tool provided by google at trends.google.com. It allows a user to see what terms are being searched regularly along with any terms that are currently rising in the search ranks. For this graph I am using the "Rising" data instead of "Top" data. This is due to the desire to see what search terms are changing around based on the current events. If the top data was chosen to be graphed then it would have stayed as a single search term for all data points (in this case apple watch). Below is a sample of what rising vs top data looks like.
Furthermore you may see that there are searches that are food related and then those that are company related. This is because the google trends analysis tool only gives data based on the input string and doesn't have a way to help filter out the results. This can lead to some random terms popping into the graph.
Data Collection and Parsing
The google trends data is collected with python and brought into observable by converting the data to a csv file and manually importing and can be seen here: Google Colab Link. This is because there is no official google trends api.
The Stocks data is brought in from yahoo finance through its API. The API call is tailored to the company that is requesting data for instead of requesting all companies at once.
StocksData = TypeError: Failed to fetch
Once the trends data is imported using a csv it becomes easy to parse the csv files to build our data arrays.
TrendsData = Array(52) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, …]
RelatedData = Array(51) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, …]
There are two arrays that need to be created by hand which is an array of the companies that are being graphed along with the stock symbols for those companies. The two arrays need to be in the same order so the indexes of the companies are the same for both.
Names = Array(5) ["Apple", "NVIDIA", "Intel", "AMD", "Samsung"]
Symbols = Array(5) ["AAPL", "NVDA", "INTC", "AMD", "005930.KS"]
Functions
Three functions are needed to make data manipulation much easier. First is a function to parse the dates from the data arrays so that they all have the exact same format and will be able to be graphed on the same x axis. The final two functions are for extracting the data from the data arrays when only parts of the arrays are being used as data points.
parseDate = ƒ(e)
unpack = ƒ(rows, key)
unpackDate = ƒ(rows, key)
Includes
Plotly = Object {version: "1.58.5", register: ƒ(t), plot: ƒ(t, e, i, a), newPlot: ƒ(t, e, n, i), restyle: ƒ(t, e, n, i), relayout: ƒ(t, e, r), redraw: ƒ(t), update: ƒ(t, e, n, i), react: ƒ(t, e, n, i), extendTraces: ƒ(e, n, i, a), prependTraces: ƒ(e, n, i, a), addTraces: ƒ(e, n), deleteTraces: ƒ(e, n), moveTraces: ƒ(e, n, i), purge: ƒ(t), addFrames: ƒ(t, e, r), deleteFrames: ƒ(t, e), animate: ƒ(t, e, r), setPlotConfig: ƒ(t), toImage: ƒ(t, e), …}
d3 = Object {format: ƒ(t), formatPrefix: ƒ(t, n), timeFormat: ƒ(t), timeParse: ƒ(t), utcFormat: ƒ(t), utcParse: ƒ(t), Adder: class, Delaunay: class, FormatSpecifier: ƒ(t), InternMap: class, InternSet: class, Node: ƒ(t), Path: class, Voronoi: class, ZoomTransform: ƒ(t, n, e), active: ƒ(t, n), arc: ƒ(), area: ƒ(t, n, e), areaRadial: ƒ(), ascending: ƒ(t, n), …}