MACDを作る JsTrader

MACDを作る

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

MACDの作成

MACDのサンプルコードは下記です。

export default (): ContainerIndicator<{
    fastEmaPeriod: number,
    slowEmaPeriod: number,
    signalSmaPeriod: number,
    macdColor: string,
    signalColor: string,
    osciColor: string,
    initialHeight: number | string
}, {
    macdBuffer: IndicatorBuffer
    signalBuffer: IndicatorBuffer
    osciBuffer: IndicatorBuffer
}> => {
    return {
        type: "c"
        , name: "tutor.MACD"
        , onInit() {
            const { initialHeight, osciColor, macdColor, signalColor } = this.params;
            this.initialHeight = initialHeight;
            const fastEmaBuffer = this.addNoopBuffer();
            const slowEmaBuffer = this.addNoopBuffer();
            this.macdBuffer = this.addLineBuffer({ lineColor: macdColor, lineWidth: 2 });
            this.signalBuffer = this.addLineBuffer({ lineColor: signalColor, lineWidth: 2 });
            this.osciBuffer = this.addHistgramBuffer({ backgroundColor: osciColor, baseValue: 0 }); //1
            this.shortName = "MACD";
            this.digits = 0;
            this.showScaleLine(0, "darkgray")
            this.onChartChange = ({ index, closes }) => {
                const macdBuffer = this.macdBuffer,
                    signalBuffer = this.signalBuffer,
                    osciBuffer = this.osciBuffer,
                    { fastEmaPeriod, slowEmaPeriod, signalSmaPeriod } = this.params;
                closes.ema(fastEmaBuffer, fastEmaPeriod, index);
                closes.ema(slowEmaBuffer, slowEmaPeriod, index)
                const macdVal = fastEmaBuffer.get(index) - slowEmaBuffer.get(index);
                macdBuffer.set(index, macdVal);
                const signalVal = macdBuffer.ema(signalBuffer, signalSmaPeriod, index, slowEmaPeriod)
                osciBuffer.set(index, macdVal - signalVal);
            }
            this.getDisplayData = (index) => {
                return {
                    macd: this.macdBuffer.get(index),
                    signal: this.signalBuffer.get(index),
                    osci: this.osciBuffer.get(index)
                }
            }
        }
        , params:
        {
            fastEmaPeriod: 12,
            slowEmaPeriod: 26,
            signalSmaPeriod: 9,
            macdColor: "green",
            signalColor: "cyan",
            osciColor: "gray",
            initialHeight: "25%"
        }
    }
}
                   
1 価格の変動を棒グラフで表すにはcreateHisgramBuffer関数を使ってHistgramBufferを作成します。