OPEN-SOURCE SCRIPT
Institutional Risk Engine v3

//version=6
strategy(
"Institutional Risk Engine v3",
overlay=true,
initial_capital=100000,
pyramiding=0,
default_qty_type=strategy.percent_of_equity,
default_qty_value=10,
calc_on_order_fills=true)
// ==========================================================
// 1️⃣ MULTI-ASSET DATA
// ==========================================================
btc = request.security("BINANCE:BTCUSDT", timeframe.period, close)
eth = request.security("BINANCE:ETHUSDT", timeframe.period, close)
es = request.security("CME_MINI:ES1!", timeframe.period, close)
// Returns
btc_ret = math.log(btc/btc[1])
eth_ret = math.log(eth/eth[1])
es_ret = math.log(es/es[1])
// Volatility
btc_vol = ta.stdev(btc_ret, 50)
eth_vol = ta.stdev(eth_ret, 50)
es_vol = ta.stdev(es_ret, 50)
// Correlations
corr_be = ta.correlation(btc_ret, eth_ret, 50)
corr_bs = ta.correlation(btc_ret, es_ret, 50)
corr_es = ta.correlation(eth_ret, es_ret, 50)
// ==========================================================
// 2️⃣ VOL PARITY WITH CORRELATION ADJUSTMENT
// ==========================================================
inv_btc = btc_vol != 0 ? 1/btc_vol : 0
inv_eth = eth_vol != 0 ? 1/eth_vol : 0
inv_es = es_vol != 0 ? 1/es_vol : 0
sum_inv = inv_btc + inv_eth + inv_es
w_btc = inv_btc / sum_inv
w_eth = inv_eth / sum_inv
w_es = inv_es / sum_inv
// Approx portfolio variance
portfolio_var = (
w_btc*w_btc*btc_vol*btc_vol +
w_eth*w_eth*eth_vol*eth_vol +
w_es*w_es*es_vol*es_vol +
2*w_btc*w_eth*corr_be*btc_vol*eth_vol +
2*w_btc*w_es*corr_bs*btc_vol*es_vol +
2*w_eth*w_es*corr_es*eth_vol*es_vol
)
// ==========================================================
// 3️⃣ 12-MONTH SHARPE TARGETING (252 trading days proxy)
// ==========================================================
ret = math.log(close/close[1])
mean_ret = ta.sma(ret, 252)
vol_ret = ta.stdev(ret, 252)
sharpe = vol_ret != 0 ? (mean_ret / vol_ret) * math.sqrt(252) : 0
target_sharpe = 1.5
sharpe_scale =
sharpe > target_sharpe ? 1 :
sharpe > 1 ? 0.7 :
0.4
// ==========================================================
// 4️⃣ RISK OF RUIN
// ==========================================================
wins = strategy.wintrades
loss = strategy.losstrades
total = strategy.closedtrades
p = total > 0 ? wins / total : 0.5
q = 1 - p
risk_per_trade = 0.01
capital_units = strategy.equity * risk_per_trade
risk_of_ruin =
p > q ? math.pow(q/p, capital_units) : 1
// ==========================================================
// 5️⃣ PROP FIRM SURVIVAL MODEL
// ==========================================================
var float peak_equity = na
peak_equity := na(peak_equity) ? strategy.equity : math.max(peak_equity, strategy.equity)
trailing_dd = (strategy.equity - peak_equity) / peak_equity
// Daily
var float day_start = na
new_day = ta.change(time("D")) != 0
if new_day
day_start := strategy.equity
daily_pnl = strategy.equity - day_start
daily_loss_limit = day_start * 0.03
trailing_limit = -0.10
prop_ok =
daily_pnl > -daily_loss_limit and
trailing_dd > trailing_limit
// Near violation compression
prop_scale =
trailing_dd > -0.05 ? 1 :
trailing_dd > -0.08 ? 0.6 :
0.3
// ==========================================================
// FINAL CAPITAL SCALING
// ==========================================================
base_alloc = 0.6
final_scale = base_alloc * sharpe_scale * prop_scale
position_pct = final_scale * 100
// ==========================
// ENTRY
// ==========================
long_signal = close > ta.ema(close, 20) and ta.crossover(ta.rsi(close, 6), 50)
// Prop condition default (avoid empty block issues)
if strategy.position_size == 0 and prop_ok
if long_signal
strategy.entry("LONG", strategy.long)
// ==========================================================
// EXIT
// ==========================================================
if strategy.position_size != 0
avg = strategy.position_avg_price
strategy.exit("EXIT",
limit = avg * 1.01,
stop = avg * 0.995)
// ==========================================================
// DASHBOARD
// ==========================================================
var table dash = table.new(position.top_right, 2, 8)
if barstate.islast
table.cell(dash, 0, 0, "Portfolio Vol")
table.cell(dash, 0, 1, "Sharpe")
table.cell(dash, 1, 1, str.tostring(sharpe,"#.##"))
table.cell(dash, 0, 2, "Risk of Ruin")
table.cell(dash, 1, 2, str.tostring(risk_of_ruin,"#.#####"))
table.cell(dash, 0, 3, "Trailing DD")
table.cell(dash, 1, 3, str.tostring(trailing_dd*100,"#.##")+"%")
table.cell(dash, 0, 4, "Prop OK")
table.cell(dash, 1, 4, str.tostring(prop_ok))
table.cell(dash, 0, 5, "Sharpe Scale")
table.cell(dash, 1, 5, str.tostring(sharpe_scale,"#.##"))
table.cell(dash, 0, 6, "Prop Scale")
table.cell(dash, 1, 6, str.tostring(prop_scale,"#.##"))
table.cell(dash, 0, 7, "Position %")
table.cell(dash, 1, 7, str.tostring(position_pct,"#.##"))
strategy(
"Institutional Risk Engine v3",
overlay=true,
initial_capital=100000,
pyramiding=0,
default_qty_type=strategy.percent_of_equity,
default_qty_value=10,
calc_on_order_fills=true)
// ==========================================================
// 1️⃣ MULTI-ASSET DATA
// ==========================================================
btc = request.security("BINANCE:BTCUSDT", timeframe.period, close)
eth = request.security("BINANCE:ETHUSDT", timeframe.period, close)
es = request.security("CME_MINI:ES1!", timeframe.period, close)
// Returns
btc_ret = math.log(btc/btc[1])
eth_ret = math.log(eth/eth[1])
es_ret = math.log(es/es[1])
// Volatility
btc_vol = ta.stdev(btc_ret, 50)
eth_vol = ta.stdev(eth_ret, 50)
es_vol = ta.stdev(es_ret, 50)
// Correlations
corr_be = ta.correlation(btc_ret, eth_ret, 50)
corr_bs = ta.correlation(btc_ret, es_ret, 50)
corr_es = ta.correlation(eth_ret, es_ret, 50)
// ==========================================================
// 2️⃣ VOL PARITY WITH CORRELATION ADJUSTMENT
// ==========================================================
inv_btc = btc_vol != 0 ? 1/btc_vol : 0
inv_eth = eth_vol != 0 ? 1/eth_vol : 0
inv_es = es_vol != 0 ? 1/es_vol : 0
sum_inv = inv_btc + inv_eth + inv_es
w_btc = inv_btc / sum_inv
w_eth = inv_eth / sum_inv
w_es = inv_es / sum_inv
// Approx portfolio variance
portfolio_var = (
w_btc*w_btc*btc_vol*btc_vol +
w_eth*w_eth*eth_vol*eth_vol +
w_es*w_es*es_vol*es_vol +
2*w_btc*w_eth*corr_be*btc_vol*eth_vol +
2*w_btc*w_es*corr_bs*btc_vol*es_vol +
2*w_eth*w_es*corr_es*eth_vol*es_vol
)
// ==========================================================
// 3️⃣ 12-MONTH SHARPE TARGETING (252 trading days proxy)
// ==========================================================
ret = math.log(close/close[1])
mean_ret = ta.sma(ret, 252)
vol_ret = ta.stdev(ret, 252)
sharpe = vol_ret != 0 ? (mean_ret / vol_ret) * math.sqrt(252) : 0
target_sharpe = 1.5
sharpe_scale =
sharpe > target_sharpe ? 1 :
sharpe > 1 ? 0.7 :
0.4
// ==========================================================
// 4️⃣ RISK OF RUIN
// ==========================================================
wins = strategy.wintrades
loss = strategy.losstrades
total = strategy.closedtrades
p = total > 0 ? wins / total : 0.5
q = 1 - p
risk_per_trade = 0.01
capital_units = strategy.equity * risk_per_trade
risk_of_ruin =
p > q ? math.pow(q/p, capital_units) : 1
// ==========================================================
// 5️⃣ PROP FIRM SURVIVAL MODEL
// ==========================================================
var float peak_equity = na
peak_equity := na(peak_equity) ? strategy.equity : math.max(peak_equity, strategy.equity)
trailing_dd = (strategy.equity - peak_equity) / peak_equity
// Daily
var float day_start = na
new_day = ta.change(time("D")) != 0
if new_day
day_start := strategy.equity
daily_pnl = strategy.equity - day_start
daily_loss_limit = day_start * 0.03
trailing_limit = -0.10
prop_ok =
daily_pnl > -daily_loss_limit and
trailing_dd > trailing_limit
// Near violation compression
prop_scale =
trailing_dd > -0.05 ? 1 :
trailing_dd > -0.08 ? 0.6 :
0.3
// ==========================================================
// FINAL CAPITAL SCALING
// ==========================================================
base_alloc = 0.6
final_scale = base_alloc * sharpe_scale * prop_scale
position_pct = final_scale * 100
// ==========================
// ENTRY
// ==========================
long_signal = close > ta.ema(close, 20) and ta.crossover(ta.rsi(close, 6), 50)
// Prop condition default (avoid empty block issues)
if strategy.position_size == 0 and prop_ok
if long_signal
strategy.entry("LONG", strategy.long)
// ==========================================================
// EXIT
// ==========================================================
if strategy.position_size != 0
avg = strategy.position_avg_price
strategy.exit("EXIT",
limit = avg * 1.01,
stop = avg * 0.995)
// ==========================================================
// DASHBOARD
// ==========================================================
var table dash = table.new(position.top_right, 2, 8)
if barstate.islast
table.cell(dash, 0, 0, "Portfolio Vol")
table.cell(dash, 0, 1, "Sharpe")
table.cell(dash, 1, 1, str.tostring(sharpe,"#.##"))
table.cell(dash, 0, 2, "Risk of Ruin")
table.cell(dash, 1, 2, str.tostring(risk_of_ruin,"#.#####"))
table.cell(dash, 0, 3, "Trailing DD")
table.cell(dash, 1, 3, str.tostring(trailing_dd*100,"#.##")+"%")
table.cell(dash, 0, 4, "Prop OK")
table.cell(dash, 1, 4, str.tostring(prop_ok))
table.cell(dash, 0, 5, "Sharpe Scale")
table.cell(dash, 1, 5, str.tostring(sharpe_scale,"#.##"))
table.cell(dash, 0, 6, "Prop Scale")
table.cell(dash, 1, 6, str.tostring(prop_scale,"#.##"))
table.cell(dash, 0, 7, "Position %")
table.cell(dash, 1, 7, str.tostring(position_pct,"#.##"))
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.