AI/ML model

AI model for analyzing historical stock prices

Use LLM models with FiinQuant data to assist in finding insights as required by the user.

import requests
import tqdm
from FiinQuantX import FiinSession
from bs4 import BeautifulSoup
import google.generativeai as genai
import re
import numpy as np
import pandas as pd
import os


GOOGLE_GEMINI_API_KEY = os.getenv('GOOGLE_GEMINI_API_KEY')
genai.configure(api_key=GOOGLE_GEMINI_API_KEY)
model = genai.GenerativeModel("gemini-1.5-flash")

client = FiinSession('USERNAME', 'PASSWORD').login()
VCB_data = client.Fetch_Trading_Data(
    tickers=['VCB'],
    fields=['close'],
    realtime=False,
    adjusted=True,
    by='1d', 
    from_date='2020-01-01',
    to_date='2025-01-29').get_data()

Output:

Machine learning model for stock price prediction

Retrieve stock data from the FiinQuant library

1. Linear Regression

Linear Regression

  • Main idea: The model predicts stock prices by finding the best-fit line for historical data. The general formula is y=w0​+w1​x1​+w2​x2​+...+wn​xn​+ϵ, where y is the stock price, and xi​ are influencing factors like opening price, trading volume, etc.

  • Advantages: It's simple, easy to understand, and calculations are fast.

  • Disadvantages: It assumes a linear relationship between variables, making it difficult to capture the non-linearity of stock prices.

Prepare and run the model

Linear Regression model prediction versus actual price.

2. Random Forest/ XG Boost

  • Main idea: The model uses multiple Decision Trees to make predictions, averaging their outputs to reduce overfitting.

  • Advantages:

    • Doesn't require data to have linear relationships.

    • Robust with data influenced by many factors.

  • Disadvantages:

    • Slower than Linear Regression with large datasets.

    • Not ideal for time series forecasting as it lacks the ability to remember past information.

Predicted price vs actual price

3. Long short-term memory network (LSTM)

  • Main idea: This is a variant of the Recurrent Neural Network (RNN) that can remember information for long periods thanks to its gating mechanism.

  • Application in stock price prediction: The model can learn trends and patterns from past price data to predict future prices.

  • Advantages:

    • Excellent at processing time series data.

    • Can learn long-term trends.

  • Disadvantages:

    • Requires large datasets to achieve good performance.

    • High computational complexity, needs a GPU for fast training.

For an LSTM network to effectively learn stock price characteristics, it needs additional features beyond open, high, low, and volume.

  • Create a "Label" column, which is the 'Close' price of the previous day.

  • Tạo thêm feature

  • Standardize data

  • Model

.....

Last updated