Advertisement
sygma1982

Untitled

Apr 24th, 2023
267
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 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. // © CaLaHe
  3.  
  4. // Changelog
  5. // Thanks to LIQUID and Trade Tactics
  6. // He agregado el apalancamiento y el Filtro ADX
  7. // Preconfigurado para FXSUSDT BINANCE 15min, solo Copia y pega
  8.  
  9. // 29/03/23 @xgamerhlc
  10. // [Bug Fix] adding displacement long and short entries conditions with ichimoku cloud
  11. // [Refactor] settings, indicators groups and logic.
  12.  
  13. //@version=5
  14. strategy(title="Ichimoku, EMA200, MACD, ADX", shorttitle="Ichimoku, EMA200, MACD, ADX", overlay=true,
  15. initial_capital = 1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_value = 0.05)
  16.  
  17. showBB = input.bool(false, "Show Ichimoku Cloud", group = "General")
  18. showTrade = input.bool(false, 'Show TP/SL', group = "General")
  19.  
  20. // ------------------------------------------------------------------------------------------------------------------
  21. // INDICATORS
  22. // ------------------------------------------------------------------------------------------------------------------
  23.  
  24. // Ichimoku Calculation
  25. conversionPeriods = input.int(4, minval=1, title="Conversion Line Length", group="Ichimoku")
  26. basePeriods = input.int(4, minval=1, title="Base Line Length", group="Ichimoku")
  27. laggingSpan2Periods = input.int(50, minval=1, title="Leading Span B Length", group="Ichimoku")
  28. displacement = input.int(20, minval=1, title="Lagging Span", group="Ichimoku")
  29.  
  30. donchian(len) => math.avg(ta.lowest(len), ta.highest(len))
  31. conversionLine = donchian(conversionPeriods)
  32. baseLine = donchian(basePeriods)
  33. leadLine1 = math.avg(conversionLine, baseLine)
  34. leadLine2 = donchian(laggingSpan2Periods)
  35.  
  36. // Plot the Ichimoku Cloud
  37. plot(showBB ? conversionLine : na, color=#2962FF, title="Conversion Line")
  38. plot(showBB ? baseLine : na, color=#B71C1C, title="Base Line")
  39. plot(showBB ? close : na, offset = -displacement + 1, color=#43A047, title="Lagging Span")
  40. p1 = plot(showBB ? leadLine1 : na, offset = displacement - 1, color=#A5D6A7,
  41. title="Leading Span A")
  42. p2 = plot(showBB ? leadLine2 : na, offset = displacement - 1, color=#EF9A9A,
  43. title="Leading Span B")
  44. plot(leadLine1 > leadLine2 ? leadLine1 : leadLine2, offset = displacement - 1, title = "Kumo Cloud Upper Line", display = display.none)
  45. plot(leadLine1 < leadLine2 ? leadLine1 : leadLine2, offset = displacement - 1, title = "Kumo Cloud Lower Line", display = display.none)
  46. fill(p1, p2, color = leadLine1 > leadLine2 ? color.rgb(67, 160, 71, 90) : color.rgb(244, 67, 54, 90))
  47.  
  48. // EMA 200
  49. ema = ta.ema(close, 200)
  50. plot(showBB ? ema : na, color = color.yellow)
  51.  
  52. // MACD Calculation
  53. fast_length = input(title="Fast Length", defval=12, group="MACD")
  54. slow_length = input(title="Slow Length", defval=26, group="MACD")
  55. src = input(title="Source", defval=close, group="MACD")
  56. signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9, group="MACD")
  57. [macdLine, signalLine, histLine] = ta.macd(src, fast_length, slow_length, signal_length)
  58.  
  59. // ADX Calculation
  60. adxEnabled = input.bool(defval = true , title = "Use Average Directional Index (ADX)", tooltip = "", group ="ADX Filter" )
  61. adxlen = input(12, title="ADX Smoothing", group="ADX Filter")
  62. adxdilen = input(13, title="DI Length", group="ADX Filter")
  63. adxabove = input(24, title="ADX Threshold", group="ADX Filter")
  64.  
  65. adxdirmov(len) =>
  66. adxup = ta.change(high)
  67. adxdown = -ta.change(low)
  68. adxplusDM = na(adxup) ? na : (adxup > adxdown and adxup > 0 ? adxup : 0)
  69. adxminusDM = na(adxdown) ? na : (adxdown > adxup and adxdown > 0 ? adxdown : 0)
  70. adxtruerange = ta.rma(ta.tr, len)
  71. adxplus = fixnan(100 * ta.rma(adxplusDM, len) / adxtruerange)
  72. adxminus = fixnan(100 * ta.rma(adxminusDM, len) / adxtruerange)
  73. [adxplus, adxminus]
  74. adx(adxdilen, adxlen) =>
  75. [adxplus, adxminus] = adxdirmov(adxdilen)
  76. adxsum = adxplus + adxminus
  77. adx = 100 * ta.rma(math.abs(adxplus - adxminus) / (adxsum == 0 ? 1 : adxsum), adxlen)
  78.  
  79. adxsig = adxEnabled ? adx(adxdilen, adxlen) : na
  80. isADXEnabledAndAboveThreshold = adxEnabled ? (adxsig > adxabove) : true
  81.  
  82. // ------------------------------------------------------------------------------------------------------------------
  83. // RISK MANAGER
  84. // ------------------------------------------------------------------------------------------------------------------
  85. i_leverage = input.float(1, 'Leverage', step=.5, group='Leverage')
  86. i_percentOfEquityToTrade = input.float(100, "% of Equity to Stake Per Trade", minval=0.01, maxval=100, step=5, group='Leverage') * .01
  87. contracts = (i_percentOfEquityToTrade * strategy.equity / close * i_leverage)
  88.  
  89. import TradingView/Strategy/2 as css
  90.  
  91. //DATE RANGE FILTER {
  92. i_tradeDirection = input.string(title='Trade Direction', defval=strategy.direction.all, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short], group='Trade Filters')
  93. strategy.risk.allow_entry_in(i_tradeDirection)
  94. i_startTime = input.time(defval=timestamp('01 Jan 2012 00:00 +0000'), title='Start Time', group='Trade Filters')
  95. i_endTime = input.time(defval=timestamp('01 Jan 2099 00:00 +0000'), title='End Time', group='Trade Filters')
  96. inDateRange = time >= i_startTime and time <= i_endTime
  97. //}
  98. //SL TP PERCENT {
  99. string sltp= "STOP/TAKE PROFIT"
  100. usetpsl = input.bool(true, 'Use TP/SL', inline=sltp, group=sltp)
  101. float percentStop = input.float(2.00, "Stop %", minval = 0.0, step = 0.25, group=sltp, inline='percent')
  102. float percentTP = input.float(3.25, "Limit %", minval = 0.0, step = 0.25, group=sltp, inline='percent')
  103. //}
  104. //ALERTS {
  105. i_alert_txt_entry_long = input.text_area(defval = "", title = "Long Entry Message", group = "Alerts")
  106. i_alert_txt_entry_short = input.text_area(defval = "", title = "Short Entry Message", group = "Alerts")
  107. i_alert_txt_exit_long = input.text_area(defval = "", title = "Long Exit Message", group = "Alerts")
  108. i_alert_txt_exit_short = input.text_area(defval = "", title = "Short Exit Message", group = "Alerts")
  109. //}
  110.  
  111. // Using the input stop/ limit percent, we can convert to ticks and use the ticks to level functions.
  112. // This can be used to calculate the take profit and stop levels.
  113. float sl = css.ticksToStopLevel (css.percentToTicks (percentStop))
  114. float tp = css.ticksToTpLevel (css.percentToTicks (percentTP))
  115.  
  116. exitPrice = strategy.closedtrades.exit_price(strategy.closedtrades-1)
  117. bias = math.sign(strategy.position_size)
  118. avg = strategy.position_avg_price
  119.  
  120. // Conditions used to reference position and determine trade bias
  121. bool long = strategy.position_size > 0
  122. bool short = strategy.position_size < 0
  123. bool enterLong = long and not long [1]
  124. bool enterShort = short and not short[1]
  125. bool enter = enterLong or enterShort
  126. bool exit = strategy.position_size == 0 and not (strategy.position_size == 0)[1]
  127. bool flat = strategy.position_size == 0
  128.  
  129. //STRATEGY PLOTS {
  130. avgerage = plot(showTrade ? avg : na, "ENTRY", not enter ? color.new(color.white, 0) : na, 2, plot.style_linebr)
  131. slp = plot(showTrade ? sl : na, "STOP LOSS", not enter ? color.new(color.red, 60) : na, 4, plot.style_linebr)
  132. tpp = plot(showTrade ? tp : na, "TAKE PROFIT", not enter ? color.new(color.green, 60) : na, 4, plot.style_linebr)
  133.  
  134. fill(tpp, avgerage, color = color.new(color.green, 80), title='TP Background')
  135. fill(avgerage, slp, color = color.new(color.red, 80) , title= 'SL Background')
  136. //}
  137.  
  138. if usetpsl == true and long
  139. css.exitPercent("exit long", percentStop, percentTP, alertMessage=i_alert_txt_exit_long)
  140.  
  141. if usetpsl == true and short
  142. css.exitPercent("exit short", percentStop, percentTP, alertMessage=i_alert_txt_exit_short)
  143.  
  144. // ------------------------------------------------------------------------------------------------------------------
  145. // STRATEGY
  146. // ------------------------------------------------------------------------------------------------------------------
  147. longConditionCalc =
  148. (close > ema) and
  149. (close > leadLine1[displacement - 1]) and (close > leadLine2[displacement - 1]) and (close > conversionLine) and (close > baseLine) and
  150. macdLine > signalLine and isADXEnabledAndAboveThreshold
  151. shortConditionCalc =
  152. (close < ema) and
  153. (close < leadLine1[displacement - 1]) and (close < leadLine2[displacement - 1]) and (close < conversionLine) and (close < baseLine) and
  154. macdLine < signalLine and isADXEnabledAndAboveThreshold
  155.  
  156. if longConditionCalc and inDateRange
  157. strategy.close("SHORT", "SX" , alert_message=i_alert_txt_exit_short)
  158. strategy.entry("LONG", strategy.long, qty=contracts, alert_message=i_alert_txt_entry_long)
  159.  
  160. if shortConditionCalc and inDateRange
  161. strategy.close("LONG", "LX", alert_message=i_alert_txt_exit_long)
  162. strategy.entry("SHORT", strategy.short, qty=contracts, alert_message=i_alert_txt_entry_short)
  163.  
  164. if shortConditionCalc or (strategy.position_size > 0) and (close < leadLine1[displacement - 1])
  165. strategy.close("Long", "Close Long", alert_message=i_alert_txt_exit_long)
  166. if longConditionCalc or (strategy.position_size < 0) and (close > leadLine1[displacement - 1])
  167. strategy.close("Short", "Close Short", alert_message=i_alert_txt_exit_short)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement