File size: 1,068 Bytes
5980fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pandas as pd
import numpy as np
import requests
from sklearn.preprocessing import MinMaxScaler
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import joblib
from tensorflow.keras.models import load_model
import os

# Load model and scaler
model = load_model("model.h5")
scaler = joblib.load("scaler.pkl") if os.path.exists("scaler.pkl") else MinMaxScaler()
analyzer = SentimentIntensityAnalyzer()

# Fetch PSX data
def get_psx_data(symbol="HBL"):
    dates = pd.date_range(end=pd.Timestamp.today(), periods=200)
    prices = np.linspace(100, 150, 200) + np.random.normal(0, 2, 200)
    df = pd.DataFrame({"Close": prices}, index=dates)
    return df

# Sentiment analysis 
def get_sentiment(stock="HBL"):
    return np.random.uniform(-0.05, 0.05)

# Predict next price
def predict_next(df):
    data = scaler.fit_transform(df[["Close"]])
    last60 = data[-60:].reshape(1, 60, 1)
    pred = model.predict(last60, verbose=0)[0][0]
    pred_real = scaler.inverse_transform([[pred]])[0][0]
    return pred_real