Home Python Trading Strategy How I Made A Profitable OBV Strategy Using Python

# How I Made A Profitable OBV Strategy Using Python

This article will explain how to implement the OBV (On Balance Volume) to the Amazon (AMZN) stock. The first part will explain the idea behind the indicator, while the second part will implement a trading strategy based on the OBV. The article is written only for illustrative purposes: How I made a profitable OBV strategy using Python.

## On Balance Volume

This indicator takes the volume and closing price to predict the price trend; it uses the daily change in the closing price to add Volume values to the indicator. We have explained the OBV indicator earlier.

The formula is:

This is equation (1)

There are three possible scenarios for this indicator. The first scenario is when the closing price is higher than its previous value; in this case, the OBV is

The second case is when the closing price is equal to its previous value; in this case, the OBV is

Finally, when the closing price is lower than its former value; in this case, the

## How to calculate OBV? Example

The following table shows how to apply equation 1:

In green, you will find the scenario

For example, the closing price for the second day is higher than its preceding value (12 > 10); therefore, the OBV is its prior value (0) + volume (210).

On the third day, the daily closing price change is zero (12-12=0); therefore, the OBV is its previous value. On the fourth day, the daily variation in the closing price is negative (11<12); therefore, the OBV is 210-80 =130.

## OBV Python Example

In this example, we will use historical data from Amazon (AMZN); the initial and end times are 2021-01-01 and 2023-01-01, respectively; the library for downloading data is yfinance. The Python procedure is:

## Daily Variation in Closing Price

This part will calculate the closing price variation:

For a pandas data frame, its procedure is to implement .diff():

In the previous picture, data[“Variation”] stores the daily variation of the closing price. Meanwhile, print(data[[“Close”, “Variation”]].head()) prints the first five results for both variables.

## How to calculate On Balance Volume (OBV)

In this section, we will generate the OBV. For this example, we will use the pandas method .iloc. This method allows us to access the value of a specific row and column.

For illustration purposes, let’s see how to use .iloc.

Python uses 0 to indicate the first element; therefore, the element in the first row and first column, the number one (1) highlighted in red color is obtained with .iloc[0, 0]; Meanwhile, the number 200, highlighted in blue located in the second row and the third column is .iloc[1,2].

Let’s continue with the estimation of the OBV; in the following image, we create data[“OBV”] that contains zero values; next, we create nelements that indicate the number of rows in our data, while pvariation, pvolume, and pobv represent the position of the variables Variation, Volume and OBV in number in our data frame. The previous elements are the input to implement the for-loop “for i in range(nelements):”.

Inside of the loop, data.iloc[i, pvariation] indicates that we will get the values from the variable Variation; when i=0, this means data.iloc[0, pvariation], so, you will get the first element from data[“Variation”], i=1 means the second element, etc; in the same way, data.iloc[i, pvolume] has a similar explanation to data[“Volume”]. The important part is that every time that daily_change > 0, daily_change == 0, or daily_change < 0, the variable volume will be positive data.iloc[i, pvolume], zero or negative -data.iloc[i, pvolume]. Finally, we calculate the new OBV value with data.iloc[i-1, pobv] + volume.

Plotting OBV

In the following image, there is the code to plot the OBV:

Let’s see the plot:

The strategy to implement is:

To implement these conditions, we will make another for-loop to calculate the buy and sell signals; the following image illustrates the code:

In the previous image, the most important variable is signal_ that will be 1, when

and

in the code is [current_obv > previous_obv and current_price > previous_price];

or signal_ will be -1, when

and

in the code is [current_obv < previous_obv and current_price < previous_price].

## Calculate Strategy Performance

In this part, we will calculate the strategy performance; the following picture illustrates the procedure:

## Plotting Equity Curve

Finally, we plot the strategy and compare it with Amazon’s performance; the following image shows its calculation:

The chart from the previous picture is:

The OBV strategy performed well during 2021 and 2022 – much better than buy and hold. However, it needs to be backtested on more stocks and longer time frames. This article is just for information purposes.