Below is a complete Pine-Script® v5 strategy that implements your multi-timeframe MA breakout logic with a fixed $100 stop‐loss and a $300 (3 ×) take-profit. Copy & paste it into TradingView’s Pine editor:
//@version=5
strategy(
title="Six AI Algo Strategy (v5)",
shorttitle="SixAI",
overlay=true,
initial_capital=10000,
default_qty_type=strategy.fixed,
default_qty_value=1 // we will calculate size manually
)
// ─── INPUTS ─────────────────────────────────────────────────────────────────────
risk_group = "🔹 Risk Management 🔹"
risk_amount = input.float(100, title="Risk Amount ($)", minval=1, group=risk_group)
risk_reward = input.float(3.0, title="Risk : Reward", minval=0.1, step=0.1, group=risk_group)
lookbackBars = input.int(10, title="Lookback Bars for S/L R/L", minval=1, group=risk_group)
// Current timeframe MA
tfC = input.bool(true, "Use Current TF?", group="🔹 Timeframes 🔹")
maTypeC = input.string("SMA", "MA Type (Curr)", options=["SMA","EMA","WMA","VWMA","RMA","SMMA","HullMA","TMA","DEMA","TEMA"])
maLenC = input.int(200, "MA Length (Curr)", minval=1)
maSrcC = input.source(close, "MA Source (Curr)")
depthLevC = input.float(0.89, "Depth Level (Curr)", minval=0, step=0.01)
histLenC = input.int(200, "Depth Lookback (Curr)", minval=1)
// TF1 MA
tf1 = input.bool(true, "Use Timeframe-1?")
res1 = input.timeframe("2", "TF1 Resolution")
maType1 = input.string("SMA", "MA Type (TF1)", options=["SMA","EMA","WMA","VWMA","RMA","SMMA","HullMA","TMA","DEMA","TEMA"])
maLen1 = input.int(200, "MA Length (TF1)", minval=1)
maSrc1 = input.source(close, "MA Source (TF1)")
depthLev1 = input.float(0.89, "Depth Level (TF1)", minval=0, step=0.01)
histLen1 = input.int(200, "Depth Lookback (TF1)", minval=1)
// ─── CUSTOM MA FUNCTION ──────────────────────────────────────────────────────────
f_ma(_type, _src, _len) =>
smaVal = ta.sma(_src, _len)
emaVal = ta.ema(_src, _len)
wmaVal = ta.wma(_src, _len)
vwmaVal = ta.vwma(_src, _len)
rmaVal = ta.rma(_src, _len)
smmaVal = nz(smmaVal[1], smaVal)
smmaVal := (smmaVal * (_len - 1) + _src) / _len
hullVal = ta.wma(2*ta.wma(_src, _len/2) - ta.wma(_src, _len), math.round(math.sqrt(_len)))
tmaVal = ta.sma(smaVal, _len)
demaVal = 2*emaVal - ta.ema(emaVal, _len)
temaVal = 3*(emaVal - ta.ema(emaVal, _len)) + ta.ema(ta.ema(emaVal, _len), _len)
switch _type
"SMA" => smaVal
"EMA" => emaVal
"WMA" => wmaVal
"VWMA" => vwmaVal
"RMA" => rmaVal
"SMMA" => smmaVal
"HullMA" => hullVal
"TMA" => tmaVal
"DEMA" => demaVal
"TEMA" => temaVal
=> smaVal
// ─── CALCULATE DISTANCE & DEPTH PROFILES ─────────────────────────────────────────
maC = f_ma(maTypeC, maSrcC, maLenC)
distC = math.abs(maSrcC - maC)
profC = ta.highest(distC, histLenC) * depthLevC
// TF1 values via security()
[ma1, src1] = request.security(
syminfo.tickerid, res1,
[f_ma(maType1, maSrc1, maLen1), maSrc1]
)
dist1 = math.abs(src1 - ma1)
prof1 = request.security(syminfo.tickerid, res1, ta.highest(dist1, histLen1) * depthLev1)
// ─── SIGNALS ────────────────────────────────────────────────────────────────────
isGreenC = (distC >= profC) and (maSrcC < maC)
isRedC = (distC >= profC) and (maSrcC > maC)
isGreen1 = (dist1 >= prof1) and (src1 < ma1)
isRed1 = (dist1 >= prof1) and (src1 > ma1)
greenSig = (tfC and isGreenC) or (tf1 and isGreen1)
redSig = (tfC and isRedC) or (tf1 and isRed1)
// ─── STATE TRACKING ─────────────────────────────────────────────────────────────
var bool greenActive = false
var bool redActive = false
if greenSig and not greenActive
greenActive := true
redActive := false
if redSig and not redActive
redActive := true
greenActive := false
// ─── HEIKIN ASHI TREND FILTER ──────────────────────────────────────────────────
haClose = (open + high + low + close) / 4
haOpen = nz(haOpen[1], (open+close)/2)
haOpen := (haOpen[1] + haClose[1]) / 2
haGreen = haClose > haOpen
haRed = haClose < haOpen
// ─── ENTRY CONDITIONS ───────────────────────────────────────────────────────────
longCond = greenActive and haGreen and barstate.isconfirmed and strategy.position_size == 0
shortCond = redActive and haRed and barstate.isconfirmed and strategy.position_size == 0
// ─── POSITION SIZE & EXECUTION ──────────────────────────────────────────────────
f_posSize(entry, stop) =>
d = math.abs(entry - stop)
s = d>0 ? risk_amount / d : 0
math.round(s)
if longCond
// use lowest of last lookbackBars (excluding current bar)
stopL = ta.lowest(low[1], lookbackBars)
ent = open
d = ent - stopL
if d > 0
tp = ent + d * risk_reward
qty= f_posSize(ent, stopL)
strategy.entry ( "Long", strategy.long, qty )
strategy.exit ( "L Exit","Long", stop=stopL, limit=tp )
greenActive := false
if shortCond
stopH = ta.highest(high[1], lookbackBars)
ent = open
d = stopH - ent
if d > 0
tp = ent - d * risk_reward
qty= f_posSize(ent, stopH)
strategy.entry ( "Short", strategy.short, qty )
strategy.exit ( "S Exit","Short", stop=stopH, limit=tp )
redActive := false
// ─── PLOTS & VISUALS ────────────────────────────────────────────────────────────
plotshape(longCond, title="Buy", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(shortCond, title="Sell", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
bgcolor(greenSig ? color.new(color.green, 90) : redSig ? color.new(color.red, 90) : na)
How it works:
- You choose to use Current TF and/or a second TF (TF1).
- On each TF we calculate your chosen MA, measure a “depth” threshold from recent deviation, and flag green or red breakouts.
- A Heikin-Ashi filter ensures we only enter long on HA-green bars and short on HA-red bars.
- When a signal occurs, we record it and wait for the next confirmed bar that matches the HA color.
- Stop-Loss is the extreme (low or high) of the prior lookbackBars (excluding the signal bar), giving a fixed $100 risk.
- Take-Profit = stop distance × risk_reward (3× by default ⇒ $300).
- Position size is auto-calculated so that the dollar-risk is always $100.
Drop this into TradingView’s Pine editor, save & add to a chart. Adjust inputs (MA types/lengths, risk, TF1) as needed. Let me know if you need any tweaks! 🚀