An introduction into algorithmic scripting

This is my first article and I’ve given some thought on the subject. There is a ton to choose from and a lot of choices would have been a nice and easy start, but I wanted to show you what the goal of the coming series is.

So for this article, I will be telling you all about one of my favourite indicators, ADX &DI, which shows strength and directional movement, how I made it my own and why it is now one of the pillars of my automated trading style. I will start off with some basic info about this indicator and focus on usability. You can expect more technical articles with code examples in the future, so hopefully, you can make your own!


The ADX is a combination of two other indicators developed by J. Welles Wilder, the positive directional indicator (abbreviated +DI) and negative directional indicator (-DI). The Average Directional Index (ADX) line shows the strength of the trend. The higher the ADX value, the stronger the trend. According to Wilder, a trend is present when the ADX is above the value of 25. DI values range between zero and 100.

The +DI generally moves in sync with price, which means the +DI rises when price rises, and it falls when the price falls. It is important to note that the -DI behaves in the opposite manner and moves counter-directional to price. The -DI rises when the price falls, and it falls when the price rises. This takes a little getting used to. Just remember that the strength of a price move up or down is always recorded by a peak in the respective DMI line.

Reading directional signals is easy. When the +DI is dominant and rising, price direction is up. When the -DI is dominant and rising, price direction is down. But the strength of price must also be considered. DI strength ranges from a low of 0 to a high of 100. The higher the DI value, the stronger the prices swing. DI values over 25 mean price is directionally strong. DI values under 25 mean price is directionally weak.

So, how does it look?

ADX (black), +DI (green) and -DI (red)

Just by looking at the picture above, you can see the 3 dynamic lines moving all over the place. Without even knowing what any of these lines mean, you could observe the following:

  • The ADX can be higher than both the +DI and the -DI
  • The -DI can be higher than both the ADX and the +DI
  • The +DI can be between the ADX and the -DI
  • The +DI can be moving down and the -DI can move up at the same time
  • The space between the +DI and the -DI can be smaller or bigger if you compare it to the previous period(s)
  • And so on and so on…

This is just to illustrate we can define an insane amount of combinations that can give us a lot of information. Wilder himself had a few simple standard rules for this indicator:

  • ADX above 25 equals a strong price trend
  • Strong trend and+DI crosses up the -DI should signal a long entry
  • Strong trend and -DI crosses up the +DI should signal a short entry

If you code those rules into pine script, it paints the following picture.

Long entry’s (green circles) and short entry’s (red circles), while trend is strong on the H4 timeframe

I don’t need to zoom in to see that besides these signals being mostly right, they are also a bit late. Not ideal for maximizing your profits, especially when you zoom in on the lower timeframes as in the picture below.

Same rules, lower timeframe (M15)

So why is this one of my favourite indicators? Because by now I can finally make my own rules and turn this lagging indicator into a leading one.

Make your own rules

I first came across this indicator in the public library of TradingView and at that point it was in version 2, which wasn’t the greatest because it handles price data in an awful way.

At that time I didn’t know how to port it to version 3, so I left it alone for a while until I could code better. This took longer than I care to admit, but when I was finally porting it, I noticed that some of the code was off and did some more research into the math behind the indicator.

I then changed the code and it finally gave the right values. At this point it looks like it does in the pictures above, very rough. The next step is smoothing out the data to eliminate false positives and bad signals as much as possible. To do this, you use filters.


Filters come in many forms, the most known is probably the SMA, or Simple Moving Average, it smooths out the price data over a specified period. Turns out, you can smooth prety much everything. However, filters have their own problems as they introduce lag. So looking for the right mix between smoothing data and reducing lag as much as possible seems to be one of the holy grails of algorithmic trading.

As you do your research you come across the most intricate filters of which some of the code will make you go cry in a corner of the room. After a while they grow on you and personally I’ve been very impressed by the filters of John Ehler, who has a background in engineering and talks about Spectral Dilation, Cycle Analytics and Digital Signal Processing.

I incorporated his SuperSmoother and Laguerre filters into my scripts and it made a big difference. If you want to know more about that, he has written a book, Rocket Science for Traders, which I recommend if you want to go down that rabbit hole.

So, how does the ADX & DI look with those filters on it. Take a look.

Normal ADX & DI below, filterd version and different rules in the upper ribbon

You don’t have to look very hard to see the difference it makes. By coding my own rules it makes better trades, all because of smooth data baby. In this case it gave the following result in a short period.

Perfect result for marketing

I joke a bit about the result of course, the sample size is way too small. Here is another one on the M5 chart of $XRPUSD:

Net result after 0.075% trade commission. 260 trades, all made on candle close in 1 month.

This is the kind of result I’m working for and it took me a while to get here for sure. After reading this, I hope you’re motivated to start coding yourself. Don’t worry, you can make these scripts as complicated as you are able to, but some of the easiest codes can make all the difference in a good way. This was an opening article to show you where it lead me and maybe can lead you too.

FYI: I just published the ADX & DI script to TradingView and you can use it if you want to! I’m also running a bot on Telegram for free so you can check out the results live! You can find the link to the ADX & DI script and the free bot on If you enjoyed reading this, do give me a follow for future updates or shoot me a DM. Thanks for reading and hope to see you next time!

This post was initially posted on Medium and reposted with Coinpocalypse’s approval. Minor changes have been made to improve readability or fix spelling errors. You can read the original post here.