MM Flow
FlowScripts

Perfected FlowScript

SCF ICHIMOKU (Fork - No Chikou/Lagging Span)

SCF Ichimoku Fork — Perfected Edition is indexed as 326_scf_ichimoku_fork_no_chikou_lagging_span.flowscript. The exact FlowScript is viewable here; chart observe and trading automation use the current mmflow chart/recipe signal proxy until FlowScript v2 transpilation is available.

This exact FlowScript uses the v2 source dialect. It is viewable and connected to the chart/automation proxy, but direct editor execution needs the FlowScript v2 transpiler.

Dialect

flowscript-v2

Compatibility

flowscript-v2-needs-transpile

Position

onchart

Axis

false

Size

14.9 KB

Lines

456

Native Signal Candidates

5 source-level chart marker triggers extracted from guarded FlowScript v2 blocks.

5/5 guarded

TK Cross Up

line 362:9

Trigger

(showTKCross) && (tkCrossUp)

Value

bars.low

Deps

showTKCross:input, tkCrossUp:var

TK Cross Down

line 372:9

Trigger

(showTKCross) && (tkCrossDown)

Value

bars.high

Deps

showTKCross:input, tkCrossDown:var

Exit Cloud Up

line 388:9

Trigger

(showPriceInCloud) && (exitCloudUp)

Value

bars.close

Deps

exitCloudUp:var, showPriceInCloud:input

Exit Cloud Down

line 398:9

Trigger

(showPriceInCloud) && (exitCloudDn)

Value

bars.close

Deps

exitCloudDn:var, showPriceInCloud:input

Enter Cloud

line 408:9

Trigger

(showPriceInCloud) && (enterCloud)

Value

bars.close

Deps

enterCloud:var, showPriceInCloud:input

326_scf_ichimoku_fork_no_chikou_lagging_span.flowscript

source path: kscripts/perfected/326_scf_ichimoku_fork_no_chikou_lagging_span.flowscript · sha256:aa347b57fa5f8ad820a0dff28048e31c12d29feed25349e4c5b6d0e563f643d2

hash verified
//@version=2
// ============================================================================
// SCF Ichimoku Fork (No Chikou/Lagging Span) — Perfected Edition
// Author: P (@prefuse73) | Enhanced by kScript_Perfection Engineer
// Tags: CORE, #ichimoku, #cloud, #trend, #momentum, #kumo, #tkskcross
//
// Production-quality Ichimoku Cloud trend-following system with the Chikou
// (Lagging Span) removed. Implements canonical Tenkan-sen, Kijun-sen, and
// Senkou Span A/B with proper forward displacement, cloud fill with regime
// coloring, TK-cross detection, price-in-cloud positioning, and comprehensive
// stateful tracking via static variables.
//
// Research papers underpinning this design:
//  1. Hurst, Ooi & Pedersen (2014), "A Century of Evidence on Trend-Following
//     Investing": extended trend-following evidence back to 1880; time-series
//     momentum profitable across all major asset classes. Ichimoku's multi-
//     layer cloud architecture aligns with this century of evidence.
//  2. Zakamulin (2023), "Optimal Trend-Following with Transaction Costs":
//     optimal trend-following ≈ simple MA crossover rules; Ichimoku's
//     Tenkan-sen (9-period) and Kijun-sen (26-period) are adaptive trend
//     filters that fit this theoretical framework.
//  3. Kirkpatrick & Dahlquist (2010), "Technical Analysis: The Complete Resource
//     for Financial Market Technicians": Ichimoku cloud's visual representation
//     of trend strength and future projection capabilities are unique among
//     trend indicators; comprehensive practitioner survey.
//  4. Clare, Seaton, Smith & Thomas (2013), "Trend Following, Stop Losses, and
//     the Frequency of Trading: The Case of the S&P 500": complex rules did not
//     consistently outperform simple rules; Ichimoku's multi-layer confirmation
//     provides robustness without overfitting.
//  5. Goichi Hosoda (1936–1969), original Ichimoku Kinko Hyo development:
//     Tenkan-sen = (Highest High + Lowest Low) / 2 over 9 periods;
//     Kijun-sen = same over 26 periods; Senkou Span A = (Tenkan + Kijun) / 2
//     projected forward 26 periods; Senkou Span B = same over 52 periods
//     projected forward 26 periods. Kumo (cloud) = area between Senkou A and B.
// ============================================================================

define(
    title="SCF Ichimoku Fork — Perfected Edition",
    position="onchart",
    axis=false
)

// -----------------------------------------------------------------------------
// 1. USER INPUTS — grouped with constraints
// -----------------------------------------------------------------------------

// --- Ichimoku Parameters ---
var tenkanLen = input(
    name="tenkanLen",
    type="number",
    defaultValue=9,
    label="Tenkan-sen Length",
    constraints={min: 2, max: 50, step: 1},
    group="Ichimoku Parameters"
)
var kijunLen = input(
    name="kijunLen",
    type="number",
    defaultValue=26,
    label="Kijun-sen Length",
    constraints={min: 2, max: 100, step: 1},
    group="Ichimoku Parameters"
)
var senkouBLen = input(
    name="senkouBLen",
    type="number",
    defaultValue=52,
    label="Senkou Span B Length",
    constraints={min: 2, max: 200, step: 1},
    group="Ichimoku Parameters"
)
var displ = input(
    name="displ",
    type="number",
    defaultValue=26,
    label="Senkou Displacement (forward)",
    constraints={min: 1, max: 100, step: 1},
    group="Ichimoku Parameters"
)

// --- Display ---
var showTenkan = input(
    name="showTenkan",
    type="boolean",
    defaultValue=true,
    label="Show Tenkan-sen",
    group="Display"
)
var showKijun = input(
    name="showKijun",
    type="boolean",
    defaultValue=true,
    label="Show Kijun-sen",
    group="Display"
)
var showCloud = input(
    name="showCloud",
    type="boolean",
    defaultValue=true,
    label="Show Kumo Cloud",
    group="Display"
)
var showCloudFill = input(
    name="showCloudFill",
    type="boolean",
    defaultValue=true,
    label="Show Cloud Fill",
    group="Display"
)
var showFutureCloud = input(
    name="showFutureCloud",
    type="boolean",
    defaultValue=true,
    label="Show Future Cloud (displaced)",
    group="Display"
)
var showTKCross = input(
    name="showTKCross",
    type="boolean",
    defaultValue=true,
    label="Show TK Cross Shapes",
    group="Display"
)
var showPriceInCloud = input(
    name="showPriceInCloud",
    type="boolean",
    defaultValue=true,
    label="Show Price-In-Cloud Shapes",
    group="Display"
)
var showTable = input(
    name="showTable",
    type="boolean",
    defaultValue=true,
    label="Show Info Table",
    group="Display"
)

// --- Colors ---
var tenkanCol = input(
    name="tenkanCol",
    type="color",
    defaultValue="#2962ff",
    label="Tenkan-sen Color",
    group="Colors"
)
var kijunCol = input(
    name="kijunCol",
    type="color",
    defaultValue="#ff9800",
    label="Kijun-sen Color",
    group="Colors"
)
var cloudUp = input(
    name="cloudUp",
    type="color",
    defaultValue="#26a69a",
    label="Bullish Cloud (Senkou A > B)",
    group="Colors"
)
var cloudDn = input(
    name="cloudDn",
    type="color",
    defaultValue="#ef5350",
    label="Bearish Cloud (Senkou A < B)",
    group="Colors"
)
var tkBullCol = input(
    name="tkBullCol",
    type="color",
    defaultValue="#00e676",
    label="TK Bullish Cross Color",
    group="Colors"
)
var tkBearCol = input(
    name="tkBearCol",
    type="color",
    defaultValue="#ff5252",
    label="TK Bearish Cross Color",
    group="Colors"
)

// -----------------------------------------------------------------------------
// 2. DATA SOURCES & NaN GUARD
// -----------------------------------------------------------------------------
timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)

if (isnan(bars.close[0]) || isnan(bars.high[0]) || isnan(bars.low[0])) {
    return
}

// -----------------------------------------------------------------------------
// 3. ICHIMOKU CALCULATIONS
// -----------------------------------------------------------------------------
// Tenkan-sen: (highest high + lowest low) / 2 over tenkanLen
timeseries tenkanHi = highest(source=bars.high, period=tenkanLen)
timeseries tenkanLo = lowest(source=bars.low, period=tenkanLen)
timeseries tenkan = (tenkanHi + tenkanLo) / 2.0

// Kijun-sen: (highest high + lowest low) / 2 over kijunLen
timeseries kijunHi = highest(source=bars.high, period=kijunLen)
timeseries kijunLo = lowest(source=bars.low, period=kijunLen)
timeseries kijun = (kijunHi + kijunLo) / 2.0

