更新日:2021/10/17

JSTraderカスタムインジケーターサンプル

[
    {
        //単純移動平均
        indicatorName: "sample.SMA"
        , indicatorType: INDICATOR_TYPE.ITEM
        , init: function () {
            var params = this.params,
                props = {
                    strokeStyle: params.lineColor,
                    lineWidth: params.lineWidth,
                    lineDash: params.lineDash
                };
            this.smaLineBuffer = this.addLineBuffer(props);
            this.setIndicatorShortName("SMA")
                .setBufferInfo(this.smaLineBuffer, "sma");
        }
        , calc: function (index, times, opens, highs, lows, closes, spreads) {
            var smaPeriod = this.params.smaPeriod,
                smaBuffer = this.smaLineBuffer;
            closes.sma(smaBuffer, smaPeriod, index)
        }
        , params: [
            { smaPeriod: 25 },
            { lineColor: "red" },
            { lineWidth: 2 },
            { lineDash: [3, 3] }
        ]
    },
    {
        //指数移動平均
        indicatorName: "sample.EMA"
        , indicatorType: INDICATOR_TYPE.ITEM
        , init: function () {
            var params = this.params,
                props = {
                    strokeStyle: params.lineColor,
                    lineWidth: params.lineWidth,
                    lineDash: params.lineDash
                };
            this.emaLineBuffer = this.addLineBuffer(props);
            this.setIndicatorShortName("EMA")
                .setBufferInfo(this.emaLineBuffer, "ema");
        }
        , calc: function (index, times, opens, highs, lows, closes, spreads) {
            var emaPeriod = this.params.emaPeriod;
            var emaBuffer = this.emaLineBuffer
            closes.ema(emaBuffer, emaPeriod, index);
        }
        , params: [
            { emaPeriod: 25 },
            { lineColor: "green" },
            { lineWidth: 2 },
            { lineDash: [3, 3] }
        ]
    },
    {
        //平均足
        indicatorName: "sample.HeikinAshi"
        , indicatorType: INDICATOR_TYPE.CONTAINER
        , init: function () {
            var params = this.params;
            var candleStickBufferArr = this.addCandleStickBuffer([
                params.upColor,
                params.downColor
            ]);
            this.openBuffer = candleStickBufferArr[0];
            this.highBuffer = candleStickBufferArr[1];
            this.lowBuffer = candleStickBufferArr[2];
            this.closeBuffer = candleStickBufferArr[3];
            this.setIndicatorShortName("heikin")
                .setBufferInfo(this.openBuffer, "hOpen")
                .setBufferInfo(this.closeBuffer, "hClose")
                .setShowDefaultScaleLine(true)
                .setHeight(this.params.height)
        }
        , calc: function (index, times, opens, highs, lows, closes, spreads) {
            var preOpen = opens.get(index - 1),
                preClose = closes.get(index - 1);
            var 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);
        }
        , acceptDropFrom: function () {
            return true;
        }
        , params: [
            { upColor: "blue" },
            { downColor: "red" },
            { height: "25%"}
        ]
    },
    {
        //MACD 
        indicatorName: "sample.MACD"
        , indicatorType: INDICATOR_TYPE.CONTAINER
        , init: function () {
            var params = this.params;
            this.osciBuffer = this.addHistgramBuffer({ fillStyle: params.osciColor, globalAlpha: 0.5 });
            this.fastEmaBuffer = this.addNoopBuffer();
            this.slowEmaBuffer = this.addNoopBuffer();
            this.macdBuffer = this.addLineBuffer({ strokeStyle: params.macdColor, lineWidth: 2 });
            this.signalBuffer = this.addLineBuffer({ strokeStyle: params.signalColor, lineWidth: 2 });
            this.setIndicatorShortName("MACD")
                .setIndicatorDigit(0)
                .setBufferInfo(this.macdBuffer, "macd")
                .setBufferInfo(this.signalBuffer, "signal")
                .setBufferInfo(this.osciBuffer, "osci")
                .showScaleLine(0, "darkgray")
                .setHeight(this.params.height);
        }
        , calc: function (index, times, opens, highs, lows, closes, spreads) {
            var fastEmaBuffer = this.fastEmaBuffer,
                slowEmaBuffer = this.slowEmaBuffer,
                macdBuffer = this.macdBuffer,
                signalBuffer = this.signalBuffer,
                osciBuffer = this.osciBuffer,
                params = this.params,
                fastEmaPeriod = params.fastEmaPeriod,
                slowEmaPeriod = params.slowEmaPeriod,
                signalSmaPeriod = params.signalSmaPeriod;
            closes.ema(fastEmaBuffer, fastEmaPeriod, index);
            closes.ema(slowEmaBuffer, slowEmaPeriod, index)
            var macdVal = fastEmaBuffer.get(index) - slowEmaBuffer.get(index);
            macdBuffer.set(index, macdVal);
            var signalVal = macdBuffer.ema(signalBuffer, signalSmaPeriod, index, slowEmaPeriod)
            osciBuffer.set(index, macdVal - signalVal);
        }
        , params: [
            { fastEmaPeriod: 12 },
            { slowEmaPeriod: 26 },
            { signalSmaPeriod: 9 },
            { macdColor: "green" },
            { signalColor: "cyan" },
            { osciColor: "gray" },
            { height: "25%"}
        ]
    },
    {
        //ストキャスティクス
        indicatorName: "sample.Stochastics",
        indicatorType: INDICATOR_TYPE.CONTAINER ,
        init: function () {
            const colors = this.params.colors;
            this.kBuffer = this.addLineBuffer({strokeStyle: colors[0]});
            this.dBuffer = this.addLineBuffer({strokeStyle: colors[1]});
            this.sdBuffer = this.addLineBuffer({strokeStyle: colors[2]});
            this.numeratorBuffer = this.addNoopBuffer();
            this.denominatorBuffer = this.addNoopBuffer();
            this.setIndicatorShortName("stochastics")
            this.setIndicatorDigit(2)
    		.setBufferInfo(this.kBuffer, "%K")
    		.setBufferInfo(this.dBuffer, "%D")
    		.setBufferInfo(this.sdBuffer, "SD")
            .showScaleLine(80*100, "darkgray")
            .showScaleLine(20*100, "darkgray")
    		.setHeight(this.params.height)
        },
        calc: function (index, times, opens, highs, lows, closes, spreads) {
            const kBuffer = this.kBuffer
            const dBuffer = this.dBuffer,
                sdBuffer = this.sdBuffer,
                numeratorBuffer = this.numeratorBuffer,
                denominatorBuffer = this.denominatorBuffer;
            const periods = this.params.periods,
                kPeriod = periods[0],dPeriod = periods[1], sdPeriod = periods[2];
            const lowest = lows.min(index - kPeriod + 1, index),
                highest = highs.max(index - kPeriod + 1, index),
                close = closes.get(index),num = close - lowest, denom = highest - lowest;
            numeratorBuffer.set(index, num);
            denominatorBuffer.set(index, denom);
            const val = num / denom * 100 * 100;
            kBuffer.set(index, val)
            const sumNumerator = numeratorBuffer.sum(index - dPeriod + 1, index),
                sumDenominator = denominatorBuffer.sum(index - dPeriod + 1, index)
            dBuffer.set(index, sumNumerator / sumDenominator * 100 * 100);
            const avg = dBuffer.avg(index - sdPeriod + 1, index);
            sdBuffer.set(index, avg);
        },
        params: {
            periods: [14, 3, 3],
            colors: ["aqua", "green", "gold"],
            height: "25%"
        }
    }
]