在 GitHub 上查看

ASCtrend 策略

该策略使用威廉 %R 指标来捕捉类似 ASCtrend 的快速反转。当指标从超卖区迅速上升到超买区时做空,反之则做多。

细节

  • 入场条件
    • 当威廉 %R 从超卖区(低于 x2)上穿超买区(高于 x1)时做空。
    • 当威廉 %R 从超买区(高于 x1)下穿超卖区(低于 x2)时做多。
  • 多空方向:双向。
  • 出场条件
    • 反向信号平仓并反向开仓。
  • 止损:无。
  • 默认值
    • Risk = 4
    • CandleType = 1 小时
  • 筛选
    • 类型:反转
    • 方向:双向
    • 指标:威廉 %R
    • 止损:否
    • 复杂度:低
    • 时间框架:任意
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中
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>
/// ASCtrend strategy based on Williams %R reversals.
/// </summary>
public class ASCtrendStrategy : Strategy
{
	private readonly StrategyParam<int> _risk;
	private readonly StrategyParam<DataType> _candleType;

	private bool _wasOversold;
	private bool _wasOverbought;

	/// <summary>
	/// Risk factor that adjusts threshold levels.
	/// </summary>
	public int Risk
	{
		get => _risk.Value;
		set => _risk.Value = value;
	}

	/// <summary>
	/// Candle type used by the strategy.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initialize the strategy.
	/// </summary>
	public ASCtrendStrategy()
	{
		_risk = Param(nameof(Risk), 4)
			.SetRange(1, 50)
			.SetDisplay("Risk", "Risk parameter", "General");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_wasOversold = false;
		_wasOverbought = false;
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var wpr = new WilliamsR
		{
			Length = 3 + Risk * 2
		};

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var value2 = 100m - Math.Abs(wpr);
		var x1 = 67m + Risk;
		var x2 = 33m - Risk;

		if (value2 < x2)
			_wasOversold = true;
		else if (value2 > x1)
			_wasOverbought = true;

		if (_wasOversold && value2 > x1 && Position >= 0)
		{
			SellMarket();
			_wasOversold = false;
			_wasOverbought = false;
			return;
		}

		if (_wasOverbought && value2 < x2 && Position <= 0)
		{
			BuyMarket();
			_wasOversold = false;
			_wasOverbought = false;
		}
	}
}