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 close > ema200
- shortCondition = ema3 < ema6 and close < 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
- var line stopLossLine = na
- // Entry logic with stop loss tracking
- if longCondition and confirmedBreakAway
- bandColor := color.green
- strategy.entry('Long', strategy.long)
- highestPrice := close
- stopLossLevel := highestPrice * 0.98
- strategy.exit("Long Stop", "Long", stop=stopLossLevel)
- if not na(stopLossLine)
- line.delete(stopLossLine)
- stopLossLine := line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=2)
- if shortCondition and confirmedBreakAway
- bandColor := color.red
- strategy.entry('Short', strategy.short)
- lowestPrice := close
- stopLossLevel := lowestPrice * 1.02
- strategy.exit("Short Stop", "Short", stop=stopLossLevel)
- if not na(stopLossLine)
- line.delete(stopLossLine)
- stopLossLine := line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=2)
- // Update stop loss for existing positions
- if strategy.position_size > 0
- highestPrice := na(highestPrice) ? close : math.max(highestPrice, close)
- if close > highestPrice
- highestPrice := close
- stopLossLevel := highestPrice * 0.98
- strategy.exit("Long Stop", "Long", stop=stopLossLevel)
- if not na(stopLossLine)
- line.delete(stopLossLine)
- stopLossLine := line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=2)
- if strategy.position_size < 0
- lowestPrice := na(lowestPrice) ? close : math.min(lowestPrice, close)
- if close < lowestPrice
- lowestPrice := close
- stopLossLevel := lowestPrice * 1.02
- strategy.exit("Short Stop", "Short", stop=stopLossLevel)
- if not na(stopLossLine)
- line.delete(stopLossLine)
- stopLossLine := line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=2)
- // 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