ML Algorithm to Predict Stock Price | by Sarit Maitra | Towards Data Science

https://sarit-maitra.medium.com/membership
Algorithmic trading expected to follow rules, and furthermore, they can form objective, unbiased estimates of risk and represent a revolution in both the art and theory of finance.
Here, we will see how machine learning can solve the puzzle of forecasting which will be forecasting the difference between today’s price and tomorrow’s price (which is unknown).
The data can be loaded through open API provided by world trade data. The financial time-series problem is quite complex from the ML perspective. We need to think about financial data structures more than about particular events like open, high, low and close.
# Plot the Opening pricesdataset['Open'].plot(grid=True, figsize=(10, 6))plt.title('Nasdaq Composite open price')plt.ylabel('price ($)')# Show the plotplt.show()
Let’s start preparing the data for the analysis. I will create some features based on short window simple moving average.
High Low % provides some percent volatility in the market.
percent change reflects the daily percent change
1 day window open price.
Current day volume increment.
Current day volume rate of change.
Current day open price increment.
open price.
Now, we keep the relevant attributes in the data-frame which are predictor variables here.
Instead of dropping all NaN values we have replaced NaN with some number which will be treated as outlier in the data-set. We will try gradient boosted trees which in general being robust to outliers.
Let’s define our target variable. we will have a classification variable because the average price will either go up or down the next day. So, we will target to predict the difference between today’s price and tomorrow’s price.
From the correlation plot below, we have an idea that some of the attributes are correlated with each other and may not be required for the model. However, we will identify this later from gradient boosting feature importance plot.
# Checking Correlation for easy understandingsns.set(style='darkgrid', context='talk', palette='Dark2')plt.figure(figsize=(14,5))dataset.corr()['Open'].sort_values(ascending = False).plot(kind='bar')plt.show()
Our data-set has a total of 5158 samples, and 10 features. We have used time-series cross validator which provides train/test indices to split time series data samples that are observed at fixed time intervals. In each split, test indices must be higher than before, and thus shuffling in cross validator is inappropriate.
print('Total dataset has {} samples, and {} features.'.format(dataset.shape[0], dataset.shape[1]))X = (dataset.drop(['target'], 1))y = (dataset['target'])print(len(X), len(y))X = np.array(dataset.drop(['target', 'open1'], 1)) # dropping openy = np.array(dataset['target'])#timeseries splittscv = TimeSeriesSplit(max_train_size=None, n_splits=5)for train_samples, test_samples in tscv.split(X):  #print("TRAIN:", train_samples, "TEST:", test_samples)  X_train, X_test = X[train_samples], X[test_samples]  y_train, y_test = y[train_samples], y[test_samples]#separating features namesfeature_names = ['ma1','ma2','ma3','HL','pct_change','vol_increment','open_increment','Open']X_train = pd.DataFrame(data=X_train, columns=feature_names)X_test = pd.DataFrame(data=X_test, columns=feature_names)print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
Let us convert the target variable to binary classification. A positive change in the value of prices will be classified as 1 and a non-positive change as 0.
y_train = pd.DataFrame(y_train) # converting to data framey_train.rename(columns = {0: 'target'}, inplace=True)y_test = pd.DataFrame(y_test)y_test.rename(columns = {0: 'target'}, inplace=True)def getBinary(val):  if val>0:    return 1  else:    return 0y_test_binary = pd.DataFrame(y_test["target"].apply(getBinary))
We will initiate XGB regressor to test the model. fit the training set
Our model accuracy score is 78.81% and can predict the daily price change (average) for next 8 trading days .
It is relatively easy and straight forward to retrieve importance score for each features once the boosted trees are constructed. This importance is calculated explicitly for each attribute in the data-set, allowing attributes to be ranked and compared to each other.
We can see from the feature importance plot that, not all the features are really important for the algorithm to perform; so, we can probably run the algorithm with just 4 features to attain the same accuracy score. The model can be further trained discarding attributes with no correlation.
This is a simple model with no big complexities involved and the model attained quite a decent accuracy score. So, we can see that, no longer do we need to rely on descriptive statistics of human outputs as a sort of loose justification of inference.
I can be reached here.