在 GitHub 上查看

Super Take策略

该策略在多空之间交替交易,并在每次亏损后按马丁系数增加止盈距离。止损保持固定,盈利后止盈恢复为基础值。通过不断反向开仓并在亏损后提高目标,策略试图弥补回撤。

只有当没有持仓时才会开新仓,首次交易默认做多。之后的每笔交易都与上一笔平仓方向相反。

细节

  • 入场条件
    • 做多:无持仓且上一笔平仓为做空或没有历史。
    • 做空:无持仓且上一笔平仓为做多。
  • 多空方向:双向。
  • 出场条件
    • 当价格触及动态止盈或固定止损时平仓。
  • 止损/止盈:固定止损;亏损后止盈按马丁系数放大。
  • 默认值
    • TakeProfit = 10
    • StopLoss = 15
    • MartinFactor = 1.8
  • 过滤器
    • 分类:反转
    • 方向:双向
    • 指标:无
    • 止损:有
    • 复杂度:简单
    • 时间框架:任意
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:高
using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Alternating buy/sell strategy with martingale take profit.
/// Opens trades in opposite direction after each close,
/// enlarges take profit after losses.
/// </summary>
public class SuperTakeStrategy : Strategy
{
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<decimal> _martinFactor;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;
	private decimal _currentTakeProfit;
	private decimal _lastTakeProfitDistance;
	private bool _isLong;
	private bool _lastTradeWasLoss;
	private bool? _lastClosedWasBuy;

	public decimal TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
	public decimal StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }
	public decimal MartinFactor { get => _martinFactor.Value; set => _martinFactor.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SuperTakeStrategy()
	{
		_takeProfit = Param(nameof(TakeProfit), 3000m)
			.SetGreaterThanZero()
			.SetDisplay("Take Profit", "Base take profit distance", "Risk");

		_stopLoss = Param(nameof(StopLoss), 5000m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss", "Stop loss distance", "Risk");

		_martinFactor = Param(nameof(MartinFactor), 1.5m)
			.SetGreaterThanZero()
			.SetDisplay("Martingale Factor", "Multiplier after losing trade", "Risk");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_entryPrice = 0;
		_currentTakeProfit = 0;
		_lastTakeProfitDistance = 0;
		_isLong = false;
		_lastTradeWasLoss = false;
		_lastClosedWasBuy = null;
	}

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

		_lastTakeProfitDistance = TakeProfit;

		SubscribeCandles(CandleType)
			.Bind(ProcessCandle)
			.Start();
	}

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

		if (Position != 0)
		{
			if (_isLong)
			{
				var profit = candle.ClosePrice - _entryPrice;

				if (profit >= _currentTakeProfit)
				{
					SellMarket();
					_lastTradeWasLoss = false;
					_lastTakeProfitDistance = _currentTakeProfit;
					_lastClosedWasBuy = true;
					_entryPrice = 0;
				}
				else if (profit <= -StopLoss)
				{
					SellMarket();
					_lastTradeWasLoss = true;
					_lastTakeProfitDistance = _currentTakeProfit;
					_lastClosedWasBuy = true;
					_entryPrice = 0;
				}
			}
			else
			{
				var profit = _entryPrice - candle.ClosePrice;

				if (profit >= _currentTakeProfit)
				{
					BuyMarket();
					_lastTradeWasLoss = false;
					_lastTakeProfitDistance = _currentTakeProfit;
					_lastClosedWasBuy = false;
					_entryPrice = 0;
				}
				else if (profit <= -StopLoss)
				{
					BuyMarket();
					_lastTradeWasLoss = true;
					_lastTakeProfitDistance = _currentTakeProfit;
					_lastClosedWasBuy = false;
					_entryPrice = 0;
				}
			}

			return;
		}

		var openBuy = _lastClosedWasBuy is not true;

		_currentTakeProfit = _lastTradeWasLoss
			? _lastTakeProfitDistance * MartinFactor
			: TakeProfit;

		if (openBuy)
		{
			BuyMarket();
			_entryPrice = candle.ClosePrice;
			_isLong = true;
		}
		else
		{
			SellMarket();
			_entryPrice = candle.ClosePrice;
			_isLong = false;
		}
	}
}