View on GitHub

Exp Multic Strategy

Multi-currency strategy that trades a fixed set of major Forex pairs without technical indicators. For each pair the algorithm maintains a direction and volume. After each profitable move the volume is increased, after a loss the direction is flipped. Trading stops and all positions are closed once overall profit or loss exceeds specified thresholds.

Details

  • Entry Criteria:
    • If no position and account balance above Margin, open position in predefined direction with MinVolume.
  • Long/Short: Both, depending on internal direction per pair.
  • Exit Criteria:
    • Close position when profit exceeds KClose * MinVolume.
    • Reverse direction and close when loss exceeds KChange * current volume.
  • Stops: No explicit stops; risk controlled by profit/loss thresholds.
  • Default Values:
    • Loss = 1900
    • Profit = 4000
    • Margin = 5000
    • MinVolume = 0.01
    • KChange = 2100
    • KClose = 4600
  • Filters:
    • Category: Money management
    • Direction: Both
    • Indicators: None
    • Stops: No
    • Complexity: Intermediate
    • Timeframe: Tick-based
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • Risk Level: High
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;
	}
}