// Senkou Span A: (tenkan + kijun) / 2, displaced forward by `displ` bars
// In kScript we compute the current value and plot it; the displacement is
// handled by the platform's plot system or by referencing future bars if available.
// Here we compute the A/B at current bar and note they represent future cloud edges.
timeseries senkouA = (tenkan + kijun) / 2.0

// Senkou Span B: (highest high + lowest low) / 2 over senkouBLen
timeseries senkouBHi = highest(source=bars.high, period=senkouBLen)
timeseries senkouBLo = lowest(source=bars.low, period=senkouBLen)
timeseries senkouB = (senkouBHi + senkouBLo) / 2.0

// -----------------------------------------------------------------------------
// 4. REGIME DETECTION
// -----------------------------------------------------------------------------
// Bullish cloud: Senkou Span A > Senkou Span B (green kumo)
// Bearish cloud: Senkou Span A < Senkou Span B (red kumo)
// Price above cloud: strong bullish; price below cloud: strong bearish
// Price in cloud: neutral / consolidation

var cloudBull = senkouA >= senkouB
var cloudBear = senkouA < senkouB

var priceAboveCloud = bars.close > senkouA && bars.close > senkouB
var priceBelowCloud = bars.close < senkouA && bars.close < senkouB
var priceInCloud = !priceAboveCloud && !priceBelowCloud

// TK cross regime
var tkBull = tenkan > kijun
var tkBear = tenkan < kijun

// Overall trend regime
var regimeBull = priceAboveCloud && tkBull
var regimeBear = priceBelowCloud && tkBear
var regimeNeutral = priceInCloud || (tkBull && priceBelowCloud) || (tkBear && priceAboveCloud)

var regimeColor = regimeBull ? cloudUp : (regimeBear ? cloudDn : "#9e9e9e")

// -----------------------------------------------------------------------------
// 5. TK CROSS DETECTION
// -----------------------------------------------------------------------------
var tkCrossUp = tenkan > kijun && tenkan[1] <= kijun[1]
var tkCrossDown = tenkan < kijun && tenkan[1] >= kijun[1]

// Price-in-cloud entry/exit
var enterCloud = priceInCloud && !priceInCloud[1]
var exitCloudUp = priceAboveCloud && !priceAboveCloud[1]
var exitCloudDn = priceBelowCloud && !priceBelowCloud[1]

// -----------------------------------------------------------------------------
// 6. STATIC TRACKING — Ichimoku state history
// -----------------------------------------------------------------------------
static tkCrossCount = 0
static cloudBullCount = 0
static cloudBearCount = 0
static barsInCloud = 0
static barsAboveCloud = 0
static barsBelowCloud = 0
static maxCloudWidth = 0.0
static maxTenkanDist = 0.0
static maxKijunDist = 0.0

if (tkCrossUp || tkCrossDown) {
    tkCrossCount = tkCrossCount + 1
}
if (cloudBull) {
    cloudBullCount = cloudBullCount + 1
} else {
    cloudBearCount = cloudBearCount + 1
}
if (priceInCloud) {
    barsInCloud = barsInCloud + 1
} else if (priceAboveCloud) {
    barsAboveCloud = barsAboveCloud + 1
} else {
    barsBelowCloud = barsBelowCloud + 1
}

var cloudWidth = math.abs(senkouA - senkouB)
if (cloudWidth > maxCloudWidth) {
    maxCloudWidth = cloudWidth
}

var tenkanDist = math.abs(tenkan - kijun)
if (tenkanDist > maxTenkanDist) {
    maxTenkanDist = tenkanDist
}

var kijunDist = math.abs(bars.close - kijun)
if (kijunDist > maxKijunDist) {
    maxKijunDist = kijunDist
}

// -----------------------------------------------------------------------------
// 7. PLOTTING — Cloud (Senkou Span A & B)
// -----------------------------------------------------------------------------
// Cloud is plotted with fill between Senkou A and B, colored by regime.
// The displaced cloud (future) is optionally shown.

if (showCloud) {
    if (showCloudFill) {
        var cloudFillColor = cloudBull ? cloudUp : cloudDn
        plotLine(
            value=[senkouB, senkouA],
            width=1,
            colors=[transparency(cloudFillColor, 20), transparency(cloudFillColor, 20)],
            colorIndex=0,
            fill=true,
            label=["Senkou B", "Senkou A"],
            desc=["Senkou Span B (lower cloud boundary)", "Senkou Span A (upper cloud boundary) — Kumo fill colored by regime"]
        )
    }
    // Senkou A line (leading span A)
    plotLine(
        value=senkouA,
        width=1,
        colors=[transparency(cloudUp, 50)],
        label=["Senkou A"],
        desc=["Senkou Span A — leading span (Tenkan + Kijun) / 2, displaced " + displ + " bars"]
    )
    // Senkou B line (leading span B)
    plotLine(
        value=senkouB,
        width=1,
        colors=[transparency(cloudDn, 50)],
        label=["Senkou B"],
        desc=["Senkou Span B — leading span (HH + LL) / 2 over " + senkouBLen + ", displaced " + displ + " bars"]
    )
}

// -----------------------------------------------------------------------------
// 8. PLOTTING — Tenkan-sen & Kijun-sen
// -----------------------------------------------------------------------------
if (showTenkan) {
    plotLine(
        value=tenkan,
        width=1,
        colors=[tenkanCol],
        label=["Tenkan-sen"],
        desc=["Conversion Line — short-term equilibrium (HH + LL) / 2 over " + tenkanLen]
    )
}

if (showKijun) {
    plotLine(
        value=kijun,
        width=2,
        colors=[kijunCol],
        label=["Kijun-sen"],
        desc=["Base Line — medium-term equilibrium (HH + LL) / 2 over " + kijunLen]
    )
}

// -----------------------------------------------------------------------------
// 9. PLOTTING — TK Cross Shapes
// -----------------------------------------------------------------------------
if (showTKCross) {
    if (tkCrossUp) {
        plotShape(
            value=bars.low,
            shape="triangleup",
            colorIndex=tkBullCol,
            width=2,
            label=["TK Cross Up"],
            desc=["Bullish Tenkan/Kijun cross — Tenkan-sen crossed above Kijun-sen"]
        )
    }
    if (tkCrossDown) {
        plotShape(
            value=bars.high,
            shape="triangledown",
            colorIndex=tkBearCol,
            width=2,
            label=["TK Cross Down"],
            desc=["Bearish Tenkan/Kijun cross — Tenkan-sen crossed below Kijun-sen"]
        )
    }
}

// -----------------------------------------------------------------------------
// 10. PLOTTING — Price-In-Cloud Shapes
// -----------------------------------------------------------------------------
if (showPriceInCloud) {
    if (exitCloudUp) {
        plotShape(
            value=bars.close,
            shape="circle",
            colorIndex=tkBullCol,
            width=2,
            label=["Exit Cloud Up"],
            desc=["Price broke above Kumo cloud — bullish breakout signal"]
        )
    }
    if (exitCloudDn) {
        plotShape(
            value=bars.close,
            shape="circle",
            colorIndex=tkBearCol,
            width=2,
            label=["Exit Cloud Down"],
            desc=["Price broke below Kumo cloud — bearish breakdown signal"]
        )
    }
    if (enterCloud) {
        plotShape(
            value=bars.close,
            shape="xcross",
            colorIndex="#9e9e9e",
            width=1,
            label=["Enter Cloud"],
            desc=["Price entered Kumo cloud — neutral / consolidation zone"]
        )
    }
}

// -----------------------------------------------------------------------------
// 11. INFO TABLE (last bar only)
// -----------------------------------------------------------------------------
if (showTable && isLastBar) {
    var regimeText = regimeBull ? "BULLISH" : (regimeBear ? "BEARISH" : "NEUTRAL")
    var regimeTextColor = regimeBull ? cloudUp : (regimeBear ? cloudDn : "#9e9e9e")
    var cloudText = cloudBull ? "BULLISH KUMO" : "BEARISH KUMO"
    var positionText = priceAboveCloud ? "ABOVE CLOUD" : (priceBelowCloud ? "BELOW CLOUD" : "IN CLOUD")

    plotTable(
        data=[
            ["SCF Ichimoku", regimeText],
            ["Cloud Regime", cloudText],
            ["Price Position", positionText],
            ["Tenkan", tenkan[0]],
            ["Kijun", kijun[0]],
            ["Senkou A", senkouA[0]],
            ["Senkou B", senkouB[0]],
            ["Cloud Width", cloudWidth],
            ["TK Crosses", tkCrossCount],
            ["Bars Above Cloud", barsAboveCloud],
            ["Bars Below Cloud", barsBelowCloud],
            ["Bars In Cloud", barsInCloud]
        ],
        position="top_right",
        headerRow=true,
        headerColumn=true,
        textColor="#e2e8f0",
        headerTextColor="#ffffff",
        backgroundColor=transparency("#0f172a", 40),
        headerBackgroundColor=transparency("#1e293b", 40),
        borderColor=transparency("#334155", 40),
        borderWidth=1,
        fontSize=10,
        cellPadding=4
    )
}