PAXG 4-Supertend + 40Bars Breakout add

Delta_India · strategy · 275 行 · 点赞 15 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
strategy(
     title = "PAXG 4H Supertrend + 40-Bar Breakout Add",
     overlay = true,
     initial_capital = 100000,
     currency = currency.USD,
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 100,
     pyramiding = 2,
     commission_type = strategy.commission.percent,
     commission_value = 0.03,
     process_orders_on_close = false,
     calc_on_every_tick = false,
     margin_long = 50
)
 
//====================================================
// INPUTS
//====================================================
 
// Supertrend
atrLength = input.int(
     10,
     title = "Supertrend ATR Length",
     minval = 1
)
 
stMultiplier = input.float(
     4.0,
     title = "Supertrend Multiplier",
     step = 0.1
)
 
// Breakout confirmation
breakoutLength = input.int(
     40,
     title = "Breakout Lookback",
     minval = 2
)
 
// Add-on size
addPercent = input.float(
     30.0,
     title = "Add-on Position %",
     minval = 1,
     maxval = 50,
     step = 5
)
 
//====================================================
// BACKTEST DATE RANGE
//====================================================
 
// Default = 25 Sep 2021
startDate = input.time(
     1632528000000,
     title = "Backtest Start Date"
)
 
// Default = 25 Sep 2026
endDate = input.time(
     1790380740000,
     title = "Backtest End Date"
)
 
useDateFilter = input.bool(
     true,
     title = "Use Backtest Date Filter"
)
 
inDateRange =
     not useDateFilter or
     (time >= startDate and time <= endDate)
 
//====================================================
// SUPERTREND
//====================================================
 
[supertrend, direction] =
     ta.supertrend(stMultiplier, atrLength)
 
// TradingView Supertrend convention:
//
// direction < 0 = bullish
// direction > 0 = bearish
 
bullish = direction < 0
bearish = direction > 0
 
bullishFlip =
     bullish and
     direction[1] > 0
 
bearishFlip =
     bearish and
     direction[1] < 0
 
//====================================================
// 40-BAR CLOSING HIGH
//====================================================
 
// Highest CLOSE of the PREVIOUS 40 completed candles.
// Current candle is deliberately excluded.
 
highestPreviousClose =
     ta.highest(close, breakoutLength)[1]
 
breakoutSignal =
     close > highestPreviousClose
 
//====================================================
// TRADE STATE
//====================================================
 
// Prevent more than one add-on during each trade.
 
var bool addedPosition = false
 
// Reset once completely flat
if strategy.position_size == 0 and strategy.position_size[1] != 0
    addedPosition := false
 
//====================================================
// BASE ENTRY
//====================================================
 
// Supertrend flips bullish on completed 4H candle.
//
// process_orders_on_close = false means
// the market order fills on the NEXT bar open.
 
if (
     inDateRange and
     bullishFlip and
     strategy.position_size == 0
   )
 
    strategy.entry(
         id = "Base Long",
         direction = strategy.long
    )
 
//====================================================
// 30% ADD-ON
//====================================================
 
// Requirements:
//
// 1. Already long
// 2. No previous add-on during this trade
// 3. Close > highest CLOSE of previous 40 bars
//
// Order is created after this candle closes
// and normally fills on the NEXT 4H open.
 
if (
     inDateRange and
     strategy.position_size > 0 and
     not addedPosition and
     breakoutSignal
   )
 
    // 30% of current strategy equity.
    //
    // Pine requires explicit quantity for this
    // second order, so equity is converted into
    // PAXG units using current closing price.
 
    addQty =
         (strategy.equity * (addPercent / 100.0)) /
         close
 
    strategy.entry(
         id = "Breakout Add",
         direction = strategy.long,
         qty = addQty
    )
 
    addedPosition := true
 
//====================================================
// EXIT
//====================================================
 
// Exit entire position immediately after
// Supertrend changes from bullish to bearish.
//
// With process_orders_on_close = false,
// exit fills on NEXT 4H bar open.
 
if (
     strategy.position_size > 0 and
     bearishFlip
   )
 
    strategy.close_all(
         comment = "Bearish Supertrend"
    )
 
//====================================================
// END-DATE EXIT
//====================================================
 
if (
     useDateFilter and
     time > endDate and
     strategy.position_size > 0
   )
 
    strategy.close_all(
         comment = "End of Backtest"
    )
 
//====================================================
// VISUALS
//====================================================
 
plot(
     bullish ? supertrend : na,
     title = "Bullish Supertrend",
     color = color.green,
     linewidth = 2,
     style = plot.style_linebr
)
 
plot(
     bearish ? supertrend : na,
     title = "Bearish Supertrend",
     color = color.red,
     linewidth = 2,
     style = plot.style_linebr
)
 
plot(
     highestPreviousClose,
     title = "Previous 40-Bar Highest Close",
     color = color.orange,
     linewidth = 1
)
 
// Base entry marker
plotshape(
     bullishFlip and inDateRange,
     title = "Base Entry",
     location = location.belowbar,
     style = shape.triangleup,
     color = color.green,
     size = size.small,
     text = "BUY"
)
 
// Add-on marker
plotshape(
     strategy.position_size > 0 and
     breakoutSignal and
     not addedPosition[1],
     title = "30% Add",
     location = location.belowbar,
     style = shape.circle,
     color = color.blue,
     size = size.small,
     text = "+30%"
)
 
// Exit marker
plotshape(
     strategy.position_size > 0 and
     bearishFlip,
     title = "Exit",
     location = location.abovebar,
     style = shape.triangledown,
     color = color.red,
     size = size.small,
     text = "EXIT"
)

← 返回列表