This post is one step in my attempts to apply neuroscience analysis to the stock market.
I’ve written about brain oscillations before so if you need some back ground on what they are, you can go here.
Brain oscillations are the change in voltage over time in a brain region of interest. The voltage changes due to the aggregate underlying cellular activity. There are two main ways to record brain oscillations, via non-invasive EEG or invasive by putting electrodes into the brain. EEGs use a cap that fits around the skull with electrodes spaced around the head over different regions. A conductive gel is put on the hair and scalp to pick up better signals.The voltage signal from an EEG is not the cleanest as it first must pass through the non-conductive bone of the skull. EEGs are only able to pick up broad activity on the surface of the brain below a particulate electrode on the cap. Metal electrodes placed in the brain pick up very crisp signals from the surrounding brain area, which can be on the surface or in deeper brain structures.
Oscillations are broken up into different frequency bands reflecting different mechanisms which generate them. Depending on the cell composition, cell and circuit architecture, and from where inputs are entering a brain region, different oscillations can be observed. For example, during exploration, learning, and memory recall the hippocampus displays a classic 4-12Hz rhythm called the Theta rhythm. Slower Delta (1-4Hz) rhythms can be seen during sleep throughout the brain. Faster rhythms (~40Hz) may aid in feature binding. Thus, different rhythms are associated with different behaviors and brain functions. The interplay of different rhythms can also be meaningful. For example when a meaningful cue is presented, the theta rhythm decreases in voltage while the beta rhythm (15-30Hz) increases in voltage. This could indicate a brain region switching its processing state to better process meaningful information.
So can we analyze a stock like we do neural oscillations? Can we see shifts in sentiment to a company reflected in shifts in stock oscillations? Can we predict future volatility based on oscillatory changes?
Stock Data to Brain Oscillations
To analyze a stock like brain oscillations I first need to transform the stock data to look like brain data. Stocks are measured over time and time is an inherent property of oscillations. Without time, there is no oscillation. Secondly there needs to be an analog to voltage. Brain oscillations often have a voltage range in which they oscillate in. As many neurons fire, the voltage decreases as ions move into cells. As many neurons stop firing, voltage increases as ions are pushed out of cells. Stock price does this as well, price goes up, price goes down. But a stock price often travels in a general direction over time rather than oscillations around a baseline. Of course there are exceptions when stocks “trade sideways” for a given length of time. But with a large enough time horizon, stocks go up on average.
So rather than simply stock price points I decided to look at the price change from one time point to another. This can be done as the change in the stock’s price; price per share from one time point to another. Or a percentage can be used; percent change from one time point to another. We know market activity is relatively stable with a normal distribution around 0%. Below you can see a histogram of the Dow Jones Industrial average.
So I have a measure (price change) over time which will oscillate around 0. I’m going to try this first transformation step both on price change and % price change.
Now where to get data… I have limited resources so free is best. Historical pricing data can be very expensive depending on the level of detail you want, such as stock prices every 5 minutes. However, Yahoo Finance and Google Finance allow you to download, for free, a CSV of historical price data on the range of days. This can easy be viewed in Excel. From Yahoo Finance I grabbed data for Apple. The data is laid out like so:
For each row you have a date, the opening price of the stock when trading began, the highest price the stock traded at during the day, the lowest intraday price, the final closing price, the volume (number of shares traded), and the adjusted close price. The adjusted closing price takes into account dividends and when the stock splits. Stock splits are when a company splits the shares of its stock into more shares. This can increase the liquidity of a stock but it does not change the underlying value of the company. Some companies split their stock when the price per share gets very high. Usually splits are 2 for 1 or 3 for 1 but there can be much higher. So when a stock splits 2 for 1, if you have 10 shares worth $10 each valued at $100, you will then have 20 shares worth $5 still valued at $100. This will be important later.
So all I need to do is subtract the adj close price from Day n from Day n-1 to see how the value of the stock changed.
Day n price change = Day n adjusted close price – Day n-1 adjusted close price
For percent change, I take the Day n price change and divide it by Day n-1 adjusted close price.
Day n price change percent = Day n price change / Day n-1 adjusted close price
With some basic excel equations, my data now looks like so:
|Date||Open||High||Low||Close||Volume||Adj Close||Change||% Change|
Yahoo Finance goes to when the stock went public so I have pricing data from today (July 11 2015) till December 12th, 1980. The first trading day will be ignored. All prices were rounded to two decimal places for simplicity. I next import the data to Matlab and make a vector for Change and % Change. I can use these vectors along with a third vector for Dates to plot the data.
I quickly encountered my first problem. By using the price change, due to the the stock splits Apple has had, three 2 for 1’s and one 7 for 1, the adjusted closing price change is very small for older dates when there were fewer shares. Funny enough the dot come boom still comes up clearly in the earlier 2000’s. You can see the massive drop in price on September 29th 2000 when AAPL fell by 50%. You can see in the below table the problem more clearly. When I go back to earlier dates between Adj Change and the actual price Change:
|Date||Open||High||Low||Close||Volume||Adj Close||Adj Change||% Adj Change||Change||% Change|
Take for example the 18th to the 19th. The close price went up from $26.63 to $28.28, a change of $1.62 or 6.1%. However, using adjusted close price, we end up with an adjusted change of $0.02. However, our % change is still the same at 6.1%. So for now on I will focus on using % Change of the Adjusted Closing Price. You can see a plot below looks much nicer.
Again you can see the massive drop on September 29th 2000 as well as the large amount of change, or volatility as investors say, around the dot com boom and again during the 2008-2009 financial crisis. Importantly for this project, we can see a lot of activity that oscillates around the 0% without any drift.
For comparison, here is what a brain oscillations from one of my rats looks like:
They are quite similar so I think there may be some merit to applying neural analysis to the stock data. The real neural oscillations look to be less volatile than the stock data. Rarely is there a massive change in voltage in a brain recording and when there is it is very often just noise, such as the rat scratching his brain implant (which is likely what you see around the 0 time mark above). The brain is an amazing machine with checks and balances, such as feed-back and feed-forward loops, to keep volatility low. High amounts volatility that cannot be controlled are the main cause of seizures. Below you can see health brain activity in blue and seizure activity in red. Notice how massive the voltage changes are during a seizure:
A final step before analysis will be applying frequency band filters to the stock data to see if that works. If different frequency bands can be filtered out using conventional filtering methods, more complex analysis can be done. I’m going to first try to use a simple Butterworth filter for the stock data.
Above are two Butterworth filtered plots for the Theta frequency range (4-12Hz) from Apple and one from Joey the Rat’s hippocampus. I messed around with the sampling rate for Apple to be quarterly (90 days) or yearly (365 days). Generally the sampling period in neurological data is how many voltage points you collect in one second, expressed in Hertz. These range depending on machine and application from 250Hz to 4,800Hz+. I’m not entirely sure about the implications of using small sampling windows such as 90. You can see the Quarterly data barely looks filtered (when zoomed out) while the Yearly sample rate is smoothed. I’ll have to keep testing this when I move on to the analysis phase. I only have 8,719 days of stock data for Apple, I think sampling at too high of a rate will smooth out a lot data. This may or may not be a good thing. If you have any advice on this please feel free to email me: email@example.com.
Since filtering works on stock % price change I’ll be moving on to more detailed data analysis in another post. The occasional quick and large movement in a stock price may be an impedance in applying neural analysis to stocks as brains do not normally do this. Overall, it appears that stocks do oscillate like brains but with a little bit of seizure-like activity.
Below is my Matlab scripts for the filtering process. Based on code written by David Bilkey for analyzing brain data.
nPoles = 2;
Low_frequency = 4;
High_frequency = 12;
Fs = 90; %sampling rate
aapl_change_filter = ax_bp_filter_stocks(nPoles,Low_frequency,High_frequency,aapl_change_percent,Fs);
function aapl_change_filter = ax_bp_filter_stocks(nPoles,HPass,LPass,EEG,Fs);
% filter using butterworth filter with n poles and high pass low
% Note that LP should be a higher value than the HP value
Wn=[HPass LPass]/(Fs/2); % frequencies are normalised to half the sampling rate
% Wn = normalised cutoff freq – must be < nyquist limit
aapl_change_filter=filtfilt(b,a,EEG); % now filter with zero phase shift
function [figure_plot_filtered] = basic_plot_stock_filtered(filtered_stock,dates)
%% Basic plotting script
title(‘Apple Price Change Percent from Adjusted Close Price’);
ylabel(‘Price Change %’)
Brain on cover image from emost-med.
Apple stock chart from finviz.