1. Log Regression Oscillator Channel Strategy Description
The indicator fits a sloping channel to the last 150 candles (a logarithmic regression line with a band of 1.5 standard deviations on each side), then draws an oscillator (RSI by default) and its signal line INSIDE that channel, tilted to follow it: oscillator 30 sits on the lower line, 70 on the upper line, 50 on the middle line.
1.1 Indicator Concept
The indicator answers two questions with one picture: "Where is the trend heading, and how stretched is price inside that trend right now?" Instead of putting the oscillator in a separate pane, it bends the oscillator to fit the trend channel, so the trend direction and the overbought / oversold reading are seen at the same place on the price chart.
- The trend line: a logarithmic regression. A regression line is the straight line that best fits a cloud of points. Here the points are the logarithm of the closing prices of the last 150 candles. Fitting the logarithm means the line represents a trend of steady percentage growth (like compound interest) rather than steady dollar growth — a better fit for markets that move in percentages.
- The channel: ± 1.5 standard deviations. Standard deviation measures how widely the closing prices of the window are spread. The upper and lower lines sit 1.5 × that spread above and below the regression line. A calm market gives a narrow channel, a wide-swinging market a wide one.
- The oscillator inside the channel. RSI (or Stochastic, Stochastic RSI, MFI) normally lives in a 0–100 pane. Here the range 30 to 70 is stretched over the height of the channel: an oscillator reading of 30 is drawn on the lower line, 70 on the upper line, and 50 exactly on the middle line. Readings above 70 or below 30 are drawn outside the channel. Because the frame follows the slope of the trend, the oscillator line is tilted along with it.
- The signal line. A moving average (SMA, 14 by default) of the same oscillator, drawn with the same mapping. When the oscillator crosses it, momentum is changing.
The underlying belief: price tends to oscillate around its trend line, and an oscillator reaching the edge of the channel — at the very place where price itself would be stretched — is a more meaningful overbought / oversold reading than the same number in a detached pane. Note that this is a live snapshot: the channel and the oscillator curve exist only for the most recent 150 candles, at the right edge of the chart.
1.2 Indicator Features
-
Display:
- Upper and lower channel lines: two solid, thick (width 2) lines in light gray-blue (#a7abb9), sloping with the trend. They start 150 candles back and end on the newest candle.
- Mid line: a thin gray dashed line, the regression line itself, halfway between the two outer lines (can be switched off).
- Background fill: a very transparent (90%) purple fill between the upper and lower lines (can be switched off). Its color follows the oscillator color, not the line colors.
- Oscillator line: a purple line (width 2) that follows the oscillator over the last 150 candles, positioned inside / around the channel as described above.
- Signal line: a thinner yellow line (width 1) for the moving average of the oscillator (can be switched off).
- Three text labels at the right end of the channel: the oscillator's name and current value on the oscillator line (for example RSI: 55.3), the lower scale number (30) at the lower line and the upper scale number (70) at the upper line.
- Alerts: the indicator has NO alertcondition() or alert() calls, and no buy / sell arrows. It is a pure drawing tool; you read signals yourself or add your own alerts.
1.3 How to Use the Indicator
- Read the tilt of the channel as the trend. A channel rising to the right = uptrend over the last 150 candles, falling = downtrend, nearly flat = range. The distance between the outer lines tells you how widely price has been swinging.
- Read the oscillator against the channel edges. The purple line touching or poking out through the upper line means the oscillator is at or above its upper scale value (70): "stretched up". At or below the lower line (30): "stretched down". Crossing the mid line means the oscillator crossed 50.
- Combine the two readings (a common way to use it, not built into the code). In a rising channel, an oscillator that dips to the lower line and turns back up is a "buy the dip inside an uptrend" picture; in a falling channel, an oscillator that rises to the upper line and turns down is a "sell the rally inside a downtrend" picture. In a flat channel the edges can be treated as range extremes.
- Use the yellow signal line for timing. The oscillator crossing above its yellow line = momentum turning up; crossing below = turning down. Crossings near the channel edges are the strongest, crossings near the mid line are the weakest.
- Pick the oscillator type to match your style. RSI (default) is smooth; STOCHASTIC reacts faster and swings between extremes more often; STOCHASTIC RSI is the most sensitive; MFI also uses volume, so it needs a symbol with real volume data.
- Use "Scale" to decide what counts as extreme. At 70 the edges mean 30 / 70. At 80 the edges mean 20 / 80, so the oscillator must go further to touch them (fewer, stronger touches). At 100 the whole 0–100 range is stretched over the channel.
- Tune "Lookback Period" to your timeframe. The default 150 on M15 covers about 37 hours of trading; on H1 about 6 days; on H4 about 25 days. A longer lookback gives a smoother, steadier channel; a shorter one hugs recent price and tilts quickly.
- Do not read the oscillator line as a price. Its height shows where the oscillator stands relative to the channel, not a price level. Crossing the channel edge is a statement about the oscillator, not a price breakout.
- Remember the picture is redrawn. Everything is rebuilt for the last 150 candles on every new candle, so the shape of the purple line inside the channel changes as the channel moves, even though the oscillator values themselves do not change. Use it for the current situation, not to study how it looked in the past.
1.4 How the Indicator Works
Inputs & their role
- length (shown as "Lookback Period", integer, default 150, group "Channel"): the number of candles in the window. It sets the window for the regression, for the standard deviation, for the oscillator curve that is drawn, and where the lines start. Example: at 150 the channel is fitted on the last 150 closes; at 50 only on the last 50, so it is tighter and tilts faster. Practical ceiling: the script declares max_bars_back = 500, so values near or above ~500 will probably cause a "too far back in history" error.
- channel_width (shown as "Channel Width", decimal, default 1.5, step 0.1): the multiplier on the standard deviation. Example: if the standard deviation is 58 price points, the lines sit 87 points (58 × 1.5) above and below the middle line; at 2.0 they would sit 116 points away, at 1.0 only 58. A wider channel also compresses the oscillator vertically (see Flow 4).
- mid_disp ("Mid Line", tick box, default on) and fill_band ("Fill Background", tick box, default on): these two ticks share one row in Settings (inline = "s"). They only switch the mid line and the fill on or off; they do not change any calculation. (See the notes about the Mid Line tick.)
- col_up / col_mid / col_low ("Channel Lines Color: Upper", "Mid", "Lower", color pickers, default light gray-blue / gray / light gray-blue): three swatches on one row (inline = "Col"). Colors only.
- osc (shown as "Type:", dropdown RSI / STOCHASTIC / STOCHASTIC RSI / MFI, default RSI, group "Oscillator"): which oscillator is drawn. See Flow 3 for the exact formulas.
- lengtht (the unnamed number box on the same row as "Type:", integer, default 14): the oscillator's period. Example: RSI(14) looks at the last 14 candles; RSI(7) would be twitchier. For STOCHASTIC RSI it is used twice (the RSI period and the stochastic period).
- osc_col (the color swatch on the same row, default purple #7e57c2): the color of the oscillator line and of the background fill (at 90% transparency).
- upper_threshold (shown as "Scale", integer, default 70): the oscillator value that is drawn on the upper line. The lower scale value is computed automatically as 100 − Scale (30 at the default). Example: Scale 80 → lower line = 20, upper line = 80. Keep it above 50 (see the notes).
- sig_disp ("Signal Line", tick box, default on), length_sig (the unnamed number box, integer, default 14) and sig_line (the color swatch, default yellow): these three sit on one row (inline = "sig"). They switch the signal line on / off, set the length of the moving average of the oscillator, and set its color. Example: length 14 averages the last 14 oscillator values; length 5 makes the yellow line follow the purple one much more closely.
Hard-coded constants (NOT inputs)
- The regression is on close prices only and the deviation is the plain standard deviation of the closing prices (not of the regression residuals, not of logarithms).
- STOCHASTIC RSI smoothing: the result is averaged with a 3-candle SMA; the plain STOCHASTIC is not smoothed. MFI is computed on hlc3 (the average of high, low and close).
- Drawing details: the outer lines are width 2, the mid line is width 1 dashed, the oscillator line is width 2, the signal line is width 1, the fill is 90% transparent.
- max_bars_back = 500: how far back in history the script may look.
Main logic blocks
🧭 Flow 0 — What runs when
- On every candle the script computes the regression, the channel prices, the oscillator and its signal line, and moves three line objects (the outer lines and the mid line) to the new coordinates. A "line object" is a drawing that the script creates once and then keeps repositioning.
- Only on the last candle (barstate.islast, which stays true on every tick of the live candle) does it draw the oscillator curve and the three text labels, and it deletes the previous copies before drawing new ones.
- The result is a rolling snapshot: you see one channel and one oscillator curve, for the most recent 150 candles. Earlier candles carry no channel and no curve, because the same few drawings are simply moved forward each time.
📐 Flow 1 — The logarithmic regression (every candle)
- The code walks through the last length candles. For candle i candles ago (i = 0 is the newest) it takes the natural logarithm of the close and gives it a position number per = i + 1 (so the newest candle is 1 and the oldest is 150). Note that the position numbers count backwards in time.
- It adds up four sums over the window — the sum of the position numbers, of the logs, of the squared positions, and of position × log — and plugs them into the standard "least squares" formula to get a slope (how fast ln(price) changes per candle going back) and an intercept (the starting value). Because the positions run backwards, in a rising market the slope is negative (older candles are lower).
- Turning the line into prices: reg_start = exp(intercept + slope × length) is the regression price at the oldest end of the window, reg_end = exp(intercept) is the price at the newest end. (exp undoes the logarithm.)
- Concrete example: over 150 candles the closes climb steadily from about 2,500 to about 2,700. The fit gives reg_start ≈ 2,500 and reg_end ≈ 2,700, and the slope is about −0.0005 (ln(2500 / 2700) ÷ 150, negative because the older end is lower).
📏 Flow 2 — The channel lines (every candle)
- deviation = ta.stdev(close, length): the standard deviation of the last 150 closing prices, in price units. Example: for closes spread evenly between 2,500 and 2,700 it is about 58.
- Four channel prices: upper start / end = reg_start / reg_end + deviation × channel_width; lower start / end = the same − deviation × channel_width. Concrete example (58 × 1.5 = 87): upper 2,587 → 2,787; mid 2,500 → 2,700; lower 2,413 → 2,613.
- Straight lines: each line is a plain straight segment from the candle 150 bars back (start price) to the newest candle (end price). The same amount is added at both ends, so the three lines are parallel. A true logarithmic regression is a gentle curve; the indicator draws only the straight line joining its two ends, so in a strongly accelerating trend the real curve bows away from the drawn middle line.
- Create once, then move: if a line does not exist yet it is created; otherwise its two end points are updated on every candle. The mid line is only created when "Mid Line" is ticked.
- Fill: when "Fill Background" is ticked, a fill between the upper and lower lines is created on every candle with the oscillator color at 90% transparency.
📊 Flow 3 — The oscillator and its signal line (every candle)
- Depending on "Type:" the oscillator is one of:
- RSI: ta.rsi(close, lengtht) — the standard Relative Strength Index, 0–100.
- MFI: ta.mfi(hlc3, lengtht) — Money Flow Index, an RSI-like reading that weighs each candle by its volume, 0–100.
- STOCHASTIC: ta.stoch(close, high, low, lengtht) — where the close sits between the lowest low and highest high of the last lengtht candles, 0–100, not smoothed.
- STOCHASTIC RSI: the same stochastic formula applied to the RSI instead of the price, then smoothed with a 3-candle SMA.
- Signal line: sma_osc = ta.sma(Oscillator, length_sig) — the simple average of the last 14 oscillator values (default).
- Concrete example: RSI(14) of the last 14 candles = 60; the average of the last 14 RSI values = 55 → oscillator 60, signal 55.
🧲 Flow 4 — Squeezing the oscillator into the channel (last candle)
- lower_threshold = 100 − upper_threshold (30 at the default Scale 70).
- step = (upper_end − lower_end) ÷ (upper_threshold − lower_threshold): how many price points one oscillator point is worth. The channel height is the numerator, 40 oscillator points (30 → 70) the denominator. Example: channel height at the newest candle 2,787 − 2,613 = 174 → step = 174 ÷ 40 = 4.35 price points per oscillator point.
- slope_ = (reg_start − reg_end) ÷ length: how much the channel's price drops per candle going back in time. Example: (2,500 − 2,700) ÷ 150 = −1.33 per candle.
- For every candle in the window (i = 0 newest … 149 oldest) one point of the curve is placed at: y = lower_end + slope_ × i + step × (oscillator[i] − lower_threshold). In words: take the lower line's price at that candle (lower_end + slope_ × i follows the lower line exactly) and go up by (oscillator − 30) × step.
- Concrete example (newest candle, i = 0): RSI = 60 → 2,613 + (60 − 30) × 4.35 = 2,743.5. RSI = 50 → 2,613 + 20 × 4.35 = 2,700 = exactly the mid line. RSI = 30 → 2,613 (the lower line). RSI = 70 → 2,787 (the upper line). RSI = 80 → 2,613 + 50 × 4.35 = 2,830.5, above the upper line (2,787).
- Concrete example (older candle, i = 75): the lower line there is 2,613 − 1.33 × 75 ≈ 2,513 (the middle of the lower line, as expected). An RSI of 40 on that candle is drawn at 2,513 + 10 × 4.35 = 2,556.5.
- Consequence: the vertical position of the curve at any candle is its position relative to the channel at that candle, so the curve tilts with the channel. And since step depends on the channel height, a wider channel flattens the curve, a narrower one exaggerates it.
🖌️ Flow 5 — Drawing the curve and the labels (last candle only)
- For the oscillator and (if ticked) the signal line, the script builds a list of 150 points (candle position + price from Flow 4) and draws them as one connected polyline in the chosen color (oscillator width 2, signal width 1). The previous polyline is deleted first.
- Only the oscillator curve gets labels. Three labels are placed at the newest candle, anchored to the right: the oscillator name and value (RSI: 60.0) at the curve's end, the lower scale value (30) at the lower line's end, the upper scale value (70) at the upper line's end. The previous labels are deleted so only one set stays.
Outputs & their role in trading
- The channel (outer lines + mid line + fill): the trend direction (tilt), its center (mid line) and the normal swing around it.
- The oscillator curve: momentum measured in the same frame as price — touches of the edges show an extreme, crossing the mid line shows the 50 level.
- The signal line: timing of momentum turns through crossings with the oscillator.
- The three labels: the exact oscillator value and the scale numbers that the two outer lines represent.
- The indicator is a snapshot, not a history. The channel, the curves and the labels exist only at the right edge, rebuilt on every new candle. You cannot scroll back to see old channels or old signals as they were, and there is nothing here that a backtest could replay candle by candle. The oscillator curve is also re-projected every time: when the channel moves, the purple curve changes shape even though the oscillator values themselves have not changed.
- The "log" channel is drawn with straight lines. Only the two end points come from the logarithmic regression; the segment between them is straight. The middle line is therefore a chord of the true curve, not the curve itself.
- The channel width is measured on prices, not on the regression. The deviation is the spread of the closes around their own average over the window, not around the trend line. In a strong trend that spread is large only because price travelled far, so the channel gets wide even if price hugged the trend line tightly. In a flat range it is narrow. (Example: a straight climb of 200 points already produces a deviation near 58, i.e. a ±87 channel at the default width, however clean the climb was.)
- "Scale" must stay above 50. The lower value is 100 − Scale, so at Scale = 50 both ends are 50 and the step becomes a division by zero (the curve breaks); below 50 the lower value is larger than the upper one and the curve is drawn upside down. Use values between 51 and 100.
- "Mid Line" unticked: probable runtime error (not tested). The code creates the mid line only when the tick is on, but otherwise tries to move it anyway. With the tick off the mid line never exists, and moving a non-existent line is expected to stop the script with an error. If you want to hide the mid line, it is safer to make its color transparent than to untick it.
- A new fill every candle. The background fill is created again on every candle and the old ones are never deleted by the code. They all sit on the same two lines, so TradingView's drawing limit keeps only the newest ones, and the stacked translucent copies can make the fill look more opaque than 90% transparency suggests.
- One-candle offset. The oscillator window counts the newest candle as position 1 and the oldest as 150, while the line is drawn from the candle 151 back (bar_index[length]) to the newest candle, with the end price taken at position 0. The effect is a shift of about one candle and is practically invisible.
- The live candle moves everything. The window includes the current, unfinished candle, so on every tick the regression, the channel, the oscillator value and the labels are recalculated and can shift until the candle closes.
- MFI needs volume. On a symbol without real volume data, MFI is meaningless (flat or empty).


