#### Discover more from All About Trading!

# Coding the Directional Probability Index in TradingView.

### Creating the Directional Probability Index in Pine Script.

This article discusses a simple indicator used to detect the current regime and to give contrarian signals. It is important to understand how to use indicators and how they are calculated in order to tweak them and know their limitations.

I have just released a new book after the success of my previous one “*The Book of Trading Strategies*”. It features advanced trend-following indicators and strategies with a **GitHub **page dedicated to the continuously updated code. Also, this book features the original colors after having optimized for printing costs. If you feel that this interests you, feel free to visit the below Amazon link, or if you prefer to buy the PDF version, you could contact me on **LinkedIn**.

### The Directional Probability Index

The index is a simple calculation that will be transformed into a two-in-one indicator which has the following goals:

**Regime detection.****Inflection point detection.**

The first step is to calculate the percentage of times when the close price was higher than the open and divide it by a lookback period which is by default 13.

This gives us a sort of probability of the current regime. As simplistic as it may be, some times these types of methods help us find good opportunities.

Whenever the DPI is showing a value greater than 50%, we can consider the market to be in a bullish regime and whenever it is showing a value lower than 50%, we can consider the market to be in a bearish regime. One more utility, is that we can also use it as a reversal indicator through the concept of statistical extremes.

Whenever the DPI approaches 80%, we like to think that the bullish stance is overheating and might correct and whenever the DPI approaches 20%, we can expect that the bearish stance is at its extreme and might correct with a bullish reaction.

This article will add a 3-period moving average applied on the DPI values in order to smooth them out and improve interpretability.

### Coding the Directional Probability Index in Pine Script

We want to create and plot the DPI values on the TradingView platform. Note that you must create an account to be able to view the charts, the good news is that it is free. Locate the **Chart **button on the home screen and then choose any asset you would like to calculate the indicator on. Now, on the bottom of the screen, locate **Pine Editor** and warm up your fingers to do some coding.

The first step is to specify the version of Pine Script. In our case it is 5 which has some minor changes from the fourth version.

`//@version = 5`

The next step is to specify the name of the indicator by using the following syntax.

`indicator("Directional Probability Index")`

We can now set the default lookback period values and the multiplier using the **input()** function.

```
lookback_dpi = input(defval = 13, title = 'Lookback')
lookback_ma = input(defval = 3, title = 'MA Lookback')
```

The next line of code shows the condition of where the close price must be greater then the open price. This is the basis of the indicator

`condition = close > open? 1: 0`

And now, we use the **math.sum()** function to calculate the number of green candles over the designated lookback period.

`summation = math.sum(condition, lookback_dpi)`

To calculate the DPI, we divide the summation on the lookback we have chosen earlier and multiply by 100.

`dpi = summation / lookback_dpi * 100`

And finally, we can smooth out the results using a 3-period simple moving average applied on the DPI values.

`ma_dpi = ta.sma(dpi, lookback_ma)`

```
plot(ma_dpi, color = color.red)
hline(20)
hline(80)
```

The full code can be found below.

```
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sofien-Kaabar
```

```
//@version=5
indicator("Directional Probability Index")
```

```
lookback_dpi = input(defval = 13, title = 'Lookback')
lookback_ma = input(defval = 3, title = 'MA Lookback')
```

`condition = close > open? 1: 0`

`summation = math.sum(condition, lookback_dpi)`

`dpi = summation / lookback_dpi * 100`

`ma_dpi = ta.sma(dpi, lookback_ma)`

```
plot(ma_dpi, color = color.red)
hline(20)
hline(80)
```

The strategy that can be applied on this indicator is a simple oversold / overbought technique between 20 and 80.

If you are also interested by more technical indicators and strategies, then my book might interest you:

### Conclusion

Remember to always do your back-tests. You should always believe that other people are **wrong**. My indicators and style of trading may work for me but maybe not for you.

I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.

To sum up, are the strategies I provide realistic? *Yes*, but only by optimizing the environment (robust algorithm, low costs, honest broker, proper risk management, and order management). Are the strategies provided only for the sole use of trading? **No**, it is to stimulate brainstorming and getting more trading ideas as we are **all sick of hearing about an oversold RSI as a reason to go short or a resistance being surpassed as a reason to go long. I am trying to introduce a new field called Objective Technical Analysis where we use hard data to judge our techniques rather than rely on outdated classical methods.**