EMA 2-35 クロスオーバー戦略
この戦略は2つの指数移動平均線のシンプルなクロスオーバーに従います。長さ2の高速EMAは価格変動に素早く反応し、長さ35の低速EMAは長期トレンドを表します。高速EMAが低速EMAをクロスするとポジションが開かれ、逆のクロスが発生すると反転されます。
リスク管理は価格ステップで表された固定のストップロスとテイクプロフィットレベルで処理されます。また、取引が有利な方向に動くにつれて利益を確保するためのトレーリングストップも適用されます。
詳細
- エントリー条件:
- ロング: EMA(2)がEMA(35)を上抜ける。
- ショート: EMA(2)がEMA(35)を下抜ける。
- ロング/ショート: 両方。
- エグジット条件:
- 反対のクロスオーバー。
- ストップロスまたはテイクプロフィットに達する。
- トレーリングストップが発動。
- ストップ: 固定のストップロス、テイクプロフィット、トレーリングストップ(すべて価格ステップ単位)。
- デフォルト値:
FastLength= 2SlowLength= 35StopLoss= 50TakeProfit= 150TrailingStop= 50
- フィルター:
- カテゴリ: トレンドフォロー
- 方向: 両方
- インジケーター: 移動平均線
- ストップ: あり
- 複雑さ: シンプル
- 時間軸: 短期
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>
/// EMA(2) / EMA(35) crossover strategy.
/// </summary>
public class Ema235CrossStrategy : Strategy
{
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFast;
private decimal _prevSlow;
private bool _hasPrev;
public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public Ema235CrossStrategy()
{
_fastLength = Param(nameof(FastLength), 10)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Fast EMA length", "Parameters");
_slowLength = Param(nameof(SlowLength), 35)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Slow EMA length", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevFast = 0;
_prevSlow = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fast = new ExponentialMovingAverage { Length = FastLength };
var slow = new ExponentialMovingAverage { Length = SlowLength };
SubscribeCandles(CandleType)
.Bind(fast, slow, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastVal, decimal slowVal)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevFast = fastVal;
_prevSlow = slowVal;
_hasPrev = true;
return;
}
var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;
if (crossUp && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (crossDown && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevFast = fastVal;
_prevSlow = slowVal;
}
}
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class ema_2_35_cross_strategy(Strategy):
def __init__(self):
super(ema_2_35_cross_strategy, self).__init__()
self._fast_length = self.Param("FastLength", 10) \
.SetDisplay("Fast EMA", "Fast EMA length", "Parameters")
self._slow_length = self.Param("SlowLength", 35) \
.SetDisplay("Slow EMA", "Slow EMA length", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle type", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
@property
def fast_length(self):
return self._fast_length.Value
@property
def slow_length(self):
return self._slow_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(ema_2_35_cross_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(ema_2_35_cross_strategy, self).OnStarted2(time)
fast = ExponentialMovingAverage()
fast.Length = self.fast_length
slow = ExponentialMovingAverage()
slow.Length = self.slow_length
self.SubscribeCandles(self.candle_type) \
.Bind(fast, slow, self.process_candle) \
.Start()
def process_candle(self, candle, fast_val, slow_val):
if candle.State != CandleStates.Finished:
return
fast_val = float(fast_val)
slow_val = float(slow_val)
if not self._has_prev:
self._prev_fast = fast_val
self._prev_slow = slow_val
self._has_prev = True
return
cross_up = self._prev_fast <= self._prev_slow and fast_val > slow_val
cross_down = self._prev_fast >= self._prev_slow and fast_val < slow_val
if cross_up and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif cross_down and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_fast = fast_val
self._prev_slow = slow_val
def CreateClone(self):
return ema_2_35_cross_strategy()