在 GitHub 上查看

ARD Order Management 策略

该策略使用 DeMarker 指标,当指标穿越 0.5 阈值时开仓。

当 DeMarker 从上方跌破阈值时做多;当 DeMarker 从下方突破阈值时做空。相反信号平仓。没有止损或止盈。

细节

  • 入场条件:
    • 多头:DeMarker 从上方跌破 Threshold
    • 空头:DeMarker 从下方突破 Threshold
  • 多/空:双向
  • 出场条件:相反信号
  • 止损:无
  • 默认值
    • DeMarkerPeriod = 2
    • Threshold = 0.5
    • CandleType = TimeSpan.FromMinutes(1).TimeFrame()
  • 筛选
    • 类别:指标
    • 方向:双向
    • 指标:DeMarker
    • 止损:无
    • 复杂度:基础
    • 时间框架:日内
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中
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 DeMarker crossing a threshold.
/// </summary>
public class ArdOrderManagementStrategy : Strategy
{
	private readonly StrategyParam<int> _deMarkerPeriod;
	private readonly StrategyParam<decimal> _threshold;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _previousValue;
	private bool _hasPrev;

	public int DeMarkerPeriod
	{
		get => _deMarkerPeriod.Value;
		set => _deMarkerPeriod.Value = value;
	}

	public decimal Threshold
	{
		get => _threshold.Value;
		set => _threshold.Value = value;
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public ArdOrderManagementStrategy()
	{
		_deMarkerPeriod = Param(nameof(DeMarkerPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("DeMarker Period", "DeMarker indicator period", "Parameters");

		_threshold = Param(nameof(Threshold), 0.5m)
			.SetDisplay("Threshold", "DeMarker crossing level", "Parameters");

		_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();
		_previousValue = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var deMarker = new DeMarker { Length = DeMarkerPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(deMarker, ProcessCandle).Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, deMarker);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, decimal deMarkerValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_hasPrev)
		{
			_previousValue = deMarkerValue;
			_hasPrev = true;
			return;
		}

		var buySignal = _previousValue > Threshold && deMarkerValue < Threshold;
		var sellSignal = _previousValue < Threshold && deMarkerValue > Threshold;

		if (buySignal && Position <= 0)
			BuyMarket();
		else if (sellSignal && Position >= 0)
			SellMarket();

		_previousValue = deMarkerValue;
	}
}