Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- // Copyright (c) 2019-present, Alex Orekhov (everget)
- // Jurik RSX script may be freely distributed under the MIT license.
- // https://www.tradingview.com/script/20TzvKPk-Jurik-RSX/
- //-------------------------------------------------------------------
- // Acknowledgements:
- //---- Base script:
- // RSX Divergence — SharkCIA by Jaggedsoft
- // https://www.tradingview.com/script/ujh3sCzy-RSX-Divergence-SharkCIA/
- // Jurik Moving Average by Everget
- // https://www.tradingview.com/script/nZuBWW9j-Jurik-Moving-Average/
- //---- Divergences/Signals:
- // Libertus RSI Divergences
- // https://www.tradingview.com/script/42wcFfql-Relative-Strength-Index-Divergences-Libertus/
- // Price Divergence Dectector V3 by JustUncle
- // https://www.tradingview.com/script/YJdzOueI-Price-Divergence-Detector-V3-revised-by-JustUncleL/
- // Price Divergence Detector V2 by RicardoSantos
- // https://www.tradingview.com/script/3oeDh0Yq-RS-Price-Divergence-Detector-V2/
- // Stochastic RSI with Divergences by Neobutane
- // https://www.tradingview.com/script/3ogyZpSE-Stochastic-RSI-with-Divergences/
- // CCI Stochastic by Daveatt
- // https://www.tradingview.com/script/XZyG5SOx-CCI-Stochastic-and-a-quick-lesson-on-Scalping-Trading-Systems/
- //---- Misc. Reference:
- // RSI SMA/EMA Cu by Auroagwei
- // https://www.tradingview.com/script/vYRYjPyi-rsi-sma-ema-cu/
- // CBCI Cu by Auroagwei
- // https://www.tradingview.com/script/JhootEsL-cbci-cu/
- // Chop and explode by fhenry0331
- // https://www.tradingview.com/script/5X5mhe3z-Chop-and-explode/
- // T-Step LSMA by RafaelZioni
- // https://www.tradingview.com/script/N4HM2Q00-T-Step-LSMA-VPT/
- // Scripts by Jaggedsoft for structure and formatting
- // Scripts by Everget for structure and formatting
- // If you read this you are gay//
- //-------------------------------------------------------------------
- // RSX-D [ID: AC-P] v08
- // Author: Auroagwei
- // https://www.tradingview.com/u/auroagwei/
- 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)
- // Note: Designed for darkmode by default.
- singnaltext = input(false, "===============ENTRY SIGNALS==================")
- earlycrossings = input(true, "Early M-crossings")
- latecrossings = false//input(false, "Late M-crossings")
- RSXLcrossings = input(true, "RSX/LSMA crossings")
- LibertusDivs = false//input(false, "RSX Divergence Entries")
- NeoRSXRegDivs = false//input(false, "NeoButane RSX regular divs")
- NeoRSXHidDivs = false//input(false, "NeoButane RSX hidden divs")
- NeoLSMARegDivs = false//input(false, "NeoButane LSMA-D regular divs")
- NeoLSMAHidDivs = false//input(false, "NeoButane LSMA-D hidden divs")
- tradeflip = input(true, "Trade Flips after stop-outs")
- singnaltex5 = input(false, "===============EXIT SIGNALS===================")
- DivExit = input(true, "Divergence Take Profit")
- divscalp = input(true, "Regular Div Scalps")
- //hiddiv1 = input(false, "Early Exit on Hidden RSX Div")
- //hiddiv2 = input(false, "Early Exit on Hidden LSMA-D Div")
- singnaltex1 = input(false, "===============OPTIONS==================")
- scalppercent = input(2.0, "Scalp Zone %", step=.1)
- SLpercent = input(0.8, "Stop Loss %", step=.1)
- usestops = input(true, "Use Stop Loss")
- wicktolerant = input(true, "SL wait for close")
- HDban = input(true, "If stopped prevent re-entry until div detected")
- updatestop = input(true, "update stop loss % to avg price (offset by 1 entry)")
- updatescalp = input(true, "update scalp zone % to avg price (offset by 1 entry)")
- singnaltexB = input(false, "===============UNBANS==================")
- LibertusBan = input(true, "RSX Libertus Divergence")
- NeoRSXRegBan = input(false, "NeoButane RSX regular divs")
- NeoRSXHidBan = input(false, "NeoButane RSX hidden divs")
- NeoLSMARegBan = input(false, "NeoButane LSMA-D regular divs")
- NeoLSMAHidBan = input(false, "NeoButane LSMA-D hidden divs")
- HMAban = input(false, "HMA trend")
- timedstop = false//input(false, "use stop loss timeout")
- SLcandles = 200//input(200, "timeout candles")
- margin = input(defval=1.0, title="X leverage")
- totalfunds=margin * strategy.equity
- positionadjuster = input(1, "Equity division")
- pyramiding = input(5, "Pyramiding (copy value from other page!)")
- positionsize = (totalfunds / close) / positionadjuster
- fundingthresh = input(0.001, "Funding Rate Threshold for entries", step=.001)
- fundinglimit = input(0.25, "Funding Rate pump/dump level", step=.01)
- contract = "XBTUSD"
- capFunding = true//input(true, "Cap Funding")
- //alot of calculations lol
- CONTRACT_XBTUSD = "XBTUSD"
- clamp(lowest, highest, subject) =>
- max(lowest, min(highest, subject))
- total(src, length) =>
- result = 0.0
- for i = 0 to length - 1 by 1
- if i < 0
- result := na
- break
- result := result + src[i]
- result
- result
- simpleMovingAverage(src, length) =>
- total(src, length) / length
- barsSince(condition) =>
- bars = 0
- bars := condition ? 0 : bars[1] + 1
- bars
- twap(resetWhen, src) =>
- bars = barsSince(resetWhen) + 1
- simpleMovingAverage(src, bars)
- isBitmexFundingBar = (hour == 4 or hour == 12 or hour == 20) and minute == 0 or timeframe.isdwm
- fundingTwap(src) =>
- twap(isBitmexFundingBar, src)
- bitmexInterestRateTwap(asset) =>
- interestBaseIndex = security("BITMEX:" + asset + "BON", timeframe.period, close) // The Interest Rate for borrowing the Base currency
- interestQuoteIndex = security("BITMEX:USDBON", timeframe.period, close) // The Interest Rate for borrowing the Quote currency
- fundingInterval = 3 // (Since funding occurs every 8 hours)
- interestRate = nz((interestQuoteIndex - interestBaseIndex) / fundingInterval, 0.0001)
- fundingTwap(interestRate)
- bitmexPremiumIndexTwap(asset) =>
- premiumIndexTwap_fallback = security("BITMEX:" + asset + "USDPI", timeframe.period, fundingTwap(ohlc4))
- premiumIndexTwap = security("BITMEX:" + asset + "USDPI", "1", fundingTwap(ohlc4))
- [premiumIndexTwap, premiumIndexTwap_fallback]
- bitmexPredictedFundingRate(asset, capFunding, fundingCapAmount, dampenerAmount, interestRateTwap, premiumIndexTwap) =>
- predictedBaseFundingRateRatio = premiumIndexTwap +
- clamp(-dampenerAmount, dampenerAmount, interestRateTwap - premiumIndexTwap)
- clamp__1 = clamp(-fundingCapAmount, fundingCapAmount, predictedBaseFundingRateRatio)
- predictedFundingRateRatio = capFunding ? clamp__1 : predictedBaseFundingRateRatio
- predictedFundingRateRatio * 100
- bitmexNextFundingRate(predictedFundingRate) =>
- nextFundingRate = 0.01
- nextFundingRate := isBitmexFundingBar ? predictedFundingRate[1] : nextFundingRate[1]
- dwmIntervalModifier = timeframe.isdaily ? 3 :
- timeframe.isweekly ? 3 * 7 : timeframe.ismonthly ? 3 * 30 : na
- dwmModifier = timeframe.multiplier * dwmIntervalModifier
- dwmModifier
- asset = contract == CONTRACT_XBTUSD ? "XBT" : "XBT"
- [premiumIndexTwap_ideal, premiumIndexTwap_fallback] = bitmexPremiumIndexTwap(asset)
- premiumIndexTwap = na(premiumIndexTwap_ideal) ? premiumIndexTwap_fallback : premiumIndexTwap_ideal
- interestRateTwap = bitmexInterestRateTwap(asset)
- predictedFundingRate = bitmexPredictedFundingRate(asset, capFunding, 0.00375, 0.0005, interestRateTwap, premiumIndexTwap)
- //nextFundingRate = bitmexNextFundingRate(predictedFundingRate)
- premium = premiumIndexTwap*100
- confirmations = input(0, "confirmation candle delay")
- isLONG=false
- isLONG:=nz(isLONG[1])
- isSHORT=false
- isSHORT:=nz(isSHORT[1])
- flipmode=false
- flipmode:=nz(flipmode[1])
- /////////////////////////////////////////////////////////////////////////////////ATR
- //nATRPeriod = input(75, "Period")
- //nATRMultip = input(9.5, "Multiplier", minval=0.5, maxval=1000, step=0.1)
- //xATR = atr(nATRPeriod)
- //nLoss = nATRMultip * xATR
- //xATRTrailingStop = 0.0
- //xATRTrailingStop :=
- //iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
- //iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss),
- //iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
- //
- //pos = 0
- //pos :=
- //iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
- //iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
- //atrcolor = flipmode and pos == -1 ? color.red: flipmode and pos == 1 ? color.lime : na
- //patr=plot(xATRTrailingStop, color=atrcolor, linewidth=2, title="ATR Trailing Stop", transp=0)
- n1=input(title="HMA length",defval=150)
- HMAbars=input(1, "# of bars to confirm slope", minval=1)
- n1ma1=2*wma(close,round(n1/2))
- n1ma2=wma(close,n1)
- diff1=n1ma1-n1ma2
- sqn1=round(sqrt(n1))
- hma1 = wma(diff1,sqn1)
- HMAtrendUP = barssince(hma1 < hma1[1]) >= HMAbars
- HMAtrendDOWN = barssince(hma1 > hma1[1]) >= HMAbars
- HMAcolor =
- HMAtrendUP and isLONG? color.green :
- HMAtrendDOWN and isSHORT? color.red :
- HMAtrendUP and isSHORT and premium <= fundingthresh? color.blue :
- HMAtrendDOWN and isLONG and premium >= (fundingthresh*-1)? color.blue : color.blue
- ma1=plot(hma1,color=(flipmode==false? na : HMAcolor),linewidth=2, title="HMA")
- //-------------------------------------------------------------------
- //---- Input Parameters
- length = input(title="RSX: Length", type=input.integer, defval=14)
- src = input(title="RSX: Source", type=input.source, defval=ohlc4)
- obLevel = input(title="RSX: OB Level", type=input.integer, defval=70)
- osLevel = input(title="RSX: OS Level", type=input.integer, defval=30)
- show_rsx= true//input(true, "Show RSX?")
- highlightBreakouts_ema = false//input(false, title="Show EMA21/55 Up/Down Fill")
- highlightBreakouts = true//input(title="RSX: Highlight Overbought/Oversold Breakouts?", type=input.bool, defval=true)
- showdivs_lib = false//input(true, title="RSX: Hide Divs? [Libertus Divs]")
- if showdivs_lib == false
- showdivs_lib := true
- else
- showdivs_lib := false
- piv = true//input(true, "RSX: Hide pivots? [Libertus Divs]")
- shrt = true//input(true, "RSX: Shorter labels for pivots and Divs? [Libertus Divs]")
- xbars = input(650, "RSX: Div lookback period (bars)? [Libertus Divs]", input.integer, minval=1)
- showdivs = true//input(false, title="RSX Divergences [Neobutane Divs]")
- showdiv_labels2 = true//input(true,title="RSX Divergences: Mark Hidden Divs [Neobutane Divs]")
- showchan = false //input(false, title="RSX Divergences: Draw Channel [Neobutane Divs]")
- showhidden = false//input(false, title="RSX Divergences: Show Hidden Divs with low strikerate [Neobutane Divs]")
- showdivs2 = true//input(false, title="LSMA-D Divergences [Neobutane Divs]")
- showdiv_labels22 = true//input(true,title="LSMA-D Divergences: Mark Hidden Divs [Neobutane Divs]")
- showchan2 = false //input(false, title="LSMA-D Divergences: Draw Channel [Neobutane Divs]")
- showhidden2 = false//input(false, title="LSMA-D Divergences: Show Hidden Divs with low strikerate [Neobutane Divs]")
- //---- M1/M2, Midline Trend indication/Zones
- show_m1 = true//input(true, title="Show M1 Level Marker")
- show_m2 = true//input(true, title="Show M2 Level Marker")
- m1Level = input(title="RSX: M1 Level", type=input.integer, defval=85) //60
- m2Level = input(title="RSX: M2 Level", type=input.integer, defval=15) //40
- showArrows = true//input(true, "Show/Hide all zone indication")
- showArrowsCenter = false//input(false, "RSX Midline cross indication")
- showArrowsEnter = true//input(true, "RSX M1/M2 Enter zone indication")
- showArrowsExit = true//input(true, "RSX M1/M2 Exit zone indication")
- showArrowsrsx_lsmaD = true//input(true, "RSX/LSMA-D cross indication")
- rsx_lsmaD_obLevel = input(title="RSX/LSMA-D cross: OB Level", type=input.integer, defval=70)
- rsx_lsmaD_osLevel = input(title="RSX/LSMA-D cross : OS Level", type=input.integer, defval=30)
- //---- LSMA-K and LSMA-D Components
- show_lsmaD = true//input(true,defval=false,type=input.bool,title="Show LSMA-D line?")
- length_lsmaA_lsma = input(minval=2,defval=20,type=input.integer,title="LSMA-D: LSMA-A Length")
- length_lsmaB_lsma = input(minval=2,defval=40,type=input.integer,title="LSMA-D: LSMA-B Length")
- length_lsmaC_lsma = input(minval=2,defval=80,type=input.integer,title="LSMA-D: LSMA-C Length")
- length_lsmaD_lsma = input(minval=2,defval=10,type=input.integer,title="LSMA-D: Length")
- offset_lsmaA = input(defval=0,type=input.integer,title="LSMA-D: LSMA-A Offset")
- offset_lsmaB = input(defval=0,type=input.integer,title="LSMA-D: LSMA-B Offset")
- offset_lsmaC = input(defval=0,type=input.integer,title="LSMA-D: LSMA-C Offset")
- offset_lsmaD = input(defval=-2,type=input.integer,title="LSMA-D: Offset")
- //-------------------------------------------------------------------
- //---- RSX Component (Everget, Jaggedsoft)
- //---- Note: Probably a solid idea to not touch this section.
- f8 = 100 * src
- f10 = nz(f8[1])
- v8 = f8 - f10
- f18 = 3 / (length + 2)
- f20 = 1 - f18
- f28 = 0.0
- f28 := f20 * nz(f28[1]) + f18 * v8
- f30 = 0.0
- f30 := f18 * f28 + f20 * nz(f30[1])
- vC = f28 * 1.5 - f30 * 0.5
- f38 = 0.0
- f38 := f20 * nz(f38[1]) + f18 * vC
- f40 = 0.0
- f40 := f18 * f38 + f20 * nz(f40[1])
- v10 = f38 * 1.5 - f40 * 0.5
- f48 = 0.0
- f48 := f20 * nz(f48[1]) + f18 * v10
- f50 = 0.0
- f50 := f18 * f48 + f20 * nz(f50[1])
- v14 = f48 * 1.5 - f50 * 0.5
- f58 = 0.0
- f58 := f20 * nz(f58[1]) + f18 * abs(v8)
- f60 = 0.0
- f60 := f18 * f58 + f20 * nz(f60[1])
- v18 = f58 * 1.5 - f60 * 0.5
- f68 = 0.0
- f68 := f20 * nz(f68[1]) + f18 * v18
- f70 = 0.0
- f70 := f18 * f68 + f20 * nz(f70[1])
- v1C = f68 * 1.5 - f70 * 0.5
- f78 = 0.0
- f78 := f20 * nz(f78[1]) + f18 * v1C
- f80 = 0.0
- f80 := f18 * f78 + f20 * nz(f80[1])
- v20 = f78 * 1.5 - f80 * 0.5
- f88_ = 0.0
- f90_ = 0.0
- f88 = 0.0
- f90_ := nz(f90_[1]) == 0 ? 1 :
- nz(f88[1]) <= nz(f90_[1]) ? nz(f88[1]) + 1 : nz(f90_[1]) + 1
- f88 := nz(f90_[1]) == 0 and length - 1 >= 5 ? length - 1 : 5
- f0 = f88 >= f90_ and f8 != f10 ? 1 : 0
- f90 = f88 == f90_ and f0 == 0 ? 0 : f90_
- v4_ = f88 < f90 and v20 > 0 ? (v14 / v20 + 1) * 50 : 50
- rsx = v4_ > 100 ? 100 : v4_ < 0 ? 0 : v4_
- rsxColor = rsx > obLevel ? #0ebb23 : rsx < osLevel ? #ff0000 : color.white //#512DA8
- lsmaA = linreg(rsx,length_lsmaA_lsma,offset_lsmaA)
- lsmaB = linreg(rsx,length_lsmaB_lsma,offset_lsmaB)
- lsmaC = linreg(rsx,length_lsmaC_lsma,offset_lsmaC)
- lsmaD = linreg(((lsmaA + lsmaB + lsmaC) / 3), length_lsmaD_lsma,offset_lsmaD)
- //-------------------------------------------------------------------
- //---- Chassis
- transparent = color.new(color.white, 100)
- //maxLevelPlot = hline(100, title="Max Level", linestyle=hline.style_dotted, color=transparent)
- //obLevelPlot = hline(obLevel, title="Overbought Level", linestyle=hline.style_dotted)
- //midline = hline(50, title="Middle Level/Midline", linestyle=hline.style_dotted,color=color.white) //color.silver)
- //osLevelPlot = hline(osLevel, title="Oversold Level", linestyle=hline.style_dotted)//,color=color.gray)
- //minLevelPlot = hline(0, title="Min Level", linestyle=hline.style_dotted, color=transparent)
- //fill(obLevelPlot, osLevelPlot, color=color.purple, transp=100,title="RSX OB <--> OS Fill")
- //---- M1/M2 Marker
- // Custom M1/M2 levels of 40/60 by default
- // Set to transparent to keep out of the way if not using/needed
- m1_color = show_m1 ? color.aqua : transparent
- m2_color = show_m2 ? color.orange : transparent
- //m1LevelPlot = hline(m1Level, title="RSX: M1 Level", linestyle=hline.style_dotted,color=m1_color)
- //m2LevelPlot = hline(m2Level , title="RSX: M2 Level", linestyle=hline.style_dotted,color=m2_color)
- //----
- obFillColor = rsx > obLevel and highlightBreakouts ? #008000 : transparent
- osFillColor = rsx < osLevel and highlightBreakouts ? #FF0000 : transparent
- //fill(maxLevelPlot, obLevelPlot, color=obFillColor, transp=90, title="RSX OB Fill")
- //fill(minLevelPlot, osLevelPlot, color=osFillColor, transp=90, title="RSX OS Fill")
- //////////////////
- ema21 = ema(close, 21) //fib
- ema55 = ema(close, 55) //fib
- upEmaFillColor = ema21 > ema55 and highlightBreakouts_ema ? color.orange : transparent
- downEmaFillColor = ema21 < ema55 and highlightBreakouts_ema ? color.aqua : transparent
- //fill(obLevelPlot, m1LevelPlot, color=upEmaFillColor, transp=70,title="EMA 21/55 Up Fill")
- //fill(osLevelPlot, m2LevelPlot, color=downEmaFillColor, transp=70,title="EMA 21/55 Down Fill")
- ////////////////
- //-------------------------------------------------------------------
- //plot(show_rsx ? rsx : na , title="RSX", linewidth=2, color=rsxColor, transp=0)
- //-------------------------------------------------------------------
- // MA of RSX
- show_sma = false//input(false, "SMA of RSX")
- show_ema = false//input(false, "EMA of RSX")
- len_sma = 9//input(9, minval=1, title="SMA of RSX Length")
- len_ema = 45//input(45, minval=1, title="EMA of RSX Length")
- smaRSX = ema(rsx,len_sma )
- emaRSX = ema(rsx,len_ema )
- //plot(show_sma ? smaRSX : na, title="SMA of RSX", style=plot.style_line, linewidth=2, color=#26c6da,transp=0)
- //plot(show_ema ? emaRSX : na, title="EMA of RSX", style=plot.style_line, linewidth=2, color=#008000 ,transp=0)
- //
- //plot(show_lsmaD ? lsmaD : na, color=color.fuchsia, linewidth=2,title="LSMA-D",transp=0)
- //-------------------------------------------------------------------
- //---- Pivots and Libertus Divergences Component
- hb = abs(highestbars(rsx, xbars)) // Finds bar with highest value in last X bars
- lb = abs(lowestbars(rsx, xbars)) // Finds bar with lowest value in last X bars
- max = float(na)
- max_rsi = float(na)
- min = float(na)
- min_rsi = float(na)
- pivoth = bool(na)
- pivotl = bool(na)
- divbear = bool(na)
- divbull = bool(na)
- // If bar with lowest / highest is current bar, use it's value
- max := hb == 0 ? close : na(max[1]) ? close : max[1]
- max_rsi := hb == 0 ? rsx : na(max_rsi[1]) ? rsx : max_rsi[1]
- min := lb == 0 ? close : na(min[1]) ? close : min[1]
- min_rsi := lb == 0 ? rsx : na(min_rsi[1]) ? rsx : min_rsi[1]
- // Compare high of current bar being examined with previous bar's high
- // If curr bar high is higher than the max bar high in the lookback window range
- if close > max // we have a new high
- max := close // change variable "max" to use current bar's high value
- max
- if rsx > max_rsi // we have a new high
- max_rsi := rsx // change variable "max_rsi" to use current bar's RSI value
- max_rsi
- if close < min // we have a new low
- min := close // change variable "min" to use current bar's low value
- min
- if rsx < min_rsi // we have a new low
- min_rsi := rsx // change variable "min_rsi" to use current bar's RSI value
- min_rsi
- // Finds pivot point with at least 2 right candles with lower value
- pivoth := max_rsi == max_rsi[2] and max_rsi[2] != max_rsi[3] ? true : na
- pivotl := min_rsi == min_rsi[2] and min_rsi[2] != min_rsi[3] ? true : na
- // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
- if max[1] > max[2] and rsx[1] < max_rsi and rsx <= rsx[1]
- divbear := true
- divbear
- if min[1] < min[2] and rsx[1] > min_rsi and rsx >= rsx[1]
- divbull := true
- divbull
- //-------------------------------------------------------------------
- //---- Secondary Divergence Component for RSX + LSMA-D (Neobutane Divergences)
- k=rsx
- k2=lsmaD
- //----
- uselog = true // input(true, title="Log")
- //@RicardoSantos' Divergence Script (https://www.tradingview.com/script/3oeDh0Yq-RS-Price-Divergence-Detector-V2/)
- f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
- f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
- f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
- //----
- fractal_top = f_fractalize(k) > 0 ? k[2] : na
- fractal_bot = f_fractalize(k) < 0 ? k[2] : na
- fractal_top2 = f_fractalize(k2) > 0 ? k2[2] : na
- fractal_bot2 = f_fractalize(k2) < 0 ? k2[2] : na
- high_prev = valuewhen(fractal_top, k[2], 0)[2]
- high_price = valuewhen(fractal_top, high[2], 0)[2]
- low_prev = valuewhen(fractal_bot, k[2], 0)[2]
- low_price = valuewhen(fractal_bot, low[2], 0)[2]
- high_prev2 = valuewhen(fractal_top2, k2[2], 0)[2]
- high_price2 = valuewhen(fractal_top2, high[2], 0)[2]
- low_prev2 = valuewhen(fractal_bot2, k2[2], 0)[2]
- low_price2 = valuewhen(fractal_bot2, low[2], 0)[2]
- regular_bearish_div = fractal_top and high[2] > high_price and k[2] < high_prev
- hidden_bearish_div = fractal_top and high[2] < high_price and k[2] > high_prev
- regular_bullish_div = fractal_bot and low[2] < low_price and k[2] > low_prev
- hidden_bullish_div = fractal_bot and low[2] > low_price and k[2] < low_prev
- regular_bearish_div2 = fractal_top2 and high[2] > high_price2 and k[2] < high_prev2
- hidden_bearish_div2 = fractal_top2 and high[2] < high_price2 and k[2] > high_prev2
- regular_bullish_div2 = fractal_bot2 and low[2] < low_price2 and k[2] > low_prev2
- hidden_bullish_div2 = fractal_bot2 and low[2] > low_price2 and k[2] < low_prev2
- col1 = regular_bearish_div ? #FF0000 : hidden_bearish_div and showhidden ? #FF0000 : na
- col2 = regular_bullish_div ? #00FF00 : hidden_bullish_div and showhidden ? #00FF00 : na
- col3 = regular_bearish_div ? #FF0000 : hidden_bearish_div and showhidden ? #FF0000 : showchan ? color.gray : na
- col4 = regular_bullish_div ? #00FF00 : hidden_bullish_div and showhidden ? #00FF00 : showchan ? color.gray : na
- col12 = regular_bearish_div2 ? #FF0000 : hidden_bearish_div2 and showhidden2 ? #FF0000 : na
- col22 = regular_bullish_div2 ? #00FF00 : hidden_bullish_div2 and showhidden2 ? #00FF00 : na
- col32 = regular_bearish_div2 ? #FF0000 : hidden_bearish_div2 and showhidden2 ? #FF0000 : showchan ? color.gray : na
- col42 = regular_bullish_div2 ? #00FF00 : hidden_bullish_div2 and showhidden2 ? #00FF00 : showchan ? color.gray : na
- //---- M1/M2 and Trending Signal Alert componenent [Daveatt CCI Stoch]
- // showArrows = true
- // showArrowsEnter = true
- // showArrowsExit = true
- // showArrowsCenter = true
- trend_enter = if showArrowsEnter
- if crossunder(rsx, m2Level)
- 1
- else
- if crossover(rsx, m1Level)
- -1
- trend_exit = if showArrowsExit
- if crossunder(rsx, m1Level)
- -1
- else
- if crossover(rsx, m2Level)
- 1
- trend_center = if showArrowsCenter
- if crossunder(rsx, 50)
- -1
- else
- if crossover(rsx, 50)
- 1
- rsx_lsmaD_cu = rsx >= rsx_lsmaD_obLevel and crossunder(rsx, lsmaD)
- rsx_lsmaD_co = rsx <= rsx_lsmaD_osLevel and crossover(rsx, lsmaD)
- //RSXCOMid = (showArrowsCenter and trend_center == 1)
- //RSXCUMid = (showArrowsCenter and trend_center == -1)
- RSXCUM1 = (showArrowsExit and trend_exit == -1)
- RSXCOM1 = (showArrowsEnter and trend_enter == -1)
- RSXCUM2 = (showArrowsEnter and trend_enter == 1)
- RSXCOM2 = (showArrowsExit and trend_exit == 1)
- RSXCULSMAD = (rsx_lsmaD_cu)
- RSXCOLSMAD = (rsx_lsmaD_co)
- entryPrice=0.0
- entryPrice:=nz(entryPrice[1])
- SLlevel=0.0
- SLlevel:=nz(SLlevel[1])
- scalpzone=0.0
- scalpzone:=nz(scalpzone[1])
- longban=false
- longban:=nz(longban[1])
- shortban=false
- shortban:=nz(shortban[1])
- maxentries = false
- maxentries:=nz(maxentries[1])
- if longban and (predictedFundingRate < (fundingthresh*-1)) and
- ((LibertusBan ? divbull : false) or
- (NeoRSXRegBan ? regular_bullish_div and premium < (fundingthresh*-1) : false) or
- (NeoLSMARegBan ? regular_bullish_div2 and premium < (fundingthresh*-1) : false) or
- (NeoRSXHidBan ? hidden_bullish_div and premium < (fundingthresh*-1) : false) or
- (NeoRSXHidBan ? hidden_bullish_div2 and premium < (fundingthresh*-1) : false))
- longban:=false
- if longban and HMAban and HMAtrendUP
- longban:=false
- if shortban and predictedFundingRate > fundingthresh and
- ((LibertusBan ? divbear : false) or
- (NeoRSXRegBan ? regular_bearish_div and premium > fundingthresh : false) or
- (NeoLSMARegBan ? regular_bearish_div2 and premium > fundingthresh : false) or
- (NeoRSXHidBan ? hidden_bearish_div and premium > fundingthresh : false) or
- (NeoRSXHidBan ? hidden_bearish_div2 and premium > fundingthresh : false))
- shortban:=false
- if shortban and HMAban and HMAtrendDOWN
- shortban:=false
- LONG = not longban and (predictedFundingRate > (-1*fundinglimit)) and
- (predictedFundingRate < (fundingthresh*-1)) and
- ((earlycrossings ? RSXCUM2 : false) or
- (latecrossings ? RSXCOM2 : false) or
- (RSXLcrossings ? RSXCOLSMAD : false) or
- (LibertusDivs ? divbull : false) or
- (NeoRSXRegDivs ? regular_bullish_div : false) or
- (NeoRSXHidDivs ? hidden_bullish_div : false) or
- (NeoLSMARegDivs ? regular_bullish_div2 : false) or
- (NeoLSMAHidDivs ? hidden_bullish_div2 : false))
- SHORT = not shortban and (predictedFundingRate < fundinglimit) and
- (predictedFundingRate > fundingthresh) and
- ((earlycrossings ? RSXCOM1 : false) or
- (latecrossings ? RSXCUM1 : false) or
- (RSXLcrossings ? RSXCULSMAD : false) or
- (LibertusDivs ? divbear : false) or
- (NeoRSXRegDivs ? regular_bearish_div : false) or
- (NeoRSXHidDivs ? hidden_bearish_div : false) or
- (NeoLSMARegDivs ? regular_bearish_div2 : false) or
- (NeoLSMAHidDivs ? hidden_bearish_div2 : false))
- if LONG and not isLONG
- entryPrice:=close
- SLlevel:=entryPrice * (1 - SLpercent*.01)
- scalpzone:=entryPrice * (1 + scalppercent*.01)
- isSHORT:=false
- shortban:=false
- maxentries:=false
- isLONG:=true
- if SHORT and not isSHORT
- entryPrice:=close
- SLlevel:=entryPrice * (1 + SLpercent*.01)
- scalpzone:=entryPrice * (1 - scalppercent*.01)
- isLONG:=false
- longban:=false
- maxentries:=false
- isSHORT:=true
- if LONG and flipmode
- entryPrice:=close
- SLlevel:=entryPrice * (1 - SLpercent*.01)
- scalpzone:=entryPrice * (1 + scalppercent*.01)
- isSHORT:=false
- shortban:=false
- maxentries:=false
- isLONG:=true
- flipmode:=false
- if SHORT and flipmode
- entryPrice:=close
- SLlevel:=entryPrice * (1 + SLpercent*.01)
- scalpzone:=entryPrice * (1 - scalppercent*.01)
- isLONG:=false
- longban:=false
- maxentries:=false
- isSHORT:=true
- flipmode:=false
- longprofit = not flipmode and DivExit and ((predictedFundingRate > fundingthresh) and premium > fundingthresh and divbear and not LibertusDivs) and isLONG
- longstopped = not flipmode and (wicktolerant ? close : low) < SLlevel and usestops and (premium > fundingthresh or predictedFundingRate < (-1*fundinglimit)) and isLONG
- //longtimeout = not flipmode and usestops and timedstop and isLONG and barssince((wicktolerant ? close : low) >= SLlevel) > SLcandles
- longscalped = not flipmode and divscalp and (scalpzone > close) and divbear and isLONG
- shortprofit = not flipmode and DivExit and ((predictedFundingRate < (fundingthresh*-1)) and premium < (fundingthresh*-1) and divbull and not LibertusDivs) and isSHORT
- shortstopped= not flipmode and (wicktolerant ? close : high) > SLlevel and usestops and (premium < (fundingthresh*-1) or predictedFundingRate > fundinglimit) and isSHORT
- //shorttimeout= not flipmode and usestops and timedstop and isSHORT and barssince((wicktolerant ? close : high) <= SLlevel) > SLcandles
- shortscalped= not flipmode and divscalp and (scalpzone < close) and divbull and isSHORT
- longclose = longstopped or longscalped or longprofit// or longtimeout
- shortclose= shortstopped or shortscalped or shortprofit// or shorttimeout
- if longclose
- isLONG:=false
- entryPrice:=0.0
- SLlevel:=0.0
- scalpzone:=0.0
- maxentries:=false
- if shortclose
- isSHORT:=false
- entryPrice:=0.0
- SLlevel:=0.0
- scalpzone:=0.0
- maxentries:=false
- if longstopped and HDban
- longban:=true
- if shortstopped and HDban
- shortban:=true
- if tradeflip and (shortstopped or longstopped)
- flipmode:=true
- fliplong = flipmode and ((not flipmode[1] and HMAtrendUP) or (flipmode[1] and isSHORT and HMAtrendUP and premium < (fundingthresh*-1)))
- flipshort = flipmode and ((not flipmode[1] and HMAtrendDOWN) or (flipmode[1] and isLONG and HMAtrendDOWN and premium > fundingthresh))
- if fliplong
- entryPrice:=close
- //SLlevel:=entryPrice * (1 - SLpercent*.01)
- //scalpzone:=entryPrice * (1 + scalppercent*.01)
- isSHORT:=false
- maxentries:=false
- isLONG:=true
- if flipshort
- entryPrice:=close
- //SLlevel:=entryPrice * (1 + SLpercent*.01)
- //scalpzone:=entryPrice * (1 - scalppercent*.01)
- isLONG:=false
- maxentries:=false
- isSHORT:=true
- bgcolor(divbull and predictedFundingRate < (fundingthresh*-1)? color.green : na)
- bgcolor(divbear and predictedFundingRate > fundingthresh? color.red : na)
- barcolor(divbull? color.yellow : na)
- barcolor(divbear? color.blue : na)
- //barcolor(flipmode and isLONG? color.green : flipmode and isSHORT ? color.red : na)
- plotshape(longban and not longban[1], style=shape.triangleup, location=location.belowbar, color=color.yellow, size=size.small) //text="Longs Banned", textcolor=color.red
- 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
- plotshape(shortban and not shortban[1], style=shape.triangledown, location=location.abovebar, color=color.yellow, size=size.small) //text="Shorts Banned", textcolor=color.green
- 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
- // === INPUT BACKTEST RANGE ===
- singnaltex2 = input(false, "============Backtest Range==============")
- FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
- FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
- FromYear = input(defval = 2019, title = "From Year")
- ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
- ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
- ToYear = input(defval = 9999, title = "To Year")
- // === FUNCTION EXAMPLE ===
- start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
- finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
- window() => time >= start and time <= finish ? true : false // create function "within window of time"
- strategy.entry("LONG", long=true, when = (LONG[confirmations] or (tradeflip and fliplong)) and window(), qty=positionsize)
- strategy.close("LONG", when=longclose and not SHORT and window())
- strategy.entry("SHORT", long=false, when = (SHORT[confirmations] or (tradeflip and flipshort)) and window(), qty=positionsize)
- strategy.close("SHORT", when=shortclose and not LONG and window())
- if strategy.opentrades>strategy.opentrades[1] and not maxentries
- entryPrice:=strategy.position_avg_price
- if updatestop and LONG and isLONG[1] and not maxentries
- SLlevel:=entryPrice * (1 - SLpercent*.01)
- if updatescalp and LONG and isLONG[1] and not maxentries
- scalpzone:=entryPrice * (1 + scalppercent*.01)
- if updatestop and SHORT and isSHORT[1] and not maxentries
- SLlevel:=entryPrice * (1 + SLpercent*.01)
- if updatescalp and SHORT and isSHORT[1] and not maxentries
- scalpzone:=entryPrice * (1 - scalppercent*.01)
- if strategy.opentrades==pyramiding
- maxentries:=true
- plot(entryPrice, color=color.green, title="Average Entry Price")
- plot(SLlevel, color=((usestops and premium < (fundingthresh*-1) and isSHORT) or (usestops and premium > fundingthresh and isLONG)) ? color.red : na, title="Stop Loss Level")
- 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