本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("RSI Split Candles - Multi-Timeframe", shorttitle = "RSISplit", overlay = false, behind_chart = false,
max_bars_back = 5000, max_lines_count = 500, max_labels_count = 50)
gLeg = "Legend: what each profile does (use the second timeframe)"
gProf = "0 Trading profile"
profileIn = input.string("Day trading", "Trading profile", options = ["Position trading", "Day trading", "Scalping", "Micro scalping", "Custom"], group = gProf, tooltip = "Position trading: the weekly arms and the daily triggers. Day trading: the daily arms and the four hour triggers. Scalping: the four hour arms and the fifteen minutes trigger. Micro scalping: the fifteen minutes arm and the one minute triggers. Custom: the two timeframes below. Each profile carries its own fixed internal filter that is not exposed in the settings. Open your chart on the SECOND, LOWER timeframe of the profile, because that is where the trigger is evaluated and where the signals are printed. Scalping on a four hour chart will not show the scalping signals, and scalping on a fifteen minute chart will.", display = display.all - display.status_line)
customHtf = input.timeframe("D", "Custom profile: higher timeframe", group = gProf, tooltip = "Used only when the profile is set to Custom.", display = display.all - display.status_line)
customLtf = input.timeframe("240", "Custom profile: lower timeframe", group = gProf, tooltip = "Used only when the profile is set to Custom.", display = display.all - display.status_line)
legendA = input.string("1W / 1D", "Position trading", group = gLeg, tooltip = "Reference only. The first timeframe arms the setup, the second one triggers it. Use the second, lower timeframe as your chart timeframe, otherwise the trigger timeframe sits below the chart and the signals will not match what you see.", display = display.all - display.status_line)
legendB = input.string("1D / 4H", "Day trading", group = gLeg, tooltip = "Reference only. The first timeframe arms the setup, the second one triggers it. Use the second, lower timeframe as your chart timeframe, otherwise the trigger timeframe sits below the chart and the signals will not match what you see.", display = display.all - display.status_line)
legendC = input.string("4H / 15M", "Scalping", group = gLeg, tooltip = "Reference only. The first timeframe arms the setup, the second one triggers it. Use the second, lower timeframe as your chart timeframe, otherwise the trigger timeframe sits below the chart and the signals will not match what you see.", display = display.all - display.status_line)
legendD = input.string("15M / 1M", "Micro scalping", group = gLeg, tooltip = "Reference only. The first timeframe arms the setup, the second one triggers it. Use the second, lower timeframe as your chart timeframe, otherwise the trigger timeframe sits below the chart and the signals will not match what you see.", display = display.all - display.status_line)
gNR = "1 Non-repainting"
waitClose = input.bool(true, "Wait for candle close confirmation", group = gNR, tooltip = "ON (default): the forming candle is not drawn and every locked value holds its last closed reading, so nothing you see can change afterwards. OFF: everything updates live while the bar forms, history is never rewritten.", display = display.all - display.status_line)
latched = input.bool(true, "Let a pending cross wait for the filters", group = gNR, tooltip = "ON (default): a cross that happens while a filter is still closed is remembered and printed as soon as everything aligns. If both a 50 cross and a 70 cross are waiting, the 50 cross prints first, as the first impulse, and the 70 cross is then re-armed and needs a fresh crossing. OFF: a square prints only on the bar of the cross itself and is dropped if a filter is not yet open, so a cross that precedes the filter is lost.", display = display.all - display.status_line)
armDev = input.bool(true, "Arm uses the developing period", group = gNR, tooltip = "ON (default): the arm leg is rebuilt bar by bar on your chart, for every profile (weekly for Position trading, daily for Day trading, 4H for Scalping, 15m for Micro scalping). It reacts as soon as the arm condition is met instead of waiting for that timeframe to close, and every completed period still gives the identical value. OFF: the arm uses only the closed higher timeframe value, which steps once per period and reacts later.", display = display.all - display.status_line)
gR = "2 Candle RSI (candle colours only)"
rsiLen = input.int(14, "RSI length", minval = 2, group = gR, tooltip = "Length used to colour the candles and to size the buy / sell split. It drives the candles only: the five oscillator rows have their own length in groups 5a to 5e, and the signal engine has its own lengths. Change this only if you want a different candle colouring than the rows.", display = display.all - display.status_line)
rsiSrc = input.source(close, "RSI source", group = gR, tooltip = "Price series the reading is calculated from. Close is the standard choice.", display = display.all - display.status_line)
rsiVW = input.bool(false, "Volume-weighted RSI", group = gR, tooltip = "Weights each move by volume, so a move on heavy volume counts more. Affects the candle colours only; the five oscillator rows and the signal engine are never volume weighted.", display = display.all - display.status_line)
obLvl = input.int(70, "Overbought level (green)", minval = 1, maxval = 99, group = gR, tooltip = "Upper threshold. Above it the candles, the rows and the extreme signal turn green.", display = display.all - display.status_line)
osLvl = input.int(30, "Oversold level (red)", minval = 1, maxval = 99, group = gR, tooltip = "Lower threshold. Below it the candles, the rows and the extreme signal turn red.", display = display.all - display.status_line)
gCol = "2b Candle colours"
cZOB = input.color(#1fff00, "Candles: RSI > overbought", group = gCol, tooltip = "Candle colour when the reading is above the overbought level.", display = display.all - display.status_line)
cZUp = input.color(#ffffff, "Candles: RSI 50 to overbought", group = gCol, tooltip = "Candle colour between 50 and the overbought level.", display = display.all - display.status_line)
cZDn = input.color(#000000, "Candles: RSI oversold to 50", group = gCol, tooltip = "Candle colour between the oversold level and 50.", display = display.all - display.status_line)
cZOS = input.color(#cb0000, "Candles: RSI < oversold", group = gCol, tooltip = "Candle colour below the oversold level.", display = display.all - display.status_line)
wickT = input.int(0, "Wick transparency", minval = 0, maxval = 100, group = gCol, tooltip = "0 = solid wick. 100 hides the wick.", display = display.all - display.status_line)
gOsc = "2c Oscillator rows"
rowT = input.int(55, "Row transparency", minval = 0, maxval = 90, group = gOsc, tooltip = "Transparency of all five oscillator rows.", display = display.all - display.status_line)
rowW = input.int(1, "Row width (histogram and line)", minval = 1, maxval = 20, group = gOsc, tooltip = "Thickness used by the Columns and Histogram row styles.", display = display.all - display.status_line)
showSep = input.bool(false, "Show the 1-5 row separators", group = gOsc, tooltip = "Draws a thin line at each of the 1 to 5 row boundaries.", display = display.all - display.status_line)
sepCol = input.color(#2A2A2A, "Row separator colour", group = gOsc, tooltip = "Colour of the row separators.", display = display.all - display.status_line)
gP = "3 Buy / sell pressure"
showPress = input.bool(true, "Show buy / sell split", group = gP, tooltip = "Splits every candle into a buyer block and a seller block.", display = display.all - display.status_line)
splitM = input.string("Range proxy", "Split estimation", options = ["Range proxy", "Intrabar (LTF)", "Fixed 50 / 50"], group = gP, tooltip = "How the buyer and seller share is estimated. Range proxy uses the position of the close inside the high to low range. Intrabar reads a lower timeframe inside each bar and weighs the volume by direction. Fixed 50 / 50 splits every bar equally.", display = display.all - display.status_line)
ltfMode = input.string("Auto", "Intrabar timeframe", options = ["Auto", "Manual"], group = gP, tooltip = "Auto resolves a lower timeframe from your chart timeframe. Manual must be lower than the chart timeframe.", display = display.all - display.status_line)
ltfTF = input.timeframe("5", "Manual intrabar timeframe", group = gP, tooltip = "Lower timeframe used to measure the buyer and seller share inside each candle. Must be lower than the chart timeframe.", display = display.all - display.status_line)
sellTrans = input.int(55, "Seller segment transparency", minval = 0, maxval = 90, group = gP, tooltip = "Transparency of the seller part of the split. 55 gives a solid buyer block and a faded seller block.", display = display.all - display.status_line)
transMode = input.string("Fixed", "Seller transparency behaviour", options = ["Fixed", "Scales with sellers"], group = gP, tooltip = "Fixed: every seller segment uses the same transparency. Scales with sellers: the transparency grows with the seller share.", display = display.all - display.status_line)
anchorMode = input.string("Bottom", "Split anchor", options = ["Bottom", "Close side"], group = gP, tooltip = "Bottom: buyers always occupy the bottom of the split, the seller share sits on top. Close side: buyers hug the close.", display = display.all - display.status_line)
splitArea = input.string("Body", "Split area", options = ["Body", "Full range (high to low)"], group = gP, tooltip = "Body splits only the candle body. Full range splits the whole high to low range.", display = display.all - display.status_line)
gMix = "3b Seller tone blending"
blendMode = input.string("Pre-blended (opaque)", "Seller tone rendering", options = ["Pre-blended (opaque)", "True transparency"], group = gMix, tooltip = "Pre-blended: the faded seller tone is computed against the chart background and painted fully opaque, so the colour is identical to the pane and nothing behind the candle can bleed through. True transparency only matches the pane when the normal candles are hidden.", display = display.all - display.status_line)
blendBg = input.color(#807C5A, "Pre-blend colour (match your chart background)", group = gMix, tooltip = "Used only by the Pre-blended seller tone. It does not paint the chart: it is the colour the faded seller tone is mixed with, so the result looks like real transparency. Set it to your chart background colour. The default 807C5A is the olive background of this setup.", display = display.all - display.status_line)
gRen = "3c Candle rendering"
pressMode = input.string("Split body", "Pressure rendering", options = ["Split body", "Whole body (alpha)"], group = gRen, tooltip = "Split body divides the candle at the buyer and seller boundary. Whole body applies a single tone.", display = display.all - display.status_line)
coverFrame = input.bool(true, "Cover the native candle frame", group = gRen, tooltip = "A native TradingView candle is the body fill plus a one pixel border. With this ON our border is painted in its own colour, so our candle occupies exactly the same footprint as the native one and hides it completely.", display = display.all - display.status_line)
showDiv = input.bool(false, "Divider line at the buy / sell boundary", group = gRen, tooltip = "Draws a short line at the buyer and seller boundary of every split candle.", display = display.all - display.status_line)
divCol = input.color(#000000, "Divider colour", group = gRen, tooltip = "Colour of the divider line.", display = display.all - display.status_line)
divW = input.int(1, "Divider width", minval = 1, maxval = 4, group = gRen, tooltip = "Thickness of the divider line in pixels.", display = display.all - display.status_line)
gReg = "4 Signal markers"
showSig = input.string("Markers", "Signal markers", options = ["Markers", "Off"], group = gReg, tooltip = "Markers are printed only when the higher timeframe is armed and the lower timeframe triggers. Bullish markers always sit below, bearish markers above. Off: no markers on the chart.", display = display.all - display.status_line)
markerStyleIn = input.string("Square", "Marker style", options = ["Square", "Circle", "Diamond", "Triangle up", "Triangle down", "Cross", "X-cross", "Flag"], group = gReg, tooltip = "Shape used for every signal marker.", display = display.all - display.status_line)
placeMode = input.string("Pane edges", "Marker placement", options = ["Price offset", "Pane edges", "Bar offset"], group = gReg, tooltip = "Price offset: bullish markers a fixed distance below the lowest low of the window and bearish markers the same distance above the highest high, so they never touch the candles. Pane edges: bullish at the bottom edge of the chart and bearish at the top edge. Bar offset: bullish below the bar and bearish above the bar.", display = display.all - display.status_line)
sqLook = input.int(150, "Offset window (bars)", minval = 20, group = gReg, tooltip = "How many bars are scanned for the extreme that anchors the markers.", display = display.all - display.status_line)
sqGap = input.float(0.6, "Gap from that extreme (x ATR)", minval = 0.0, maxval = 5.0, step = 0.1, group = gReg, tooltip = "Distance between the marker and the extreme of the window, measured in ATR.", display = display.all - display.status_line)
gSigCol = "4b Signal colours"
sigUp1 = input.color(#268C17, "First impulse bullish", group = gSigCol, tooltip = "Colour of the first impulse bullish signal, printed when the reading crosses 50 up.", display = display.all - display.status_line)
sigUp2 = input.color(#1FFF00, "Extreme signal bullish", group = gSigCol, tooltip = "Colour of the extreme bullish signal, printed when the reading crosses the overbought level up.", display = display.all - display.status_line)
sigDn1 = input.color(#A51212, "First impulse bearish", group = gSigCol, tooltip = "Colour of the first impulse bearish signal, printed when the reading crosses 50 down.", display = display.all - display.status_line)
sigDn2 = input.color(#CB0000, "Extreme signal bearish", group = gSigCol, tooltip = "Colour of the extreme bearish signal, printed when the reading crosses the oversold level down.", display = display.all - display.status_line)
gTf = "5 Dashboard timeframes (top to bottom)"
tfB = input.timeframe("15", "Row 2", group = gTf, tooltip = "Timeframe of the second row.", display = display.all - display.status_line)
tfC = input.timeframe("240", "Row 3", group = gTf, tooltip = "Timeframe of the third row.", display = display.all - display.status_line)
tfD = input.timeframe("D", "Row 4", group = gTf, tooltip = "Timeframe of the fourth row.", display = display.all - display.status_line)
tfE = input.timeframe("W", "Row 5 (bottom)", group = gTf, tooltip = "Timeframe of the fifth row.", display = display.all - display.status_line)
gR1 = "5a Row 1 - chart"
r1Show = input.bool(true, "Show this row", group = gR1, tooltip = "Hides or shows this row and its value label.", display = display.all - display.status_line)
r1Len = input.int(14, "RSI length", minval = 2, maxval = 200, group = gR1, tooltip = "Lookback of this row only. All five rows default to 14 and are independent of each other and of the signal engine.", display = display.all - display.status_line)
r1Style = input.string("Area", "Row style", options = ["Area", "Columns", "Histogram"], group = gR1, tooltip = "Drawing style of this row. Area fills the band, Columns and Histogram draw one bar per candle.", display = display.all - display.status_line)
r1OB = input.color(#1fff00, "RSI > overbought", group = gR1, tooltip = "Colour when this row is above the overbought level.", display = display.all - display.status_line)
r1Up = input.color(#ffffff, "RSI 50 to overbought", group = gR1, tooltip = "Colour between 50 and the overbought level.", display = display.all - display.status_line)
r1Dn = input.color(#000000, "RSI oversold to 50", group = gR1, tooltip = "Colour between the oversold level and 50.", display = display.all - display.status_line)
r1OS = input.color(#cb0000, "RSI < oversold", group = gR1, tooltip = "Colour below the oversold level.", display = display.all - display.status_line)
r1Sty = r1Style == "Columns" ? plot.style_columns : r1Style == "Histogram" ? plot.style_histogram : plot.style_area
gR2 = "5b Row 2 - 15"
r2Show = input.bool(true, "Show this row", group = gR2, tooltip = "Hides or shows this row and its value label.", display = display.all - display.status_line)
r2Len = input.int(14, "RSI length", minval = 2, maxval = 200, group = gR2, tooltip = "Lookback of this row only. All five rows default to 14 and are independent of each other and of the signal engine.", display = display.all - display.status_line)
r2Style = input.string("Area", "Row style", options = ["Area", "Columns", "Histogram"], group = gR2, tooltip = "Drawing style of this row. Area fills the band, Columns and Histogram draw one bar per candle.", display = display.all - display.status_line)
r2OB = input.color(#1fff00, "RSI > overbought", group = gR2, tooltip = "Colour when this row is above the overbought level.", display = display.all - display.status_line)
r2Up = input.color(#ffffff, "RSI 50 to overbought", group = gR2, tooltip = "Colour between 50 and the overbought level.", display = display.all - display.status_line)
r2Dn = input.color(#000000, "RSI oversold to 50", group = gR2, tooltip = "Colour between the oversold level and 50.", display = display.all - display.status_line)
r2OS = input.color(#cb0000, "RSI < oversold", group = gR2, tooltip = "Colour below the oversold level.", display = display.all - display.status_line)
r2Sty = r2Style == "Columns" ? plot.style_columns : r2Style == "Histogram" ? plot.style_histogram : plot.style_area
gR3 = "5c Row 3 - 240"
r3Show = input.bool(true, "Show this row", group = gR3, tooltip = "Hides or shows this row and its value label.", display = display.all - display.status_line)
r3Len = input.int(14, "RSI length", minval = 2, maxval = 200, group = gR3, tooltip = "Lookback of this row only. All five rows default to 14 and are independent of each other and of the signal engine.", display = display.all - display.status_line)
r3Style = input.string("Area", "Row style", options = ["Area", "Columns", "Histogram"], group = gR3, tooltip = "Drawing style of this row. Area fills the band, Columns and Histogram draw one bar per candle.", display = display.all - display.status_line)
r3OB = input.color(#1fff00, "RSI > overbought", group = gR3, tooltip = "Colour when this row is above the overbought level.", display = display.all - display.status_line)
r3Up = input.color(#ffffff, "RSI 50 to overbought", group = gR3, tooltip = "Colour between 50 and the overbought level.", display = display.all - display.status_line)
r3Dn = input.color(#000000, "RSI oversold to 50", group = gR3, tooltip = "Colour between the oversold level and 50.", display = display.all - display.status_line)
r3OS = input.color(#cb0000, "RSI < oversold", group = gR3, tooltip = "Colour below the oversold level.", display = display.all - display.status_line)
r3Sty = r3Style == "Columns" ? plot.style_columns : r3Style == "Histogram" ? plot.style_histogram : plot.style_area
gR4 = "5d Row 4 - D"
r4Show = input.bool(true, "Show this row", group = gR4, tooltip = "Hides or shows this row and its value label.", display = display.all - display.status_line)
r4Len = input.int(14, "RSI length", minval = 2, maxval = 200, group = gR4, tooltip = "Lookback of this row only. All five rows default to 14 and are independent of each other and of the signal engine.", display = display.all - display.status_line)
r4Style = input.string("Area", "Row style", options = ["Area", "Columns", "Histogram"], group = gR4, tooltip = "Drawing style of this row. Area fills the band, Columns and Histogram draw one bar per candle.", display = display.all - display.status_line)
r4OB = input.color(#1fff00, "RSI > overbought", group = gR4, tooltip = "Colour when this row is above the overbought level.", display = display.all - display.status_line)
r4Up = input.color(#ffffff, "RSI 50 to overbought", group = gR4, tooltip = "Colour between 50 and the overbought level.", display = display.all - display.status_line)
r4Dn = input.color(#000000, "RSI oversold to 50", group = gR4, tooltip = "Colour between the oversold level and 50.", display = display.all - display.status_line)
r4OS = input.color(#cb0000, "RSI < oversold", group = gR4, tooltip = "Colour below the oversold level.", display = display.all - display.status_line)
r4Sty = r4Style == "Columns" ? plot.style_columns : r4Style == "Histogram" ? plot.style_histogram : plot.style_area
gR5 = "5e Row 5 - W"
r5Show = input.bool(true, "Show this row", group = gR5, tooltip = "Hides or shows this row and its value label.", display = display.all - display.status_line)
r5Len = input.int(14, "RSI length", minval = 2, maxval = 200, group = gR5, tooltip = "Lookback of this row only. All five rows default to 14 and are independent of each other and of the signal engine.", display = display.all - display.status_line)
r5Style = input.string("Area", "Row style", options = ["Area", "Columns", "Histogram"], group = gR5, tooltip = "Drawing style of this row. Area fills the band, Columns and Histogram draw one bar per candle.", display = display.all - display.status_line)
r5OB = input.color(#1fff00, "RSI > overbought", group = gR5, tooltip = "Colour when this row is above the overbought level.", display = display.all - display.status_line)
r5Up = input.color(#ffffff, "RSI 50 to overbought", group = gR5, tooltip = "Colour between 50 and the overbought level.", display = display.all - display.status_line)
r5Dn = input.color(#000000, "RSI oversold to 50", group = gR5, tooltip = "Colour between the oversold level and 50.", display = display.all - display.status_line)
r5OS = input.color(#cb0000, "RSI < oversold", group = gR5, tooltip = "Colour below the oversold level.", display = display.all - display.status_line)
r5Sty = r5Style == "Columns" ? plot.style_columns : r5Style == "Histogram" ? plot.style_histogram : plot.style_area
gDbg = "4c Profile label"
showArmTxt = input.bool(true, "Show the engine status text", group = gDbg, tooltip = "Prints the trading profile in capitals above the oscillator rows, so you always know which profile is running.", display = display.all - display.status_line)
armTxtCol = input.color(#FFFFFF, "Status text colour", group = gDbg, tooltip = "Colour of the profile name shown above the rows. Change it on a light background.", display = display.all - display.status_line)
armTxtSizeIn = input.string("Small", "Status text size", options = ["Tiny", "Small", "Normal", "Large"], group = gDbg, tooltip = "Text size of the profile name.", display = display.all - display.status_line)
armTxtSize = armTxtSizeIn == "Tiny" ? size.tiny : armTxtSizeIn == "Normal" ? size.normal : armTxtSizeIn == "Large" ? size.large : size.small
gLbl = "6 Value labels"
showLbl = input.bool(true, "Show value labels", group = gLbl, tooltip = "A small text with the zone reading, placed to the right of the last candle. The colour is the zone colour, the same as the oscillator and the candles.", display = display.all - display.status_line)
lblOff = input.int(50, "Offset from the last candle (bars)", minval = 1, maxval = 500, group = gLbl, tooltip = "How many bars to the right of the last candle the labels are placed. Raise it if the labels sit on top of the coloured rows when you zoom in. The chart also needs empty space to the right of the last candle, otherwise the labels are pushed back onto it, so widen the right margin in the chart settings if they seem stuck in place.", display = display.all - display.status_line)
lblSizeIn = input.string("Normal", "Label text size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = gLbl, tooltip = "Text size of the value labels.", display = display.all - display.status_line)
lblPrefix = input.bool(true, "Show the timeframe name before the value", group = gLbl, tooltip = "Shows the timeframe name in front of each value.", display = display.all - display.status_line)
lblRows = input.bool(true, "Show the value on every oscillator row", group = gLbl, tooltip = "Shows one value label per oscillator row. Turn it off to keep only the profile name.", display = display.all - display.status_line)
calcZone(source, len, useVol) =>
hi = ta.highest(source, len)
lo = ta.lowest(source, len)
rng = hi - lo
d = source - source[1]
diff = hi > hi[1] ? rng : lo < lo[1] ? -rng : d
vm = useVol ? volume : 1.0
num = ta.rma(diff * vm, len)
den = ta.rma(math.abs(diff) * vm, len)
den == 0.0 ? 50.0 : num / den * 50.0 + 50.0
rsiFromCloses(array<float> arr, int len) =>
float out = na
int n = array.size(arr)
if n > 1
float num = na
float den = na
int seen = 0
for k = 0 to n - 1
float c0 = array.get(arr, k)
float h0 = c0
float l0 = c0
float h1 = na
float l1 = na
int backA = math.min(len - 1, k)
for i = 0 to backA
float vA = array.get(arr, k - i)
h0 := math.max(h0, vA)
l0 := math.min(l0, vA)
int backB = math.min(len, k - 1)
if backB >= 0
for j = 0 to backB
float vB = array.get(arr, k - 1 - j)
h1 := na(h1) ? vB : math.max(h1, vB)
l1 := na(l1) ? vB : math.min(l1, vB)
float dp = k > 0 ? c0 - array.get(arr, k - 1) : 0.0
float rng = h0 - l0
float df = na(h1) ? dp : h0 > h1 ? rng : l0 < l1 ? -rng : dp
if na(num)
num := df
den := math.abs(df)
else
if seen < len
num := (num * seen + df) / (seen + 1)
den := (den * seen + math.abs(df)) / (seen + 1)
else
num := df / len + num * (1.0 - 1.0 / len)
den := math.abs(df) / len + den * (1.0 - 1.0 / len)
seen := seen + 1
out := na(den) or den == 0.0 ? 50.0 : num / den * 50.0 + 50.0
out
mixCol(color base, color over, float k) =>
color.rgb(int(math.round(color.r(base) * (1.0 - k) + color.r(over) * k)),
int(math.round(color.g(base) * (1.0 - k) + color.g(over) * k)),
int(math.round(color.b(base) * (1.0 - k) + color.b(over) * k)))
zoneColorCandle(float v) =>
na(v) ? color.new(color.white, 100) : v > obLvl ? cZOB : v > 50 ? cZUp : v > osLvl ? cZDn : cZOS
zoneColFor(float v, color cOB, color cUp, color cDn, color cOS) =>
na(v) ? color.new(color.white, 100) : v > obLvl ? cOB : v > 50 ? cUp : v > osLvl ? cDn : cOS
bool priceMode = placeMode == "Price offset"
string bullLoc = priceMode ? location.absolute : placeMode == "Pane edges" ? location.bottom : location.belowbar
string bearLoc = priceMode ? location.absolute : placeMode == "Pane edges" ? location.top : location.abovebar
markerStyle = markerStyleIn == "Circle" ? shape.circle : markerStyleIn == "Diamond" ? shape.diamond : markerStyleIn == "Triangle up" ? shape.triangleup : markerStyleIn == "Triangle down" ? shape.triangledown : markerStyleIn == "Cross" ? shape.cross : markerStyleIn == "X-cross" ? shape.xcross : markerStyleIn == "Flag" ? shape.flag : shape.square
lblSize = lblSizeIn == "Tiny" ? size.tiny : lblSizeIn == "Small" ? size.small : lblSizeIn == "Normal" ? size.normal : lblSizeIn == "Large" ? size.large : size.huge
zoneRsiBase = calcZone(rsiSrc, rsiLen, rsiVW)
float zoneRsi = zoneRsiBase
color zoneCol = zoneColorCandle(zoneRsi)
tfS = timeframe.in_seconds()
ltfAuto = tfS <= 60 ? "" : tfS <= 300 ? "1" : tfS <= 900 ? "5" : tfS <= 3600 ? "15" : tfS <= 86400 ? "60" : "240"
ltfUse = ltfMode == "Auto" ? ltfAuto : ltfTF
int ltfSec = ltfUse == "" ? 0 : int(nz(timeframe.in_seconds(ltfUse), 0))
bool hasLtf = ltfUse != "" and ltfSec < tfS
float iBuy = 0.0
float iTot = 0.0
if hasLtf
[ltfC, ltfO, ltfV] = request.security_lower_tf(syminfo.tickerid, ltfUse, [close, open, volume], ignore_invalid_timeframe = true)
if not na(ltfV) and array.size(ltfV) > 0
for i = 0 to array.size(ltfV) - 1
vv = array.get(ltfV, i)
cc = array.get(ltfC, i)
oo = array.get(ltfO, i)
iTot += vv
iBuy += (cc > oo ? vv : cc < oo ? 0.0 : vv * 0.5)
splitME = splitM == "Intrabar (LTF)" and not hasLtf ? "Range proxy" : splitM
rangePct = high == low ? 0.5 : (close - low) / (high - low)
float buyPct = 0.5
if splitME == "Range proxy"
buyPct := rangePct
else if splitME == "Intrabar (LTF)"
buyPct := iTot > 0 ? math.max(0.0, math.min(1.0, iBuy / iTot)) : rangePct
float sellPct = 1.0 - buyPct
bool liveOff = waitClose and not barstate.isconfirmed
bool forming = waitClose and not barstate.isconfirmed
float spanTop = splitArea == "Full range (high to low)" ? high : math.max(open, close)
float spanBot = splitArea == "Full range (high to low)" ? low : math.min(open, close)
float bRng = spanTop - spanBot
float buyH = bRng * buyPct
bool buyersAtTop = anchorMode == "Close side" ? close >= open : false
float buyTop = buyersAtTop ? spanTop : spanBot + buyH
float buyBot = buyersAtTop ? spanTop - buyH : spanBot
float boundary = buyersAtTop ? buyBot : buyTop
float sellerK = (transMode == "Scales with sellers" ? sellTrans * sellPct : sellTrans) / 100.0
color sellCol = blendMode == "Pre-blended (opaque)" ? mixCol(zoneCol, blendBg, sellerK) : color.new(zoneCol, int(math.round(sellerK * 100)))
color buyCol = zoneCol
bool splitMode = showPress and pressMode == "Split body"
bool segOn = splitMode and bRng > 0.0 and buyH > 0.0 and not liveOff and not na(zoneCol)
color baseBodyC = na
if not liveOff and not na(zoneCol)
baseBodyC := showPress ? sellCol : zoneCol
color baseBorderC = coverFrame ? baseBodyC : na
color buyBorderC = coverFrame ? buyCol : na
color wickCol = liveOff or na(zoneCol) ? na : segOn and splitArea == "Full range (high to low)" ? na : color.new(zoneCol, wickT)
float pO = liveOff ? na : open
float pH = liveOff ? na : high
float pL = liveOff ? na : low
float pC = liveOff ? na : close
plotcandle(pO, pH, pL, pC, title = "RSI candle", color = baseBodyC, wickcolor = wickCol, bordercolor = baseBorderC, force_overlay = true)
plotcandle(segOn ? buyBot : na, segOn ? buyTop : na, segOn ? buyBot : na, segOn ? buyTop : na,
title = "Buyer segment", color = buyCol, wickcolor = na, bordercolor = buyBorderC, force_overlay = true)
if showDiv and segOn and bar_index > last_bar_index - 450
line.new(bar_index - 1, boundary, bar_index + 1, boundary, color = divCol, width = divW, force_overlay = true)
string sA = timeframe.period
string sB = tfB == "" ? timeframe.period : tfB
string sC = tfC == "" ? timeframe.period : tfC
string sD = tfD == "" ? timeframe.period : tfD
string sE = tfE == "" ? timeframe.period : tfE
var array<float> rowDCls = array.new<float>()
var float rowDPend = na
bool rowDNew = nz(ta.change(time(sD))) != 0
if rowDNew
if not na(rowDPend)
array.push(rowDCls, rowDPend)
if array.size(rowDCls) > r4Len + 3
array.shift(rowDCls)
rowDPend := close
var array<float> rowWCls = array.new<float>()
var float rowWPend = na
bool rowWNew = nz(ta.change(time(sE))) != 0
if rowWNew
if not na(rowWPend)
array.push(rowWCls, rowWPend)
if array.size(rowWCls) > r5Len + 3
array.shift(rowWCls)
rowWPend := close
float fbD = rsiFromCloses(rowDCls, r4Len)
float fbW = rsiFromCloses(rowWCls, r5Len)
float localA = calcZone(rsiSrc, r1Len, false)
float localB = calcZone(rsiSrc, r2Len, false)
float localC = calcZone(rsiSrc, r3Len, false)
float localD = calcZone(rsiSrc, r4Len, false)
float localE = calcZone(rsiSrc, r5Len, false)
bool onA = int(nz(timeframe.in_seconds(sA), 0)) == tfS
bool onB = int(nz(timeframe.in_seconds(sB), 0)) == tfS
bool onC = int(nz(timeframe.in_seconds(sC), 0)) == tfS
bool onD = int(nz(timeframe.in_seconds(sD), 0)) == tfS
bool onE = int(nz(timeframe.in_seconds(sE), 0)) == tfS
float zA = onA ? localA : request.security(syminfo.tickerid, sA, calcZone(rsiSrc, r1Len, false)[1], lookahead = barmerge.lookahead_off)
float zB = onB ? localB : request.security(syminfo.tickerid, sB, calcZone(rsiSrc, r2Len, false)[1], lookahead = barmerge.lookahead_off)
float zC = onC ? localC : request.security(syminfo.tickerid, sC, calcZone(rsiSrc, r3Len, false)[1], lookahead = barmerge.lookahead_off)
float zD = onD ? localD : nz(request.security(syminfo.tickerid, sD, calcZone(rsiSrc, r4Len, false)[1], lookahead = barmerge.lookahead_off), fbD)
float zE = onE ? localE : nz(request.security(syminfo.tickerid, sE, calcZone(rsiSrc, r5Len, false)[1], lookahead = barmerge.lookahead_off), fbW)
var array<float> zLock = array.new<float>(5, na)
if barstate.isconfirmed
array.set(zLock, 0, zA)
array.set(zLock, 1, zB)
array.set(zLock, 2, zC)
array.set(zLock, 3, zD)
array.set(zLock, 4, zE)
float dA = forming ? nz(array.get(zLock, 0), zA) : zA
float dB = forming ? nz(array.get(zLock, 1), zB) : zB
float dC = forming ? nz(array.get(zLock, 2), zC) : zC
float dD = forming ? nz(array.get(zLock, 3), zD) : zD
float dE = forming ? nz(array.get(zLock, 4), zE) : zE
string armTf = profileIn == "Position trading" ? "W" : profileIn == "Day trading" ? "D" : profileIn == "Scalping" ? "240" : profileIn == "Micro scalping" ? "15" : customHtf
string trgTf = profileIn == "Position trading" ? "D" : profileIn == "Day trading" ? "240" : profileIn == "Scalping" ? "15" : profileIn == "Micro scalping" ? "1" : customLtf
int armLen = 14
int armSecRaw = int(nz(timeframe.in_seconds(armTf), 0))
int trgSecRaw = int(nz(timeframe.in_seconds(trgTf), 0))
int armSec = armSecRaw
int trgSec = trgSecRaw
bool armOnChart = armSec <= tfS
bool trgOnChart = trgSec <= tfS
float armLocalRsi = calcZone(rsiSrc, armLen, false)
float armSecRsi = request.security(syminfo.tickerid, armTf, calcZone(rsiSrc, armLen, false), lookahead = barmerge.lookahead_off)
float hRsi = armOnChart ? (waitClose ? nz(armLocalRsi[1], armLocalRsi) : armLocalRsi) : armSecRsi
float armE5L = ta.ema(close, 5)
float armE8L = ta.ema(close, 8)
[armE5Sec, armE8Sec] = request.security(syminfo.tickerid, armTf, [ta.ema(close, 5), ta.ema(close, 8)], lookahead = barmerge.lookahead_off)
float hFast = armOnChart ? (waitClose ? nz(armE5L[1], armE5L) : armE5L) : armE5Sec
float hSlow = armOnChart ? (waitClose ? nz(armE8L[1], armE8L) : armE8L) : armE8Sec
float trgLocalRsi = calcZone(rsiSrc, 14, false)
float trgSecRsi = request.security(syminfo.tickerid, trgTf, calcZone(rsiSrc, 14, false), lookahead = barmerge.lookahead_off)
float lRsi = trgOnChart ? (waitClose ? nz(trgLocalRsi[1], trgLocalRsi) : trgLocalRsi) : trgSecRsi
float lPrev = nz(lRsi[1], lRsi)
float trgE8L = ta.ema(close, 8)
float trgE12L = ta.ema(close, 12)
[trgE8Sec, trgE12Sec] = request.security(syminfo.tickerid, trgTf, [ta.ema(close, 8), ta.ema(close, 12)], lookahead = barmerge.lookahead_off)
float tFast = trgOnChart ? (waitClose ? nz(trgE8L[1], trgE8L) : trgE8L) : trgE8Sec
float tSlow = trgOnChart ? (waitClose ? nz(trgE12L[1], trgE12L) : trgE12L) : trgE12Sec
bool trgUp = tFast > tSlow
bool trgDn = tFast < tSlow
bool armDevOn = armDev and not armOnChart
bool newArmBar = nz(ta.change(time(armTf))) != 0
var float devHi = high
var float devLo = low
var float devVol = volume
var float devCls = close
var array<float> armClsArr = array.new<float>()
var array<float> armVolArr = array.new<float>()
var float numRmaA = na
var float denRmaA = na
var float numDevLast = na
var float denDevLast = na
var int armSeen = 0
var float e5A = na
var float e8A = na
var float e5DevLast = na
var float e8DevLast = na
if newArmBar
if not na(numDevLast)
numRmaA := numDevLast
denRmaA := denDevLast
if not na(e5DevLast)
e5A := e5DevLast
e8A := e8DevLast
if not na(devCls)
array.push(armClsArr, devCls)
array.push(armVolArr, devVol)
if array.size(armClsArr) > armLen + 3
array.shift(armClsArr)
array.shift(armVolArr)
devHi := high
devLo := low
devVol := volume
armSeen := armSeen + 1
else
devHi := math.max(devHi, high)
devLo := math.min(devLo, low)
devVol := devVol + volume
devCls := close
int nA = array.size(armClsArr)
float hiPrevA = na
float loPrevA = na
float hiCloA = na
float loCloA = na
int kP = math.min(armLen, nA)
int kD = math.min(armLen - 1, nA)
if kP > 0
for i = 0 to kP - 1
float vP = array.get(armClsArr, nA - 1 - i)
hiPrevA := na(hiPrevA) ? vP : math.max(hiPrevA, vP)
loPrevA := na(loPrevA) ? vP : math.min(loPrevA, vP)
if kD > 0
for j = 0 to kD - 1
float vD = array.get(armClsArr, nA - 1 - j)
hiCloA := na(hiCloA) ? vD : math.max(hiCloA, vD)
loCloA := na(loCloA) ? vD : math.min(loCloA, vD)
float hiDevA = na(hiCloA) ? devCls : math.max(hiCloA, devCls)
float loDevA = na(loCloA) ? devCls : math.min(loCloA, devCls)
float prevClsA = nA > 0 ? array.get(armClsArr, nA - 1) : na
float dDevA = na(prevClsA) ? 0.0 : devCls - prevClsA
float rngDevA = hiDevA - loDevA
float diffDevA = hiDevA > hiPrevA ? rngDevA : loDevA < loPrevA ? -rngDevA : dDevA
float vmDevA = 1.0
float numRawA = diffDevA * vmDevA
float denRawA = math.abs(diffDevA) * vmDevA
float aA = 1.0 / armLen
float numDevA = na(numRmaA) ? numRawA : (armSeen < armLen ? (numRmaA * armSeen + numRawA) / (armSeen + 1) : aA * numRawA + (1.0 - aA) * numRmaA)
float denDevA = na(denRmaA) ? denRawA : (armSeen < armLen ? (denRmaA * armSeen + denRawA) / (armSeen + 1) : aA * denRawA + (1.0 - aA) * denRmaA)
float rsiDevA = denDevA == 0.0 ? 50.0 : numDevA / denDevA * 50.0 + 50.0
float e5DevA = na(e5A) ? devCls : e5A + (2.0 / 6.0) * (devCls - e5A)
float e8DevA = na(e8A) ? devCls : e8A + (2.0 / 9.0) * (devCls - e8A)
numDevLast := numDevA
denDevLast := denDevA
e5DevLast := e5DevA
e8DevLast := e8DevA
float hRsiDev = waitClose ? nz(rsiDevA[1], rsiDevA) : rsiDevA
float hFastDev = waitClose ? nz(e5DevA[1], e5DevA) : e5DevA
float hSlowDev = waitClose ? nz(e8DevA[1], e8DevA) : e8DevA
float hRsiUse = armDevOn ? hRsiDev : hRsi
float hFastUse = armDevOn ? hFastDev : hFast
float hSlowUse = armDevOn ? hSlowDev : hSlow
bool armL = hRsiUse > 50 and hFastUse > hSlowUse
bool armS = hRsiUse < 50 and hFastUse < hSlowUse
bool longOK = hRsiUse > 50 and hFastUse > hSlowUse and lRsi > 50 and tFast > tSlow
bool shortOK = hRsiUse < 50 and hFastUse < hSlowUse and lRsi < 50 and tFast < tSlow
bool xUp50 = lRsi > 50 and lPrev <= 50
bool xDn50 = lRsi < 50 and lPrev >= 50
bool xUpOB = lRsi > obLvl and lPrev <= obLvl
bool xDnOS = lRsi < osLvl and lPrev >= osLvl
var bool pendL50 = false
var bool pendL70 = false
var bool pendS50 = false
var bool pendS30 = false
if xUp50
pendL50 := true
if xUpOB
pendL70 := true
if lRsi <= 50
pendL50 := false
if lRsi <= obLvl
pendL70 := false
if xDn50
pendS50 := true
if xDnOS
pendS30 := true
if lRsi >= 50
pendS50 := false
if lRsi >= osLvl
pendS30 := false
bool readyL50 = latched ? pendL50 : xUp50
bool readyL70 = latched ? pendL70 : xUpOB
bool readyS50 = latched ? pendS50 : xDn50
bool readyS30 = latched ? pendS30 : xDnOS
bool fireL50 = longOK and armL and trgUp and readyL50
bool fireL70 = longOK and armL and trgUp and readyL70
bool fireS50 = shortOK and armS and trgDn and readyS50
bool fireS30 = shortOK and armS and trgDn and readyS30
if fireL50
pendL50 := false
pendL70 := false
else if fireL70
pendL70 := false
if fireS50
pendS50 := false
pendS30 := false
else if fireS30
pendS30 := false
color bullCol = fireL70 ? sigUp2 : sigUp1
color bearCol = fireS30 ? sigDn2 : sigDn1
float gapP = sqGap * nz(ta.atr(14), syminfo.mintick * 10)
float yBull = ta.lowest(low, sqLook) - gapP
float yBear = ta.highest(high, sqLook) + gapP
bool showMarkers = showSig == "Markers"
bool bullFire = showMarkers and (fireL50 or fireL70)
bool bearFire = showMarkers and (fireS50 or fireS30)
float bullSeries = bullFire ? (priceMode ? yBull : 1.0) : na
float bearSeries = bearFire ? (priceMode ? yBear : 1.0) : na
plotshape(bullFire ? bullSeries : na, title = "Bullish signal", style = markerStyle, location = bullLoc, color = bullCol, size = size.tiny, force_overlay = true)
plotshape(bearFire ? bearSeries : na, title = "Bearish signal", style = markerStyle, location = bearLoc, color = bearCol, size = size.tiny, force_overlay = true)
plot(r1Show ? 5.0 : na, "Row 1 (top)", color = color.new(zoneColFor(dA, r1OB, r1Up, r1Dn, r1OS), rowT), style = r1Sty, histbase = 4.0, linewidth = rowW, display = display.all - display.price_scale)
plot(r2Show ? 4.0 : na, "Row 2", color = color.new(zoneColFor(dB, r2OB, r2Up, r2Dn, r2OS), rowT), style = r2Sty, histbase = 3.0, linewidth = rowW, display = display.all - display.price_scale)
plot(r3Show ? 3.0 : na, "Row 3", color = color.new(zoneColFor(dC, r3OB, r3Up, r3Dn, r3OS), rowT), style = r3Sty, histbase = 2.0, linewidth = rowW, display = display.all - display.price_scale)
plot(r4Show ? 2.0 : na, "Row 4", color = color.new(zoneColFor(dD, r4OB, r4Up, r4Dn, r4OS), rowT), style = r4Sty, histbase = 1.0, linewidth = rowW, display = display.all - display.price_scale)
plot(r5Show ? 1.0 : na, "Row 5 (bottom)", color = color.new(zoneColFor(dE, r5OB, r5Up, r5Dn, r5OS), rowT), style = r5Sty, histbase = 0.0, linewidth = rowW, display = display.all - display.price_scale)
hline(1, "1", color = showSep ? sepCol : na)
hline(2, "2", color = showSep ? sepCol : na)
hline(3, "3", color = showSep ? sepCol : na)
hline(4, "4", color = showSep ? sepCol : na)
hline(5, "5", color = showSep ? sepCol : na)
var label lblArm = na
var array<label> lblRow = array.new<label>(5, na)
if barstate.islast
label.delete(lblArm)
for i = 0 to 4
label.delete(array.get(lblRow, i))
if showArmTxt
string armTxt = str.upper(profileIn)
lblArm := label.new(bar_index + lblOff, 5.55, text = armTxt, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(color.black, 100), style = label.style_none, textcolor = armTxtCol, size = armTxtSize)
if showLbl and lblRows
string lblNameA = "CHART"
string tA = (lblPrefix ? lblNameA + " " : "") + str.tostring(dA, "#.#")
string tB = (lblPrefix ? sB + " " : "") + str.tostring(dB, "#.#")
string tC = (lblPrefix ? sC + " " : "") + str.tostring(dC, "#.#")
string tD = (lblPrefix ? sD + " " : "") + str.tostring(dD, "#.#")
string tE = (lblPrefix ? sE + " " : "") + str.tostring(dE, "#.#")
if r1Show
array.set(lblRow, 0, label.new(bar_index + lblOff, 4.5, text = tA, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(color.black, 100), style = label.style_none, textcolor = zoneColFor(dA, r1OB, r1Up, r1Dn, r1OS), size = lblSize))
if r2Show
array.set(lblRow, 1, label.new(bar_index + lblOff, 3.5, text = tB, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(color.black, 100), style = label.style_none, textcolor = zoneColFor(dB, r2OB, r2Up, r2Dn, r2OS), size = lblSize))
if r3Show
array.set(lblRow, 2, label.new(bar_index + lblOff, 2.5, text = tC, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(color.black, 100), style = label.style_none, textcolor = zoneColFor(dC, r3OB, r3Up, r3Dn, r3OS), size = lblSize))
if r4Show
array.set(lblRow, 3, label.new(bar_index + lblOff, 1.5, text = tD, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(color.black, 100), style = label.style_none, textcolor = zoneColFor(dD, r4OB, r4Up, r4Dn, r4OS), size = lblSize))
if r5Show
array.set(lblRow, 4, label.new(bar_index + lblOff, 0.5, text = tE, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(color.black, 100), style = label.style_none, textcolor = zoneColFor(dE, r5OB, r5Up, r5Dn, r5OS), size = lblSize))
bool anyBull = fireL50 or fireL70
bool anyBear = fireS50 or fireS30
alertcondition(fireL50 and not fireL70, "First impulse bullish", "First impulse bullish: the arm timeframe is bullishly armed and the trigger timeframe crossed 50 up.")
alertcondition(fireL70, "Extreme signal bullish", "Extreme signal bullish: the arm timeframe is bullishly armed and the trigger timeframe crossed the overbought level up.")
alertcondition(fireS50 and not fireS30, "First impulse bearish", "First impulse bearish: the arm timeframe is bearishly armed and the trigger timeframe crossed 50 down.")
alertcondition(fireS30, "Extreme signal bearish", "Extreme signal bearish: the arm timeframe is bearishly armed and the trigger timeframe crossed the oversold level down.")
alertcondition(anyBull, "Any bullish signal", "Any bullish signal, first impulse or extreme.")
alertcondition(anyBear, "Any bearish signal", "Any bearish signal, first impulse or extreme.")
alertcondition(anyBull or anyBear, "Any signal", "Any signal in either direction.")
if anyBull
alert(str.upper(profileIn) + " bullish " + (fireL70 ? "extreme" : "first impulse") + " at " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
if anyBear
alert(str.upper(profileIn) + " bearish " + (fireS30 ? "extreme" : "first impulse") + " at " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)