2026/04/20

1-9. NK-Fixed RCI

這是一款具備「可自由調整在 K 棒收盤前 0 秒至 5 分鐘內,何時開始實時(Real-time)繪圖」功能的 RCI。此外也整合了其他多項實用的輔助功能。


[RCI]

RCI(順位相關指標)是一種將計算期間內「日期(時間)的排序」與「價格的排序」之間具備多少相關性(連動性),量化為 -100% 至 +100% 範圍內的指標。當「價格每天連續不斷上漲」時,數值會達到 +100%;反之,若「價格每天連續不斷下跌」,則會來到 -100%。由於它忽略了價格的「波動幅度(Volatility)」,僅萃取「上漲或下跌的客觀事實(排序)」,因此具備線條極為平滑,且能有效抵抗市場雜訊(如突發的上下影線)的強大特性。


TradingView 上的 RCI 顯示範例
來源: TradingView

[NOTICE & LICENSE]
・僅供學習與提供資訊之用,不構成投資建議。對於因使用本工具而產生之任何經濟損失,概不負責。
・已於 Pine Script v6 測試運作。以「現狀 (As-is)」提供,不保證因未來 TradingView 系統規格變更而進行更新,亦不接受個別的安裝支援或修改請求。
・適用 MIT 授權條款
// SPDX-FileCopyrightText: 2026 NK-report https://www.nk-report.com/
// SPDX-License-Identifier: MIT
//
// Disclaimer: This script is for educational purposes only and does not constitute investment advice.

//@version=6
indicator("NK-Fixed RCI", shorttitle="NK-Fixed RCI", overlay=false)

// ==============================================================================
// 【01】 免責・ライセンス
// ==============================================================================
//
// 本指標免費公開: https://www.nk-report.com/p/tw-tradingview.html
//
// 免責聲明:本腳本僅供學習與資訊參考之用,不構成任何投資建議。
//
// 1. 本程式碼截至 2026 年,已在 Pine Script v6 環境下完成運作測試。
//    日後若因規格變動而導致任何異常,恕不提供個別的技術支援或修改服務。
// 2. 本腳本係基於一般的計算邏輯,並透過 LLM 獨立編寫而成。
//    本程式碼採用 MIT 授權條款 (MIT License) 進行發布。
// ------------------------------------------------------------------------------
// MIT License 
//
// Copyright 2026 NK-report https://www.nk-report.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// ==============================================================================
// 【02】 UI設定と言語
// ==============================================================================
// 1. 言語辞書の定義(テキストの定数化)

const string GRP_FIXED   = "▼ 繪圖控制"
const string LBL_SEC     = "即時繪圖開始 (K棒收盤前X秒)"
const string TT_SEC      = "可在0秒至300秒(5分鐘)之間調整。在達到指定時間(例如:收盤前10秒)之前,將隱藏當前K棒的繪圖。\n若輸入0秒,則停用隱藏功能,並始終即時繪圖。"

const string GRP_RCI     = "▼ RCI設定"
const string LBL_SHORT   = "短線長度"
const string LBL_MID     = "中線長度"
const string LBL_LONG    = "長線長度"
const string LBL_SRC     = "來源"

// 2. UI(ユーザー入力画面)の構築 & 3. トップダウン処理への適合

i_sec      = input.int(0, title=LBL_SEC, minval=0, maxval=300, group=GRP_FIXED, tooltip=TT_SEC)

i_short    = input.int(9, title=LBL_SHORT, minval=1, group=GRP_RCI, inline="RCI1")
i_mid      = input.int(26, title=LBL_MID, minval=1, group=GRP_RCI, inline="RCI1")
i_long     = input.int(52, title=LBL_LONG, minval=1, group=GRP_RCI, inline="RCI2")
i_src      = input.source(close, title=LBL_SRC, group=GRP_RCI, inline="RCI2")


// ==============================================================================
// 【03】 全コード共通仕様(タイムゾーン・時間インフラ処理)
// ==============================================================================
// 1. 基本時間単位のシステム定義(定数化)
const int MS_PER_SEC  = 1000

// ==============================================================================
// 【04】 各カテゴリ共通仕様 (NK-Fixed Core Logic)
// ==============================================================================
// 1. カテゴリ固有のインフラ構築(コア機能の定義)
bool is_draw_ready = true

// リアルタイム足であり、かつユーザーが0秒より大きい数値を設定している場合のみ処理
if barstate.isrealtime and i_sec > 0
    if not na(time_close)
        int time_left_ms = time_close - timenow
        int threshold_ms = i_sec * MS_PER_SEC
        
        if time_left_ms > threshold_ms
            is_draw_ready := false


// ==============================================================================
// 【05】 このコード固有の計算仕様 (Specific Indicator Logic)
// ==============================================================================
// 1. インジケーター固有の純粋な数学的処理
// RCI(順位相関係数)を計算する独自関数
f_rci(src, len) =>
    float sum_sq = 0.0
    for i = 0 to len - 1
        int time_rank = i + 1
        float price_rank = 1.0
        int same_count = 0
        for j = 0 to len - 1
            if src[j] > src[i]
                price_rank += 1.0
            else if src[j] == src[i]
                same_count += 1
        // 同値(タイ)の順位を平均化する処理
        price_rank := price_rank + (same_count - 1) / 2.0
        sum_sq += math.pow(time_rank - price_rank, 2)
    float rci = (1.0 - (6.0 * sum_sq) / (len * (math.pow(len, 2) - 1.0))) * 100.0
    rci

float calc_short = f_rci(i_src, i_short)
float calc_mid   = f_rci(i_src, i_mid)
float calc_long  = f_rci(i_src, i_long)

// 2. ロジックの完全な隔離と差し替えの容易性

// 3. 視覚的装飾の排除とデータの引き渡し
float final_short = is_draw_ready ? calc_short : na
float final_mid   = is_draw_ready ? calc_mid   : na
float final_long  = is_draw_ready ? calc_long  : na


// ==============================================================================
// 【06】 描画と出力 (Rendering & Outputs)
// ==============================================================================
// 1. チャートへの視覚的出力(プロットと装飾)
// 水平線と背景の塗りつぶし
ob_line = hline(80,  title="Overbought", color=color.new(color.gray, 50), linestyle=hline.style_dashed)
os_line = hline(-80, title="Oversold",   color=color.new(color.gray, 50), linestyle=hline.style_dashed)
hline(0, title="Zero Line", color=color.new(color.gray, 50), linestyle=hline.style_dotted)

fill(ob_line, os_line, title="OB/OS Background", color=color.new(#9E9E9E, 80))

plot(final_short, title="RCI Short", color=#F44336, linewidth=2)
plot(final_mid,   title="RCI Mid",   color=#4CAF50, linewidth=2)
plot(final_long,  title="RCI Long",  color=#2196F3, linewidth=2)

// 2. ロジックとデザインの完全分離

// 3. アラート(通知)条件の統合
// ※投資助言を回避するため、方向性を指示する言葉(Buy/Sell等)は使わず、純粋な事実・状態のみを通知します
bool cross_over_sm  = ta.crossover(calc_short, calc_mid) and is_draw_ready
bool cross_under_sm = ta.crossunder(calc_short, calc_mid) and is_draw_ready
bool cross_over_0   = ta.crossover(calc_short, 0) and is_draw_ready
bool cross_under_0  = ta.crossunder(calc_short, 0) and is_draw_ready

alertcondition(cross_over_sm,  title="RCI Short Cross Over Mid",  message="RCI Short line crossed over RCI Mid line")
alertcondition(cross_under_sm, title="RCI Short Cross Under Mid", message="RCI Short line crossed under RCI Mid line")
alertcondition(cross_over_0,   title="RCI Short Cross Over 0",    message="RCI Short line crossed over Zero line")
alertcondition(cross_under_0,  title="RCI Short Cross Under 0",   message="RCI Short line crossed under Zero line")

* 設定好喜歡的顏色配置後,請在設定分頁中點擊「儲存為預設值」。
* 若未按照「新增 → 指標 → 貼上」的順序操作,可能無法正確顯示。