crypto_dataset / btc_trading_bot.py
KavinduHansaka's picture
Update btc_trading_bot.py
bc90a93 verified
import torch
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from datasets import load_dataset
def predict_btc():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dataset = load_dataset("KavinduHansaka/btc-minute-data")
df = dataset["train"].to_pandas()
df["price"] = df["price"].astype(float)
prices = df["price"].values.reshape(-1, 1)
# Normalize Data
scaler = MinMaxScaler(feature_range=(0, 1))
prices_scaled = scaler.fit_transform(prices)
# Ensure input shape is exactly (1, 60, 1)
input_seq = np.array(prices_scaled[-60:]).reshape(1, 60, 1)
input_tensor = torch.tensor(input_seq, dtype=torch.float32).to(device)
# Load optimized model
model = torch.jit.load("btc_lstm_optimized.pth").to(device)
model.eval()
# Predict BTC price
with torch.no_grad():
predicted_scaled = model(input_tensor).cpu().numpy()
# Ensure output shape is exactly (15,)
predicted_prices = scaler.inverse_transform(predicted_scaled.reshape(-1, 1)).flatten()
return f"Next 15 min BTC Prices: {predicted_prices.tolist()}"