更新日:2022/10/21
JSTraderカスタムストラテジー
[
{
name: "sample.smaCrossStrategy"
, onInit() {
this.shortName = "smaCross"
const params = this.params;
//短期SMA
this.fastSMA = this.mainContainerIndicator.addItemIndicator("builtin.SMA", params.fast)
//長期SMA
this.slowSMA = this.mainContainerIndicator.addItemIndicator("builtin.SMA", params.slow)
//ポジション保有数 (今後ポジション情報を参照できるように修正予定ですが、現状参照できない為このようにストラテジーの中で管理してください)
this.buyPositionCount = 0;
this.sellPositionCount = 0;
}
, onChartChange({ index, times, opens, highs, lows, closes, spreads, isBarChanged }) { // バーまたはレートが変わった時の処理
const fastBuffer = this.fastSMA.smaLineBuffer
const slowBuffer = this.slowSMA.smaLineBuffer
const symbol = this.symbol;
if (isBarChanged) { //バーが変わった時
if (fastBuffer.get(index - 2) <= slowBuffer.get(index - 2)
&& fastBuffer.get(index - 1) > slowBuffer.get(index - 1) // ゴールデンクロス判定
) {
if (this.sellPositionCount > 0) { //売りポジションを持っていれば
symbol.newMarketOrder(BuySell.Buy, 1) // 買を実行(売りポジションを決済)
this.sellPositionCount = 0;
}
symbol.newMarketOrder(BuySell.Buy, 1) //買を実行(新規)
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.newMarketOrder(BuySell.Sell, 1) // 売りを実行(買いポジションを決済)
this.buyPositionCount = 0
}
symbol.newMarketOrder(BuySell.Sell, 1) // 売りを実行(新規)
this.sellPositionCount = 1;
}
}
}
, params: [
{
fast: [// 短期SMAのデフォルト値
{ smaPeriod: 5 },
{ lineColor: "lime" }
]
},
{
slow: [ // 長期SMAのデフォルト値
{ smaPeriod: 20 },
{ lineColor: "cyan" }
]
}
]
}
]