Advertisement
AMONRA75

AMON CE 2

Apr 30th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 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=longFillColor, transp=90)
  43. //fill(midPricePlot, shortStopPlot, title='Short State Filling', color=shortFillColor, transp=90)
  44. longFillColor = highlightState ? dir == 1 ? longColor : na : na
  45. shortFillColor = highlightState ? dir == -1 ? shortColor : na : na
  46. fill(midPricePlot, longStopPlot, title='Long State Filling', color=color.new(longFillColor, 90))
  47. fill(midPricePlot, shortStopPlot, title='Short State Filling', color=color.new(shortFillColor, 90))
  48.  
  49.  
  50. long_short = input.bool(true, 'Long-Short', group='Strategy Settings')
  51.  
  52. start = input.time(timestamp('2019-01-01'), 'Date', group='Strategy Settings')
  53. finish = input.time(timestamp('2025-01-01'), 'Date', group='Strategy Settings')
  54. window() =>
  55. time >= start and time <= finish ? true : false
  56.  
  57. slRatio = input.float(5, 'Stop Loss Ratio', minval=0, group='Strategy Settings')
  58. tpRatio = input.float(20, 'Take Profit Ratio', minval=0, group='Strategy Settings')
  59. tsStartRatio = input.float(10, 'Trailing Stop Start Ratio', minval=0, group='Strategy Settings')
  60. tsRatio = input.float(5, 'Trailing Stop Ratio', minval=1, group='Strategy Settings')
  61.  
  62. lastBuyPrice = strategy.position_avg_price
  63.  
  64. diffHiPriceRatio = (high - lastBuyPrice) / lastBuyPrice * 100
  65. diffLoPriceRatio = (close - lastBuyPrice) / lastBuyPrice * 100
  66. posHiRatio = 0.0
  67. posHiRatio := strategy.position_size > 0 ? diffHiPriceRatio > posHiRatio[1] ? diffHiPriceRatio : posHiRatio[1] : 0
  68.  
  69. s_diffHiPriceRatio = (low - lastBuyPrice) / lastBuyPrice * 100
  70. s_diffLoPriceRatio = (close - lastBuyPrice) / lastBuyPrice * 100
  71. s_posHiRatio = 0.0
  72. s_posHiRatio := strategy.position_size < 0 ? s_diffLoPriceRatio < s_posHiRatio[1] ? s_diffLoPriceRatio : s_posHiRatio[1] : 0
  73.  
  74. //strategy.entry('LONG', strategy.long, when=window() and buySignal)
  75. //strategy.close('LONG', when=window() and sellSignal)
  76. //strategy.close('LONG', when=diffLoPriceRatio < slRatio * -1, comment='STOP-LONG')
  77. //strategy.close('LONG', when=diffHiPriceRatio > tpRatio, comment='TAKE-PROFIT-LONG')
  78. //strategy.close('LONG', when=posHiRatio[1] > tsStartRatio and posHiRatio[1] - diffHiPriceRatio > tsRatio, comment='TRAILING-STOP-LONG')
  79.  
  80. //if long_short
  81. // strategy.entry('SHORT', strategy.short, when=window() and sellSignal)
  82. // strategy.close('SHORT', when=window() and buySignal)
  83. // strategy.close('SHORT', when=s_diffLoPriceRatio > slRatio, comment='STOP-SHORT')
  84. // strategy.close('SHORT', when=s_diffHiPriceRatio < tpRatio * -1, comment='TAKE-PROFIT-SHORT')
  85. // strategy.close('SHORT', when=s_posHiRatio[1] * -1 > tsStartRatio and (s_posHiRatio[1] - s_diffLoPriceRatio) * -1 > tsRatio, comment='TRAILING-STOP-SHORT')
  86. if window() and buySignal
  87. strategy.entry('LONG', strategy.long)
  88.  
  89. if window() and sellSignal
  90. strategy.close('LONG')
  91.  
  92. if diffLoPriceRatio < slRatio * -1
  93. strategy.close('LONG', comment='STOP-LONG')
  94.  
  95. if diffHiPriceRatio > tpRatio
  96. strategy.close('LONG', comment='TAKE-PROFIT-LONG')
  97.  
  98. if posHiRatio[1] > tsStartRatio and posHiRatio[1] - diffHiPriceRatio > tsRatio
  99. strategy.close('LONG', comment='TRAILING-STOP-LONG')
  100.  
  101. if long_short
  102. if window() and sellSignal
  103. strategy.entry('SHORT', strategy.short)
  104.  
  105. if window() and buySignal
  106. strategy.close('SHORT')
  107.  
  108. if s_diffLoPriceRatio > slRatio
  109. strategy.close('SHORT', comment='STOP-SHORT')
  110.  
  111. if s_diffHiPriceRatio < tpRatio * -1
  112. strategy.close('SHORT', comment='TAKE-PROFIT-SHORT')
  113.  
  114. if s_posHiRatio[1] * -1 > tsStartRatio and (s_posHiRatio[1] - s_diffLoPriceRatio) * -1 > tsRatio
  115. strategy.close('SHORT', comment='TRAILING-STOP-SHORT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement