Advertisement
AMONRA75

chandelier work

May 9th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 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='CE-STG', overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000, currency=currency.USD, commission_value=0.03, 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=longFillColor, transp=90)
  43. fill(midPricePlot, shortStopPlot, title='Short State Filling', color=shortFillColor, transp=90)
  44.  
  45.  
  46. long_short = input.bool(true, 'Long-Short', group='Strategy Settings')
  47.  
  48. start = input.time(timestamp('2019-01-01'), 'Date', group='Strategy Settings')
  49. finish = input.time(timestamp('2025-01-01'), 'Date', group='Strategy Settings')
  50. window() =>
  51. time >= start and time <= finish ? true : false
  52.  
  53. slRatio = input.float(5, 'Manuel Stop Loss Ratio', minval=0, group='Strategy Settings')
  54. tpRatio = input.float(20, 'Take Profit Ratio', minval=0, group='Strategy Settings')
  55. tsStartRatio = input.float(10, 'Trailing Stop Start Ratio', minval=0, group='Strategy Settings')
  56. tsRatio = input.float(5, 'Trailing Stop Ratio', minval=1, group='Strategy Settings')
  57.  
  58. lastBuyPrice = strategy.position_avg_price
  59.  
  60. diffHiPriceRatio = (high - lastBuyPrice) / lastBuyPrice * 100
  61. diffLoPriceRatio = (close - lastBuyPrice) / lastBuyPrice * 100
  62. posHiRatio = 0.0
  63. posHiRatio := strategy.position_size > 0 ? diffHiPriceRatio > posHiRatio[1] ? diffHiPriceRatio : posHiRatio[1] : 0
  64.  
  65. s_diffHiPriceRatio = (low - lastBuyPrice) / lastBuyPrice * 100
  66. s_diffLoPriceRatio = (close - lastBuyPrice) / lastBuyPrice * 100
  67. s_posHiRatio = 0.0
  68. s_posHiRatio := strategy.position_size < 0 ? s_diffLoPriceRatio < s_posHiRatio[1] ? s_diffLoPriceRatio : s_posHiRatio[1] : 0
  69.  
  70. strategy.entry('LONG', strategy.long, when=window() and buySignal)
  71. strategy.close('LONG', when=window() and sellSignal)
  72. strategy.close('LONG', when=diffLoPriceRatio < slRatio * -1, comment='STOP-LONG')
  73. strategy.close('LONG', when=diffHiPriceRatio > tpRatio, comment='TAKE-PROFIT-LONG')
  74. strategy.close('LONG', when=posHiRatio[1] > tsStartRatio and posHiRatio[1] - diffHiPriceRatio > tsRatio, comment='TRAILING-STOP-LONG')
  75.  
  76. if long_short
  77. strategy.entry('SHORT', strategy.short, when=window() and sellSignal)
  78. strategy.close('SHORT', when=window() and buySignal)
  79. strategy.close('SHORT', when=s_diffLoPriceRatio > slRatio, comment='STOP-SHORT')
  80. strategy.close('SHORT', when=s_diffHiPriceRatio < tpRatio * -1, comment='TAKE-PROFIT-SHORT')
  81. strategy.close('SHORT', when=s_posHiRatio[1] * -1 > tsStartRatio and (s_posHiRatio[1] - s_diffLoPriceRatio) * -1 > tsRatio, comment='TRAILING-STOP-SHORT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement