3D Money Flow Index [UAlgo]3D Money Flow Index is a visual enhancement of the Money Flow Index that transforms a classic momentum oscillator into a pseudo 3D ribbon rendered inside its own pane. Instead of displaying MFI as only a single flat line, the script builds a front surface, a back surface, connecting edges, and shaded faces, then projects those elements through a camera style transformation using configurable yaw and pitch angles. The result is a depth based MFI visualization that makes momentum shifts, expansion, compression, and reversals much more expressive than a standard oscillator plot.
The indicator runs in a separate pane ( overlay=false ) and combines several components into one visual framework:
A custom MFI style calculation
A 3D ribbon built from projected historical MFI values
Optional dynamic ribbon depth based on volatility
Buy and sell markers when MFI crosses key threshold levels
Regular bullish and bearish divergence detection using MFI pivots versus price pivots
Projected guide levels for 80, 50, and 20
This makes the script useful for traders who want both analysis and presentation. It keeps the familiar MFI logic at the core, but wraps it in a more intuitive spatial display that can help visually separate trend persistence, reversal attempts, and divergence structures.
Educational tool only. Not financial advice.
🔹 Features
🔸 1) 3D Ribbon Style MFI Visualization
The core feature of the script is a pseudo 3D MFI ribbon. For each historical bar inside the selected history length, the indicator creates a front and back layer around the same MFI value, connects those layers with side edges, and fills the face between them. This gives the oscillator a ribbon like body rather than a single thin line.
The ribbon is projected into the pane using time for the horizontal axis and MFI value for the vertical axis, which creates a clean depth illusion without leaving the oscillator panel.
🔸 2) Adjustable Camera Style Projection
The script includes two visual controls that change how the ribbon appears in space:
Yaw Angle changes the left right visual rotation of the ribbon
Pitch Angle changes the vertical tilt of the ribbon
These controls allow the user to choose a flatter, more technical display or a more dramatic perspective oriented look.
🔸 3) Configurable Ribbon Depth
The Ribbon Depth setting controls how thick the 3D body appears along the synthetic Z axis. Lower values create a thinner ribbon, while higher values create a deeper and more dramatic structure.
This is especially useful when adapting the visualization for different screen sizes or preferred chart density.
🔸 4) Optional Dynamic Volatility Based Depth
When enabled, the script automatically scales ribbon depth using current ATR relative to its longer average. This means the visual thickness expands during higher volatility and compresses during quieter periods.
The result is a ribbon that can communicate both oscillator behavior and relative volatility regime at the same time.
🔸 5) Custom Money Flow Index Calculation
Instead of using the built in ta.mfi() , the script calculates its own MFI style series from positive and negative money flow sums derived from price change and volume. This gives the indicator full internal control over the oscillator values used by the 3D engine, divergence logic, and threshold signals.
🔸 6) Buy and Sell Threshold Markers
The script generates event markers when MFI crosses important momentum thresholds:
Buy style event when MFI crosses above 20
Sell style event when MFI crosses below 80
These events are rendered as small 3D boxes attached to the ribbon, which keeps the signal presentation consistent with the indicator’s depth based design.
🔸 7) Regular Divergence Detection
The indicator can detect regular divergence by comparing MFI pivots to price pivots:
Bearish divergence when price makes a higher high but MFI makes a lower high
Bullish divergence when price makes a lower low but MFI makes a higher low
Divergence is optional and can be turned on or off through the settings.
🔸 8) 3D Aligned Divergence Lines
When a divergence is detected, the script draws a thicker line between the two MFI pivot points, positioned on the ribbon’s front face so the divergence appears visually attached to the 3D structure instead of floating away from it.
It also draws dotted connector lines from the divergence line back to the ribbon body, reinforcing the spatial relationship.
🔸 9) Historical Ribbon Length Control
The History Length input limits how many bars of 3D ribbon are drawn. This helps balance visual richness with performance and keeps the pane from becoming overcrowded.
🔸 10) Gradient Color Mapping by MFI Level
The ribbon is colored dynamically using a gradient based on MFI value:
Lower readings lean bearish
Higher readings lean bullish
This means the ribbon itself functions as a live regime map, not just a structural shape.
🔸 11) Projected Guide Levels
The script draws perspective aligned guide levels for:
80
50
20
These are not flat horizontal pane lines. They are projected using the same camera logic as the ribbon, which keeps the entire display visually coherent.
🔸 12) Full Last Bar Redraw for Visual Consistency
All 3D objects are deleted and rebuilt on the last bar. This ensures that the current camera angles, ribbon depth, divergence set, and markers are always rendered consistently with the latest data.
🔸 13) Object Based Design for Maintainability
The script uses several custom types:
Point3D for synthetic 3D coordinates
Point2D for projected time / value coordinates
Camera for projection controls
DivLine for stored divergence events
This makes the visual engine and signal logic more structured and easier to extend.
🔹 Calculations
1) Custom MFI Style Calculation
The script computes money flow using separate positive and negative sums based on the change in the selected source:
float upper = math.sum(volume * (ta.change(src) <= 0 ? 0 : src), length)
float lower = math.sum(volume * (ta.change(src) >= 0 ? 0 : src), length)
Then it computes an MFI style output:
float ratio = lower == 0 ? 0 : upper / lower
100.0 - (100.0 / (1.0 + ratio))
Interpretation:
Positive source changes contribute to the upper flow sum.
Negative source changes contribute to the lower flow sum.
The resulting ratio is converted into an oscillator style value on a 0 to 100 scale.
Important implementation note:
This is a custom MFI style calculation, not the built in TradingView MFI function. The script uses its own edge case handling when lower == 0 .
2) Volatility Based Depth Scaling
The dynamic depth option uses ATR relative to a longer ATR average:
float atr = ta.atr(14)
float avg_atr = ta.sma(atr, 100)
float depth_scaler = use_dynamic_depth ? math.max(0.5, math.min(2.5, atr / avg_atr)) : 1.0
Interpretation:
If current ATR is above its longer average, the ribbon becomes deeper.
If current ATR is below its longer average, the ribbon becomes thinner.
The multiplier is clamped between 0.5 and 2.5 for stability.
3) 3D Coordinate Model
Each ribbon segment uses synthetic 3D coordinates:
x represents bars back in history
y represents the MFI value
z represents the ribbon depth offset
For each bar, the ribbon creates:
A front point at z = -depth / 2
A back point at z = depth / 2
This creates the geometry needed for the front edge, back edge, side edge, and face fill.
4) Camera Projection Logic
The script projects each 3D point into 2D coordinates using yaw and pitch rotations:
float x1 = p.x * math.cos(rad_yaw) - p.z * math.sin(rad_yaw)
float z1 = p.x * math.sin(rad_yaw) + p.z * math.cos(rad_yaw)
float y1 = p.y * math.cos(rad_pitch) - z1 * math.sin(rad_pitch)
Then it converts the projected coordinates into chart coordinates:
int proj_time = int(ref_time - (x1 * time_step))
float proj_price = y1
Interpretation:
The script does not use true 3D rendering. It uses geometric projection math to simulate depth within normal chart objects.
5) Time Step Mapping
The horizontal spacing of projected points is derived from current chart time:
int dt = time - time
if bar_index == 0
dt := 60000
This lets the projected ribbon stay aligned with the current timeframe interval.
6) Ribbon Segment Construction
For each bar pair in the selected history window, the script creates:
Front line from point A front to point B front
Back line from point A back to point B back
Connector line from point A front to point A back
A filled face polygon between the front and back edges
This produces the actual ribbon body. The fill is created only for recent segments to stay within object limits:
if i < 90
...
polylines.push(polyline.new(points, ... fill_color=face_col ...))
7) Gradient Color Logic for the Ribbon
The ribbon color is mapped from current MFI value:
color base_col = color.from_gradient(val_a, 20, 80, col_bear, col_bull)
Interpretation:
Lower MFI values shift toward the bearish color.
Higher MFI values shift toward the bullish color.
Midrange values naturally blend between the two.
8) Buy and Sell Signal Logic
The script defines simple threshold crossing events:
bool sig_buy = ta.crossover(mfi_val, 20)
bool sig_sell = ta.crossunder(mfi_val, 80)
Interpretation:
Buy event means MFI rises back above the lower threshold, which can suggest recovery from oversold pressure.
Sell event means MFI falls back below the upper threshold, which can suggest rejection from overbought pressure.
These are event markers, not standalone entry guarantees.
9) 3D Marker Drawing
When a buy or sell signal occurs, the script draws a small 3D box marker using the same projection engine as the ribbon. The marker is built from four projected corners and connected with line segments so it appears attached to the ribbon structure.
This keeps the signal styling consistent with the rest of the indicator.
10) Pivot Detection for Divergence
The divergence engine finds pivot highs and lows on the MFI series:
float ph = ta.pivothigh(mfi_val, piv_len, piv_len)
float pl = ta.pivotlow(mfi_val, piv_len, piv_len)
Each pivot is aligned to its true pivot bar using:
int curr_piv_bar = bar_index - piv_len
This ensures divergence anchors are placed at the actual turning points, not the later confirmation bar.
11) Bearish Divergence Logic
When an MFI pivot high is confirmed, the script compares it with the prior MFI pivot high:
bool bear_div = (curr_price_high > last_price_ph) and (curr_piv_val < last_ph_val)
Interpretation:
Price makes a higher high
MFI makes a lower high
If true, a bearish divergence line is stored.
12) Bullish Divergence Logic
When an MFI pivot low is confirmed, the script compares it with the prior MFI pivot low:
bool bull_div = (curr_price_low < last_price_pl) and (curr_piv_val > last_pl_val)
Interpretation:
Price makes a lower low
MFI makes a higher low
If true, a bullish divergence line is stored.
13) Divergence Storage and Cleanup
Detected divergences are stored in an array of DivLine objects. Older divergence entries are removed once they fall too far outside the active visual window:
if (bar_index - divergences.get(0).start_bar) > (history_len + 100)
divergences.shift()
This prevents old divergence structures from accumulating forever.
14) 3D Aligned Divergence Rendering
When a divergence is drawn, the script places it on the front face of the ribbon by using:
float z_offset = -current_depth / 2.0
This is an important visual detail because it keeps the divergence line attached to the ribbon surface rather than offset in empty space.
The script also draws dotted connector lines from the divergence line endpoints back to the ribbon center plane, reinforcing the 3D attachment.
15) Projected Guide Level Rendering
The indicator draws projected guide levels at 80, 50, and 20 using the same projection method:
draw_grid_line(80, color.red)
draw_grid_line(50, color.gray)
draw_grid_line(20, color.green)
This keeps the threshold references visually aligned with the ribbon perspective instead of using flat horizontal lines that would break the illusion.
16) Full Last Bar Rebuild Process
On the last bar, the script:
Deletes all existing lines, polylines, and labels
Recreates the camera
Rebuilds the ribbon over the selected history length
Replots signal markers
Renders divergence lines
Draws guide levels
This full redraw approach ensures visual consistency whenever the latest bar changes, the camera angles change, or volatility depth changes.
Pine Script®指标








