• Fetch stock market data using Zerodha API
  • Preprocess and store the data
  • Download DeepSeek-8B using huggingface-cli
  • Install CUDA-Compatible PyTorch
  • Fine-tune the DeepSeek-8B model using LoRA
  • Fix issues related to GPU offloading, meta tensors, and memory management
  • Deploy the trained model for real-time stock predictions

Step 1: Install Required Packages

Install all necessary Python packages using:

pip install -r requirements.txt

Create requirements.txt and add:

torch>=2.0.0  
torchvision  
torchaudio  
numpy  
pandas  
scipy  
tqdm  
datasets  
huggingface_hub  
transformers>=4.31.0  
accelerate>=0.21.0  
sentencepiece  
peft  
bitsandbytes  
einops  
wandb  
kiteconnect  
scipy  
protobuf  

To manually install packages, use:

pip install torch torchvision torchaudio  
pip install numpy pandas scipy tqdm datasets huggingface_hub transformers accelerate  
pip install peft bitsandbytes einops wandb  
pip install kiteconnect  
pip install sentencepiece protobuf  

Step 2: Install CUDA-Compatible PyTorch

Check Your PyTorch Installation

Run this command in Python to check if CUDA is available:

import torch
print("CUDA Available:", torch.cuda.is_available())
print("Torch Version:", torch.__version__)
  • If CUDA Available: False, it means PyTorch was installed without GPU support.
  • If your Torch version does not show CUDA, you need to reinstall PyTorch with GPU support.

Install CUDA-Compatible PyTorch

1️⃣ Uninstall the CPU-only version of PyTorch:

pip uninstall torch torchvision torchaudio -y

2️⃣ Install the CUDA-enabled version of PyTorch:

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
  • This installs PyTorch with CUDA 12.1 support.

3️⃣ Verify the Installation

import torch
print("CUDA Available:", torch.cuda.is_available())  # Should return True
  • If True, PyTorch is now using your GPU.

Step 3: Download DeepSeek-8B Model

1️⃣ Login to Hugging Face

huggingface-cli login  

2️⃣ Download DeepSeek-8B using

huggingface-cli download deepseek-ai/DeepSeek-R1-Distill-Llama-8B --local-dir deepseek-8b  

3️⃣ Verify the Model Files

ls deepseek-8b  

Expected output:

config.json  
model.safetensors  
tokenizer.json  
tokenizer_config.json  

Step 4: Fetch Stock Data from Zerodha API

This script:

  • Fetches 5 years of stock data from Zerodha API
  • Calculates technical indicators (EMA, MACD, Support/Resistance)
  • Saves processed data to CSV files

fetch_stock_data.py

import os  
import datetime  
import pandas as pd  
from kiteconnect import KiteConnect  

API_KEY = "your_api_key_here"  
ACCESS_TOKEN = "your_access_token_here"  

kite = KiteConnect(api_key=API_KEY)  
kite.set_access_token(ACCESS_TOKEN)  

stockNames = ["PGEL", "SHAKTIPUMP", "GODFRYPHLP", "BSE", "JSWHL", "TARIL"]  

DATA_DIR = "data"  
os.makedirs(DATA_DIR, exist_ok=True)  

def fetch_historical_data(stock_name):  
    instruments = kite.instruments("NSE")  
    instrument_token = next((x["instrument_token"] for x in instruments if x["tradingsymbol"] == stock_name), None)  

    if not instrument_token:  
        print(f"❌ Instrument token not found for {stock_name}")  
        return None  

    from_date = (datetime.datetime.today() - datetime.timedelta(days=5*365)).strftime('%Y-%m-%d')  
    to_date = datetime.datetime.today().strftime('%Y-%m-%d')  

    print(f"📊 Fetching data for {stock_name}...")  
    data = kite.historical_data(instrument_token, from_date, to_date, interval="day")  

    df = pd.DataFrame(data)  
    df["stock"] = stock_name  

    df["9_EMA"] = df["close"].ewm(span=9, adjust=False).mean()  
    df["21_EMA"] = df["close"].ewm(span=21, adjust=False).mean()  
    df["MACD"] = df["close"].ewm(span=12, adjust=False).mean() - df["close"].ewm(span=26, adjust=False).mean()  
    df["Signal_Line"] = df["MACD"].ewm(span=9, adjust=False).mean()  
    df["Support"] = df["low"].rolling(window=20).min()  
    df["Resistance"] = df["high"].rolling(window=20).max()  
    df["Premium_Zone"] = df["Resistance"] - (df["Resistance"] * 0.05)  
    df["Discount_Zone"] = df["Support"] + (df["Support"] * 0.05)  

    file_path = os.path.join(DATA_DIR, f"{stock_name}.csv")  
    df.to_csv(file_path, index=False)  
    print(f"✅ Data saved: {file_path}")  

for stock in stockNames:  
    fetch_historical_data(stock)  

print("✅ All stock data processed successfully!")  

📌 Step 5: Train DeepSeek-8B with LoRA

train_deepseek.py

from transformers import AutoModelForCausalLM, Trainer, TrainingArguments
from peft import LoraConfig, get_peft_model, TaskType
import torch

device = "cuda" if torch.cuda.is_available() else "cpu"

model = AutoModelForCausalLM.from_pretrained(
    "deepseek-8b",
    torch_dtype=torch.float16,
    device_map="auto",
    offload_folder="./offload"
)

config = LoraConfig(
    task_type=TaskType.CAUSAL_LM,
    r=8,
    lora_alpha=32,
    lora_dropout=0.1
)

model = get_peft_model(model, config)

training_args = TrainingArguments(
    output_dir="./fine_tuned_deepseek",
    per_device_train_batch_size=1,
    gradient_accumulation_steps=4,
    num_train_epochs=3,
    logging_dir="./logs",
    save_strategy="epoch",
    fp16=True
)

trainer = Trainer(model=model, args=training_args, train_dataset=dataset["train"])
trainer.train()

model.save_pretrained("fine_tuned_deepseek")
tokenizer.save_pretrained("fine_tuned_deepseek")

print("🎉 Training complete! Model saved to `fine_tuned_deepseek`.")  

Next Steps

  • Deploy the trained model as an API
  • Set up Telegram alerts for key stock movements
  • Optimize the model further for intraday trading predictions

Was this article helpful?
YesNo

Similar Posts