更新日:2022/10/21
JSTraderカスタムインジケーターサンプル
[
{
// 単純移動平均
name: "sample.SMA"
, onInit() {
const params = this.params,
props = {
strokeStyle: params.lineColor,
lineWidth: params.lineWidth,
lineDash: params.lineDash
};
this.smaLineBuffer = this.addLineBuffer(props);
this.shortName="SMA"
}
, onChartChange({index, times, opens, highs, lows, closes, spreads,isBarChanged}) {
const smaPeriod = this.params.smaPeriod,
smaBuffer = this.smaLineBuffer;
closes.sma(smaBuffer, smaPeriod, index)
}
, getDisplayData(index) {
return [
{ key: "sma", rate: this.smaLineBuffer.get(index) }
]
}
, params: [
{ smaPeriod: 25 },
{ lineColor: "red" },
{ lineWidth: 2 },
{ lineDash: [3, 3] }
]
},
{
// 指数移動平均
name: "sample.EMA"
, onInit() {
const params = this.params,
props = {
strokeStyle: params.lineColor,
lineWidth: params.lineWidth,
lineDash: params.lineDash
};
this.emaLineBuffer = this.addLineBuffer(props);
this.shortName="EMA"
}
, onChartChange({index, times, opens, highs, lows, closes, spreads,isBarChanged}) {
const emaPeriod = this.params.emaPeriod,
emaBuffer = this.emaLineBuffer
closes.ema(emaBuffer, emaPeriod, index);
}
, getDisplayData(index) {
const emaBuffer = this.emaLineBuffer;
return [
{ key: "ema", rate: emaBuffer.get(index) }
]
}
, params: [
{ emaPeriod: 25 },
{ lineColor: "green" },
{ lineWidth: 2 },
{ lineDash: [3, 3] }
]
},
{
// 平均足
name: "sample.HeikinAshi"
, onInit() {
const params = this.params;
const [openBuffer,highBuffer,lowBuffer,closeBuffer] = this.addCandleStickBuffer([
params.upColor,
params.downColor
]);
this.openBuffer = openBuffer
this.highBuffer = highBuffer
this.lowBuffer = lowBuffer
this.closeBuffer = closeBuffer
this.shortName="heikin"
this.setIsShowDefaultScaleLine=true
this.initialHeight=params.initialHeight
}
, onChartChange({index, times, opens, highs, lows, closes, spreads,isBarChanged}) {
const preOpen = opens.get(index - 1),
preClose = closes.get(index - 1);
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);
}
, getDisplayData(index) {
return [
{ key: "hOpen", rate: this.openBuffer.get(index) },
{ key: "hClose", rate: this.closeBuffer.get(index) }
]
}
, params: [
{ upColor: "blue" },
{ downColor: "red" },
{ initialHeight: "25%" }
]
},
{
// MACD
name: "sample.MACD"
, onInit() {
const 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.shortName="MACD"
this.digit=0
this.showScaleLine(0, "darkgray")
this.initialHeight=params.initialHeight;
}
, getDisplayData(index) {
return [
{ key: "macd", rate: this.macdBuffer.get(index) },
{ key: "signal", rate: this.signalBuffer.get(index) },
{ key: "osci", rate: this.osciBuffer.get(index) }
]
}
, onChartChange({index, times, opens, highs, lows, closes, spreads,isBarChanged}) {
const 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)
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);
}
, params: [
{ fastEmaPeriod: 12 },
{ slowEmaPeriod: 26 },
{ signalSmaPeriod: 9 },
{ macdColor: "green" },
{ signalColor: "cyan" },
{ osciColor: "gray" },
{ initialHeight: "25%" }
]
},
{
// ストキャスティクス
name: "sample.Stochastics",
onInit() {
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.shortName="stochastics"
this.digit=2
this.showScaleLine(80 * 100, "darkgray")
.showScaleLine(20 * 100, "darkgray")
this.initialHeight=this.params.initialHeight
},
onChartChange({index, times, opens, highs, lows, closes, spreads,isBarChanged}) {
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);
},
getDisplayData(index) {
return [
{ key: "%K", rate: this.kBuffer.get(index) },
{ key: "%D", rate: this.dBuffer.get(index) },
{ key: "%SD", rate: this.sdBuffer.get(index) },
]
},
params: {
periods: [14, 3, 3],
colors: ["aqua", "green", "gold"],
initialHeight: "25%"
}
}
]