Simplifying Stock Market Decisions with Real-Time Data

How to enhance your stock trading decisions by leveraging real-time data and advanced machine learning techniques?

EODHD APIs
6 min readJul 19, 2024

Ever wondered how to make better investment decisions in the stock market? I embarked on this journey myself and discovered a fantastic tool called the Live (Delayed) Stock Prices API. This API offers nearly real-time information on any stock, with a 15-minute delay. Inspired by this, I decided to create my own stock market assistant. With this live data at hand, you can make more informed choices without needing expensive platforms, and you can even develop your own investment strategy based on historical data.

I explored various ways to access up-to-date stock prices and trends quickly, but most solutions didn’t meet my needs until I found this API that provides live stock data.

Here’s an example output from the API when queried for TSLA stock:

{'code': 'TSLA.US',
'timestamp': 1693941000,
'gmtoffset': 0,
'open': 245,
'high': 258,
'low': 244.86,
'close': 257.44,
'volume': 112942754,
'previousClose': 245.01,
'change': 12.43,
'change_p': 5.0733}

This output gives you all the necessary information to build a successful investment strategy. Let’s dive into how you can leverage this data to simplify your financial decision-making.

Python Implementation

1. Importing the Necessary Packages

Start by importing the essential Python packages required for this project. These packages will help you manage data, train models, and perform other tasks.

import pandas as pd
from eodhd import APIClient
import numpy as np
from xgboost import XGBClassifier
from sklearn.ensemble import IsolationForest
from sklearn.ensemble import RandomForestClassifier

Here’s a brief overview of what each package does:

- Pandas: Assists with various data manipulation tasks.

- Numpy: Supports mathematical operations in Python.

- XGBoost, Random Forest, and Isolation Forest: Classification models for our project.

- eodhd: The official EODHD library for accessing their APIs.

2. API Key Activation

To utilize the EODHD API functions, you need to register your EODHD API key. If you don’t have an API key, visit their website to complete the registration process, create an EODHD account, and find your secret API key in Dashboard. Ensure this key remains confidential. Activate the API key using the following code:

api_key = '<YOUR API KEY>'
client = APIClient(api_key)

In this code snippet, we store the secret EODHD API key in the `api_key` variable. Next, we use the `APIClient` class from the `eodhd` package to activate the API key and store the response in the `client` variable.

Remember to replace `<YOUR API KEY>` with your actual EODHD API key. For enhanced security, consider using environmental variables to store the API key instead of hardcoding it directly into your script.

3. Loading Historical Data

To train your model effectively, you’ll need historical stock data for your desired timeframe. This data will be the foundation for your model’s training process. Utilize the Intraday historical market data API endpoint through the `eodhd` package to retrieve this data.

def get_historical_data(ticker, start_date, end_date):
json_resp = client.get_historical_data(symbol = ticker, period = '5m', from_date = start_date, to_date = end_date, order = 'a')
df = pd.DataFrame(json_resp)
df = df.set_index('date')
df.index = pd.to_datetime(df.index)
return df

TSLA = get_historical_data('TSLA', '2021-08-02', '2021-09-02')

In the code above, the `get_historical_data` function from the `eodhd` package is used to extract Tesla’s split-adjusted historical stock data. The function parameters include:

- ticker: The stock symbol for the desired data.

- period: The interval between data points (5 minutes in this example).

- from_date and to_date: The start and end dates for the data in “YYYY-MM-DD” format.

- order: An optional parameter to sort the dataframe in ascending (`a`) or descending (`d`) order based on dates.

4. Obtaining Live Data for Prediction

Next, access live stock data using EODHD’s Live (Delayed) Stock Prices API via the `eodhd` package. This real-time data is essential for making informed trading decisions.

def extract_intraday(symbol):
raw_df = client.get_live_stock_prices(ticker = symbol)
df = pd.DataFrame([raw_df])
return df

tsla_intraday = extract_intraday('TSLA')

This function fetches live stock information for a given stock symbol, converts the response into a Pandas dataframe, and returns it.

5. Preprocessing the Data

Before training your model, you must clean and prepare the data to ensure accurate predictions. Key preprocessing steps include:

  • Handling Class Imbalances: Address any imbalances in your dataset using techniques such as:

- Oversampling: Increase the number of instances in the underrepresented class.

