Institutional Risk Engine v3 — Strategy by bettencourt37

By version

Performance Metrics

Description

//version=6strategy( "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)// Returnsbtc_ret = math.log(btc/btc[1])eth_ret = math.log(eth/eth[1])es_ret = math.log(es/es[1])// Volatilitybtc_vol = ta.stdev(btc_ret, 50)eth_vol = ta.stdev(eth_ret, 50)es_vol = ta.stdev(es_ret, 50)// Correlationscorr_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 : 0inv_eth = eth_vol != 0 ? 1/eth_vol : 0inv_es = es_vol != 0 ? 1/es_vol : 0sum_inv = inv_btc + inv_eth + inv_esw_btc = inv_btc / sum_invw_eth = inv_eth / sum_invw_es = inv_es / sum_inv// Approx portfolio varianceportfolio_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) : 0target_sharpe = 1.5sharpe_scale = sharpe > target_sharpe ? 1 : sharpe > 1 ? 0.7 : 0.4// ==========================================================// 4️⃣ RISK OF RUIN// ==========================================================wins = strategy.wintradesloss = strategy.losstradestotal = strategy.closedtradesp = total > 0 ? wins / total : 0.5q = 1 - prisk_per_trade = 0.01capital_units = strategy.equity * risk_per_traderisk_of_ruin = p > q ? math.pow(q/p, capital_units) : 1// ==========================================================// 5️⃣ PROP FIRM SURVIVAL MODEL// ==========================================================var float peak_equity = napeak_equity := na(peak_equity) ? strategy.equity : math.max(peak_equity, strategy.equity)trailing_dd = (strategy.equity - peak_equity) / peak_equity// Dailyvar float day_start = nanew_day = ta.change(time("D")) != 0if new_day day_start := strategy.equitydaily_pnl = strategy.equity - day_startdaily_loss_limit = day_start * 0.03trailing_limit = -0.10prop_ok = daily_pnl > -daily_loss_limit and trailing_dd > trailing_limit// Near violation compressionprop_scale = trailing_dd > -0.05 ? 1 : trailing_dd > -0.08 ? 0.6 : 0.3// ==========================================================// FINAL CAPITAL SCALING// ==========================================================base_alloc = 0.6final_scale = base_alloc * sharpe_scale * prop_scaleposition_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,"#.##"))

Browse all 5,900+ TradingView Pine Script strategies

View on TradingView