Advertisement
xmd79

Knight of success

Jan 11th, 2023
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.55 KB | None | 0 0
  1. // این اندیکاتور توسط ئویا تسلیمی تغییر یافته است و کابرد آن برای استاپ لاست و سینگنال در تایم فرم 4 ساعت میباشد "setup.system.60@gmail.com"
  2. //
  3. //@version=5
  4. indicator('Knight of success', overlay=true) // These had to be taken out as they're not compatible with plotting a table... , timeframe="", timeframe_gaps=false
  5.  
  6. // Inputs
  7. shortlen = input.int(9, "Short MA Length", minval=1)
  8. longlen = input.int(30, "Long MA Length", minval=1)
  9.  
  10. short = ta.sma(close, shortlen)
  11. long = ta.sma(close, longlen)
  12. plot(short, color = color.rgb(26, 218, 20))
  13. plot(long, color = #f80000)
  14. plot(ta.cross(short, long) ? short : na, color=#2962FF, style = plot.style_cross, linewidth = 4)
  15.  
  16.  
  17. atrPeriod = input.int(title='ATR Period', defval=40, minval=1, group="ATR Bands Standard Settings", tooltip="This setting is used in the raw ATR value calculation. Lower values will be more reactive to recent price movement, while higher values will better indicate loger-term trend.\n\n" +
  18. "Most often this is set at either 14 or 21.\nDefault: 3")
  19.  
  20. // While it seemed like a nice idea at the time, having separately-configurable upper and lower bands just doesn't really seem that useful as 90% of the time the settings for both are the same.
  21. // Therefore, We're going to simplify the config to make these settings unified for both bands, as it would otherwise just add even more confusion with the addition of take-profit bands as well...
  22. //
  23. // atrMultiplierUpper = input.float(title='ATR Upper Band Scale Factor', defval=2.5, step=0.1, minval=0.01, group="ATR Upper Band Settings", tooltip="Scaling factor (aka multiplier) for the ATR to use for plotting the ATR bands. " +
  24. // "This will usually be between 1 and 3.")
  25. // srcUpper = input.source(title='ATR Upper Offset Source', defval=close, group="ATR Upper Band Settings", tooltip="This setting determines the offset point for ATR bands. " +
  26. // "For this band, 'high' and 'close' (default) are generally the most appropriate values.")
  27. //
  28. atrMultiplier = input.float(title='ATR Band Scale Factor', defval=4, step=0.1, minval=0.01, group="ATR Bands Standard Settings", tooltip="Scaling factor (aka multiplier) for the ATR to use for plotting the ATR bands. " +
  29. "This will usually be between 1 and 3.\n\nDefault: 2.5")
  30. // On second thought, I'm going to nix this setting and force it to be the "close" source. Having the ability to offset based on the wicks was a nice idea, but doesn't really seem to have any notable practical application.
  31. atrSourceRef = "close"
  32. //atrSourceRef = input.string(title='ATR Upper Offset Source', defval="close", options=["close","wicks"], group="ATR Bands Standard Settings", tooltip="This setting determines the offset point for ATR bands. " +
  33. // "The default value 'close' should be your go-to, but 'wicks' might provide a bit more breathing room in securities that tend to have large wicks.")
  34. //
  35. // See above - these are deprecated and no longer used...
  36. //
  37. // atrMultiplierLower = input.float(title='ATR Lower Band Scale Factor', defval=2.5, step=0.1, minval=0.01, group="ATR Lower Band Settings", tooltip="Scaling factor (aka multiplier) for the ATR to use for plotting the ATR bands. " +
  38. // "This will usually be between 1 and 3.")
  39. // srcLower = input.source(title='ATR Lower Offset Source', defval=close, group="ATR Lower Band Settings", tooltip="This setting determines the offset point for ATR bands. " +
  40. // "For this band, 'low' and 'close' (default) are generally the most appropriate values.")
  41. //
  42. //
  43. // Take-Profit band settings
  44. showTPBands = input.bool(title="Show opposite bands for take-profit zones", defval=false, tooltip="If enalbled, the existing ATR bands will be treated as 'stop-loss' bands, and 'take-profit' bands will be plotted " +
  45. "to depict potential take-profit targets that are scaled based on the 'stop-loss' band and an additional reward/risk scaling factor (see below).\n\nDefault: Unchecked", group="Take-Profit Settings")
  46. tpScaleFactor = input.float(title="Take-Profit Scale Factor", defval=1.5, minval=1, step=0.1, tooltip="This is a secondary scaling factor used based on the 'stop-loss' ATR bands to calculate and plot a potential take-profit target.\n\n" +
  47. "The easiest way to think of this is as a desired reward/risk ratio, where the primary ATR Bands represent the risk/stop-loss.\n\nDefault: 1.5")
  48. //
  49. //
  50. // As an added bonus, give the option to plot a table containing exact figures for all of the bands...
  51. // Functional settings
  52. showTable = input.bool(title="Show Table for Stops and Targets", defval=false, group="Table Settings", tooltip="If enabled, a table will be placed on-chart showing exact values for both stop bands, as well as optional take-profit bands/targets.\n\n" +
  53. "Note: Take-profit values are based on the 'Take-Profit Scale Factor' setting above.\n\nDefault: Unchecked")
  54. allowTableRepainting = input.bool(title="Allow Table Repainting", defval=false, group="Table Settings", tooltip="If enabled, table data will show real-time values, which will inherently repaint. This may be desirable for people preparing to enter prior " +
  55. "to candle close, but should be used with extreme caution.\n\nDefault: Unchecked")
  56. showTPinTable = input.bool(title="Include additional rows/columns to display take-profit values.", defval=false, group="Table Settings", tooltip="If enabled, additional table rows/columns will be drawn and populated with take-profit band/target values.\n\n" +
  57. "Note: Take-profit values are based on the 'Take-Profit Scale Factor' setting above.\n\nDefault: Unchecked")
  58. //
  59. // Display settings
  60. alignTableVertically = input.bool(title="Align Table Vertically", defval=true, group="Table Settings", tooltip="If enabled, the table will be re-aligned to display vertically (headers to the left) instead of horizontally (headers on top).\n\nDefault: Checked")
  61. tablePosition = input.string(title="Table Location", defval="Bottom Right", options=["Top Right","Top Left","Top Center","Middle Right","Middle Left","Middle Center","Bottom Right","Bottom Left","Bottom Center"], group="Table Settings",
  62. tooltip='This setting controls the position on the chart where the table will be located.\n\nDefault: Bottom Right')
  63. tableColor = input.color(title="Table Color: ", defval=color.rgb(0, 175, 200, 20), group="Table Settings", inline="A")
  64. tableTextHeaderColor = input.color(title="Table Header Color: ", defval=color.rgb(255, 255, 0, 0), group="Table Settings", tooltip="These settings determine the colors used for the table cell borders/outlines, and the text inside the table cells used as data headers.", inline="A")
  65. tableTextColor = input.color(title="Table Text Color: ", defval=color.rgb(255, 255, 255, 0), group="Table Settings", tooltip="This setting determines the color used for the text inside the table cells.", inline="B")
  66. // tableTooltipColor = input.color(title="Table Tooltip Color: ", defval=color.rgb(255, 75, 255, 0), group="Table Display Settings", tooltip="This setting determines the color used for any cell tooltips.") // Not used
  67. tableLongBGColor = input.color(title="Table Background Color - Long: ", defval=color.rgb(0, 255, 0, 90), group="Table Settings", inline="C")
  68. tableShortBGColor = input.color(title="Short: ", defval=color.rgb(255, 0, 0, 80), group="Table Settings", tooltip="These settings determine the background fill colors used for long/short position info.", inline="C")
  69.  
  70.  
  71. // Functions
  72. //
  73. // Function to convert the input "source" to a proper "source"
  74. getBandOffsetSource(srcIn, isUpperBand) =>
  75. // Initialize the return to our fail-safe 'close', which is also the default input, then update thru the switch statement
  76. ret = close
  77. switch srcIn
  78. "close" => ret := close
  79. "wicks" => ret := isUpperBand ? high : low
  80. => ret := close
  81. ret
  82. //
  83. // Function to convert table position input to a an appropriate argument
  84. getTablePosition(posIn) =>
  85. posOut = position.bottom_right
  86. switch (posIn)
  87. "Top Right" => posOut := position.top_right
  88. "Top Left" => posOut := position.top_left
  89. "Top Center" => posOut := position.top_center
  90. "Middle Right" => posOut := position.middle_right
  91. "Middle Left" => posOut := position.middle_left
  92. "Middle Center" => posOut := position.middle_center
  93. "Bottom Right" => posOut := position.bottom_right
  94. "Bottom Left" => posOut := position.bottom_left
  95. "Bottom Center" => posOut := position.bottom_center
  96. => posOut := position.bottom_right
  97. posOut
  98.  
  99. // ATR
  100. atr = ta.atr(atrPeriod)
  101. scaledATR = atr * atrMultiplier
  102. upperATRBand = getBandOffsetSource(atrSourceRef, true) + scaledATR
  103. lowerATRBand = getBandOffsetSource(atrSourceRef, false) - scaledATR
  104. //
  105. // Since we can calcualte ATR bands based on either close or wicks, we need to be sure to normalize the true distance
  106. // from the close to the "stop band" before we can then apply our take-profit scaler and calculate the TP bands...
  107. scaledTPLong = close + ((close - lowerATRBand) * tpScaleFactor)
  108. scaledTPShort = close - ((upperATRBand - close) * tpScaleFactor)
  109.  
  110. // OG ATR Band Plotting
  111. plot(upperATRBand, title="Upper ATR Band", color=color.rgb(0, 255, 0, 50), linewidth=2)
  112. plot(lowerATRBand, title="Lower ATR Band", color=color.rgb(255, 0, 0, 50), linewidth=2)
  113.  
  114. // TP band plots
  115. plot(showTPBands ? scaledTPLong : na, title="Upper Take-Profit Band", color=color.rgb(255, 255, 255, 80), linewidth=1)
  116. plot(showTPBands ? scaledTPShort : na, title="Lower Take-Profit Band", color=color.rgb(255, 255, 0, 80), linewidth=1)
  117.  
  118. // ATR and TP table...
  119. if (showTable)
  120. // It's nice that TV will automagically shrink/reposition table cells to not have gaps if a specific row/column are missing,
  121. // so we can define the table to the max number of rows/columns possible for this indicator in any configuration and let TV handle the "shrinking".
  122. var atrTable = table.new(position=getTablePosition(tablePosition), columns=8, rows=8)
  123. //
  124. // Set the base table styles...
  125. table.set_border_width(atrTable, 1)
  126. table.set_frame_width(atrTable, 1)
  127. table.set_border_color(atrTable, tableColor)
  128. table.set_frame_color(atrTable, tableColor)
  129. //
  130. // Since we're giving the option to display the table with 2 different formats (horizontal vs vertical), we need to build out both variations and
  131. // incorporate a method to switch from one to the other based on the 'alignTableVertically' user input setting. While we probably COULD do
  132. // conditional logic inside the 'table.cell' functions, it will be far more intuitive to "read" if we simply break it into an 'if-else' clause.
  133. //
  134. // While this WILL result in a pretty notable duplication of code, it's acceptable in this case as we have a finite number of options (2).
  135. //
  136. // Vertical orientation
  137. if (alignTableVertically)
  138. // Define the Title/Header cells
  139. table.cell(atrTable, 0, 0, text="Long ATR Stop", text_color=tableTextHeaderColor, bgcolor=tableLongBGColor, tooltip="Test")
  140. table.cell(atrTable, 0, 1, text="Long ATR Stop Dist", text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
  141. if (showTPinTable)
  142. table.cell(atrTable, 0, 2, text="Long ATR TP", text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
  143. // If the TP scale factor is exactly 1, we can nix the TP distance columns as it will be exactly the same as the stop distance.
  144. if (tpScaleFactor != 1)
  145. table.cell(atrTable, 0, 3, text="Long ATR TP Dist", text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
  146. table.cell(atrTable, 0, 4, text="Short ATR Stop", text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
  147. table.cell(atrTable, 0, 5, text="Short ATR Stop Dist", text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
  148. if (showTPinTable)
  149. table.cell(atrTable, 0, 6, text="Short ATR TP", text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
  150. // If the TP scale factor is exactly 1, we can nix the TP distance columns as it will be exactly the same as the stop distance.
  151. if (tpScaleFactor != 1)
  152. table.cell(atrTable, 0, 7, text="Short ATR TP Dist", text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
  153. //
  154. // Now for table values for each header...
  155. // Start with Long position...
  156. table.cell(atrTable, 1, 0, text=str.tostring(allowTableRepainting ? lowerATRBand : lowerATRBand[1], format.mintick), text_color=tableTextColor, bgcolor=tableLongBGColor, tooltip="Test")
  157. table.cell(atrTable, 1, 1, text=str.tostring(math.round_to_mintick(allowTableRepainting ? close - lowerATRBand : close[1] - lowerATRBand[1])), text_color=tableTextColor, bgcolor=tableLongBGColor)
  158. if (showTPinTable)
  159. table.cell(atrTable, 1, 2, text=str.tostring(allowTableRepainting ? scaledTPLong : scaledTPLong[1], format.mintick), text_color=tableTextColor, bgcolor=tableLongBGColor)
  160. // If the TP scale factor is exactly 1, we can nix the TP distance columns as it will be exactly the same as the stop distance.
  161. if (tpScaleFactor != 1)
  162. table.cell(atrTable, 1, 3, text=str.tostring(math.round_to_mintick(allowTableRepainting ? scaledATR * tpScaleFactor : scaledATR[1] * tpScaleFactor)), text_color=tableTextColor, bgcolor=tableLongBGColor)
  163. // Now the Short position...
  164. table.cell(atrTable, 1, 4, text=str.tostring(allowTableRepainting ? upperATRBand : upperATRBand[1], format.mintick), text_color=tableTextColor, bgcolor=tableShortBGColor, tooltip="Test 2")
  165. table.cell(atrTable, 1, 5, text=str.tostring(math.round_to_mintick(allowTableRepainting ? upperATRBand - close : upperATRBand[1] - close[1])), text_color=tableTextColor, bgcolor=tableShortBGColor)
  166. if (showTPinTable)
  167. table.cell(atrTable, 1, 6, text=str.tostring(allowTableRepainting ? scaledTPShort : scaledTPShort[1], format.mintick), text_color=tableTextColor, bgcolor=tableShortBGColor)
  168. // If the TP scale factor is exactly 1, we can nix the TP distance columns as it will be exactly the same as the stop distance.
  169. if (tpScaleFactor != 1)
  170. table.cell(atrTable, 1, 7, text=str.tostring(math.round_to_mintick(allowTableRepainting ? scaledATR * tpScaleFactor : scaledATR[1] * tpScaleFactor)), text_color=tableTextColor, bgcolor=tableShortBGColor)
  171.  
  172.  
  173.  
  174. //
  175. // Horizontal orientation
  176. else
  177. // Define the Title/Header cells
  178. table.cell(atrTable, 0, 0, text="Long ATR Stop", text_color=tableTextHeaderColor, bgcolor=tableLongBGColor, tooltip="Test")
  179. table.cell(atrTable, 1, 0, text="Long ATR Stop Dist", text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
  180. if (showTPinTable)
  181. table.cell(atrTable, 2, 0, text="Long ATR TP", text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
  182. // If the TP scale factor is exactly 1, we can nix the TP distance columns as it will be exactly the same as the stop distance.
  183. if (tpScaleFactor != 1)
  184. table.cell(atrTable, 3, 0, text="Long ATR TP Dist", text_color=tableTextHeaderColor, bgcolor=tableLongBGColor)
  185. table.cell(atrTable, 4, 0, text="Short ATR Stop", text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
  186. table.cell(atrTable, 5, 0, text="Short ATR Stop Dist", text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
  187. if (showTPinTable)
  188. table.cell(atrTable, 6, 0, text="Short ATR TP", text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
  189. // If the TP scale factor is exactly 1, we can nix the TP distance columns as it will be exactly the same as the stop distance.
  190. if (tpScaleFactor != 1)
  191. table.cell(atrTable, 7, 0, text="Short ATR TP Dist", text_color=tableTextHeaderColor, bgcolor=tableShortBGColor)
  192. //
  193. // Now for table values for each header...
  194. // Start with Long position...
  195. table.cell(atrTable, 0, 1, text=str.tostring(allowTableRepainting ? lowerATRBand : lowerATRBand[1], format.mintick), text_color=tableTextColor, bgcolor=tableLongBGColor, tooltip="Test")
  196. table.cell(atrTable, 1, 1, text=str.tostring(math.round_to_mintick(allowTableRepainting ? close - lowerATRBand : close[1] - lowerATRBand[1])), text_color=tableTextColor, bgcolor=tableLongBGColor)
  197. if (showTPinTable)
  198. table.cell(atrTable, 2, 1, text=str.tostring(allowTableRepainting ? scaledTPLong : scaledTPLong[1], format.mintick), text_color=tableTextColor, bgcolor=tableLongBGColor)
  199. // If the TP scale factor is exactly 1, we can nix the TP distance columns as it will be exactly the same as the stop distance.
  200. if (tpScaleFactor != 1)
  201. table.cell(atrTable, 3, 1, text=str.tostring(math.round_to_mintick(allowTableRepainting ? scaledATR * tpScaleFactor : scaledATR[1] * tpScaleFactor)), text_color=tableTextColor, bgcolor=tableLongBGColor)
  202. // Now the Short position...
  203. table.cell(atrTable, 4, 1, text=str.tostring(allowTableRepainting ? upperATRBand : upperATRBand[1], format.mintick), text_color=tableTextColor, bgcolor=tableShortBGColor, tooltip="Test 2")
  204. table.cell(atrTable, 5, 1, text=str.tostring(math.round_to_mintick(allowTableRepainting ? upperATRBand - close : upperATRBand[1] - close[1])), text_color=tableTextColor, bgcolor=tableShortBGColor)
  205. if (showTPinTable)
  206. table.cell(atrTable, 6, 1, text=str.tostring(allowTableRepainting ? scaledTPShort : scaledTPShort[1], format.mintick), text_color=tableTextColor, bgcolor=tableShortBGColor)
  207. // If the TP scale factor is exactly 1, we can nix the TP distance columns as it will be exactly the same as the stop distance.
  208. if (tpScaleFactor != 1)
  209. table.cell(atrTable, 7, 1, text=str.tostring(math.round_to_mintick(allowTableRepainting ? scaledATR * tpScaleFactor : scaledATR[1] * tpScaleFactor)), text_color=tableTextColor, bgcolor=tableShortBGColor)
  210.  
  211.  
  212.  
  213.  
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement