Parabolic SAR CCI 戦略
この戦略はParabolic SAR CCIインジケーターを使ってシグナルを生成します。 Price > SAR && CCI < CciOversold(売られすぎ条件での上昇トレンド)のときロングエントリー。Price < SAR && CCI > CciOverbought(買われすぎ条件での下降トレンド)のときショートエントリー。 混合市場での機会を求めるトレーダーに適しています。
テストでは年平均リターン約49%を示しています。暗号通貨市場で最もパフォーマンスが良好です。
詳細
- エントリー条件:
- ロング: Price > SAR && CCI < CciOversold (売られすぎ条件での上昇トレンド)
- ショート: Price < SAR && CCI > CciOverbought (買われすぎ条件での下降トレンド)
- ロング/ショート: 両方向。
- エグジット条件:
- ロング: 価格がSARを下回ったらロングポジションを退場
- ショート: 価格がSARを上回ったらショートポジションを退場
- ストップ: いいえ。
- デフォルト値:
SarAccelerationFactor= 0.02SarMaxAccelerationFactor= 0.2CciPeriod= 20CciOversold= -100CciOverbought= 100 -100と100はルールが示すCCI水準の既定値です。エントリーはSARが示すトレンド内の押し目・戻りで、SAR自体がトレーリング決済になります。終値がSARを下回ればロングを、上回ればショートを閉じます。 保有ポジションと逆方向のエントリーシグナルはドテンになります。CandleType= TimeSpan.FromMinutes(5)
- フィルター:
- カテゴリ: 混合
- 方向: 両方
- インジケーター: Parabolic SAR CCI
- ストップ: いいえ
- 複雑さ: 中級
- 時間軸: イントラデイ
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// Parabolic SAR CCI strategy.
/// A close above the Parabolic SAR with CCI below CciOversold buys the dip in the uptrend and a close below the SAR with CCI above
/// CciOverbought sells the rally in the downtrend,
/// reversing an opposite position. The SAR is the trailing stop: a long closes when price closes below it and a short when price closes above it.
/// </summary>
public class ParabolicSarCciStrategy : Strategy
{
private readonly StrategyParam<decimal> _sarAccelerationFactor;
private readonly StrategyParam<decimal> _sarMaxAccelerationFactor;
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<decimal> _cciOversold;
private readonly StrategyParam<decimal> _cciOverbought;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// Initial acceleration factor of the SAR.
/// </summary>
public decimal SarAccelerationFactor
{
get => _sarAccelerationFactor.Value;
set => _sarAccelerationFactor.Value = value;
}
/// <summary>
/// Maximum acceleration factor of the SAR.
/// </summary>
public decimal SarMaxAccelerationFactor
{
get => _sarMaxAccelerationFactor.Value;
set => _sarMaxAccelerationFactor.Value = value;
}
/// <summary>
/// Period of CCI.
/// </summary>
public int CciPeriod
{
get => _cciPeriod.Value;
set => _cciPeriod.Value = value;
}
/// <summary>
/// CCI level for longs.
/// </summary>
public decimal CciOversold
{
get => _cciOversold.Value;
set => _cciOversold.Value = value;
}
/// <summary>
/// CCI level for shorts.
/// </summary>
public decimal CciOverbought
{
get => _cciOverbought.Value;
set => _cciOverbought.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public ParabolicSarCciStrategy()
{
_sarAccelerationFactor = Param(nameof(SarAccelerationFactor), 0.02m)
.SetGreaterThanZero()
.SetDisplay("SAR Acceleration", "Initial acceleration factor of the SAR", "SAR");
_sarMaxAccelerationFactor = Param(nameof(SarMaxAccelerationFactor), 0.2m)
.SetGreaterThanZero()
.SetDisplay("SAR Max Acceleration", "Maximum acceleration factor of the SAR", "SAR");
_cciPeriod = Param(nameof(CciPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "Period of CCI", "CCI");
_cciOversold = Param(nameof(CciOversold), -100m)
.SetDisplay("CCI Oversold", "CCI level for longs", "CCI");
_cciOverbought = Param(nameof(CciOverbought), 100m)
.SetDisplay("CCI Overbought", "CCI level for shorts", "CCI");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sar = new ParabolicSar
{
Acceleration = SarAccelerationFactor,
AccelerationMax = SarMaxAccelerationFactor,
};
var cci = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(sar, cci, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sar);
DrawOwnTrades(area);
var oscillators = CreateChartArea();
if (oscillators != null)
{
DrawIndicator(oscillators, cci);
}
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue sarValue, IIndicatorValue cciValue)
{
if (candle.State != CandleStates.Finished)
return;
// The first SAR value is formed but empty.
if (!sarValue.IsFormed || sarValue.IsEmpty || !cciValue.IsFormed)
return;
var sar = sarValue.GetValue<decimal>();
var cci = cciValue.GetValue<decimal>();
var close = candle.ClosePrice;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (close > sar && cci < CciOversold && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (close < sar && cci > CciOverbought && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
else if (Position > 0 && close < sar)
SellMarket(Position);
else if (Position < 0 && close > sar)
BuyMarket(-Position);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ParabolicSar, CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
class parabolic_sar_cci_strategy(Strategy):
"""
Parabolic SAR CCI strategy.
A close above the Parabolic SAR with CCI below CciOversold buys the dip in the uptrend and a close below the SAR with CCI above
CciOverbought sells the rally in the downtrend,
reversing an opposite position. The SAR is the trailing stop: a long closes when price closes below it and a short when price closes above it.
"""
def __init__(self):
super(parabolic_sar_cci_strategy, self).__init__()
self._sar_acceleration_factor = self.Param("SarAccelerationFactor", 0.02).SetGreaterThanZero().SetDisplay("SAR Acceleration", "Initial acceleration factor of the SAR", "SAR")
self._sar_max_acceleration_factor = self.Param("SarMaxAccelerationFactor", 0.2).SetGreaterThanZero().SetDisplay("SAR Max Acceleration", "Maximum acceleration factor of the SAR", "SAR")
self._cci_period = self.Param("CciPeriod", 20).SetGreaterThanZero().SetDisplay("CCI Period", "Period of CCI", "CCI")
self._cci_oversold = self.Param("CciOversold", -100.0).SetDisplay("CCI Oversold", "CCI level for longs", "CCI")
self._cci_overbought = self.Param("CciOverbought", 100.0).SetDisplay("CCI Overbought", "CCI level for shorts", "CCI")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(parabolic_sar_cci_strategy, self).OnStarted2(time)
sar = ParabolicSar()
sar.Acceleration = Decimal(self._sar_acceleration_factor.Value)
sar.AccelerationMax = Decimal(self._sar_max_acceleration_factor.Value)
cci = CommodityChannelIndex()
cci.Length = self._cci_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(sar, cci, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sar)
self.DrawOwnTrades(area)
oscillators = self.CreateChartArea()
if oscillators is not None:
self.DrawIndicator(oscillators, cci)
def _process_candle(self, candle, sar_value, cci_value):
if candle.State != CandleStates.Finished:
return
# The first SAR value is formed but empty.
if not sar_value.IsFormed or sar_value.IsEmpty or not cci_value.IsFormed:
return
sar = sar_value.GetValue[Decimal](None)
cci = cci_value.GetValue[Decimal](None)
close = candle.ClosePrice
if not self.IsFormedAndOnlineAndAllowTrading():
return
if close > sar and cci < Decimal(self._cci_oversold.Value) and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif close < sar and cci > Decimal(self._cci_overbought.Value) and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
elif self.Position > 0 and close < sar:
self.SellMarket(self.Position)
elif self.Position < 0 and close > sar:
self.BuyMarket(-self.Position)
def CreateClone(self):
return parabolic_sar_cci_strategy()