平均足を作る JsTrader

平均足を例にコンテナインジケーターを作成する

ver1.8.4.0よりカスタムインジケーター作成とストレタジーテスターは個人利用データ使用時のみ可能になりました。 FXCM配信データを使っている場合はご利用いただけません。

概要

コンテナインジケーターの作成方法を平均足を例に説明します。

  • コンテナインジケーターとは、「高さ」を持ち、単独で表示できるインジケーターです。
アイテムインジケーターとの共通箇所は説明を省略します。必要に応じてアイテムインジケーターの説明も参照してください。
参照:単純移動平均を作る

操作

インジケーター「新規登録>コンテナインジケーター」からインジケーター作成画面を開いてください。

コンテナインジケーターの作成

ここでは平均足を例にコンテナインジケータの実装方法の説明をします。

下記のコードを入力、登録してください。

export default (): ContainerIndicator<{
    initialHeight: number | string
    upColor: string
    sameColor: string
    downColor: string
}, {
    openBuffer: IndicatorBuffer
    highBuffer: IndicatorBuffer
    lowBuffer: IndicatorBuffer
    closeBuffer: IndicatorBuffer
}
> => {
    return {
        type: "c", //1
        name: "tutor.HeikinAshi",
        onInit() {
            const { initialHeight, upColor, sameColor, downColor } = this.params;
            this.initialHeight = initialHeight; //2
            const [openBuffer, highBuffer, lowBuffer, closeBuffer] = this.addCandleStickBuffer([ //3
                upColor, sameColor, downColor
            ]);
            this.openBuffer = openBuffer
            this.highBuffer = highBuffer
            this.lowBuffer = lowBuffer
            this.closeBuffer = closeBuffer
            this.shortName = "heikin"
            this.isShowDefaultScaleLine = true //4
            this.onChartChange = ({ index, times, opens, highs, lows, closes, spreads }) => {
                if (index == 0) {
                    return; //5
                }
                const preOpen = opens.get(index - 1),
                    preClose = closes.get(index - 1);
                const openVal = (preOpen + preClose) / 2,
                    highVal = highs.get(index),
                    lowVal = lows.get(index),
                    closeVal = (opens.get(index) + highs.get(index) + lows.get(index) + closes.get(index)) / 4;
                this.openBuffer.set(index, (preOpen + preClose) / 2);
                this.highBuffer.set(index, highs.get(index));
                this.lowBuffer.set(index, lows.get(index));
                this.closeBuffer.set(index, (opens.get(index) + highs.get(index) + lows.get(index) + closes.get(index)) / 4);
            }
            this.getDisplayData = (index) => {
                return {
                    openPrice: this.openBuffer.get(index),
                    closePrice: this.closeBuffer.get(index)
                }
            }
        },
        params:
        {
            initialHeight: "25%",
            upColor: "blue",
            sameColor: "black",
            downColor: "red"
        },

    }
}
                   
1 コンテナインジケーターはtypeプロパティに"c"を指定します
2 コンテナインジケーターの高さとなるinitialHeightをparamsに定義しています。
ピクセル指定(px) 、パーセント指定(%)が可能です。数値はピクセル指定となります。
デフォルトは25%です。
3
  • addCandleStickBufferメソッドはローソク足を描画するインジケーターバッファーを格納した配列を戻します。
    • 配列を引数にした場合、lengthによって下記のバーの色となります。
    • 文字列を引数にした場合、length:1の配列を指定した場合と同じになります。
    • length:1
      index バー
      0 陽線
      十字線
      陰線
      length:2
      index バー
      0 陽線
      十字線
      1 陰線
      length:3
      index バー
      0 陽線
      1 十字線
      2 陰線
    • オブジェクトを引数にした場合、より詳細な指定が可能です。 設定する値は配列・文字列(数値)で対応するバーは上記と同じです。
      キー 説明
      bodyColor
      • バーの色を指定
      { bodyColor:['blue','black','red']}
      shadowColor
      • ヒゲの色
      • 省略時はbodyColorと同じ
      { bodyColor:['blue','black','red'], shadowColor:'black' }
      shadowWidth
      • ヒゲの太さ
      • 省略時は1
      { bodyColor:['blue','black','red'], shadowColor:'black', shadowWidth:2 }
    • 戻り値は「始値、安値、高値、終値」のインジケーターバッファーの配列です。
4 メインローソク足と同様の価格ラインを表示する設定をしています。
5 平均足はindex:0は計算できないため、index:0を無視しています。
index:0を無視しない場合、高値と安値は計算可能の為、ヒゲだけが描画されます。