- Undersampling: Decrease the number of instances in the overrepresented class.

- Normalizing the Data: Standardize your features to ensure they are on the same scale. Common techniques include:

- Standard Scaler: Adjusts data to have a mean of 0 and a standard deviation of 1.

- MinMax Scaler: Scales data to a specified range, typically between 0 and 1.

- Dropping Unnecessary Columns: Remove irrelevant columns to streamline your model and enhance performance.

Here’s an example of how to preprocess your data:

dataF = dataF.drop(['timestamp',  'gmtoffset',  'datetime'],axis =1)
tsla_intraday = tsla_intraday.drop(['code', 'timestamp', 'gmtoffset', 'previousClose', 'change', 'change_p'], axis=1)

In this example, unnecessary columns such as ‘timestamp’ and ‘gmtoffset’ are removed from the dataset to prepare it for model training.

6. Forming a Strategy

Classify your training data based on your unique strategy to help your model make accurate predictions. For example, you can use a simple strategy with three classes: “waiting” (0), “buying” (1), and “selling” (2).

Here’s an example:

def signal_generator(df):
open = df.Open.iloc[-1]
close = df.Close.iloc[-1]
previous_open = df.Open.iloc[-2]
previous_close = df.Close.iloc[-2]

if (open > close and previous_open < previous_close and close < previous_open and open >= previous_close):
return 1 # Buying
elif (open < close and previous_open > previous_close and close > previous_open and open <= previous_close):
return 2 # Selling
else:
return 0 # Waiting

signal = [0] # Initialize with "waiting"
for i in range(1, len(dataF)):
df = dataF[i - 1:i + 1]
signal.append(signal_generator(df))

dataF["signal"] = signal

7. Loading and Training the Models

Next, select and train your models. This tutorial uses three different models: XGBoost, Isolated Random Forest, and Random Forest.

- XGBoost: A robust boosting algorithm ideal for structured data like stock prices.

from xgboost import XGBClassifier
model1 = XGBClassifier()
model1.fit(X_train, y_train)

- Isolated Random Forest: Excellent for anomaly detection using binary trees.

from sklearn.ensemble import IsolationForest

random_state = np.random.RandomState(42)
model=IsolationForest(n_estimators=100,max_samples='auto',contamination=float(0.2),random_state=random_state)

model.fit(X_train, y_train)

- Random Forest: A machine learning algorithm that uses multiple decision trees for predictions or classifications.

from sklearn.ensemble import RandomForestClassifier

model1 = RandomForestClassifier()
model1.fit(X_train, y_train)

8. Prediction on Live Data for Suggestions

With your trained model, you can now make predictions on test data and live data. Evaluate your predictions using metrics like precision, recall, accuracy, and F1 score.

# Make predictions for test data
y_pred1 = model1.predict(X_test)
predictions1 = [round(value) for value in y_pred1]

# Evaluate the predictions
from sklearn.metrics import confusion_matrix, recall_score, precision_score, f1_score, accuracy_score
cm = confusion_matrix(y_test, predictions)

rf_Recall = recall_score(y_test, predictions1, average='macro')
rf_Precision = precision_score(y_test, predictions1, average='macro')
rf_f1 = f1_score(y_test, predictions1, average='macro')
rf_accuracy = accuracy_score(y_test, predictions1)

Where to Go from Here

Having embarked on this journey, the possibilities are endless. Fine-tune your model, explore different machine learning algorithms, and analyze various stocks. With access to live stock data using the Live (Delayed) Stock Prices API and the knowledge from this tutorial, you’re well-equipped to make more informed decisions in the dynamic world of stock trading.

The original article was published in the EODHD Academy by Nikhil Adithyan.

Please note that this article is for informational purposes only and should not be taken as financial advice. We do not bear responsibility for any trading decisions made based on the content of this article. Readers are advised to conduct their own research or consult with a qualified financial professional before making any investment decisions.

For those eager to delve deeper into such insightful articles and broaden their understanding of different strategies in financial markets, we invite you to follow our account and subscribe for email notifications.

Stay tuned for more valuable articles that aim to enhance your data science skills and market analysis capabilities.

--

--

EODHD APIs
EODHD APIs

Written by EODHD APIs

eodhd.com — stock market fundamental and historical prices API for stocks, ETFs, mutual funds and bonds all over the world.

Responses (3)