I accidentally create a Monster with Artificial Intelligence — PHP

Carlos Branco
6 min readOct 3, 2021

--

Simple AI script to predict stocks

The day I accidentally created a monster with artificial intelligence
I have always been fascinated by the stock market and the possibilities of making money from trading. Statistics, probability and seeing patterns in everyday things was always something that caught my attention and I believe it to be a natural skill of a programmer.

After some failures in the trading world and for the simple fact that I have to work and I can’t spending a lot of time looking at the screen and doing day trading or spending hours reading the news I decided it would be good idea to try to automate the trading process with the help of programming.

To start this task I decided to use PHP because it is a very simple programming language to use and because it is already installed in MacOS, just open the terminal, open the folder where you have your file PHP and run the following command to get a PHP server running and see the results in the browser. (python guys please keep calm I don’t know python and I don’t want to learn)

php -S localhost:8080

In the first few attempts I tried I simply tried to find patterns that I thought could be repeated. Looking at charts I realized that the market actually moves in waves, however I knew that it simply buying breakouts was a strategy that didn’t work very well in the long run.

So I decided to go for another approach and just focus on price. Detect strong price changes and try to trade the retracement.

The math behind my tests was quite simple I simply took the price of an asset with for example SPX500 (which I found on google in CSV format) and tried to read the file line by line, then depending on price movements
I opened, closed positions or did nothing.

Beautiful Data ❤

Well despite multiple tests and hours of trying to tweak the scripts the results were not very sustainable. I was not getting better results than the famous “buy and hold” and was making tons of trades a year. In other words, even if I got a 20% return my profits would be completely destroyed by the spread and fees that the broker would charge me.

I ended up putting the idea aside for several months but after a while the term artificial intelligence continued to appear in the articles I read, in the videos I watched, over and over. Since I am a programmer I even felt a little embarrassed because I had never developed anything with artificial intelligence. The closest thing I had done would be scripts with the famous “if’s and else’s” that give the idea of ​​the system being intelligent when in fact it is just carrying out conditions.

Afterwards if some research I realized that the essential difference between artificial intelligence and common programming is that:

In common programming we give the data and we give the rules and the computer returns the answer.
DATA + RULES = RESULTS

In artificial intelligence we give the data and we give the answers and the computer returns the rules.
DATA + RESULTS = RULES

So the first step was to find information to train artificial intelligence.
For this I used the Yahoo Finance website to scrap information on 500 stocks from 2011 to 2021, this information was quite complete with date, opening price, closing price, High, Low and volume.

I end not using Volume

With all this information it would be possible to train a good AI. The next step now would be to choose a library to train our artificial intelligence, for this in PHP we have two good options: PHP-ML and RubixML, I ended up using RubixML because I found it simpler to implement.

https://rubixml.com/

The last step in training an AI is to choose the algorithm that best fits the type of result we are trying to get, in my case I used Support Vector Machine Regression algorithm because I thought it was the most suitable, but maybe there are better solutions.

In my mind it made more sense to predict the direction of the next day’s movement (green or red) rather than the exact price. So I focused that the results would always be try to predict the stock movement the next day and if that forecast is good I would open the trade at market opening and close that trade at market close. So I will always have plenty of free capital and I have a better idea if the algorithm is really working correctly or if it is just luck to catch a bullish market.

I started to work and wrote the algorithm and started training the AI, and I let me tell you, the first attempt was a disaster. I tried to train the AI ​​with 600,000 datasets.

The script ran. ran… ran… at the end of 78 hours I had to manually stop the script because I got the idea that it would never finish. Then I tried another approach, I reduced the data to 120,000 datasets (I had to cut some years of information) and finally after a few hours I had an AI ready to be tested.

To test the AI ​​I decided to run predictions against information that have not been used in training. 4.5 Years of information. Initial results weren’t very good. I was getting a win ratio above 50% but the system was generating 30,000 trades in 4 years and that was too much, all my profits would be destroyed by the broker’s fees.

At this point I was using absolute return values ​​and was assuming that there is no stop loss. So I decided to make some adjustments to the script and put the lower tolerance value.

This way the artificial intelligence will be trained for a longer time as the algorithm would try to improve more often to get more correct results.

Finally the work developed started to show results, this new AI was making fewer trades but still making a lot of profit. The AI gave me back in each forecast it was a value between -3 and 3, so I took that value and I had to decide if that value was good for going LONG or good for going SHORT or good for nothing.

For this I calculated what the results would be if I was LONG or SHORT on all trades. So I could see which values ​​worked best, this also shortened the number of trades.

The final step was to get the AI ​​running daily in real time in a project I created called kairibull.com which is a stock screener and voila after 2 months at the end of 145 trades the results are incredible.

Was hard to me to believe that these signals were so good but during all the time the Win Ratio was always above 60% which is really impressive.

Kairibull.com the script is live

Despite since success I tried to replicate these results again in the forex and crypto market but without any success.

If you looking for stock signals you can check the script live.

--

--