I was hoping to create a short cut by using AI generated code, but…
I asked ChatGPT[FREE] to provide source in Python for Wilder’s Directional Movement Index. Here is what it generated.
Notice where the Positive Directional Movement pdm and Negative Directional Movement mdm are calculated. This calculation is correct, and it needs to be carried forward and used in the pdi and mdi calculations. Once calculated, they are stored in lists. PDI and mdi are then calculated using the pdm and mdm values. This code looked reasonable, but once they were calculated they were multiplied by 100 and then stored in their own lists. Increasing the pdi and mdi here by 100-fold is a glaring mistake, and this code was created by the fourth request (free version of ChatGPT).
If you allow this to run, you will eventually reach numeric overflow and ADX reaches nearly 100 on every calculation. The first iteration was closer, but still did not calculate the pdi and mdi (PlusDMI and MinusDMI) correctly – they weren’t smoothed properly. This in turn led to an incorrect ADX calculation. I fixed the code and made it work, but it took almost as much time as I spent creating the DMI from scratch. It would probably be better to ask ChatGPT or any other AI BOT to provide the pseudocode. Or perhaps spend the extra $20 and get access to ChatGPT-4. I will do that and revisit this a littler later.
This looks accurate using Wilder’s short cut for smoothing. If you want to program your own indicators, it is probably best to find the pseudocode and program it from scratch. Not only are making sure it is correct, but you are learning the indicator inside out. Also, if you have access to any free charting software that you feel is super reliable, then you can compare your results to see if your indicator is producing similar results.
Why create your own code
If you are like me, I don’t like building with blocks. Unless I know with 100% accuracy those blocks are right. Procedural/Functional programming versus OOP. I am not saying one paradigm is better than another. OOP was not an easy concept for program as needed type programmers. In the late 60s and throughout most of the 80s, mathematicians and scientists cobbled code together from BASIC and Fortran to accomplish what they simply needed at the time. They didn’t work within a team and the code was there for their personal use. Functional programming is gaining favor once again. I have a physics friend that worked at Bell Labs finally adopt Python in favor of an old, but still reliable 64-BIT version of BASIC. It is amazing to see the continued love for BASIC – and yes, it is still viable. He discovered that you did not need to reinvent the wheel when it came to his research – he read about all the scientific libraries available to him for free. Python was easy enough for him to pick up, but Pip Installing the libraries was another story. He started with a monolithic install of Python and all of the commercially available IDEs and libraries. Which is great but it most cases it is overkill. So, I had him remove it and install basic Python 3.10 with IDLE. Then I suggested a straightforward IDE such as Pyscripter – an IDE that looks a lot like the older Turbo and Think IDEs that I grew up with. It was a marriage made in heaven. Once you get the Python set in your PATH, PIP Install is easy-peasy.
Also, you may not like the existing TA libraries because they are too restraining or too difficult to implement. Finally, if you program something you will have a deeper understanding of what the code is doing. I am old school; I like bare bones code. I don’t want to have to implement one library after another. The embedded interfaces move you further away from the meat of the code. Now in some cases, a typical programmer will not have the scientific or practical or interface knowledge to implement his/her code. Then you must rely on libraries. Don’t get me wrong I use Python libraries whenever I absolutely need to. I love openPYXL. This library converts the output of my research directly into easy to open spreadsheets. There is no way I would want to delve into that type of programming. However, why not program indicators yourself, if the pseudocode just requires a little data and some loops and some if-thens. Most of you Quants out there are more than capable of programming even the most complicated indicators.