Advertisement
Guest User

Untitled

a guest
Jan 16th, 2020
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.27 KB | None | 0 0
  1. //@version=4
  2. // Copyright (c) 2019-present, Alex Orekhov (everget)
  3. // Jurik RSX script may be freely distributed under the MIT license.
  4. // https://www.tradingview.com/script/20TzvKPk-Jurik-RSX/
  5.  
  6. //-------------------------------------------------------------------
  7. // Acknowledgements:
  8. //---- Base script:
  9. // RSX Divergence — SharkCIA by Jaggedsoft
  10. // https://www.tradingview.com/script/ujh3sCzy-RSX-Divergence-SharkCIA/
  11. // Jurik Moving Average by Everget
  12. // https://www.tradingview.com/script/nZuBWW9j-Jurik-Moving-Average/
  13.  
  14. //---- Divergences/Signals:
  15. // Libertus RSI Divergences
  16. // https://www.tradingview.com/script/42wcFfql-Relative-Strength-Index-Divergences-Libertus/
  17. // Price Divergence Dectector V3 by JustUncle
  18. // https://www.tradingview.com/script/YJdzOueI-Price-Divergence-Detector-V3-revised-by-JustUncleL/
  19. // Price Divergence Detector V2 by RicardoSantos
  20. // https://www.tradingview.com/script/3oeDh0Yq-RS-Price-Divergence-Detector-V2/
  21. // Stochastic RSI with Divergences by Neobutane
  22. // https://www.tradingview.com/script/3ogyZpSE-Stochastic-RSI-with-Divergences/
  23. // CCI Stochastic by Daveatt
  24. // https://www.tradingview.com/script/XZyG5SOx-CCI-Stochastic-and-a-quick-lesson-on-Scalping-Trading-Systems/
  25.  
  26. //---- Misc. Reference:
  27. // RSI SMA/EMA Cu by Auroagwei
  28. // https://www.tradingview.com/script/vYRYjPyi-rsi-sma-ema-cu/
  29. // CBCI Cu by Auroagwei
  30. // https://www.tradingview.com/script/JhootEsL-cbci-cu/
  31. // Chop and explode by fhenry0331
  32. // https://www.tradingview.com/script/5X5mhe3z-Chop-and-explode/
  33. // T-Step LSMA by RafaelZioni
  34. // https://www.tradingview.com/script/N4HM2Q00-T-Step-LSMA-VPT/
  35. // Scripts by Jaggedsoft for structure and formatting
  36. // Scripts by Everget for structure and formatting
  37. // If you read this you are gay//
  38.  
  39. //-------------------------------------------------------------------
  40. // RSX-D [ID: AC-P] v08
  41. // Author: Auroagwei
  42. // https://www.tradingview.com/u/auroagwei/
  43. strategy("SCALPTRON RELOADED", shorttitle="SCALPTRON RELOADED", initial_capital=4000, overlay=true, default_qty_type = strategy.percent_of_equity, pyramiding=5, default_qty_value=20, commission_type="percent", commission_value=0.0, max_bars_back=481)
  44. // Note: Designed for darkmode by default.
  45.  
  46. singnaltext = input(false, "===============ENTRY SIGNALS==================")
  47. earlycrossings = input(true, "Early M-crossings")
  48. latecrossings = false//input(false, "Late M-crossings")
  49. RSXLcrossings = input(true, "RSX/LSMA crossings")
  50. LibertusDivs = false//input(false, "RSX Divergence Entries")
  51. NeoRSXRegDivs = false//input(false, "NeoButane RSX regular divs")
  52. NeoRSXHidDivs = false//input(false, "NeoButane RSX hidden divs")
  53. NeoLSMARegDivs = false//input(false, "NeoButane LSMA-D regular divs")
  54. NeoLSMAHidDivs = false//input(false, "NeoButane LSMA-D hidden divs")
  55. tradeflip = input(true, "Trade Flips after stop-outs")
  56.  
  57. singnaltex5 = input(false, "===============EXIT SIGNALS===================")
  58. DivExit = input(true, "Divergence Take Profit")
  59. divscalp = input(true, "Regular Div Scalps")
  60. //hiddiv1 = input(false, "Early Exit on Hidden RSX Div")
  61. //hiddiv2 = input(false, "Early Exit on Hidden LSMA-D Div")
  62.  
  63.  
  64. singnaltex1 = input(false, "===============OPTIONS==================")
  65. scalppercent = input(2.0, "Scalp Zone %", step=.1)
  66. SLpercent = input(0.8, "Stop Loss %", step=.1)
  67. usestops = input(true, "Use Stop Loss")
  68. wicktolerant = input(true, "SL wait for close")
  69. HDban = input(true, "If stopped prevent re-entry until div detected")
  70. updatestop = input(true, "update stop loss % to avg price (offset by 1 entry)")
  71. updatescalp = input(true, "update scalp zone % to avg price (offset by 1 entry)")
  72.  
  73. singnaltexB = input(false, "===============UNBANS==================")
  74. LibertusBan = input(true, "RSX Libertus Divergence")
  75. NeoRSXRegBan = input(false, "NeoButane RSX regular divs")
  76. NeoRSXHidBan = input(false, "NeoButane RSX hidden divs")
  77. NeoLSMARegBan = input(false, "NeoButane LSMA-D regular divs")
  78. NeoLSMAHidBan = input(false, "NeoButane LSMA-D hidden divs")
  79. HMAban = input(false, "HMA trend")
  80.  
  81. timedstop = false//input(false, "use stop loss timeout")
  82. SLcandles = 200//input(200, "timeout candles")
  83.  
  84. margin = input(defval=1.0, title="X leverage")
  85. totalfunds=margin * strategy.equity
  86. positionadjuster = input(1, "Equity division")
  87. pyramiding = input(5, "Pyramiding (copy value from other page!)")
  88. positionsize = (totalfunds / close) / positionadjuster
  89. fundingthresh = input(0.001, "Funding Rate Threshold for entries", step=.001)
  90. fundinglimit = input(0.25, "Funding Rate pump/dump level", step=.01)
  91.  
  92. contract = "XBTUSD"
  93. capFunding = true//input(true, "Cap Funding")
  94.  
  95. //alot of calculations lol
  96. CONTRACT_XBTUSD = "XBTUSD"
  97. clamp(lowest, highest, subject) =>
  98. max(lowest, min(highest, subject))
  99. total(src, length) =>
  100. result = 0.0
  101. for i = 0 to length - 1 by 1
  102. if i < 0
  103. result := na
  104. break
  105. result := result + src[i]
  106. result
  107. result
  108. simpleMovingAverage(src, length) =>
  109. total(src, length) / length
  110. barsSince(condition) =>
  111. bars = 0
  112. bars := condition ? 0 : bars[1] + 1
  113. bars
  114. twap(resetWhen, src) =>
  115. bars = barsSince(resetWhen) + 1
  116. simpleMovingAverage(src, bars)
  117. isBitmexFundingBar = (hour == 4 or hour == 12 or hour == 20) and minute == 0 or timeframe.isdwm
  118. fundingTwap(src) =>
  119. twap(isBitmexFundingBar, src)
  120. bitmexInterestRateTwap(asset) =>
  121. interestBaseIndex = security("BITMEX:" + asset + "BON", timeframe.period, close) // The Interest Rate for borrowing the Base currency
  122. interestQuoteIndex = security("BITMEX:USDBON", timeframe.period, close) // The Interest Rate for borrowing the Quote currency
  123. fundingInterval = 3 // (Since funding occurs every 8 hours)
  124. interestRate = nz((interestQuoteIndex - interestBaseIndex) / fundingInterval, 0.0001)
  125. fundingTwap(interestRate)
  126. bitmexPremiumIndexTwap(asset) =>
  127. premiumIndexTwap_fallback = security("BITMEX:" + asset + "USDPI", timeframe.period, fundingTwap(ohlc4))
  128. premiumIndexTwap = security("BITMEX:" + asset + "USDPI", "1", fundingTwap(ohlc4))
  129. [premiumIndexTwap, premiumIndexTwap_fallback]
  130. bitmexPredictedFundingRate(asset, capFunding, fundingCapAmount, dampenerAmount, interestRateTwap, premiumIndexTwap) =>
  131. predictedBaseFundingRateRatio = premiumIndexTwap +
  132. clamp(-dampenerAmount, dampenerAmount, interestRateTwap - premiumIndexTwap)
  133. clamp__1 = clamp(-fundingCapAmount, fundingCapAmount, predictedBaseFundingRateRatio)
  134. predictedFundingRateRatio = capFunding ? clamp__1 : predictedBaseFundingRateRatio
  135. predictedFundingRateRatio * 100
  136. bitmexNextFundingRate(predictedFundingRate) =>
  137. nextFundingRate = 0.01
  138. nextFundingRate := isBitmexFundingBar ? predictedFundingRate[1] : nextFundingRate[1]
  139. dwmIntervalModifier = timeframe.isdaily ? 3 :
  140. timeframe.isweekly ? 3 * 7 : timeframe.ismonthly ? 3 * 30 : na
  141. dwmModifier = timeframe.multiplier * dwmIntervalModifier
  142. dwmModifier
  143. asset = contract == CONTRACT_XBTUSD ? "XBT" : "XBT"
  144. [premiumIndexTwap_ideal, premiumIndexTwap_fallback] = bitmexPremiumIndexTwap(asset)
  145. premiumIndexTwap = na(premiumIndexTwap_ideal) ? premiumIndexTwap_fallback : premiumIndexTwap_ideal
  146. interestRateTwap = bitmexInterestRateTwap(asset)
  147. predictedFundingRate = bitmexPredictedFundingRate(asset, capFunding, 0.00375, 0.0005, interestRateTwap, premiumIndexTwap)
  148. //nextFundingRate = bitmexNextFundingRate(predictedFundingRate)
  149.  
  150. premium = premiumIndexTwap*100
  151.  
  152.  
  153.  
  154. confirmations = input(0, "confirmation candle delay")
  155.  
  156. isLONG=false
  157. isLONG:=nz(isLONG[1])
  158.  
  159. isSHORT=false
  160. isSHORT:=nz(isSHORT[1])
  161.  
  162. flipmode=false
  163. flipmode:=nz(flipmode[1])
  164.  
  165. /////////////////////////////////////////////////////////////////////////////////ATR
  166. //nATRPeriod = input(75, "Period")
  167. //nATRMultip = input(9.5, "Multiplier", minval=0.5, maxval=1000, step=0.1)
  168.  
  169. //xATR = atr(nATRPeriod)
  170. //nLoss = nATRMultip * xATR
  171. //xATRTrailingStop = 0.0
  172. //xATRTrailingStop :=
  173. //iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
  174. //iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss),
  175. //iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
  176. //
  177. //pos = 0
  178. //pos :=
  179. //iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
  180. //iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
  181.  
  182. //atrcolor = flipmode and pos == -1 ? color.red: flipmode and pos == 1 ? color.lime : na
  183. //patr=plot(xATRTrailingStop, color=atrcolor, linewidth=2, title="ATR Trailing Stop", transp=0)
  184.  
  185.  
  186. n1=input(title="HMA length",defval=150)
  187. HMAbars=input(1, "# of bars to confirm slope", minval=1)
  188. n1ma1=2*wma(close,round(n1/2))
  189. n1ma2=wma(close,n1)
  190. diff1=n1ma1-n1ma2
  191. sqn1=round(sqrt(n1))
  192. hma1 = wma(diff1,sqn1)
  193. HMAtrendUP = barssince(hma1 < hma1[1]) >= HMAbars
  194. HMAtrendDOWN = barssince(hma1 > hma1[1]) >= HMAbars
  195.  
  196. HMAcolor =
  197. HMAtrendUP and isLONG? color.green :
  198. HMAtrendDOWN and isSHORT? color.red :
  199. HMAtrendUP and isSHORT and premium <= fundingthresh? color.blue :
  200. HMAtrendDOWN and isLONG and premium >= (fundingthresh*-1)? color.blue : color.blue
  201.  
  202. ma1=plot(hma1,color=(flipmode==false? na : HMAcolor),linewidth=2, title="HMA")
  203.  
  204.  
  205.  
  206.  
  207. //-------------------------------------------------------------------
  208. //---- Input Parameters
  209. length = input(title="RSX: Length", type=input.integer, defval=14)
  210. src = input(title="RSX: Source", type=input.source, defval=ohlc4)
  211. obLevel = input(title="RSX: OB Level", type=input.integer, defval=70)
  212. osLevel = input(title="RSX: OS Level", type=input.integer, defval=30)
  213. show_rsx= true//input(true, "Show RSX?")
  214.  
  215. highlightBreakouts_ema = false//input(false, title="Show EMA21/55 Up/Down Fill")
  216. highlightBreakouts = true//input(title="RSX: Highlight Overbought/Oversold Breakouts?", type=input.bool, defval=true)
  217. showdivs_lib = false//input(true, title="RSX: Hide Divs? [Libertus Divs]")
  218.  
  219. if showdivs_lib == false
  220. showdivs_lib := true
  221. else
  222. showdivs_lib := false
  223. piv = true//input(true, "RSX: Hide pivots? [Libertus Divs]")
  224. shrt = true//input(true, "RSX: Shorter labels for pivots and Divs? [Libertus Divs]")
  225. xbars = input(650, "RSX: Div lookback period (bars)? [Libertus Divs]", input.integer, minval=1)
  226.  
  227. showdivs = true//input(false, title="RSX Divergences [Neobutane Divs]")
  228. showdiv_labels2 = true//input(true,title="RSX Divergences: Mark Hidden Divs [Neobutane Divs]")
  229. showchan = false //input(false, title="RSX Divergences: Draw Channel [Neobutane Divs]")
  230. showhidden = false//input(false, title="RSX Divergences: Show Hidden Divs with low strikerate [Neobutane Divs]")
  231.  
  232. showdivs2 = true//input(false, title="LSMA-D Divergences [Neobutane Divs]")
  233. showdiv_labels22 = true//input(true,title="LSMA-D Divergences: Mark Hidden Divs [Neobutane Divs]")
  234. showchan2 = false //input(false, title="LSMA-D Divergences: Draw Channel [Neobutane Divs]")
  235. showhidden2 = false//input(false, title="LSMA-D Divergences: Show Hidden Divs with low strikerate [Neobutane Divs]")
  236.  
  237. //---- M1/M2, Midline Trend indication/Zones
  238. show_m1 = true//input(true, title="Show M1 Level Marker")
  239. show_m2 = true//input(true, title="Show M2 Level Marker")
  240.  
  241. m1Level = input(title="RSX: M1 Level", type=input.integer, defval=85) //60
  242. m2Level = input(title="RSX: M2 Level", type=input.integer, defval=15) //40
  243.  
  244. showArrows = true//input(true, "Show/Hide all zone indication")
  245. showArrowsCenter = false//input(false, "RSX Midline cross indication")
  246. showArrowsEnter = true//input(true, "RSX M1/M2 Enter zone indication")
  247. showArrowsExit = true//input(true, "RSX M1/M2 Exit zone indication")
  248. showArrowsrsx_lsmaD = true//input(true, "RSX/LSMA-D cross indication")
  249.  
  250. rsx_lsmaD_obLevel = input(title="RSX/LSMA-D cross: OB Level", type=input.integer, defval=70)
  251. rsx_lsmaD_osLevel = input(title="RSX/LSMA-D cross : OS Level", type=input.integer, defval=30)
  252.  
  253. //---- LSMA-K and LSMA-D Components
  254. show_lsmaD = true//input(true,defval=false,type=input.bool,title="Show LSMA-D line?")
  255.  
  256. length_lsmaA_lsma = input(minval=2,defval=20,type=input.integer,title="LSMA-D: LSMA-A Length")
  257. length_lsmaB_lsma = input(minval=2,defval=40,type=input.integer,title="LSMA-D: LSMA-B Length")
  258. length_lsmaC_lsma = input(minval=2,defval=80,type=input.integer,title="LSMA-D: LSMA-C Length")
  259. length_lsmaD_lsma = input(minval=2,defval=10,type=input.integer,title="LSMA-D: Length")
  260.  
  261. offset_lsmaA = input(defval=0,type=input.integer,title="LSMA-D: LSMA-A Offset")
  262. offset_lsmaB = input(defval=0,type=input.integer,title="LSMA-D: LSMA-B Offset")
  263. offset_lsmaC = input(defval=0,type=input.integer,title="LSMA-D: LSMA-C Offset")
  264. offset_lsmaD = input(defval=-2,type=input.integer,title="LSMA-D: Offset")
  265.  
  266. //-------------------------------------------------------------------
  267. //---- RSX Component (Everget, Jaggedsoft)
  268. //---- Note: Probably a solid idea to not touch this section.
  269. f8 = 100 * src
  270. f10 = nz(f8[1])
  271. v8 = f8 - f10
  272.  
  273. f18 = 3 / (length + 2)
  274. f20 = 1 - f18
  275.  
  276. f28 = 0.0
  277. f28 := f20 * nz(f28[1]) + f18 * v8
  278.  
  279. f30 = 0.0
  280. f30 := f18 * f28 + f20 * nz(f30[1])
  281. vC = f28 * 1.5 - f30 * 0.5
  282.  
  283. f38 = 0.0
  284. f38 := f20 * nz(f38[1]) + f18 * vC
  285.  
  286. f40 = 0.0
  287. f40 := f18 * f38 + f20 * nz(f40[1])
  288. v10 = f38 * 1.5 - f40 * 0.5
  289.  
  290. f48 = 0.0
  291. f48 := f20 * nz(f48[1]) + f18 * v10
  292.  
  293. f50 = 0.0
  294. f50 := f18 * f48 + f20 * nz(f50[1])
  295. v14 = f48 * 1.5 - f50 * 0.5
  296.  
  297. f58 = 0.0
  298. f58 := f20 * nz(f58[1]) + f18 * abs(v8)
  299.  
  300. f60 = 0.0
  301. f60 := f18 * f58 + f20 * nz(f60[1])
  302. v18 = f58 * 1.5 - f60 * 0.5
  303.  
  304. f68 = 0.0
  305. f68 := f20 * nz(f68[1]) + f18 * v18
  306.  
  307. f70 = 0.0
  308. f70 := f18 * f68 + f20 * nz(f70[1])
  309. v1C = f68 * 1.5 - f70 * 0.5
  310.  
  311. f78 = 0.0
  312. f78 := f20 * nz(f78[1]) + f18 * v1C
  313.  
  314. f80 = 0.0
  315. f80 := f18 * f78 + f20 * nz(f80[1])
  316. v20 = f78 * 1.5 - f80 * 0.5
  317.  
  318. f88_ = 0.0
  319. f90_ = 0.0
  320.  
  321. f88 = 0.0
  322. f90_ := nz(f90_[1]) == 0 ? 1 :
  323. nz(f88[1]) <= nz(f90_[1]) ? nz(f88[1]) + 1 : nz(f90_[1]) + 1
  324. f88 := nz(f90_[1]) == 0 and length - 1 >= 5 ? length - 1 : 5
  325.  
  326. f0 = f88 >= f90_ and f8 != f10 ? 1 : 0
  327. f90 = f88 == f90_ and f0 == 0 ? 0 : f90_
  328.  
  329. v4_ = f88 < f90 and v20 > 0 ? (v14 / v20 + 1) * 50 : 50
  330. rsx = v4_ > 100 ? 100 : v4_ < 0 ? 0 : v4_
  331.  
  332. rsxColor = rsx > obLevel ? #0ebb23 : rsx < osLevel ? #ff0000 : color.white //#512DA8
  333.  
  334. lsmaA = linreg(rsx,length_lsmaA_lsma,offset_lsmaA)
  335. lsmaB = linreg(rsx,length_lsmaB_lsma,offset_lsmaB)
  336. lsmaC = linreg(rsx,length_lsmaC_lsma,offset_lsmaC)
  337. lsmaD = linreg(((lsmaA + lsmaB + lsmaC) / 3), length_lsmaD_lsma,offset_lsmaD)
  338.  
  339. //-------------------------------------------------------------------
  340. //---- Chassis
  341. transparent = color.new(color.white, 100)
  342. //maxLevelPlot = hline(100, title="Max Level", linestyle=hline.style_dotted, color=transparent)
  343. //obLevelPlot = hline(obLevel, title="Overbought Level", linestyle=hline.style_dotted)
  344. //midline = hline(50, title="Middle Level/Midline", linestyle=hline.style_dotted,color=color.white) //color.silver)
  345. //osLevelPlot = hline(osLevel, title="Oversold Level", linestyle=hline.style_dotted)//,color=color.gray)
  346. //minLevelPlot = hline(0, title="Min Level", linestyle=hline.style_dotted, color=transparent)
  347.  
  348. //fill(obLevelPlot, osLevelPlot, color=color.purple, transp=100,title="RSX OB <--> OS Fill")
  349.  
  350. //---- M1/M2 Marker
  351. // Custom M1/M2 levels of 40/60 by default
  352. // Set to transparent to keep out of the way if not using/needed
  353.  
  354. m1_color = show_m1 ? color.aqua : transparent
  355. m2_color = show_m2 ? color.orange : transparent
  356. //m1LevelPlot = hline(m1Level, title="RSX: M1 Level", linestyle=hline.style_dotted,color=m1_color)
  357. //m2LevelPlot = hline(m2Level , title="RSX: M2 Level", linestyle=hline.style_dotted,color=m2_color)
  358. //----
  359.  
  360. obFillColor = rsx > obLevel and highlightBreakouts ? #008000 : transparent
  361. osFillColor = rsx < osLevel and highlightBreakouts ? #FF0000 : transparent
  362. //fill(maxLevelPlot, obLevelPlot, color=obFillColor, transp=90, title="RSX OB Fill")
  363. //fill(minLevelPlot, osLevelPlot, color=osFillColor, transp=90, title="RSX OS Fill")
  364.  
  365. //////////////////
  366.  
  367. ema21 = ema(close, 21) //fib
  368. ema55 = ema(close, 55) //fib
  369.  
  370. upEmaFillColor = ema21 > ema55 and highlightBreakouts_ema ? color.orange : transparent
  371. downEmaFillColor = ema21 < ema55 and highlightBreakouts_ema ? color.aqua : transparent
  372.  
  373. //fill(obLevelPlot, m1LevelPlot, color=upEmaFillColor, transp=70,title="EMA 21/55 Up Fill")
  374. //fill(osLevelPlot, m2LevelPlot, color=downEmaFillColor, transp=70,title="EMA 21/55 Down Fill")
  375.  
  376. ////////////////
  377.  
  378. //-------------------------------------------------------------------
  379. //plot(show_rsx ? rsx : na , title="RSX", linewidth=2, color=rsxColor, transp=0)
  380. //-------------------------------------------------------------------
  381.  
  382. // MA of RSX
  383. show_sma = false//input(false, "SMA of RSX")
  384. show_ema = false//input(false, "EMA of RSX")
  385.  
  386. len_sma = 9//input(9, minval=1, title="SMA of RSX Length")
  387. len_ema = 45//input(45, minval=1, title="EMA of RSX Length")
  388. smaRSX = ema(rsx,len_sma )
  389. emaRSX = ema(rsx,len_ema )
  390. //plot(show_sma ? smaRSX : na, title="SMA of RSX", style=plot.style_line, linewidth=2, color=#26c6da,transp=0)
  391. //plot(show_ema ? emaRSX : na, title="EMA of RSX", style=plot.style_line, linewidth=2, color=#008000 ,transp=0)
  392. //
  393. //plot(show_lsmaD ? lsmaD : na, color=color.fuchsia, linewidth=2,title="LSMA-D",transp=0)
  394.  
  395. //-------------------------------------------------------------------
  396. //---- Pivots and Libertus Divergences Component
  397. hb = abs(highestbars(rsx, xbars)) // Finds bar with highest value in last X bars
  398. lb = abs(lowestbars(rsx, xbars)) // Finds bar with lowest value in last X bars
  399. max = float(na)
  400. max_rsi = float(na)
  401. min = float(na)
  402. min_rsi = float(na)
  403. pivoth = bool(na)
  404. pivotl = bool(na)
  405. divbear = bool(na)
  406. divbull = bool(na)
  407.  
  408. // If bar with lowest / highest is current bar, use it's value
  409. max := hb == 0 ? close : na(max[1]) ? close : max[1]
  410. max_rsi := hb == 0 ? rsx : na(max_rsi[1]) ? rsx : max_rsi[1]
  411. min := lb == 0 ? close : na(min[1]) ? close : min[1]
  412. min_rsi := lb == 0 ? rsx : na(min_rsi[1]) ? rsx : min_rsi[1]
  413.  
  414. // Compare high of current bar being examined with previous bar's high
  415. // If curr bar high is higher than the max bar high in the lookback window range
  416. if close > max // we have a new high
  417. max := close // change variable "max" to use current bar's high value
  418. max
  419. if rsx > max_rsi // we have a new high
  420. max_rsi := rsx // change variable "max_rsi" to use current bar's RSI value
  421. max_rsi
  422. if close < min // we have a new low
  423. min := close // change variable "min" to use current bar's low value
  424. min
  425. if rsx < min_rsi // we have a new low
  426. min_rsi := rsx // change variable "min_rsi" to use current bar's RSI value
  427. min_rsi
  428.  
  429. // Finds pivot point with at least 2 right candles with lower value
  430. pivoth := max_rsi == max_rsi[2] and max_rsi[2] != max_rsi[3] ? true : na
  431. pivotl := min_rsi == min_rsi[2] and min_rsi[2] != min_rsi[3] ? true : na
  432.  
  433. // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
  434. if max[1] > max[2] and rsx[1] < max_rsi and rsx <= rsx[1]
  435. divbear := true
  436. divbear
  437. if min[1] < min[2] and rsx[1] > min_rsi and rsx >= rsx[1]
  438. divbull := true
  439. divbull
  440.  
  441.  
  442. //-------------------------------------------------------------------
  443. //---- Secondary Divergence Component for RSX + LSMA-D (Neobutane Divergences)
  444. k=rsx
  445. k2=lsmaD
  446.  
  447. //----
  448. uselog = true // input(true, title="Log")
  449. //@RicardoSantos' Divergence Script (https://www.tradingview.com/script/3oeDh0Yq-RS-Price-Divergence-Detector-V2/)
  450. f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
  451. f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
  452. f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
  453. //----
  454. fractal_top = f_fractalize(k) > 0 ? k[2] : na
  455. fractal_bot = f_fractalize(k) < 0 ? k[2] : na
  456.  
  457. fractal_top2 = f_fractalize(k2) > 0 ? k2[2] : na
  458. fractal_bot2 = f_fractalize(k2) < 0 ? k2[2] : na
  459.  
  460. high_prev = valuewhen(fractal_top, k[2], 0)[2]
  461. high_price = valuewhen(fractal_top, high[2], 0)[2]
  462. low_prev = valuewhen(fractal_bot, k[2], 0)[2]
  463. low_price = valuewhen(fractal_bot, low[2], 0)[2]
  464.  
  465. high_prev2 = valuewhen(fractal_top2, k2[2], 0)[2]
  466. high_price2 = valuewhen(fractal_top2, high[2], 0)[2]
  467. low_prev2 = valuewhen(fractal_bot2, k2[2], 0)[2]
  468. low_price2 = valuewhen(fractal_bot2, low[2], 0)[2]
  469.  
  470. regular_bearish_div = fractal_top and high[2] > high_price and k[2] < high_prev
  471. hidden_bearish_div = fractal_top and high[2] < high_price and k[2] > high_prev
  472. regular_bullish_div = fractal_bot and low[2] < low_price and k[2] > low_prev
  473. hidden_bullish_div = fractal_bot and low[2] > low_price and k[2] < low_prev
  474.  
  475. regular_bearish_div2 = fractal_top2 and high[2] > high_price2 and k[2] < high_prev2
  476. hidden_bearish_div2 = fractal_top2 and high[2] < high_price2 and k[2] > high_prev2
  477. regular_bullish_div2 = fractal_bot2 and low[2] < low_price2 and k[2] > low_prev2
  478. hidden_bullish_div2 = fractal_bot2 and low[2] > low_price2 and k[2] < low_prev2
  479.  
  480. col1 = regular_bearish_div ? #FF0000 : hidden_bearish_div and showhidden ? #FF0000 : na
  481. col2 = regular_bullish_div ? #00FF00 : hidden_bullish_div and showhidden ? #00FF00 : na
  482. col3 = regular_bearish_div ? #FF0000 : hidden_bearish_div and showhidden ? #FF0000 : showchan ? color.gray : na
  483. col4 = regular_bullish_div ? #00FF00 : hidden_bullish_div and showhidden ? #00FF00 : showchan ? color.gray : na
  484.  
  485. col12 = regular_bearish_div2 ? #FF0000 : hidden_bearish_div2 and showhidden2 ? #FF0000 : na
  486. col22 = regular_bullish_div2 ? #00FF00 : hidden_bullish_div2 and showhidden2 ? #00FF00 : na
  487. col32 = regular_bearish_div2 ? #FF0000 : hidden_bearish_div2 and showhidden2 ? #FF0000 : showchan ? color.gray : na
  488. col42 = regular_bullish_div2 ? #00FF00 : hidden_bullish_div2 and showhidden2 ? #00FF00 : showchan ? color.gray : na
  489.  
  490.  
  491. //---- M1/M2 and Trending Signal Alert componenent [Daveatt CCI Stoch]
  492. // showArrows = true
  493. // showArrowsEnter = true
  494. // showArrowsExit = true
  495. // showArrowsCenter = true
  496. trend_enter = if showArrowsEnter
  497. if crossunder(rsx, m2Level)
  498. 1
  499. else
  500. if crossover(rsx, m1Level)
  501. -1
  502.  
  503. trend_exit = if showArrowsExit
  504. if crossunder(rsx, m1Level)
  505. -1
  506. else
  507. if crossover(rsx, m2Level)
  508. 1
  509.  
  510. trend_center = if showArrowsCenter
  511. if crossunder(rsx, 50)
  512. -1
  513. else
  514. if crossover(rsx, 50)
  515. 1
  516.  
  517. rsx_lsmaD_cu = rsx >= rsx_lsmaD_obLevel and crossunder(rsx, lsmaD)
  518. rsx_lsmaD_co = rsx <= rsx_lsmaD_osLevel and crossover(rsx, lsmaD)
  519.  
  520.  
  521.  
  522.  
  523. //RSXCOMid = (showArrowsCenter and trend_center == 1)
  524. //RSXCUMid = (showArrowsCenter and trend_center == -1)
  525.  
  526.  
  527. RSXCUM1 = (showArrowsExit and trend_exit == -1)
  528. RSXCOM1 = (showArrowsEnter and trend_enter == -1)
  529.  
  530. RSXCUM2 = (showArrowsEnter and trend_enter == 1)
  531. RSXCOM2 = (showArrowsExit and trend_exit == 1)
  532.  
  533. RSXCULSMAD = (rsx_lsmaD_cu)
  534. RSXCOLSMAD = (rsx_lsmaD_co)
  535.  
  536.  
  537. entryPrice=0.0
  538. entryPrice:=nz(entryPrice[1])
  539.  
  540. SLlevel=0.0
  541. SLlevel:=nz(SLlevel[1])
  542.  
  543. scalpzone=0.0
  544. scalpzone:=nz(scalpzone[1])
  545.  
  546. longban=false
  547. longban:=nz(longban[1])
  548.  
  549. shortban=false
  550. shortban:=nz(shortban[1])
  551.  
  552. maxentries = false
  553. maxentries:=nz(maxentries[1])
  554.  
  555.  
  556.  
  557. if longban and (predictedFundingRate < (fundingthresh*-1)) and
  558. ((LibertusBan ? divbull : false) or
  559. (NeoRSXRegBan ? regular_bullish_div and premium < (fundingthresh*-1) : false) or
  560. (NeoLSMARegBan ? regular_bullish_div2 and premium < (fundingthresh*-1) : false) or
  561. (NeoRSXHidBan ? hidden_bullish_div and premium < (fundingthresh*-1) : false) or
  562. (NeoRSXHidBan ? hidden_bullish_div2 and premium < (fundingthresh*-1) : false))
  563. longban:=false
  564.  
  565. if longban and HMAban and HMAtrendUP
  566. longban:=false
  567.  
  568. if shortban and predictedFundingRate > fundingthresh and
  569. ((LibertusBan ? divbear : false) or
  570. (NeoRSXRegBan ? regular_bearish_div and premium > fundingthresh : false) or
  571. (NeoLSMARegBan ? regular_bearish_div2 and premium > fundingthresh : false) or
  572. (NeoRSXHidBan ? hidden_bearish_div and premium > fundingthresh : false) or
  573. (NeoRSXHidBan ? hidden_bearish_div2 and premium > fundingthresh : false))
  574. shortban:=false
  575.  
  576. if shortban and HMAban and HMAtrendDOWN
  577. shortban:=false
  578.  
  579. LONG = not longban and (predictedFundingRate > (-1*fundinglimit)) and
  580. (predictedFundingRate < (fundingthresh*-1)) and
  581. ((earlycrossings ? RSXCUM2 : false) or
  582. (latecrossings ? RSXCOM2 : false) or
  583. (RSXLcrossings ? RSXCOLSMAD : false) or
  584. (LibertusDivs ? divbull : false) or
  585. (NeoRSXRegDivs ? regular_bullish_div : false) or
  586. (NeoRSXHidDivs ? hidden_bullish_div : false) or
  587. (NeoLSMARegDivs ? regular_bullish_div2 : false) or
  588. (NeoLSMAHidDivs ? hidden_bullish_div2 : false))
  589.  
  590.  
  591. SHORT = not shortban and (predictedFundingRate < fundinglimit) and
  592. (predictedFundingRate > fundingthresh) and
  593. ((earlycrossings ? RSXCOM1 : false) or
  594. (latecrossings ? RSXCUM1 : false) or
  595. (RSXLcrossings ? RSXCULSMAD : false) or
  596. (LibertusDivs ? divbear : false) or
  597. (NeoRSXRegDivs ? regular_bearish_div : false) or
  598. (NeoRSXHidDivs ? hidden_bearish_div : false) or
  599. (NeoLSMARegDivs ? regular_bearish_div2 : false) or
  600. (NeoLSMAHidDivs ? hidden_bearish_div2 : false))
  601.  
  602. if LONG and not isLONG
  603. entryPrice:=close
  604. SLlevel:=entryPrice * (1 - SLpercent*.01)
  605. scalpzone:=entryPrice * (1 + scalppercent*.01)
  606. isSHORT:=false
  607. shortban:=false
  608. maxentries:=false
  609. isLONG:=true
  610.  
  611.  
  612. if SHORT and not isSHORT
  613. entryPrice:=close
  614. SLlevel:=entryPrice * (1 + SLpercent*.01)
  615. scalpzone:=entryPrice * (1 - scalppercent*.01)
  616. isLONG:=false
  617. longban:=false
  618. maxentries:=false
  619. isSHORT:=true
  620.  
  621.  
  622. if LONG and flipmode
  623. entryPrice:=close
  624. SLlevel:=entryPrice * (1 - SLpercent*.01)
  625. scalpzone:=entryPrice * (1 + scalppercent*.01)
  626. isSHORT:=false
  627. shortban:=false
  628. maxentries:=false
  629. isLONG:=true
  630. flipmode:=false
  631.  
  632. if SHORT and flipmode
  633. entryPrice:=close
  634. SLlevel:=entryPrice * (1 + SLpercent*.01)
  635. scalpzone:=entryPrice * (1 - scalppercent*.01)
  636. isLONG:=false
  637. longban:=false
  638. maxentries:=false
  639. isSHORT:=true
  640. flipmode:=false
  641.  
  642.  
  643.  
  644.  
  645. longprofit = not flipmode and DivExit and ((predictedFundingRate > fundingthresh) and premium > fundingthresh and divbear and not LibertusDivs) and isLONG
  646. longstopped = not flipmode and (wicktolerant ? close : low) < SLlevel and usestops and (premium > fundingthresh or predictedFundingRate < (-1*fundinglimit)) and isLONG
  647. //longtimeout = not flipmode and usestops and timedstop and isLONG and barssince((wicktolerant ? close : low) >= SLlevel) > SLcandles
  648. longscalped = not flipmode and divscalp and (scalpzone > close) and divbear and isLONG
  649.  
  650. shortprofit = not flipmode and DivExit and ((predictedFundingRate < (fundingthresh*-1)) and premium < (fundingthresh*-1) and divbull and not LibertusDivs) and isSHORT
  651. shortstopped= not flipmode and (wicktolerant ? close : high) > SLlevel and usestops and (premium < (fundingthresh*-1) or predictedFundingRate > fundinglimit) and isSHORT
  652. //shorttimeout= not flipmode and usestops and timedstop and isSHORT and barssince((wicktolerant ? close : high) <= SLlevel) > SLcandles
  653. shortscalped= not flipmode and divscalp and (scalpzone < close) and divbull and isSHORT
  654.  
  655. longclose = longstopped or longscalped or longprofit// or longtimeout
  656. shortclose= shortstopped or shortscalped or shortprofit// or shorttimeout
  657.  
  658. if longclose
  659. isLONG:=false
  660. entryPrice:=0.0
  661. SLlevel:=0.0
  662. scalpzone:=0.0
  663. maxentries:=false
  664.  
  665. if shortclose
  666. isSHORT:=false
  667. entryPrice:=0.0
  668. SLlevel:=0.0
  669. scalpzone:=0.0
  670. maxentries:=false
  671.  
  672. if longstopped and HDban
  673. longban:=true
  674.  
  675. if shortstopped and HDban
  676. shortban:=true
  677.  
  678. if tradeflip and (shortstopped or longstopped)
  679. flipmode:=true
  680.  
  681.  
  682. fliplong = flipmode and ((not flipmode[1] and HMAtrendUP) or (flipmode[1] and isSHORT and HMAtrendUP and premium < (fundingthresh*-1)))
  683. flipshort = flipmode and ((not flipmode[1] and HMAtrendDOWN) or (flipmode[1] and isLONG and HMAtrendDOWN and premium > fundingthresh))
  684.  
  685.  
  686.  
  687. if fliplong
  688. entryPrice:=close
  689. //SLlevel:=entryPrice * (1 - SLpercent*.01)
  690. //scalpzone:=entryPrice * (1 + scalppercent*.01)
  691. isSHORT:=false
  692. maxentries:=false
  693. isLONG:=true
  694.  
  695.  
  696.  
  697. if flipshort
  698. entryPrice:=close
  699. //SLlevel:=entryPrice * (1 + SLpercent*.01)
  700. //scalpzone:=entryPrice * (1 - scalppercent*.01)
  701. isLONG:=false
  702. maxentries:=false
  703. isSHORT:=true
  704.  
  705.  
  706.  
  707. bgcolor(divbull and predictedFundingRate < (fundingthresh*-1)? color.green : na)
  708. bgcolor(divbear and predictedFundingRate > fundingthresh? color.red : na)
  709. barcolor(divbull? color.yellow : na)
  710. barcolor(divbear? color.blue : na)
  711. //barcolor(flipmode and isLONG? color.green : flipmode and isSHORT ? color.red : na)
  712.  
  713. plotshape(longban and not longban[1], style=shape.triangleup, location=location.belowbar, color=color.yellow, size=size.small) //text="Longs Banned", textcolor=color.red
  714. plotshape(not longban and longban[1], style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small) //text="Longs Un-Banned", textcolor=color.green
  715. plotshape(shortban and not shortban[1], style=shape.triangledown, location=location.abovebar, color=color.yellow, size=size.small) //text="Shorts Banned", textcolor=color.green
  716. plotshape(not shortban and shortban[1], style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small) //text="Shorts Un-Banned", textcolor=color.red
  717.  
  718.  
  719. // === INPUT BACKTEST RANGE ===
  720.  
  721. singnaltex2 = input(false, "============Backtest Range==============")
  722. FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
  723. FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
  724. FromYear = input(defval = 2019, title = "From Year")
  725. ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
  726. ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
  727. ToYear = input(defval = 9999, title = "To Year")
  728.  
  729. // === FUNCTION EXAMPLE ===
  730. start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
  731. finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
  732. window() => time >= start and time <= finish ? true : false // create function "within window of time"
  733.  
  734.  
  735. strategy.entry("LONG", long=true, when = (LONG[confirmations] or (tradeflip and fliplong)) and window(), qty=positionsize)
  736. strategy.close("LONG", when=longclose and not SHORT and window())
  737.  
  738.  
  739. strategy.entry("SHORT", long=false, when = (SHORT[confirmations] or (tradeflip and flipshort)) and window(), qty=positionsize)
  740. strategy.close("SHORT", when=shortclose and not LONG and window())
  741.  
  742.  
  743. if strategy.opentrades>strategy.opentrades[1] and not maxentries
  744. entryPrice:=strategy.position_avg_price
  745.  
  746. if updatestop and LONG and isLONG[1] and not maxentries
  747. SLlevel:=entryPrice * (1 - SLpercent*.01)
  748. if updatescalp and LONG and isLONG[1] and not maxentries
  749. scalpzone:=entryPrice * (1 + scalppercent*.01)
  750.  
  751. if updatestop and SHORT and isSHORT[1] and not maxentries
  752. SLlevel:=entryPrice * (1 + SLpercent*.01)
  753. if updatescalp and SHORT and isSHORT[1] and not maxentries
  754. scalpzone:=entryPrice * (1 - scalppercent*.01)
  755.  
  756. if strategy.opentrades==pyramiding
  757. maxentries:=true
  758.  
  759.  
  760.  
  761. plot(entryPrice, color=color.green, title="Average Entry Price")
  762. plot(SLlevel, color=((usestops and premium < (fundingthresh*-1) and isSHORT) or (usestops and premium > fundingthresh and isLONG)) ? color.red : na, title="Stop Loss Level")
  763. plot(scalpzone, color=(isLONG and low < scalpzone) or (isSHORT and high > scalpzone)? color.blue : na, title="Scalp Zone")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement