This project is a proof-of-concept for an automated stock trading system using the Korea Investment & Securities (KIS) API. It leverages a fine-tuned DistilBERT language model to predict daily stock price movements based on historical price data and a rich set of technical indicators.
The system is an end-to-end pipeline that handles data fetching, feature engineering, model training, strategy backtesting, and finally, live paper/real trading.
The core of the project is to transform time-series data into a natural language format (prompts), which allows the LLM to be fine-tuned for a classification task—predicting if the stock price will go up or down.
- Data Handling: Fetches historical stock data using the
yfinancelibrary. - Rich Feature Engineering: Uses
pandas-tato calculate technical indicators (SMA, RSI, MACD, ATR, ADX). - LLM-based Prediction: Fine-tunes a
DistilBERTmodel on prompts generated from financial time-series data. - Class Imbalance Handling: Implements a custom
Trainerwith class weights for robust model training. - Backtesting: Simulates trading strategies on historical data to evaluate performance.
- Live/Paper Trading: Integrates with the Korea Investment & Securities (KIS) API via the
python-kislibrary to automate trades in both paper and live accounts. - Automated Pipeline: A main script orchestrates the entire workflow from data to backtesting.
- Logging: Logs all live trading activities, including predictions, orders, and errors.
The strategy was backtested on AAPL stock data from 2020-01-01 to 2023-12-31. Adding a richer set of technical indicators significantly improved performance over a simpler model.
| Strategy | Total Return | Sharpe Ratio |
|---|---|---|
| LLM-Powered Trading (Improved) | 183.38% | 1.43 |
| Buy-and-Hold Baseline | 189.23% | - |
python_auto_trading/
├── data/ # Processed data for training
├── logs/ # Log files for training and live trading
├── models/ # Fine-tuned model files
├── src/
│ ├── __init__.py
│ ├── data_handler.py # Fetches and processes stock data
│ ├── model.py # Handles model fine-tuning
│ ├── strategy.py # Defines the trading logic
│ ├── trader.py # KIS API communication wrapper
│ └── ...
├── .env # Environment variables for API keys and config
├── live_bot.py # Main script for live/paper trading
├── main.py # Main script for backtesting pipeline
└── requirements.txt
-
Clone the repository:
git clone <repository-url> cd python_auto_trading
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Configure the environment: Create a
.envfile by copying the.env.exampleor creating it manually. You must fill in your KIS API credentials.# Stock ticker to analyze (e.g., 005930 for Samsung Electronics) TICKER=005930 # For Korea Investment & Securities API KIS_APP_KEY=YOUR_APP_KEY KIS_APP_SECRET=YOUR_APP_SECRET KIS_ACCOUNT_NO=YOUR_REAL_ACCOUNT_NUMBER KIS_VIRTUAL_ACCOUNT_NO=YOUR_PAPER_TRADING_ACCOUNT_NUMBER
There are two main ways to run this project:
To run the full data processing, model training, and backtesting pipeline, execute main.py.
python main.pyThis will use the TICKER from your .env file to generate a new model and evaluate its performance on historical data.
To run the automated trading bot, execute live_bot.py.
python live_bot.pyBy default, the bot runs in paper trading mode and does not execute real orders (they are commented out for safety). See the detailed guide for activation instructions.
This project is for educational and research purposes only. The backtesting results are not indicative of future returns. Financial markets are complex and unpredictable. This is not financial advice. Any action you take upon the information on this project is strictly at your own risk. The developers assume no liability for any losses or damages in connection with the use of this software. Do not use this system for live trading with real money unless you fully understand the code and the risks involved.