2026/04/20

1-7. NK-Fixed Stochastics

「足が閉じる何秒前にリアルタイム描画を開始するか、0秒から5分の間で調節できる」機能が付いたストキャスティクスです。その他あった方が良い機能も付けています。

[ストキャスティクス]

一定期間の「最高値」から「最安値」までのレンジを100%としたとき、直近の終値が下から何%の位置にあるかを数値化し、買われすぎ・売られすぎを判定するオシレーターです。「上昇トレンドでは終値が最高値付近に張り付きやすく、下降トレンドでは最安値付近に張り付きやすい」という価格の習性を利用しています。RSIよりも反応が早く、特にレンジ環境において有効性が高くなります。


TradingViewでのStochastics表示例
出典: TradingView



[NOTICE & LICENSE]
・学習および情報提供のみを目的としており、投資助言ではありません。ツールを使用したことによる経済的損失について、一切の責任を負いません。
・Pine Script v6 で動作確認しています。将来のTradingView側の仕様変更に対するアップデート保証や、個別の導入サポート・改修依頼は一切受け付けません(現状有姿での提供)。
MIT Licenseに則っています。
// 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 Stochastics", shorttitle="NK-Fixed Stochastics", overlay=false)

// ==============================================================================
// 【01】 免責・ライセンス
// ==============================================================================
//
// インジケーターを無料で公開しています: https://www.nk-report.com/p/jp-0_0810719344.html
//
// 免責事項: 本スクリプトは学習および情報提供のみを目的としており、投資助言ではありません。
//
// 1. 本コードは2026年時点の Pine Script v6 で動作確認を行っています。
//    将来の仕様変更に伴う不具合等について、個別のサポートや改修は行いません。
// 2. 本スクリプトは一般的な計算ロジックに基づき、LLMを用いて独自に作成したものです。
//    MITライセンスに則っています。
// ------------------------------------------------------------------------------
// 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   = "▼ 描画制御 (Fixed Settings)"
const string LBL_SEC     = "リアルタイム描画開始 (足が閉じるX秒前)"
const string TT_SEC      = "0秒から300秒(5分)の間で調節。指定時間(例:10秒前)になるまで、現在の足への描画を非表示にします。\n0秒と入力した場合は非表示機能を無効化し、常にリアルタイムで描画します。"

const string GRP_STOCH   = "▼ Stochastics設定 (Stoch Settings)"
const string LBL_K_LEN   = "%K 期間 (%K Length)"
const string LBL_K_SMOOTH= "%K 平滑化 (%K Smoothing)"
const string LBL_D_LEN   = "%D 期間 (%D Length)"

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

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

i_k_len    = input.int(14, title=LBL_K_LEN, minval=1, group=GRP_STOCH, inline="ST1")
i_k_smooth = input.int(3, title=LBL_K_SMOOTH, minval=1, group=GRP_STOCH, inline="ST1")
i_d_len    = input.int(3, title=LBL_D_LEN, minval=1, group=GRP_STOCH, inline="ST2")


// ==============================================================================
// 【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. インジケーター固有の純粋な数学的処理
float raw_k  = ta.stoch(close, high, low, i_k_len)
float calc_k = ta.sma(raw_k, i_k_smooth)
float calc_d = ta.sma(calc_k, i_d_len)

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

// 3. 視覚的装飾の排除とデータの引き渡し
float final_k = is_draw_ready ? calc_k : na
float final_d = is_draw_ready ? calc_d : na


// ==============================================================================
// 【06】 描画と出力 (Rendering & Outputs)
// ==============================================================================
// 1. チャートへの視覚的出力(プロットと装飾)
// 買われすぎ(80)・売られすぎ(20)ラインと背景の塗りつぶし
ob_line = hline(80, title="Overbought", color=color.new(color.gray, 50), linestyle=hline.style_dashed)
os_line = hline(20, title="Oversold",   color=color.new(color.gray, 50), linestyle=hline.style_dashed)
fill(ob_line, os_line, title="OB/OS Background", color=color.new(#9C27B0, 90))

// ユーザー追加用の水平線(デフォルト値はすべて50)
hline(50, title="水平線 1", color=color.gray, linestyle=hline.style_dashed)
hline(50, title="水平線 2", color=color.gray, linestyle=hline.style_dashed)
hline(50, title="水平線 3", color=color.gray, linestyle=hline.style_dashed)

plot(final_k, title="%K Line", color=#2962FF, linewidth=2)
plot(final_d, title="%D Line", color=#FF6D00, linewidth=1)

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

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

alertcondition(cross_over_d,   title="%K Cross Over %D",  message="%K line crossed over %D line")
alertcondition(cross_under_d,  title="%K Cross Under %D", message="%K line crossed under %D line")
alertcondition(cross_over_ob,  title="%K Cross OB",       message="%K line crossed into Overbought zone")
alertcondition(cross_under_os, title="%K Cross OS",       message="%K line crossed into Oversold zone")

※好みの配色に設定後、パラメータータブの「デフォルトを保存」を押してください。
※新規作成→インジケーター→ペーストする の順序で行わないと正しく表示されないことがあります。