AMONRA75

Chandelier Exit OK

May 5th, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © melihtuna
  3. //@version=5
  4. 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)
  5.  
  6. length = input(title='ATR Period', defval=22)
  7. mult = input.float(title='ATR Multiplier', step=0.1, defval=3.0)
  8. showLabels = input(title='Show Buy/Sell Labels ?', defval=false)
  9. useClose = input(title='Use Close Price for Extremums ?', defval=true)
  10. highlightState = input(title='Highlight State ?', defval=true)
  11.  
  12. atr = mult * ta.atr(length)
  13.  
  14. longStop = (useClose ? ta.highest(close, length) : ta.highest(length)) - atr
  15. longStopPrev = nz(longStop[1], longStop)
  16. longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
  17.  
  18. shortStop = (useClose ? ta.lowest(close, length) : ta.lowest(length)) + atr
  19. shortStopPrev = nz(shortStop[1], shortStop)
  20. shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
  21.  
  22. var int dir = 1
  23. dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir
  24.  
  25. var color longColor = color.green
  26. var color shortColor = color.red
  27.  
  28. longStopPlot = plot(dir == 1 ? longStop : na, title='Long Stop', style=plot.style_linebr, linewidth=2, color=color.new(longColor, 0))
  29. buySignal = dir == 1 and dir[1] == -1
  30. plotshape(buySignal ? longStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))
  31. 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))
  32.  
  33. shortStopPlot = plot(dir == 1 ? na : shortStop, title='Short Stop', style=plot.style_linebr, linewidth=2, color=color.new(shortColor, 0))
  34. sellSignal = dir == -1 and dir[1] == 1
  35. plotshape(sellSignal ? shortStop : na, title='Short Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(shortColor, 0))
  36. 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))
  37.  
  38. midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)
  39.  
  40. longFillColor = highlightState ? dir == 1 ? longColor : na : na
  41. shortFillColor = highlightState ? dir == -1 ? shortColor : na : na
  42. fill(midPricePlot, longStopPlot, title='Long State Filling', color=color.new(longFillColor, 90))
  43. fill(midPricePlot, shortStopPlot, title='Short State Filling', color=color.new(shortFillColor, 90))
  44.  
  45. long_short = input.bool(true, 'Long-Short', group='Strategy Settings')
  46.  
  47. start = input.time(timestamp('2019-01-01'), 'Date', group='Strategy Settings')
  48. finish = input.time(timestamp('2025-01-01'), 'Date', group='Strategy Settings')
  49. window() => time >= start and time <= finish ? true : false
  50.  
  51. slRatio = input.float(5, 'Stop Loss Ratio', minval=0, group='Strategy Settings')
  52. tpRatio = input.float(20, 'Take Profit Ratio', minval=0, group='Strategy Settings')
  53. tsStartRatio = input.float(10, 'Trailing Stop Start Ratio', minval=0, group='Strategy Settings')
  54. tsRatio = input.float(5, 'Trailing Stop Ratio', minval=1, group='Strategy Settings')
  55.  
  56. lastBuyPrice = strategy.position_avg_price
  57.  
  58. diffHiPriceRatio = (high - lastBuyPrice) / lastBuyPrice * 100
  59. posHiRatio = 0.0
  60. posHiRatio := strategy.position_size > 0 ? diffHiPriceRatio > posHiRatio[1] ? diffHiPriceRatio : posHiRatio[1] : 0
  61.  
  62. if window() and buySignal
  63. strategy.entry('LONG', strategy.long)
  64.  
  65. if diffHiPriceRatio > tpRatio
  66. strategy.close('LONG', comment='TAKE-PROFIT-LONG')
  67.  
  68. if diffHiPriceRatio < slRatio * -1
  69. strategy.close('LONG', comment='STOP-LONG')
  70.  
  71. if posHiRatio[1] > tsStartRatio and posHiRatio[1] - diffHiPriceRatio > tsRatio
  72. strategy.close('LONG', comment='TRAILING-STOP-LONG')
Add Comment
Please, Sign In to add comment