ColorXdinMA 策略
概述
ColorXdinMA 策略实现了 XdinMA 指标,其计算公式为 ma_main * 2 - ma_plus,两项均为不同周期的简单移动平均线。策略监控该线的斜率,并在斜率改变方向时开仓。
交易逻辑
- 当指标先下行后上转时,开多仓并平掉所有空仓。
- 当指标先上行后下转时,开空仓并平掉所有多仓。
策略只处理已经完成的K线,并使用市价单执行。
参数
| 名称 | 描述 | 默认值 |
|---|---|---|
MainLength |
主移动平均线周期。 | 10 |
PlusLength |
辅助移动平均线周期。 | 20 |
CandleType |
用于计算的K线时间框架。 | 6 小时 |
说明
该实现基于 StockSharp 的高级 API,可根据需要扩展风险管理或可视化功能。
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy based on a custom XdinMA indicator derived from two moving averages.
/// The line is calculated as <c>ma_main * 2 - ma_plus</c> and orders are generated when its slope changes direction.
/// </summary>
public class ColorXdinMAStrategy : Strategy
{
private readonly StrategyParam<int> _mainLength;
private readonly StrategyParam<int> _plusLength;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _signalCooldownBars;
private SMA _mainMa = null!;
private SMA _plusMa = null!;
private bool _isInitialized;
private decimal _prev;
private decimal _prevPrev;
private int _cooldownRemaining;
/// <summary>
/// Period of the main moving average.
/// </summary>
public int MainLength
{
get => _mainLength.Value;
set => _mainLength.Value = value;
}
/// <summary>
/// Period of the additional moving average.
/// </summary>
public int PlusLength
{
get => _plusLength.Value;
set => _plusLength.Value = value;
}
/// <summary>
/// Candle type used for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Number of closed candles to wait before a new reversal trade.
/// </summary>
public int SignalCooldownBars
{
get => _signalCooldownBars.Value;
set => _signalCooldownBars.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="ColorXdinMAStrategy"/>.
/// </summary>
public ColorXdinMAStrategy()
{
_mainLength = Param(nameof(MainLength), 10)
.SetGreaterThanZero()
.SetDisplay("Main MA Length", "Period of the main moving average", "Indicator")
.SetOptimize(5, 20, 1);
_plusLength = Param(nameof(PlusLength), 20)
.SetGreaterThanZero()
.SetDisplay("Additional MA Length", "Period of the additional moving average", "Indicator")
.SetOptimize(10, 40, 1);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
_signalCooldownBars = Param(nameof(SignalCooldownBars), 3)
.SetNotNegative()
.SetDisplay("Signal Cooldown Bars", "Closed candles to wait before a new reversal", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_mainMa = null!;
_plusMa = null!;
_isInitialized = false;
_prev = 0m;
_prevPrev = 0m;
_cooldownRemaining = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_cooldownRemaining = 0;
_mainMa = new SMA { Length = MainLength };
_plusMa = new SMA { Length = PlusLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_mainMa, _plusMa, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _mainMa);
DrawIndicator(area, _plusMa);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal main, decimal plus)
{
if (candle.State != CandleStates.Finished)
return;
if (_cooldownRemaining > 0)
_cooldownRemaining--;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var xdin = main * 2m - plus;
if (!_isInitialized)
{
if (_mainMa.IsFormed && _plusMa.IsFormed)
{
_prevPrev = _prev = xdin;
_isInitialized = true;
}
return;
}
if (_cooldownRemaining == 0 && _prev < _prevPrev && xdin > _prev && Position <= 0)
{
BuyMarket(Volume + (Position < 0 ? -Position : 0m));
_cooldownRemaining = SignalCooldownBars;
}
else if (_cooldownRemaining == 0 && _prev > _prevPrev && xdin < _prev && Position >= 0)
{
SellMarket(Volume + (Position > 0 ? Position : 0m));
_cooldownRemaining = SignalCooldownBars;
}
_prevPrev = _prev;
_prev = xdin;
}
}
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 SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class color_xdin_ma_strategy(Strategy):
def __init__(self):
super(color_xdin_ma_strategy, self).__init__()
self._main_length = self.Param("MainLength", 10) \
.SetDisplay("Main MA Length", "Period of the main moving average", "Indicator")
self._plus_length = self.Param("PlusLength", 20) \
.SetDisplay("Additional MA Length", "Period of the additional moving average", "Indicator")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._signal_cooldown_bars = self.Param("SignalCooldownBars", 3) \
.SetDisplay("Signal Cooldown Bars", "Closed candles to wait before a new reversal", "General")
self._is_initialized = False
self._prev = 0.0
self._prev_prev = 0.0
self._cooldown_remaining = 0
@property
def MainLength(self):
return self._main_length.Value
@MainLength.setter
def MainLength(self, value):
self._main_length.Value = value
@property
def PlusLength(self):
return self._plus_length.Value
@PlusLength.setter
def PlusLength(self, value):
self._plus_length.Value = value
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def SignalCooldownBars(self):
return self._signal_cooldown_bars.Value
@SignalCooldownBars.setter
def SignalCooldownBars(self, value):
self._signal_cooldown_bars.Value = value
def OnStarted2(self, time):
super(color_xdin_ma_strategy, self).OnStarted2(time)
self._cooldown_remaining = 0
main_ma = SimpleMovingAverage()
main_ma.Length = self.MainLength
plus_ma = SimpleMovingAverage()
plus_ma.Length = self.PlusLength
self.SubscribeCandles(self.CandleType) \
.Bind(main_ma, plus_ma, self.ProcessCandle) \
.Start()
def ProcessCandle(self, candle, main, plus):
if candle.State != CandleStates.Finished:
return
if self._cooldown_remaining > 0:
self._cooldown_remaining -= 1
main_val = float(main)
plus_val = float(plus)
xdin = main_val * 2.0 - plus_val
if not self._is_initialized:
self._prev_prev = xdin
self._prev = xdin
self._is_initialized = True
return
if self._cooldown_remaining == 0 and self._prev < self._prev_prev and xdin > self._prev and self.Position <= 0:
volume = self.Volume + (-self.Position if self.Position < 0 else 0)
self.BuyMarket(volume)
self._cooldown_remaining = self.SignalCooldownBars
elif self._cooldown_remaining == 0 and self._prev > self._prev_prev and xdin < self._prev and self.Position >= 0:
volume = self.Volume + (self.Position if self.Position > 0 else 0)
self.SellMarket(volume)
self._cooldown_remaining = self.SignalCooldownBars
self._prev_prev = self._prev
self._prev = xdin
def OnReseted(self):
super(color_xdin_ma_strategy, self).OnReseted()
self._is_initialized = False
self._prev = 0.0
self._prev_prev = 0.0
self._cooldown_remaining = 0
def CreateClone(self):
return color_xdin_ma_strategy()