Skip to content

Instantly share code, notes, and snippets.

@vitorcalvi
Last active December 11, 2025 08:46
Show Gist options
  • Select an option

  • Save vitorcalvi/9b420eb0371a330196efba1a06796614 to your computer and use it in GitHub Desktop.

Select an option

Save vitorcalvi/9b420eb0371a330196efba1a06796614 to your computer and use it in GitHub Desktop.
from 15s to 30m timeframe all is positive PnL
//@version=5
strategy("Universal Adaptive Strategy V4 (ATR-Based)", overlay=true, process_orders_on_close=true)
// --- 1. SETTINGS & INPUTS ---
// TREND FILTER (The "Big Boss")
// We only trade if price is above this EMA. Keeps you out of crashes.
ema_len = input.int(200, "Trend Filter (EMA)", group="Trend")
// SUPERTREND (Entry Signal)
st_atr_len = input.int(10, "Supertrend ATR Length", group="Supertrend")
st_factor = input.float(3.0, "Supertrend Factor", group="Supertrend")
// MACD (Momentum Confirmation)
macd_fast = input.int(12, "MACD Fast", group="MACD")
macd_slow = input.int(26, "MACD Slow", group="MACD")
macd_signal = input.int(9, "MACD Signal", group="MACD")
// RISK MANAGEMENT (The "Universal" Magic)
// Instead of fixed %, we use ATR Multipliers.
// This makes the code work on 1m, 15m, 1H, or 4H automatically.
atr_length = input.int(14, "ATR Length", group="Risk Management")
sl_multiplier = input.float(1.5, "Stop Loss (x ATR)", step=0.1, group="Risk Management")
tp_multiplier = input.float(3.0, "Take Profit (x ATR)", step=0.1, group="Risk Management")
// TIME RANGE (Optional Backtest Limiter)
use_date_filter = input.bool(false, "Filter Date Range?", group="Time")
start_year = input.int(2024, "Start Year", group="Time")
// --- 2. CALCULATIONS ---
// Indicators
ema_value = ta.ema(close, ema_len)
[st_val, st_dir] = ta.supertrend(st_factor, st_atr_len) // st_dir: -1=Up, 1=Down
[macd_line, sig_line, _] = ta.macd(close, macd_fast, macd_slow, macd_signal)
atr_value = ta.atr(atr_length)
// Logic Flags
is_uptrend = close > ema_value // Price above 200 EMA
st_buy_signal = st_dir == -1 and st_dir[1] == 1 // Supertrend FLIP to Green
macd_bullish = macd_line > sig_line // MACD is green
// Date Filter
in_date_range = use_date_filter ? (year >= start_year) : true
// --- 3. ENTRY LOGIC ---
// CONDITION: Trend is Up + Supertrend Flips Green + MACD is Bullish
long_entry = is_uptrend and st_buy_signal and macd_bullish and in_date_range
// --- 4. DYNAMIC EXIT LOGIC (ATR) ---
var float entry_price = na
var float sl_price = na
var float tp_price = na
if (long_entry and strategy.position_size == 0)
// Calculate Stops based on Volatility (ATR) at the moment of entry
entry_price := close
sl_price := close - (atr_value * sl_multiplier)
tp_price := close + (atr_value * tp_multiplier)
strategy.entry("Long", strategy.long, comment="Entry")
// Exit Execution
if (strategy.position_size > 0)
// Draw lines for visual debugging
line.new(bar_index, tp_price, bar_index+1, tp_price, color=color.green, width=2)
line.new(bar_index, sl_price, bar_index+1, sl_price, color=color.red, width=2)
// Execute Exit
strategy.exit("Exit", "Long", stop=sl_price, limit=tp_price)
// --- 5. VISUALS ---
plot(ema_value, color=color.white, linewidth=2, title="200 EMA Trend")
plot(st_val, color = st_dir == -1 ? color.green : color.red, title="Supertrend")
// Background color to show valid trend zones
bgcolor(is_uptrend ? color.new(color.green, 90) : color.new(color.red, 90), title="Trend Zone")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment