Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=6
- strategy('BTC 101 Michaels Bands Back to Basics', overlay=true)
- // Averages
- ema3 = ta.ema(close, 3)
- ema6 = ta.ema(close, 6)
- ema200 = ta.ema(close, 200)
- // Declare bandColor variable
- var color bandColor = na
- // Entry conditions
- longCondition = ema3 > ema6 and ema6 > ema200
- shortCondition = ema3 < ema6 and ema6 < ema200
- // Confirmed break away from EMA 200 by 1%
- breakAwayThreshold = 0.01
- confirmedBreakAway = math.abs(close - ema200) / ema200 > breakAwayThreshold
- // Variables to track stop loss levels
- var float highestPrice = na
- var float lowestPrice = na
- var float stopLossLevel = na
- // Trailing stop percentage input
- trailingStopPercent = input.float(2, title="Trailing Stop (in %)", minval=0.1) / 100
- // Entry logic with stop loss tracking
- if longCondition and confirmedBreakAway
- bandColor := color.green
- strategy.entry('Long', strategy.long)
- highestPrice := high
- strategy.position_size == 0
- if shortCondition and confirmedBreakAway
- bandColor := color.red
- strategy.entry('Short', strategy.short)
- lowestPrice := low
- strategy.position_size == 0
- // Update trailing stop for long positions
- if (strategy.position_size == 0)
- highestPrice := na(highestPrice) ? high : math.max(highestPrice, high)
- stopLossLevel := highestPrice * (1 - trailingStopPercent)
- strategy.exit('Trailing Stop', 'Long', stop=stopLossLevel)
- // Update trailing stop for short positions
- if (strategy.position_size == 0)
- lowestPrice := na(lowestPrice) ? low : math.min(lowestPrice, low)
- stopLossLevel := lowestPrice * (1 + trailingStopPercent)
- strategy.exit('Trailing Stop', 'Short', stop=stopLossLevel)
- // Plot stop loss level
- plot(stopLossLevel, title="Stop Loss Level", color=color.red, linewidth=2, style=plot.style_line)
- // Plots
- plot(ema3, color=bandColor, linewidth=1)
- plot(ema6, color=bandColor, linewidth=3)
- plot(ema200, color=color.blue, linewidth=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement