2026/04/20

1-5. NK-Fixed RSI

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


[RSI]

RSI(相對強弱指標)計算的是在指定期間內「平均上漲幅度」與「平均下跌幅度」之間的平衡,並將當前市場相對的看多與看空程度,以 0 到 100 的百分比數據化呈現。它衡量的並非價格的絕對值,而是「價格變動的速度與偏向(動能)」。


TradingView 上的 RSI 顯示範例
來源: 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 RSI", shorttitle="NK-Fixed RSI", 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_RSI     = "▼ RSI設定"
const string LBL_SH_RSI  = "RSI "
const string LBL_LEN     = "長度"
const string LBL_SRC     = "來源"

const string LBL_SH_MA1  = "平滑化 1"
const string LBL_S1_TYPE = "類型"
const string LBL_S1_LEN  = "長度"

const string LBL_SH_MA2  = "平滑化 2"
const string LBL_S2_TYPE = "類型"
const string LBL_S2_LEN  = "長度"

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

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

// RSI設定
i_show_rsi = input.bool(true, title=LBL_SH_RSI, group=GRP_RSI, inline="RSI0")
i_len      = input.int(14, title=LBL_LEN, minval=1, group=GRP_RSI, inline="RSI0")
i_src      = input.source(close, title=LBL_SRC, group=GRP_RSI, inline="RSI0")

// 平滑化1の設定
i_show_ma1 = input.bool(false, title=LBL_SH_MA1, group=GRP_RSI, inline="RSI1")
i_ma1_type = input.string("SMA", title=LBL_S1_TYPE, options=["SMA", "EMA", "WMA"], group=GRP_RSI, inline="RSI1")
i_ma1_len  = input.int(20, title=LBL_S1_LEN, minval=1, group=GRP_RSI, inline="RSI1")

// 平滑化2の設定
i_show_ma2 = input.bool(false, title=LBL_SH_MA2, group=GRP_RSI, inline="RSI2")
i_ma2_type = input.string("SMA", title=LBL_S2_TYPE, options=["SMA", "EMA", "WMA"], group=GRP_RSI, inline="RSI2")
i_ma2_len  = input.int(50, title=LBL_S2_LEN, minval=1, group=GRP_RSI, inline="RSI2")


// ==============================================================================
// 【03】 全コード共通仕様(タイムゾーン・時間インフラ処理)
// ==============================================================================
const int MS_PER_SEC  = 1000

// ==============================================================================
// 【04】 各カテゴリ共通仕様 (NK-Fixed Core Logic)
// ==============================================================================
bool is_draw_ready = true

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. 純正RSIの計算
float raw_rsi = ta.rsi(i_src, i_len)

// 2. 平滑化処理の共通関数
f_calc_smooth(type_str, src_val, len_val) =>
    float res = na
    switch type_str
        "SMA" => res := ta.sma(src_val, len_val)
        "EMA" => res := ta.ema(src_val, len_val)
        "WMA" => res := ta.wma(src_val, len_val)
    res

// 平滑化ラインの計算
float raw_ma1 = f_calc_smooth(i_ma1_type, raw_rsi, i_ma1_len)
float raw_ma2 = f_calc_smooth(i_ma2_type, raw_rsi, i_ma2_len)

// 3. データ引き渡し
float final_rsi = (is_draw_ready and i_show_rsi) ? raw_rsi : na
float final_ma1 = (is_draw_ready and i_show_ma1) ? raw_ma1 : na
float final_ma2 = (is_draw_ready and i_show_ma2) ? raw_ma2 : na


// ==============================================================================
// 【06】 描画と出力 (Rendering & Outputs)
// ==============================================================================
// カラー定義(座標指定から抽出したHEXコード)
color col_rsi    = #7E57C2 // 右から3、下から3
color col_lvl_70 = #787B86 // 右から5、上から1
color col_lvl_50 = color.black // 右から1、上から1
color col_lvl_30 = #787B86 // 右から5、上から1

// メインラインの描画
plot(final_rsi, title="NK-Fixed RSI", color=col_rsi, linewidth=1, style=plot.style_line)
plot(final_ma1, title="平滑化 1",  color=color.green,  linewidth=1, style=plot.style_line)
plot(final_ma2, title="平滑化 2",  color=color.blue,   linewidth=1, style=plot.style_line)

// 標準的な水平線の描画 (70, 50, 30)
h70 = hline(70, title="水平線 1", color=col_lvl_70, linestyle=hline.style_dashed)
h50 = hline(50, title="水平線 2", color=col_lvl_50, linestyle=hline.style_dashed)
h30 = hline(30, title="水平線 3", color=col_lvl_30, linestyle=hline.style_dashed)

// 背景の塗りつぶし (不透明度10% = Pine Scriptの透明度90)
fill(h70, h30, title="背景", color=color.new(col_rsi, 90))

// ユーザー追加ライン (Extra Line) - デフォルトは非表示(スタイルタブで設定可能)
hline(50, title="水平線 4", color=color.black, linestyle=hline.style_solid, display=display.none)
hline(50, title="水平線 5", color=color.black, linestyle=hline.style_solid, display=display.none)

// 3. アラート(通知)条件の統合
bool overbought = ta.crossover(raw_rsi, 70) and is_draw_ready
bool oversold   = ta.crossunder(raw_rsi, 30) and is_draw_ready

alertcondition(overbought, title="RSI Over 70",  message="NK-Fixed RSI crossed over 70")
alertcondition(oversold,   title="RSI Under 30", message="NK-Fixed RSI crossed under 30")

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