Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © melihtuna
- //@version=5
- strategy('Chandelier Exit - Strategy', shorttitle='AMONCE-STG', overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000, currency=currency.USD, commission_value=0.05, commission_type=strategy.commission.percent)
- length = input(title='ATR Period', defval=22)
- mult = input.float(title='ATR Multiplier', step=0.1, defval=3.0)
- showLabels = input(title='Show Buy/Sell Labels ?', defval=false)
- useClose = input(title='Use Close Price for Extremums ?', defval=true)
- highlightState = input(title='Highlight State ?', defval=true)
- atr = mult * ta.atr(length)
- longStop = (useClose ? ta.highest(close, length) : ta.highest(length)) - atr
- longStopPrev = nz(longStop[1], longStop)
- longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
- shortStop = (useClose ? ta.lowest(close, length) : ta.lowest(length)) + atr
- shortStopPrev = nz(shortStop[1], shortStop)
- shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
- var int dir = 1
- dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir
- var color longColor = color.green
- var color shortColor = color.red
- longStopPlot = plot(dir == 1 ? longStop : na, title='Long Stop', style=plot.style_linebr, linewidth=2, color=color.new(longColor, 0))
- buySignal = dir == 1 and dir[1] == -1
- plotshape(buySignal ? longStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))
- plotshape(buySignal and showLabels ? longStop : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(longColor, 0), textcolor=color.new(color.white, 0))
- shortStopPlot = plot(dir == 1 ? na : shortStop, title='Short Stop', style=plot.style_linebr, linewidth=2, color=color.new(shortColor, 0))
- sellSignal = dir == -1 and dir[1] == 1
- plotshape(sellSignal ? shortStop : na, title='Short Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(shortColor, 0))
- plotshape(sellSignal and showLabels ? shortStop : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(shortColor, 0), textcolor=color.new(color.white, 0))
- midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)
- longFillColor = highlightState ? dir == 1 ? longColor : na : na
- shortFillColor = highlightState ? dir == -1 ? shortColor : na : na
- fill(midPricePlot, longStopPlot, title='Long State Filling', color=color.new(longFillColor, 90))
- fill(midPricePlot, shortStopPlot, title='Short State Filling', color=color.new(shortFillColor, 90))
- long_short = input.bool(true, 'Long-Short', group='Strategy Settings')
- start = input.time(timestamp('2019-01-01'), 'Date', group='Strategy Settings')
- finish = input.time(timestamp('2025-01-01'), 'Date', group='Strategy Settings')
- window() => time >= start and time <= finish ? true : false
- slRatio = input.float(5, 'Stop Loss Ratio', minval=0, group='Strategy Settings')
- tpRatio = input.float(20, 'Take Profit Ratio', minval=0, group='Strategy Settings')
- tsStartRatio = input.float(10, 'Trailing Stop Start Ratio', minval=0, group='Strategy Settings')
- tsRatio = input.float(5, 'Trailing Stop Ratio', minval=1, group='Strategy Settings')
- lastBuyPrice = strategy.position_avg_price
- diffHiPriceRatio = (high - lastBuyPrice) / lastBuyPrice * 100
- posHiRatio = 0.0
- posHiRatio := strategy.position_size > 0 ? diffHiPriceRatio > posHiRatio[1] ? diffHiPriceRatio : posHiRatio[1] : 0
- if window() and buySignal
- strategy.entry('LONG', strategy.long)
- if diffHiPriceRatio > tpRatio
- strategy.close('LONG', comment='TAKE-PROFIT-LONG')
- if diffHiPriceRatio < slRatio * -1
- strategy.close('LONG', comment='STOP-LONG')
- if posHiRatio[1] > tsStartRatio and posHiRatio[1] - diffHiPriceRatio > tsRatio
- strategy.close('LONG', comment='TRAILING-STOP-LONG')
Add Comment
Please, Sign In to add comment