xmd79

TCS • TP/SL ATR+HL

May 29th, 2023
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.69 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. // © zendrer
  3.  
  4. //@version=5
  5. // One of the most famous SL or TP indicator developed on 3 levels, both long and short, to semplify the visualization of the target or multiple targets.
  6.  
  7. indicator(title="TCS • TP/SL ATR+HL", shorttitle= "TCS•TP/SL", overlay=true)
  8.  
  9. //•••••••••••••
  10. //INPUT
  11. //•••••••••••••
  12. //ATR + LEVELS
  13. //----------------------------------------------------------------
  14. type = input.string(title='SL/TP', defval='ATR', options=['ATR', 'H-L'], group = "TP/SL")
  15. atrlen = input(14, 'ATR•LEN', group = "ATR TP/SL")
  16. smoothing = input.string(title='ATR•Smoothing', defval='WMA', options=['RMA', 'SMA', 'EMA', 'WMA'], group = "ATR TP/SL")
  17. lines = input(true, "SHOW LEVELS", group = "ATR TP/SL")
  18. bands = input(true, "SHOW BANDS", group = "ATR TP/SL")
  19. labels = input(true, "SHOW LABELS", group = "ATR TP/SL")
  20. SSLx = input(true, "", inline="1", group = "ATR TP/SL")
  21. SSL = input.float(2.01, 'SHORT SL•Mlt', step=0.1, inline="1", group = "ATR TP/SL")
  22. LSLx = input(true, "", inline="2", group = "ATR TP/SL")
  23. LSL = input.float(2.01, 'LONG SL•Mlt', step=0.1, inline="2", group = "ATR TP/SL")
  24. LTPx = input(true, "", inline="3", group = "ATR TP/SL")
  25. LTP = input.float(2, 'LONG TP•Mlt 1', step=0.1, inline="3", group = "ATR TP/SL")
  26. LTP1x = input(false, "", inline="4", group = "ATR TP/SL")
  27. LTP1 = input.float(3, 'LONG TP•Mlt 2', step=0.1, inline="4", group = "ATR TP/SL")
  28. LTP2x = input(false, "", inline="5", group = "ATR TP/SL")
  29. LTP2 = input.float(4, 'LONG TP•Mlt 3', step=0.1, inline="5", group = "ATR TP/SL")
  30. STPx = input(true, "", inline="6", group = "ATR TP/SL")
  31. STP = input.float(2, 'SHORT TP•Mlt 1', step=0.1, inline="6", group = "ATR TP/SL")
  32. STP1x = input(false, "", inline="7", group = "ATR TP/SL")
  33. STP1 = input.float(3, 'SHORT TP•Mlt 2', step=0.1, inline="7", group = "ATR TP/SL")
  34. STP2x = input(false, "", inline="8", group = "ATR TP/SL")
  35. STP2 = input.float(4, 'SHORT TP•Mlt 3', step=0.1, inline="8", group = "ATR TP/SL")
  36. //----------------------------------------------------------------
  37.  
  38. //----------------------------------------------------------------
  39. // STRUCTURE H/L
  40. //----------------------------------------------------------------
  41. lB = input(20, title = "LOOKBACK", group = "HIGH-LOW TP/SL")
  42. hx = input(true, "HIGH", group = "HIGH-LOW TP/SL")
  43. lx = input(true, "LOW", group = "HIGH-LOW TP/SL")
  44. rB = 1
  45. //----------------------------------------------------------------
  46.  
  47. //----------------------------------------------------------------
  48. //CRYPTO RISK CALC
  49. //----------------------------------------------------------------
  50. risk = input(3,"ACCOUNT RISK % ", group = "ACCOUNT RISK - CRYPTO")
  51. account = input(10000,"ACCOUNT AMOUNT ", group = "ACCOUNT RISK - CRYPTO")
  52. fees = input(0.07,"BROKER FEES ENTRY %", group = "ACCOUNT RISK - CRYPTO")
  53. feesex = input(0.07,"BROKER FEES EXIT %", group = "ACCOUNT RISK - CRYPTO")
  54. //----------------------------------------------------------------
  55.  
  56. //•••••••••••••
  57. //FUNCTION
  58. //•••••••••••••
  59. //ATR
  60. //----------------------------------------------------------------
  61. ma_function(source, atrlen) =>
  62. if smoothing == 'RMA'
  63. ta.rma(source, atrlen)
  64. else
  65. if smoothing == 'SMA'
  66. ta.sma(source, atrlen)
  67. else
  68. if smoothing == 'EMA'
  69. ta.ema(source, atrlen)
  70. else
  71. ta.wma(source, atrlen)
  72. atr_slen = ma_function(ta.tr(true), atrlen)
  73. //----------------------------------------------------------------
  74.  
  75. //ATR Up/Low Bands
  76. //----------------------------------------------------------------
  77. xLTP= LSL*LTP
  78. xLTP1= LSL*LTP1
  79. xLTP2= LSL*LTP2
  80. xSTP= SSL*STP
  81. xSTP1= SSL*STP1
  82. xSTP2= SSL*STP2
  83.  
  84. SL_L=close - atr_slen * LSL
  85. SL_S= atr_slen * SSL + close
  86.  
  87. TP_L1= atr_slen * xLTP + close
  88. TP_L2= atr_slen * xLTP1 + close
  89. TP_L3= atr_slen * xLTP2 + close
  90.  
  91. TP_S1= close - atr_slen * xSTP
  92. TP_S2= close - atr_slen * xSTP1
  93. TP_S3= close - atr_slen * xSTP2
  94. //----------------------------------------------------------------
  95.  
  96. //HIGH-LOW
  97. //----------------------------------------------------------------
  98. hP = fixnan(ta.pivothigh(lB, rB)[1])
  99. lP = fixnan(ta.pivotlow(lB, rB)[1])
  100. //----------------------------------------------------------------
  101.  
  102. //SIZE CALC LONG
  103. //----------------------------------------------------------------
  104. changesl = (((close/SL_L)-1)*100)+fees
  105. accval = risk*account
  106. relative = (changesl*accval)/risk
  107. accquant = accval/relative
  108. //----------------------------------------------------------------
  109. //SIZE CALC SHORT
  110. //----------------------------------------------------------------
  111. changesl1 = (((SL_S/close)-1)*100)+fees
  112. accval1 = risk*account
  113. relative1 = (changesl1*accval1)/risk
  114. accquant1 = accval1/relative1
  115. //----------------------------------------------------------------
  116.  
  117.  
  118. //•••••••••••••
  119. //PLOT
  120. //•••••••••••••
  121. //BANDS
  122. //----------------------------------------------------------------
  123. u = plot(SL_L, 'LONG SL•Mlt', color = type == 'ATR' ?(bands ? (LSLx ? color.new(color.white, 40) : na) : na) :na , style=plot.style_stepline)
  124. l = plot(SL_S, 'SHORT SL•Mlt', color = type == 'ATR' ?(bands ? (SSLx ? color.new(color.white, 40) : na) : na) :na , style=plot.style_stepline)
  125. u1 = plot(TP_L1, 'LONG TP•Mlt 1', color = type == 'ATR' ?(bands ? (LTPx ? color.new(color.lime, 20) : na) : na) :na , style=plot.style_stepline)
  126. u2 = plot(TP_L2, 'LONG TP•Mlt 2', color = type == 'ATR' ?(bands ? (LTP1x ? color.new(color.lime, 40) : na) : na) :na , style=plot.style_stepline)
  127. u3 = plot(TP_L3, 'LONG TP•Mlt 3', color = type == 'ATR' ?(bands ? (LTP2x ? color.new(color.lime, 60) : na) : na) :na , style=plot.style_stepline)
  128. l1 = plot(TP_S1, 'SHORT TP•Mlt 1', color = type == 'ATR' ?(bands ? (STPx ? color.new(color.purple, 20) : na) : na) :na , style=plot.style_stepline)
  129. l2 = plot(TP_S2, 'SHORT TP•Mlt 2', color = type == 'ATR' ?(bands ? (STP1x ? color.new(color.purple, 40) : na) : na) :na , style=plot.style_stepline)
  130. l3 = plot(TP_S3, 'SHORT TP•Mlt 3', color = type == 'ATR' ?(bands ? (STP2x ? color.new(color.purple, 60) : na) : na) :na , style=plot.style_stepline)
  131. //----------------------------------------------------------------
  132.  
  133. //PLOT HL
  134. //----------------------------------------------------------------
  135. Hl = type == 'H-L' ? line.new(bar_index+1,hP,bar_index+9,hP, xloc = xloc.bar_index, extend = extend.none, color = lines ? ( hx ? color.new(color.lime, 0) :na) : na, style = line.style_dashed) : na
  136. Hll = type == 'H-L' ? (labels and hx ? label.new(bar_index+9, hP, color = color.new(color.white,100) , style = label.style_label_left, text="H-SL | "+str.tostring(hP), textcolor = color.lime, textalign = text.align_left) : na) : na
  137. line.delete(Hl[1])
  138. label.delete(Hll[1])
  139.  
  140. Ll = type == 'H-L' ? line.new(bar_index+1,lP,bar_index+9,lP, xloc = xloc.bar_index, extend = extend.none, color = lines ? ( lx ? color.new(color.purple, 0) :na) : na, style = line.style_dashed) : na
  141. Lll = type == 'H-L' ? (labels and lx ? label.new(bar_index+9, lP, color = color.new(color.white,100) , style = label.style_label_left, text="L-SL | "+str.tostring(lP), textcolor = color.purple, textalign = text.align_left) : na) : na
  142. line.delete(Ll[1])
  143. label.delete(Lll[1])
  144. //plot(hP, color=ta.change(hP) ? na : color.lime, linewidth=1, offset=-(rB+1), title="HIGH",style=plot.style_line)
  145. //plot(lP, color=ta.change(lP) ? na : color.lime, linewidth=1, offset=-(rB+1), title="LOW",style=plot.style_line)
  146. //----------------------------------------------------------------
  147. //-----
  148. //LINE
  149. //----------------------------------------------------------------
  150. ul = type == 'ATR' ? line.new(bar_index+1,SL_L[1],bar_index+9,SL_L[1], xloc = xloc.bar_index, extend = extend.none, color = lines ? (LSLx ? color.new(color.white, 40) : na) : na, style = line.style_dashed) : na
  151. ull = type == 'ATR' ? (labels and LSLx ? label.new(bar_index+9, SL_L[1], color = color.new(color.white,100) , style = label.style_label_left, text="L-SL | "+str.tostring(LSL[1])+" | "+str.tostring(SL_L[1]), textcolor = color.gray, textalign = text.align_left) : na) : na
  152. line.delete(ul[1])
  153. label.delete(ull[1])
  154.  
  155. ll = type == 'ATR' ? line.new(bar_index+1,SL_S[1],bar_index+9,SL_S[1], xloc = xloc.bar_index, extend = extend.none, color = lines ? (SSLx ? color.new(color.white, 40) : na) : na, style = line.style_dashed) : na
  156. lll = type == 'ATR' ? (labels and SSLx ? label.new(bar_index+9, SL_S[1], color =color.new(color.white,100), style = label.style_label_left, text="S-SL | "+str.tostring(SSL[1])+" | "+str.tostring(SL_S[1]), textcolor = color.gray, textalign = text.align_left) : na) : na
  157. line.delete(ll[1])
  158. label.delete(lll[1])
  159.  
  160. u1l = type == 'ATR' ? line.new(bar_index+1,TP_L1[1],bar_index+9,TP_L1[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (LTPx ? color.new(color.lime, 20) : na) : na, style = line.style_dashed) : na
  161. u1ll = type == 'ATR' ? (labels and LTPx ? label.new(bar_index+9, TP_L1[1], color =color.new(color.white,100), style = label.style_label_left, text="L-TP1 | "+str.tostring(LTP[1])+" | "+str.tostring(TP_L1[1]), textcolor = color.new(color.lime, 20), textalign = text.align_left) : na) : na
  162. line.delete(u1l[1])
  163. label.delete(u1ll[1])
  164.  
  165. u2l = type == 'ATR' ? line.new(bar_index+1,TP_L2[1],bar_index+9,TP_L2[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (LTP1x ? color.new(color.lime, 40) : na) : na, style = line.style_dashed) : na
  166. u2ll = type == 'ATR' ? (labels and LTP1x ? label.new(bar_index+9, TP_L2[1], color =color.new(color.white,100), style = label.style_label_left, text="L-TP2 | "+str.tostring(LTP1[1])+" | "+str.tostring(TP_L2[1]), textcolor = color.new(color.lime, 20), textalign = text.align_left) : na) : na
  167. line.delete(u2l[1])
  168. label.delete(u2ll[1])
  169.  
  170. u3l = type == 'ATR' ? line.new(bar_index+1,TP_L3[1],bar_index+9,TP_L3[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (LTP2x ? color.new(color.lime, 60) : na) : na, style = line.style_dashed) : na
  171. u3ll = type == 'ATR' ? (labels and LTP2x ? label.new(bar_index+9, TP_L3[1], color =color.new(color.white,100), style = label.style_label_left, text="L-TP3 | "+str.tostring(LTP2[1])+" | "+str.tostring(TP_L3[1]), textcolor = color.new(color.lime, 20), textalign = text.align_left) : na) : na
  172. line.delete(u3l[1])
  173. label.delete(u3ll[1])
  174.  
  175. l1l = type == 'ATR' ? line.new(bar_index+1,TP_S1[1],bar_index+9,TP_S1[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (STPx ? color.new(color.purple, 20) : na) : na, style = line.style_dashed) : na
  176. l1ll = type == 'ATR' ? (labels and STPx ? label.new(bar_index+9, TP_S1[1], color =color.new(color.white,100), style = label.style_label_left, text="S-TP1 | "+str.tostring(STP[1])+" | "+str.tostring(TP_S1[1]), textcolor = color.new(color.purple, 20), textalign = text.align_left) : na) : na
  177. line.delete(l1l[1])
  178. label.delete(l1ll[1])
  179.  
  180. l2l = type == 'ATR' ? line.new(bar_index+1,TP_S2[1],bar_index+9,TP_S2[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (STP1x ? color.new(color.purple, 40) : na) : na, style = line.style_dashed) : na
  181. l2ll = type == 'ATR' ? (labels and STP1x ? label.new(bar_index+9, TP_S2[1], color =color.new(color.white,100), style = label.style_label_left, text="S-TP2 | "+str.tostring(STP1[1])+" | "+str.tostring(TP_S2[1]), textcolor = color.new(color.purple, 40), textalign = text.align_left) : na) : na
  182. line.delete(l2l[1])
  183. label.delete(l2ll[1])
  184.  
  185. l3l = type == 'ATR' ? line.new(bar_index+1,TP_S3[1],bar_index+9,TP_S3[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (STP2x ? color.new(color.purple, 60) : na) : na, style = line.style_dashed) : na
  186. l3ll = type == 'ATR' ? (labels and STP2x ? label.new(bar_index+9, TP_S3[1], color =color.new(color.white,100), style = label.style_label_left, text="S-TP3 | "+str.tostring(STP2[1])+" | "+str.tostring(TP_S3[1]), textcolor = color.new(color.purple, 60), textalign = text.align_left) : na) : na
  187. line.delete(l3l[1])
  188. label.delete(l3ll[1])
  189.  
  190. accqty = type == 'ATR' ? (labels and (LSLx or SSLx) ? label.new(bar_index+9, close[1], color =color.new(color.white,100), style = label.style_label_left, text="QTY x "+ "|LONG|" +str.tostring(accquant,"#.##") + " • |SHORT|" +str.tostring(accquant1,"#.##"), textcolor = color.new(color.white, 20), textalign = text.align_left) : na) : na
  191. label.delete(accqty[1])
  192.  
  193. //accqty1 = type == 'ATR' ? (labels and (LSLx or SSLx) ? label.new(bar_index+9, close[1], color =color.new(color.white,100), style = label.style_label_left, text="QTY. SHORT | "+ " x " +str.tostring(accquant1), textcolor = color.new(color.white, 20), textalign = text.align_left) : na) : na
  194. //label.delete(accqty1[1])
Add Comment
Please, Sign In to add comment