1. Increase model layers and units, add GRU and ensemble placeholders:
Add this to your script for a hybrid model creation with additional power:
js
function createHybridModel(inputShape){
const input = tf.input({shape: inputShape});
// LSTM layer
const lstm1 = tf.layers.lstm({units: 128, returnSequences: true}).apply(input);
// Dropout
const dropout1 = tf.layers.dropout({rate: 0.3}).apply(lstm1);
// GRU layer
const gru1 = tf.layers.gru({units: 64, returnSequences: true}).apply(dropout1);
const dropout2 = tf.layers.dropout({rate: 0.25}).apply(gru1);
// Second LSTM layer
const lstm2 = tf.layers.lstm({units: 32}).apply(dropout2);
const dropout3 = tf.layers.dropout({rate: 0.2}).apply(lstm2);
// Dense output layer for 10-step prediction
const output = tf.layers.dense({units: 10}).apply(dropout3);
const model = tf.model({inputs: input, outputs: output});
model.compile({optimizer: tf.train.adam(0.0003), loss: 'meanSquaredError'});
return model;
}
2. Replace old createLSTMModel with above in your updateChart code and train using this model.
3. For boosting decision function:
Increase weights and smooth directions over longer history:
js
function boostedDecisionEnhanced(predArr, closes, ema10, rsi14, macdHist, dirAcc10) {
const lastClose = closes[closes.length - 1];
const lastPred = predArr[predArr.length - 1];
const direction = lastPred > lastClose ? 1 : lastPred < lastClose ? -1 : 0;
let score = direction * 0.5; // increased weight
const ema30 = calculateEMA(closes, 30);
const ema60 = calculateEMA(closes, 60);
const trend = ema10[ema10.length - 1] > ema60[ema60.length - 1] ? 1 :
ema10[ema10.length - 1] < ema60[ema60.length - 1] ? -1 : 0;
score += trend * 0.2; // more weight on longer trend
const rsiVal = rsi14[rsi14.length - 1];
let rsiSignal = 0;
if (rsiVal > 65) rsiSignal = 1;
else if (rsiVal < 35) rsiSignal = -1;
score += rsiSignal * 0.15;
const macdSignal = macdHist[macdHist.length - 1] > 0 ? 1 : (macdHist[macdHist.length - 1] < 0 ? -1 : 0);
score += macdSignal * 0.1;
if (dirAcc10 !== null) {
const weight = (dirAcc10 - 50) / 50;
score += weight * 0.15;
}
return Math.max(-1, Math.min(1, score));
}
4. Adjust smoothing alpha to be more aggressive on newer data (e.g., 0.6):
js
const smoothedPreds = smoothPredictions(matchedPreds, 0.6);
5. Add ensemble model (e.g., XGBoost) prediction fetch if available
You can integrate your hybrid model fetch code in the same dashboard with something like:
js
async function fetchHybridEnsemblePrediction() {
try {
const response = await fetch("https://yourapi.com/predictions/hybrid-model");
if (!response.ok) throw new Error("Network error");
const data = await response.json();
// Example usage: combine ensemble prediction with LSTM prediction
const ensemblePred = data.ensemble_prediction;
// Blend ensemble with your LSTM prediction with a weight factor
if(window.latestLSTMPrediction){
const blendedPred = ensemblePred * 0.5 + window.latestLSTMPrediction * 0.5;
// Use blendedPred for final decisions and display
// ...
}
} catch(e){
console.warn('Hybrid prediction fetch error:', e);
}
}
# app.py from flask import Flask, jsonify import numpy as np import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense import requests app = Flask(__name__) # Binance se last 60 minute price data fetch karna def fetch_binance_data(): url = "https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1m&limit=60" data = requests.get(url).json() closes = [float(item[4]) for item in data] return closes # LSTM input prepare karna sliding window ke form me def prepare_data(series, n_steps): X, y = [], [] for i in range(len(series) - n_steps): X.append(series[i:i+n_steps]) y.append(series[i+n_steps]) return np.array(X), np.array(y) # LSTM model train karna def train_model(series): n_steps = 10 X, y = prepare_data(series, n_steps) X = X.reshape((X.shape[0], X.shape[1], 1)) model = Sequential([ LSTM(50, activation='relu', input_shape=...
Comments
Post a Comment