Using Linear Regession to Trade | Generated by AI

Home PDF

Great question! You’re combining two tasks here:

  1. Fetching historical stock data (e.g., Tencent) using TigerOpen API
  2. Using that data with your existing numpy-based linear regression code

Will it work as-is?

No, not directly. Your current ML code expects x_train and y_train as numpy arrays of numeric values representing features and labels (e.g., population vs. profit). But the TigerOpen API returns historical stock bars (OHLCV data), which is a time series with multiple fields per timestamp.

You need to:


How to adapt your stock data for your linear regression model?

Step 1: Fetch historical data for Tencent (symbol '00700')

bars_dict = quote_client.get_bars(
    symbols=['00700'],
    period=BarPeriod.DAY,
    begin_time='2024-01-01',
    end_time=datetime.now().strftime('%Y-%m-%d'),
    limit=100,
    right=QuoteRight.BR
)

bars = bars_dict.get('00700', [])

Step 2: Extract features and labels

A simple approach:

import numpy as np

x_train = np.array([i for i in range(len(bars))])
y_train = np.array([bar.close for bar in bars])

Alternatively, you can use previous day’s close as feature to predict next day’s close, or more sophisticated features.

Step 3: Use your linear regression code

Now your x_train and y_train are numpy arrays of numbers, so your code should work fine after you fix your compute_cost and compute_gradient functions (which currently return zero or zero gradients).


Example: Implement compute_cost and compute_gradient

def compute_cost(x, y, w, b):
    m = x.shape[0]
    total_cost = 0.5 * np.sum((w * x + b - y) ** 2) / m
    return total_cost

def compute_gradient(x, y, w, b):
    m = x.shape[0]
    error = w * x + b - y
    dj_dw = np.sum(error * x) / m
    dj_db = np.sum(error) / m
    return dj_dw, dj_db

Summary


Bonus: Plotting stock prices

Your scatter plot will show the closing price over days, and the regression line will fit a trend.


If you want, I can help you write a complete example integrating TigerOpen data fetching with your numpy linear regression code! Just ask.


Back 2025.04.20 Donate