更新日:2020/6/6

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(100)
        }
        , 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" }
        ]
    },
    {
        //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")
                .setBufferInfo(this.macdBuffer, "macd", 0)
                .setBufferInfo(this.signalBuffer, "signal", 0)
                .setBufferInfo(this.osciBuffer, "osci", 0)
                .setIndicatorDigit(0)
                .showScaleLine(0, "darkgray")
                .setHeight(100);
        }
        , 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" }
        ]
    }
]