User Avatar

Leo E.

3y ago

Math enthusiast

Calculating On-Balance Volume Indicator for Shopify Stock using Python

Encouraged by yesterday's experiments I am continuing my exploration of the stock market. Today I will try to make sense of On-Balance Volume indicator. According to Investopedia, On-Balance Volume (OBV) is an indicator that uses volume flow to predict changes in stock prices.

The algorithm to calculate OBV is simple, it is a cumulative value that depends on the initial time of computation. Each step considers one of three cases:

  1. OBV = OBV' + volume if close > close'

  2. OBV = OBV' if close == close'

  3. OBV = OBV - volume if close < close'

    Where, OBV' is a previous value OBV and close' is previous closing price in some time interval (in this case, previous day).

So, OBV indicator seems fairly simple in theory. But enough of theory, let's get our hands dirty. Somewhat randomly, I decided to check Shopify stock today.

shop = stock_data.Ticker('SHOP')
shop_history = shop.history(period="5d")
volume_5d = list(shop_history['Volume'])
print(volume_5d)
> 2655400, 3159600, 3035600, 4602400, 6669931

Shopify stock looks more interesting than I expected when I started this experiment -- the volume has more than doubled in the last five days!

Now, let's check the closing price:

close_5d = list(shop_history['Close'])
print(close_5d)
[553.43, 622.38, 657.38, 780.0, 683.45]

Finally, let us find the OBV for the last five days using the algorithm above is:

[0, 3159600, 6195200, 10797600, 4127669]

(The code for computing OBV is longer, so I might post it later.)

It turns out that the individual numerical values of OBV are not important, what matters is the rate of the change. In this small sample, the first four days look like a strong signal for higher prices, however that did not materialize on day 5. Overall, this was an interesting experiment that helped me understand the basic idea behind OBV and made the stock market price changes slightly less opaque.

The all-in-one writing platform.

Write, publish everywhere, see what works, and become a better writer - all in one place.

Trusted by 80,000+ writers