Laguerre CCI MA 戦略
Laguerreフィルター、商品チャンネル指数(CCI)、指数移動平均を組み合わせた戦略。
概要
- Laguerreフィルターは0-1のスケールで買われすぎと売られすぎの極端値を強調します。
- CCIは同方向のモメンタムを確認します。
- EMAの傾きにより、優勢なトレンドに沿った取引をフィルタリングします。
エントリールール
- Laguerre値が0、EMAが上昇中、CCIが負の
CciLevel閾値を下回るときにロング。 - Laguerre値が1、EMAが下降中、CCIが正の
CciLevel閾値を上回るときにショート。
エグジットルール
- Laguerreが0.9を超えたときにロングポジションを決済。
- Laguerreが0.1を下回ったときにショートポジションを決済。
パラメーター
LagGamma– Laguerreフィルターのガンマ値。CciPeriod– CCI計算の期間。CciLevel– エントリーに使用するCCIの絶対レベル。MaPeriod– 移動平均の期間。TakeProfit– 絶対価格単位でのテイクプロフィット(オプション)。StopLoss– 絶対価格単位でのストップロス(オプション)。CandleType– インジケーターに使用するロウソク足の種類。
この戦略は完成したロウソク足のみを処理し、インジケーターにはStockSharpの高レベルAPIバインディングを使用します。
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy combining CCI crossover with EMA trend filter.
/// </summary>
public class LaguerreCciMaStrategy : Strategy
{
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevCci;
private bool _hasPrev;
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public LaguerreCciMaStrategy()
{
_cciPeriod = Param(nameof(CciPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "Period for CCI indicator", "Indicators");
_maPeriod = Param(nameof(MaPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("MA Period", "Period for moving average", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevCci = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var cci = new CommodityChannelIndex { Length = CciPeriod };
var ma = new ExponentialMovingAverage { Length = MaPeriod };
SubscribeCandles(CandleType)
.Bind(cci, ma, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal cciValue, decimal maValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevCci = cciValue;
_hasPrev = true;
return;
}
var close = candle.ClosePrice;
// Buy: CCI crosses above 0 and price above MA
if (_prevCci <= 0 && cciValue > 0 && close > maValue && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
// Sell: CCI crosses below 0 and price below MA
else if (_prevCci >= 0 && cciValue < 0 && close < maValue && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevCci = cciValue;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import CommodityChannelIndex, ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class laguerre_cci_ma_strategy(Strategy):
def __init__(self):
super(laguerre_cci_ma_strategy, self).__init__()
self._cci_period = self.Param("CciPeriod", 14) \
.SetDisplay("CCI Period", "Period for CCI indicator", "Indicators")
self._ma_period = self.Param("MaPeriod", 20) \
.SetDisplay("MA Period", "Period for moving average", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_cci = 0.0
self._has_prev = False
@property
def cci_period(self):
return self._cci_period.Value
@property
def ma_period(self):
return self._ma_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(laguerre_cci_ma_strategy, self).OnReseted()
self._prev_cci = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(laguerre_cci_ma_strategy, self).OnStarted2(time)
cci = CommodityChannelIndex()
cci.Length = self.cci_period
ma = ExponentialMovingAverage()
ma.Length = self.ma_period
self.SubscribeCandles(self.candle_type).Bind(cci, ma, self.process_candle).Start()
def process_candle(self, candle, cci_value, ma_value):
if candle.State != CandleStates.Finished:
return
cv = float(cci_value)
mv = float(ma_value)
if not self._has_prev:
self._prev_cci = cv
self._has_prev = True
return
close = float(candle.ClosePrice)
if self._prev_cci <= 0 and cv > 0 and close > mv and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif self._prev_cci >= 0 and cv < 0 and close < mv and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_cci = cv
def CreateClone(self):
return laguerre_cci_ma_strategy()