Guest User

tradingview

a guest
May 2nd, 2023
431
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.86 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. // © webument
  3.  
  4. //@version=5
  5. indicator("My script")
  6. plot(close)
  7. // Study using a scalping strategy with take-profit and trailing stop loss
  8. // © Ninorigo - v1.0
  9. // (from original idea of Lukescream)
  10. //
  11.  
  12. //@version=4
  13. study(title="Scalping using RSI 2 indicator", shorttitle="RSI 2 Study", overlay=true)
  14.  
  15.  
  16. //***********
  17. // Costants
  18. //***********
  19. def_start_date = timestamp("01 Jan 2021 07:30 +0000")
  20. def_end_date = timestamp("01 Dec 2024 07:30 +0000")
  21.  
  22. def_rsi_length = 2
  23. def_overbought_value = 90
  24. def_oversold_value = 10
  25.  
  26. def_slow_ma_length = 200
  27. def_fast_ma_length = 50
  28. def_ma_choice = "EMA"
  29.  
  30. def_tick = 0.5
  31. def_filter = true
  32.  
  33. def_ts_perc = 0.5
  34. def_tp_perc = 0.5
  35.  
  36. messageEntryL = "Long\nentry"
  37. messageEntryS = "Short\nentry"
  38. messageExitL = "Long\nexit"
  39. messageExitS = "Short\nexit"
  40.  
  41.  
  42. //***********
  43. // Change the optional parameters
  44. //***********
  45. start_time = input(title="Start date", defval=def_start_date, type=input.time)
  46. end_time = input(title="End date", defval=def_end_date, type=input.time)
  47. // RSI
  48. src = input(title="Source", defval=close, type=input.source)
  49. rsi_length = input(title="RSI Length", defval=def_rsi_length, minval=1, type=input.integer)
  50. overbought_threshold = input(title="Overbought threshold", defval=def_overbought_value, type=input.float)
  51. oversold_threshold = input(title="Oversold threshold", defval=def_oversold_value, type=input.float)
  52. // Moving average
  53. slow_ma_length = input(title="Slow MA length", defval=def_slow_ma_length, type=input.integer)
  54. fast_ma_length = input(title="Fast MA length", defval=def_fast_ma_length, type=input.integer)
  55. ma_choice = input(title="MA choice", defval="EMA", options=["SMA", "EMA"])
  56. // Input ticker
  57. //tick = input(title="Ticker size", defval=def_tick, type=input.float)
  58. filter = input(title="Trend Filter", defval=def_filter, type=input.bool)
  59. // Trailing stop (%)
  60. ts_perc = input(title="Trailing Stop %", defval=def_ts_perc, type=input.float)
  61. tp_perc = input(title='Take Profit %', defval=def_tp_perc, type=input.float)
  62. // Other input parameters
  63. show_slow_ma = input(title="Show slow MA?", defval=true, type=input.bool)
  64. show_fast_ma = input(title="Show fast MA?", defval=true, type=input.bool)
  65. show_entry_cond = input(title="Show entry conditions?", defval=true, type=input.bool)
  66. show_long_ts = input(title="Show long trailing stop?", defval=true, type=input.bool)
  67. show_short_ts = input(title="Show short trailing stop?", defval=true, type=input.bool)
  68. show_long_tp = input(title="Show long take profit?", defval=true, type=input.bool)
  69. show_short_tp = input(title="Show short take profit?", defval=true, type=input.bool)
  70. show_labels = input(title="Display labels?", defval=true, type=input.bool)
  71.  
  72.  
  73. //***********
  74. // RSI
  75. //***********
  76. // Calculate RSI
  77. up = rma(max(change(src), 0), rsi_length)
  78. down = rma(-min(change(src), 0), rsi_length)
  79. rsi = (down == 0 ? 100 : (up == 0 ? 0 : 100-100/(1+up/down)))
  80.  
  81.  
  82. //***********
  83. // Moving averages
  84. //***********
  85. slow_ma = (ma_choice == "SMA" ? sma(close, slow_ma_length) : ema(close, slow_ma_length))
  86. fast_ma = (ma_choice == "SMA" ? sma(close, fast_ma_length) : ema(close, fast_ma_length))
  87. // Show the moving averages
  88. plot(show_slow_ma ? slow_ma : na, color=color.white, title="Slow MA")
  89. plot(show_fast_ma ? fast_ma : na, color=color.yellow, title="Fast MA")
  90.  
  91. var float open_price = 0.0
  92. var float longStopPrice = 0.0
  93. var float shortStopPrice = 0.0
  94. var float longTakeProfitPrice = 0.0
  95. var float shortTakeProfitPrice = 0.0
  96.  
  97. //***********
  98. // Strategy
  99. //***********
  100. // Determine the entry conditions
  101. // Long position
  102. ConditionEntryL = time > start_time and time < end_time and (filter == true ? (fast_ma > slow_ma and close > slow_ma and rsi < oversold_threshold) : (fast_ma > slow_ma and rsi < oversold_threshold))
  103. // Short position
  104. ConditionEntryS = time > start_time and time < end_time and (filter == true ? (fast_ma < slow_ma and close < slow_ma and rsi > overbought_threshold) : (fast_ma < slow_ma and rsi > overbought_threshold))
  105.  
  106. // Submit the entry orders
  107. // Long position
  108. if ConditionEntryL
  109. open_price := open
  110. // Short position
  111. if ConditionEntryS
  112. open_price := open
  113.  
  114. // Detect what was last signal (long or short)
  115. long_short = 0
  116. long_short := ConditionEntryL ? 1 : ConditionEntryS ? -1 : long_short[1]
  117.  
  118. // Calculate the trailing stop position
  119.  
  120. // Long position
  121. longStopPrice := if long_short==1
  122. stopValue = open_price * (1 - ts_perc * 0.01)
  123. else
  124. 0
  125. // Short position
  126. shortStopPrice := if long_short==-1
  127. stopValue = open_price * (1 + ts_perc * 0.01)
  128. else
  129. 999999
  130.  
  131. // Calculate the take profit position
  132. // Long position
  133. longTakeProfitPrice := if long_short==1
  134. stopTakeValue = open_price * (1 + tp_perc * 0.01)
  135. //max(stopTakeValue, high[0])
  136. else
  137. 0
  138. // Short position
  139. shortTakeProfitPrice := if long_short==-1
  140. stopTakeValue = open_price * (1 - tp_perc * 0.01)
  141. //min(stopTakeValue, low[0])
  142. else
  143. 999999
  144.  
  145. // Check for the take profi hit during a bar
  146. longTPhit = long_short==1 and (high > longTakeProfitPrice[1] or close[0] >= longTakeProfitPrice)
  147. shortTPhit = long_short==-1 and (low < shortTakeProfitPrice[1] or close[0] <= shortTakeProfitPrice)
  148.  
  149. // Check for the Trailing Stop Loss hit during a bar
  150. // Long position
  151. longSLhit = if not(longTPhit)
  152. long_short==1 and (low < longStopPrice[1] or close[0] <= longStopPrice)
  153. else
  154. false
  155. // Short position
  156. shortSLhit = if not(shortTPhit)
  157. long_short==-1 and (high > shortStopPrice[1] or close[0] >= shortStopPrice)
  158. else
  159. false
  160.  
  161. // Determine the exit conditions
  162. ConditionExitL = (ConditionEntryL and (longTPhit or longSLhit))
  163. ConditionExitS = (ConditionEntryS and (shortTPhit or shortSLhit))
  164.  
  165.  
  166. // Plot the trailing stop
  167. plot(show_long_ts and ConditionEntryL and longSLhit ? longStopPrice : na, style=plot.style_linebr, color=color.fuchsia, transp=10, linewidth=2, title="Long Trailing Stop")
  168. plot(show_short_ts and ConditionEntryS and shortSLhit ? shortStopPrice : na, style=plot.style_linebr, color=color.fuchsia, transp=10, linewidth=2, title="Short Trailing Stop")
  169.  
  170. // Plot the take profit
  171. plot(show_long_tp and ConditionEntryL and longTPhit ? longTakeProfitPrice : na, style=plot.style_linebr, color=color.green, transp=10, linewidth=2, title="Long Fixed Take Profit")
  172. plot(show_short_tp and ConditionEntryS and shortTPhit ? shortTakeProfitPrice : na, style=plot.style_linebr, color=color.green, transp=10, linewidth=2, title="Short Fixed Take Profit")
  173.  
  174. // Plot the price of the entry conditions
  175. plot(show_entry_cond ? open_price : na, color=color.blue, transp=20, linewidth=2, title="Entry conditions")
  176.  
  177. // Highlights the take profit conditions
  178. plotshape(show_labels and ConditionEntryL and longTPhit, style=shape.labeldown, location=location.top, color=color.purple, size=size.tiny, title="Long TP", text="Long\nTP", textcolor=color.white)
  179. plotshape(show_labels and ConditionEntryL and shortTPhit, style=shape.labelup, location=location.bottom, color=color.purple, size=size.tiny, title="Short TP", text="Short\nTP", textcolor=color.white)
  180.  
  181. // Highlights the long trailing stop conditions
  182. plotshape (show_labels and ConditionEntryL and longSLhit, style=shape.labelup, text="Long\nTSL", size=size.tiny, textcolor=color.white, color=color.gray, location=location.bottom, title="Long TSL")
  183. plotshape (not(show_labels) and ConditionEntryL and longSLhit, style=shape.triangledown, text=messageExitL, size=size.tiny, color=color.purple, location=location.top, offset=0, editable=false)
  184. // Highlights the short trailing stop conditions
  185. plotshape (show_labels and ConditionEntryS and shortSLhit, style=shape.labeldown, text="Short\nSL", size=size.tiny, textcolor=color.white, color=color.gray, location=location.top, title="Short TSL")
  186. plotshape (not(show_labels) and ConditionEntryS and shortSLhit, style=shape.triangledown, text=messageExitS, size=size.tiny, color=color.lime, location=location.top, offset=0, editable=false)
  187.  
  188. // Highlights the long entry conditions
  189. plotshape (show_labels and ConditionEntryL, style=shape.labelup, text="LONG", size=size.tiny, textcolor=color.white, color=color.green, location=location.belowbar, title="Long Entry")
  190. plotshape (not(show_labels) and ConditionEntryL, style=shape.triangleup, text=messageEntryL, size=size.tiny, color=color.blue, location=location.bottom, offset=0, editable=false)
  191. bgcolor (ConditionEntryL ? color.navy : na, transp=80, offset=0, editable=true, title="Long position band")
  192. // Highlights the short entry conditions
  193. plotshape (show_labels and ConditionEntryS, style=shape.labeldown, text="SHORT", size=size.tiny, textcolor=color.white, color=color.red, location=location.abovebar, title="Short Entry")
  194. plotshape (not(show_labels) and ConditionEntryS, style=shape.triangleup, text=messageEntryS, size=size.tiny, color=color.red, location=location.bottom, offset=0, editable=false)
  195. bgcolor (ConditionEntryS ? color.olive : na, transp=80, offset=0, editable=true, title="Short position band")
  196.  
  197.  
  198. // Add the alert conditions
  199. // remember: the alertcondition does not start alerts but creates the condition. The alerts must always be created manually in the Create Alert dialog box
  200. alertcondition (condition=ConditionEntryL, title="Opening long position", message="It is time to open a long position.")
  201. alertcondition (condition=ConditionEntryS, title="Opening short position", message="It is time to open a short position.")
  202.  
  203. alertcondition (condition=ConditionExitL, title="Closing long position", message="It is time to close the long position.")
  204. alertcondition (condition=ConditionExitS, title="Closing short position", message="It is time to close the short position.")
  205.  
  206. alertcondition(condition=longTPhit, title="Long Take Profit", message='Long take profit')
  207. alertcondition(condition=shortTPhit, title="Short Take Profit", message='Short take profit')
  208.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment