//@version=6 // ============================================================================ // Trend breakout · ATR risk — PineBag strategy template // ============================================================================ // ---------------------------------------------------------------------------- // STRATEGY PROPERTIES (TradingView: Settings → Properties) // ---------------------------------------------------------------------------- // TIMEFRAME takes a standard TradingView interval. Paste one of these, by the // lowest TradingView plan that has it (tradingview.com/pricing, Charts): // Basic days "1D" "1W" "1M" "3M" "6M" "12M" // Essential minutes "1" "3" "5" "15" "30" "45" // hours "60" "120" "180" "240" // ranges "1R" "10R" "100R" "1000R" // Premium seconds "1S" "5S" "10S" "15S" "30S" "45S" // Ultimate ticks "1T" "10T" "100T" "1000T" // Other intervals are custom and not used: a week is "1W", not "7D". const string TIMEFRAME = "1D" // Chart timeframe const float INITIAL_CAPITAL = 100000 // Initial capital const string CURRENCY = currency.USD // Initial capital currency const float ORDER_SIZE = 100 // Default order size const string ORDER_SIZE_TYPE = strategy.percent_of_equity // Order size: % of equity const int PYRAMIDING = 1 // Pyramiding const bool BAR_MAGNIFIER = false // Bar detalization: Default (4 ticks per bar) const bool CALC_EVERY_TICK = false // Script execution: On bar close const bool CALC_ON_FILLS = false // Script execution: no recalculation after fills const float COMMISSION = 0 // Commission const string COMMISSION_TYPE = strategy.commission.percent // Commission: Percent const float MARGIN_LONG = 100 // Long leverage 1x (leverage = 100 / margin) const float MARGIN_SHORT = 100 // Short leverage 1x const int SLIPPAGE = 0 // Slippage, ticks const int LIMIT_FILL_TICKS = 0 // Limit order execution: Requested price const bool FILL_ON_BAR_CLOSE = false // Order execution delay: One tick strategy("Trend breakout · ATR risk", overlay = true, initial_capital = INITIAL_CAPITAL, currency = CURRENCY, default_qty_type = ORDER_SIZE_TYPE, default_qty_value = ORDER_SIZE, pyramiding = PYRAMIDING, use_bar_magnifier = BAR_MAGNIFIER, calc_on_every_tick = CALC_EVERY_TICK, calc_on_order_fills = CALC_ON_FILLS, commission_type = COMMISSION_TYPE, commission_value = COMMISSION, margin_long = MARGIN_LONG, margin_short = MARGIN_SHORT, slippage = SLIPPAGE, backtest_fill_limits_assumption = LIMIT_FILL_TICKS, process_orders_on_close = FILL_ON_BAR_CLOSE) // Stops with a clear message on any other timeframe; PineBag reads TIMEFRAME // and selects it for the run. if timeframe.period != TIMEFRAME runtime.error("Switch the chart to the " + TIMEFRAME + " timeframe.") // ---------------------------------------------------------------------------- // INPUT SETTINGS (TradingView: Settings → Inputs) // ---------------------------------------------------------------------------- fastLength = input.int(20, "Fast EMA", minval = 1) slowLength = input.int(50, "Slow EMA", minval = 2) breakoutLength = input.int(20, "Breakout lookback", minval = 2) atrLength = input.int(14, "ATR length", minval = 1) atrStop = input.float(2.0, "ATR stop multiplier", minval = 0.1, step = 0.1) // ---------------------------------------------------------------------------- // CALCULATIONS // ---------------------------------------------------------------------------- fast = ta.ema(close, fastLength) slow = ta.ema(close, slowLength) atr = ta.atr(atrLength) highestBefore = ta.highest(high, breakoutLength)[1] // ---------------------------------------------------------------------------- // ENTRIES AND EXITS // ---------------------------------------------------------------------------- longSignal = fast > slow and close > highestBefore if longSignal and strategy.position_size == 0 strategy.entry("Long", strategy.long) stopPrice = strategy.position_avg_price - atr * atrStop strategy.exit("ATR stop", "Long", stop = stopPrice) // ---------------------------------------------------------------------------- // PLOTS // ---------------------------------------------------------------------------- plot(fast, "Fast EMA", color = color.rgb(0, 153, 255)) plot(slow, "Slow EMA", color = color.rgb(123, 97, 255))