更新日:2023/10/17

JSTraderカスタムストラテジー

[
    {
        type: "s",
        name: "sample.smaCrossStrategy",
        onInit() {
            this.shortName = "smaCross"
            const params = this.params;
            const origParams = Indicator.getParameter("sample.SMA")
            const fastParams = { ...origParams, ...params.fast }
            const slowParams = { ...origParams, ...params.slow }
            const mainContainerIndicator=this.chart.mainContainerIndicator;
            this.fastSMA = mainContainerIndicator.addItemIndicator("sample.SMA", fastParams)
            this.slowSMA = mainContainerIndicator.addItemIndicator("sample.SMA", slowParams)
            this.buyPositionCount = 0;
            this.sellPositionCount = 0;
        },
        onChartChange({ index, times, opens, highs, lows, closes, spreads, isBarChanged }) {
            const symbol = this.symbol;
            const chart = this.chart;
            const fastBuffer = this.fastSMA.smaBuffer
            const slowBuffer = this.slowSMA.smaBuffer
            if (isBarChanged) {
                if (fastBuffer.get(index - 2) <= slowBuffer.get(index - 2)
                    && fastBuffer.get(index - 1) > slowBuffer.get(index - 1)
                ) {
                    if (this.sellPositionCount > 0) {
                        symbol.entry({orderType:'market',direction:'buy',lot:1,hedging:false})
                        this.sellPositionCount = 0;
                    }
                    if (this.buyPositionCount === 0) {
                        symbol.entry({orderType:'market',direction:'buy',lot:1,hedging:false})
                        this.buyPositionCount = 1;
                    }
                }
                if (fastBuffer.get(index - 2) >= slowBuffer.get(index - 2)
                    && fastBuffer.get(index - 1) < slowBuffer.get(index - 1)) {
                    if (this.buyPositionCount > 0) {
                        symbol.entry({orderType:'market',direction:'sell',lot:1,hedging:false})
                        this.buyPositionCount = 0
                    }
                    if (this.sellPositionCount === 0) {
                        symbol.entry({orderType:'market',direction:'sell',lot:1,hedging:false})
                        this.sellPositionCount = 1;
                    }
                }
            }
        },
        params: [
            {
                fast: [
                    { period: 10 },
                    { lineColor: "lime" }
                ]
            },
            {
                slow: [
                    { period: 30 },
                    { lineColor: "cyan" }
                ]
            }
        ]
    }
]