//@version=5
indicator("10m Next-Bar Predictor (non-repainting)", overlay=true)
// ---------- USER INPUTS ----------
aggTF = input.timeframe("10", "Aggregation TF (minutes)") // prediction horizon TF
tpPct = input.float(0.25, "Target % (0.25 = 0.25%)", step=0.01)
volMult = input.float(1.5, "Volume spike multiplier")
momPeriod = input.int(5, "Momentum period")
rsiLen = input.int(14, "RSI length")
stochLen = input.int(14, "Stoch-RSI window")
lrLen = input.int(8, "LinReg length (slope proxy)")
voteThreshold = input.int(2, "Vote threshold (>= -> BUY / <= - -> SELL)")
useForming = input.bool(false, "Use forming agg TF (lookahead_on)? (repaints!)", inline="f")
// ---------- request.security options ----------
gapsMode = barmerge.gaps_off
look = useForming ? barmerge.lookahead_on : barmerge.lookahead_off
// aggregated closed series (we use closed data when lookahead_off)
[o,h,l,c,v] = request.security(syminfo.tickerid, aggTF, [open, high, low, close, volume], gaps = gapsMode, lookahead = look)
// ---------- INDICATOR FEATURES on aggregated series ----------
ema5 = ta.ema(c, 5)
ema20 = ta.ema(c, 20)
rsiV = ta.rsi(c, rsiLen)
rsi_delta = rsiV - nz(rsiV[1])
stoch_low = ta.lowest(rsiV, stochLen)
stoch_high = ta.highest(rsiV, stochLen)
stochRSI = (stoch_high - stoch_low) == 0 ? 0.0 : (rsiV - stoch_low) / (stoch_high - stoch_low)
psar_v = request.security(syminfo.tickerid, aggTF, ta.sar(0.02, 0.02, 0.2), gaps = gapsMode, lookahead = look)
vwap_agg = request.security(syminfo.tickerid, aggTF, ta.vwap, gaps = gapsMode, lookahead = look)
mom = c - nz(c[momPeriod], c)
vol_avg = request.security(syminfo.tickerid, aggTF, ta.sma(volume, 10), gaps = gapsMode, lookahead = look)
vol_spike = v > vol_avg * volMult
// linear regression slope proxy (difference between current and lr value p bars ago)
lr_val = ta.linreg(c, lrLen, 0)
lr_prev = ta.linreg(c, lrLen, 1)
lr_slope = lr_val - lr_prev
// ---------- VOTE RULES (each gives +1 BUY, -1 SELL, 0 neutral) ----------
// 1) EMA cross direction
ema_vote = ema5 > ema20 ? 1 : (ema5 < ema20 ? -1 : 0)
// 2) RSI trend
rsi_vote = rsiV > 55 ? 1 : (rsiV < 45 ? -1 : 0)
// 3) StochRSI: overbought=>SELL, oversold=>BUY (as in your JS)
stoch_vote = stochRSI > 0.8 ? -1 : (stochRSI < 0.2 ? 1 : 0)
// 4) PSAR direction
psar_vote = c > psar_v ? 1 : -1
// 5) VWAP distance
vw_vote = c > vwap_agg ? 1 : -1
// 6) Momentum sign
mom_vote = mom > 0 ? 1 : (mom < 0 ? -1 : 0)
// 7) Volume spike (if spike in same direction as price move -> stronger)
vol_vote = vol_spike ? (c > nz(c[1]) ? 1 : (c < nz(c[1]) ? -1 : 0)) : 0
// 8) LinReg slope
lr_vote = lr_slope > 0 ? 1 : (lr_slope < 0 ? -1 : 0)
// optional weights (you can tune)
w_ema = 1.0
w_rsi = 1.0
w_stoch = 1.0
w_psar = 1.0
w_vw = 1.0
w_mom = 0.8
w_vol = 0.6
w_lr = 0.9
score = ema_vote*w_ema + rsi_vote*w_rsi + stoch_vote*w_stoch + psar_vote*w_psar + vw_vote*w_vw + mom_vote*w_mom + vol_vote*w_vol + lr_vote*w_lr
// ---------- PREDICTION DECISION ----------
pred_signal = score >= voteThreshold ? "BUY" : (score <= -voteThreshold ? "SELL" : "NEUTRAL")
// predicted price (simple): use last close adjusted by target pct
pred_target = pred_signal == "BUY" ? c * (1 + tpPct/100.0) : (pred_signal == "SELL" ? c * (1 - tpPct/100.0) : na)
// also compute a linear extrapolated predicted close (optional)
pred_lin_extrap = c + lr_slope * lrLen // rough extrapolation using lr_slope
// ---------- PLOT ----------
// Plot aggregated close and predicted target
aggClosePlot = request.security(syminfo.tickerid, aggTF, close, gaps = gapsMode, lookahead = look)
plot(aggClosePlot, title="Agg Close", linewidth=2)
plotshape(pred_signal == "BUY", title="PredBUY", style=shape.labelup, location=location.belowbar, color=color.new(color.green,0), text="PBUY")
plotshape(pred_signal == "SELL", title="PredSELL", style=shape.labeldown, location=location.abovebar, color=color.new(color.red,0), text="PSELL")
plot(pred_target, title="Predicted Target", style=plot.style_linebr, linewidth=2, color=color.orange)
plot(pred_lin_extrap, title="Linear Extrapolation", style=plot.style_line, linewidth=1, color=color.yellow)
// info table
var table info = table.new(position.top_right, 1, 6, border_width=1)
if barstate.islast
table.cell(info, 0, 0, "Pred: " + pred_signal + " Score:" + str.tostring(score, format.percent), text_size=size.small)
table.cell(info, 0, 1, "Agg Close: " + str.tostring(c, format.mintick), text_size=size.small)
table.cell(info, 0, 2, "TP: " + str.tostring(pred_target, format.price), text_size=size.small)
table.cell(info, 0, 3, "VolSpike: " + (vol_spike ? "YES" : "NO"), text_size=size.small)
table.cell(info, 0, 4, "LR slope: " + str.tostring(lr_slope, format.mintick), text_size=size.small)
table.cell(info, 0, 5, "UseForming(repaint): " + (useForming ? "YES" : "NO"), text_size=size.small)
// ---------- ALERT CONDITION (for convenience) ----------
alertcondition(pred_signal == "BUY", title="Pred BUY next 10m", message="Predicted BUY next 10m on {{ticker}}")
alertcondition(pred_signal == "SELL", title="Pred SELL next 10m", message="Predicted SELL next 10m on {{ticker}}")
# 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