Untitled

 avatar
unknown
plain_text
17 days ago
4.7 kB
25
Indexable
//@version=5
strategy(title="BAMLH0A0HYM2 Strategy with VIX Deployment (SPXL)",
         shorttitle="BAML-SPXL with VIX Deploy",
         overlay=true,
         initial_capital=10000,
         default_qty_type=strategy.fixed,
         default_qty_value=0,
         pyramiding=0)

// --- USER INPUTS ---
bamlSymbol         = input.symbol("BAMLH0A0HYM2", "BAML Ticker")
emaLength          = input.int(330, "BAML EMA Length (days)")
commissionPc       = input.float(0.0, "Commission (%)", step=0.1)
declinePctInput    = input.float(30.0, "Decline % (Entry)", step=0.1)
risePctInput       = input.float(30.0, "Rise % (Exit)", step=0.1)
localHighLookback  = input.int(180, "Lookback for Local High (bars)")
localLowLookback   = input.int(180, "Lookback for Local Low (bars)")
monthlyAccumulation = input.float(500.0, "Monthly Accumulation (USD)", step=1.0)
vixThreshold       = input.float(15.0, "VIX Threshold", step=0.1)

// --- DATA ---
bamlClose = request.security(bamlSymbol, timeframe.period, close)
bamlEma   = ta.ema(bamlClose, emaLength)
tickerClose = close
vixClose = request.security("VIX", timeframe.period, close)

// --- STATE VARIABLES ---
var string mode           = "flat"
var int    anchorBarIndex = na
var float  cashAvailable  = strategy.equity
var float  totalShares    = na
var float  totalInvestment = na
var float  accumulatedCash = 0.0
var int    lastMonth       = month(time)

// Initialize anchor on the first bar
if barstate.isfirst
    anchorBarIndex := bar_index

// Determine how many bars since last anchor
barsSinceAnchor = bar_index - anchorBarIndex + 1

// Actual lookback for localHigh/localLow is the smaller of:
// (bars since anchor) OR (user input)
actualHighLookback = barsSinceAnchor < localHighLookback ? barsSinceAnchor : localHighLookback
actualLowLookback  = barsSinceAnchor < localLowLookback ? barsSinceAnchor : localLowLookback

// Rolling local high/low from anchor (capped by user-defined lookback)
localHigh = ta.highest(bamlClose, actualHighLookback)
localLow  = ta.lowest(bamlClose, actualLowLookback)

// Conditions
declineCondition = bamlClose <= localHigh * (1 - declinePctInput / 100)
riseCondition    = bamlClose >= localLow * (1 + risePctInput / 100)
aboveBigEma      = bamlClose > bamlEma
vixAboveThreshold = vixClose >= vixThreshold

// --- MONTHLY ACCUMULATION ---
if month(time) != lastMonth
    accumulatedCash := accumulatedCash + monthlyAccumulation
    lastMonth := month(time)

// --- TRADE LOGIC ---
// FLAT -> LONG (Entry)
if mode == "flat" and declineCondition
    sharesToBuy = cashAvailable / tickerClose
    commissionCost = sharesToBuy * tickerClose * (commissionPc / 100)
    if cashAvailable >= (sharesToBuy * tickerClose + commissionCost)
        sharesToBuy = (cashAvailable - commissionCost) / tickerClose
        strategy.entry("Buy SPXL", strategy.long, qty=sharesToBuy)
        totalInvestment := sharesToBuy * tickerClose + commissionCost
        cashAvailable := 0
        totalShares := sharesToBuy
        mode := "long"
        anchorBarIndex := bar_index

// LONG -> FLAT (Exit)
if mode == "long" and riseCondition and aboveBigEma
    exitValue = totalShares * tickerClose
    commissionCost = exitValue * (commissionPc / 100)
    cashAvailable := exitValue - commissionCost
    strategy.close_all()
    totalShares := na
    totalInvestment := na
    mode := "flat"
    anchorBarIndex := bar_index
    accumulatedCash := 0 //reset accumulated cash after exit.

// VIX Deployment
if mode == "long" and vixAboveThreshold and accumulatedCash > 0
    sharesToBuyVix = accumulatedCash / tickerClose
    commissionCostVix = sharesToBuyVix * tickerClose * (commissionPc / 100)
    if accumulatedCash >= (sharesToBuyVix * tickerClose + commissionCostVix)
        sharesToBuyVix = (accumulatedCash - commissionCostVix) / tickerClose
        strategy.order("VIX Buy", strategy.long, qty=sharesToBuyVix)
        totalShares := totalShares + sharesToBuyVix
        totalInvestment := totalInvestment + sharesToBuyVix * tickerClose + commissionCostVix
        accumulatedCash := 0 // Reset accumulated cash after deployment

// --- PLOTS ---
buySignal = mode == "flat" and declineCondition
sellSignal = mode == "long" and riseCondition and aboveBigEma

plotshape(buySignal, style=shape.triangleup, color=color.new(color.lime, 0), size=size.tiny, title="Buy Signal", location=location.belowbar)
plotshape(sellSignal, style=shape.triangledown, color=color.new(color.red, 0), size=size.tiny, title="Sell Signal", location=location.abovebar)
plot(1, title = "Dummy Plot") //this plot will always be plotted.
Editor is loading...
Leave a Comment