在 GitHub 上查看

Time Trader 策略

基于时间的策略,在指定的时刻根据设置选择做多或做空,并使用止盈止损保护头寸。

细节

  • 入场条件:在 TradeHour:TradeMinute:TradeSecond 时,如果 AllowBuy 为真则买入,如果 AllowSell 为真则卖出
  • 多空方向:根据设置可做多或做空
  • 出场条件:止盈或止损
  • 止损:是
  • 默认值
    • Volume = 1
    • TakeProfit = 0.2
    • StopLoss = 0.2
    • TradeHour = 0
    • TradeMinute = 0
    • TradeSecond = 0
    • AllowBuy = true
    • AllowSell = true
    • CandleType = TimeSpan.FromSeconds(1).TimeFrame()
  • 过滤器
    • 分类: Time
    • 方向: 双向
    • 指标: 无
    • 止损: 是
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
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>
/// Time-based trader using EMA trend for directional entries.
/// </summary>
public class TimeTraderStrategy : Strategy
{
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevEma;
	private bool _hasPrev;

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

	public TimeTraderStrategy()
	{
		_emaPeriod = Param(nameof(EmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "EMA period", "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();
		_prevEma = 0;
		_hasPrev = false;
	}

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

		var ema = new ExponentialMovingAverage { Length = EmaPeriod };
		SubscribeCandles(CandleType).Bind(ema, ProcessCandle).Start();
	}

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

		if (!_hasPrev)
		{
			_prevEma = emaVal;
			_hasPrev = true;
			return;
		}

		var close = candle.ClosePrice;

		// EMA rising and price above EMA -> buy
		if (emaVal > _prevEma && close > emaVal && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// EMA falling and price below EMA -> sell
		else if (emaVal < _prevEma && close < emaVal && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevEma = emaVal;
	}
}