AI for Trading

Mukund Mundhra
7 min readDec 2, 2021

Stocks are shares or ownership certificates of a company, by buying stocks of a company a person becomes a shareholder of the company and gets profits if the stocks price increases and suffers loss if the stock price declines. The stock market is a very risky place as it is very hard to predict the trend or future price of any particular stock. Most people follow basic trends to invest in the stock market without sufficient data and a result they suffer loss.Those that understand AI technology and know how to handle its dangers will have opportunity throughout the early stages of its adoption. One disadvantage of AI-based trading systems is that they can provide models that are poorer than chance. Because methods based on chart patterns and indicators draw their rewards from a distribution with zero mean before transaction charges, traditional technical analysis is an unsuccessful technique of trading. Some traders are always towards the distribution’s right tail, creating the false appearance that these techniques are profitable. According to my research, long-term profitability in the futures and forex markets is impossible to achieve, regardless of strategy, because these markets are designed to favour market makers. Some dealers, on the other hand, trade for shorter periods of time. Predicting the trend of the stocks is quite a handy task and having 100 % accuracy is a next to impossible task but with the introduction of artificial intelligence and machine learning, we are doing things no one ever thought of.The price of any stock depends on many aspects and we can have different outcomes by using different methods, we need loads and loads of data that needs to be processed and trained in our system so that it can understand all the trends and how different aspects affect the prices of a stock.We are using different models to get different predictions, all the models work separately and give different results and these results are then compared with one another to find out which method gives us better accuracy and is more reliable. Some ML models used in the prediction are LSTM, RNN and NLP.Natural language processing is a part of machine learning which is used by the computer to understand, analyse and compare human language which is used in various fields like speech recognition, machine translation, sentiment analysis, auto-correct etc. In our project, we are using NLP for news sentiment analysis, we train our system by giving it data in form of news headlines and the impact of those headlines on the stock prices and it uses NLP to understand the trends and effect of news headlines on the stock prices, on that basis, it is then given news headlines for test data and it uses the previously collected data and based on those trends and patterns it predicts whether the stock prices would increase decrease or stay the same.Neural networks are basically a set of algorithms that try and act like the human brain and are build to recognise different trends and patterns. Neural networks are used in stock trend prediction as they can be trained to recognise patterns and then with the information collected about the sequence they predict the future outcomes.Recurrent neural networks are a type of neural networks which uses previous output and uses that output as input for the next step. usually in neural networks the input and output are totally independent of each other but this is not the case in RNN and this is what makes it stand out from other types of neural networks .It is usually used in handwriting recognition, speech recognition etc. RNN stores all the information about the calculations obtained from the trained data in a hidden state which is used every time we input data to make predictions. Prediction on the basis of calculation on large dataset is not only limited to the value guessing or the emerging growth but also the fact that predicting the trends of graphs based on the pattern has become a huge deal over the years , as analysis based on graphs has increased a lot and AI works on this where different patterns like hammer , flag , wedge and much more. Also analysis of charts is not just based on a specific pattern but also different particular candlestick.Companies like Kavout and many more are quite famous for such technical stuff where they use AI and apply them side by side on the graph results and make it easy for the investors/traders to make a call for a trade and basically making it much more informative for them before they make a trade. One of the many methods include Forecasting method which includes different stages starting from the Stage 1: Raw Data — here we collect previous stock data from Google Stock Price and use it to forecast future stock values in this stage. Next stage includes setting up the base , Now we import all the libraries, we use NumPy to apply mathematical operations and functions to our multidimensional arrays, then for the ease of visualization and graphical content what we are using is matplotlib, for manipulation and data analysis we use pandas, this also helps us to create a python object which has rows and columns from a basic csv or any type of excel file, and for any frontend application, it also supports the use of sql database. As the particular data that we will be using will have dates and time from which we will make references we also need the DateTime library. Then we read the dataset, next what we do is that we try and check that we don’t have any not applicable data between our dataset by the isna function, we now check the datatype of which is somewhat a mix of float and object, what basically we want is a homogenized dataset so we convert the column types, now the next thing we study is about seven days mean (rolling) of our stock, what this basically means is that each and every stock prediction we go back seven days and collect all of the transactions that fall within the range, and then we analyse them, what we get from here is the moving average of the past 30 days, now we plot the close column vs the 7 days moving average of the close column. Next stage includes data processing where ,The preprocessing stage involves data discretization, with particular importance for the numerical data we reduce the part of our data, then we perform a data transformation, which is nothing more than data normalisation, clean the data, fill in the missing values, and integrate our data files, and finally, once the data has been transformed into a clean dataset, we divide it into training and testing sets so that we can evaluate it further.

Basically what we are trying to do is creating a data structure with 60 timestamps and 1 output, first we clean the data with isma (checking for any not possibilities), then we go on to feature scaling, for which we import min-max scalar from sklearn, then we transfer features by scaling each of them to a defined range, which we set to 0 to 1. Now we’ll take the data from day 1 to day 60 and make a prediction for the 61st day, and then we’ll take the data from day 2 to day 61 and make a prediction for the 62nd day, here we run a loop in such a way that the variable goes from 60 to the end of the range, then we append it to the X_train array, the predicted data for the 61st day and so on is appended in the other array which is the Y_train. Finally after appending, we reshape the data.

Next step is Feature Extraction , In this stage what we do is we choose only those features that are going to be given to the neural network, the features that we take into consideration are highs, date, volume, and lows. So now explaining in detail, we first import Keras libraries and packages for creating the RNN . Now what we do is we import four different libraries that are sequential, dropout, lstm, and dense. Now we move on to reconciling the RNN, here we use a regression model, where we read in the sequential data, and we ascribe it to the regressor .Moving on to the next steps, the neural network must be trained. This lstm model has a sequential input layer, three lstm layers, a dense layer with activation, and an output dense layer with the linear activation function.Explaining further, first, we have the input (the input shape) layer and dropout ,then we have the layer group (3 layers), then the output layer with units equals one as we need only one output. After this, we compile the RNN, for which we need to compile the scarce model and so use the optimizer. Knowing that the type of optimizer employed has a significant impact on the rate/speed with which the algorithm converges to the minimal value, as well as the requirement for a randomization notion so that we don’t get trapped at a local minimum and so fail to reach a global minimum. There are several choices available, but we select the Adam (Adaptive Movement Estimation) optimizer since it combines the benefits of both the ADAgrad and RMSprop optimizers. Adam is in charge of calculating adaptive learning rates for each parameter based on its previous gradients. Finally, we create outputs and make predictions.

--

--