Exp X2MA 策略
Exp X2MA 策略交易双重平滑移动平均线的转折点。 价格先经过简单移动平均线平滑,再经 Jurik 移动平均线平滑。 当平滑线形成局部低点时,策略买入并平掉空头。 当形成局部高点时,策略卖出并平掉多头。 可选的固定止损和止盈保护持仓。
细节
- 数据:价格K线(默认4小时)。
- 入场条件:
- 做多:前一个 X2MA 值低于更早的值且当前值向上转折。
- 做空:前一个 X2MA 值高于更早的值且当前值向下转折。
- 出场条件:相反的极值、止损或止盈。
- 止损:固定止损和止盈(点数)。
- 默认值:
FirstMaLength= 12SecondMaLength= 5StopLossPoints= 1000TakeProfitPoints= 2000
- 筛选:
- 类别:趋势反转
- 方向:多头与空头
- 指标:SMA, JurikMovingAverage
- 止损:是
- 复杂度:低
- 风险等级:中等
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 based on turning points of double smoothed moving average.
/// Applies EMA followed by another EMA and enters on local extrema.
/// </summary>
public class ExpX2MaStrategy : Strategy
{
private readonly StrategyParam<int> _firstMaLength;
private readonly StrategyParam<int> _secondMaLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevPrevValue;
private decimal _prevValue;
private int _barCount;
public int FirstMaLength { get => _firstMaLength.Value; set => _firstMaLength.Value = value; }
public int SecondMaLength { get => _secondMaLength.Value; set => _secondMaLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public ExpX2MaStrategy()
{
_firstMaLength = Param(nameof(FirstMaLength), 12)
.SetGreaterThanZero()
.SetDisplay("First MA Length", "Period for first smoothing", "Indicators");
_secondMaLength = Param(nameof(SecondMaLength), 10)
.SetGreaterThanZero()
.SetDisplay("Second MA Length", "Period for second smoothing", "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();
_prevPrevValue = 0;
_prevValue = 0;
_barCount = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema1 = new ExponentialMovingAverage { Length = FirstMaLength };
var ema2 = new ExponentialMovingAverage { Length = SecondMaLength };
SubscribeCandles(CandleType).Bind(ema1, ema2, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal ema1Value, decimal ema2Value)
{
if (candle.State != CandleStates.Finished) return;
_barCount++;
if (_barCount >= 3)
{
var isLocalMin = _prevValue < _prevPrevValue && ema2Value > _prevValue;
var isLocalMax = _prevValue > _prevPrevValue && ema2Value < _prevValue;
if (isLocalMin && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (isLocalMax && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
}
_prevPrevValue = _prevValue;
_prevValue = ema2Value;
}
}
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 exp_x2_ma_strategy(Strategy):
def __init__(self):
super(exp_x2_ma_strategy, self).__init__()
self._first_ma_length = self.Param("FirstMaLength", 12) \
.SetDisplay("First MA Length", "Period for first smoothing", "Indicators")
self._second_ma_length = self.Param("SecondMaLength", 10) \
.SetDisplay("Second MA Length", "Period for second smoothing", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_prev_value = 0.0
self._prev_value = 0.0
self._bar_count = 0
@property
def first_ma_length(self):
return self._first_ma_length.Value
@property
def second_ma_length(self):
return self._second_ma_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(exp_x2_ma_strategy, self).OnReseted()
self._prev_prev_value = 0.0
self._prev_value = 0.0
self._bar_count = 0
def OnStarted2(self, time):
super(exp_x2_ma_strategy, self).OnStarted2(time)
ema1 = ExponentialMovingAverage()
ema1.Length = self.first_ma_length
ema2 = ExponentialMovingAverage()
ema2.Length = self.second_ma_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema1, ema2, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, ema1_value, ema2_value):
if candle.State != CandleStates.Finished:
return
self._bar_count += 1
if self._bar_count >= 3:
is_local_min = self._prev_value < self._prev_prev_value and ema2_value > self._prev_value
is_local_max = self._prev_value > self._prev_prev_value and ema2_value < self._prev_value
if is_local_min and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif is_local_max and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_prev_value = self._prev_value
self._prev_value = ema2_value
def CreateClone(self):
return exp_x2_ma_strategy()