在 GitHub 上查看

Exp Multic 策略

该策略在没有任何技术指标的情况下交易一组固定的主要外汇货币对。 对于每个货币对,算法保存方向和仓位量。每次盈利后增加交易量,亏损时反向操作。整体利润或亏损超过设定阈值时停止交易并平掉所有仓位。

细节

  • 入场条件
    • 当没有持仓且账户余额高于 Margin 时,按预设方向以 MinVolume 开仓。
  • 多空方向:根据每个货币对的内部方向可做多也可做空。
  • 离场条件
    • 当利润大于 KClose * MinVolume 时平仓。
    • 当亏损大于 KChange * 当前仓位量 时平仓并反向。
  • 止损:无显式止损;风险由收益/亏损阈值控制。
  • 默认参数
    • Loss = 1900
    • Profit = 4000
    • Margin = 5000
    • MinVolume = 0.01
    • KChange = 2100
    • KClose = 4600
  • 筛选
    • 类别:资金管理
    • 方向:双向
    • 指标:无
    • 止损:无
    • 复杂度:中等
    • 时间框架:逐笔
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险级别:高
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>
/// Momentum flip strategy: trades in current momentum direction,
/// flips direction when loss threshold hit, adds on profit.
/// Adapted from multi-currency EA to single-security candle-based approach.
/// </summary>
public class ExpMulticStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevMomentum;

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

	public ExpMulticStrategy()
	{
		_period = Param(nameof(Period), 14)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Momentum lookback period", "Parameters");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevMomentum = null;
	}

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

		var momentum = new Momentum { Length = Period };

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

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

			var area2 = CreateChartArea();
			if (area2 != null)
				DrawIndicator(area2, momentum);
		}
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevMomentum = momentumValue;
			return;
		}

		if (_prevMomentum is decimal prev)
		{
			// Momentum crosses above zero - buy
			if (prev <= 0 && momentumValue > 0 && Position <= 0)
				BuyMarket();
			// Momentum crosses below zero - sell
			else if (prev >= 0 && momentumValue < 0 && Position >= 0)
				SellMarket();
		}

		_prevMomentum = momentumValue;
	}
}