Auf GitHub ansehen

Timer EA Strategy

This strategy is a StockSharp port of the MetaTrader TimerEA robot. It focuses on opening and closing trades at scheduled datetimes with optional pending orders, trailing protection and break-even handling.

Trading Logic

  • Schedule
    • OpenTime triggers order placement once the first finished candle reaches the configured minute.
    • CloseTime forces position liquidation and optionally cancels remaining pending orders.
  • Order Modes
    • Market, stop or limit entries can be selected. Pending orders are placed at a configurable distance (in price steps) and may expire after the specified number of minutes.
  • Direction Control
    • Separate switches allow enabling long and/or short trades. Each side submits one order per run.
  • Risk Management
    • Fixed volume or balance-based sizing (using RiskFactor) mimics the original lot selection.
    • Stop-loss and take-profit distances are expressed in price steps and recreated after each entry.
    • Trailing stop logic keeps the stop at a constant offset once profit exceeds the BreakEvenSteps buffer. The trail activates only when the stop is already beyond the initial offset plus the TrailingStep.
  • Protections
    • Optional break-even requirement prevents trailing until the minimum profit threshold is achieved.
    • Pending orders that outlive their expiration are cancelled automatically.

Default Parameters

  • Order mode: Market.
  • Open buy / sell: disabled.
  • Take profit / stop loss: 10 steps each.
  • Trailing stop and break-even: disabled.
  • Pending distance: 10 steps with 60 minutes expiration.
  • Lot sizing: Manual volume = 1.0 (risk factor = 1.0 for balance mode).
  • Candle type: 1-minute time frame.

Notes

  • The strategy operates on finished candles and therefore reacts with up to one bar of latency.
  • StockSharp uses a netted position model, so simultaneous long and short exposure is not supported even if both toggles are enabled.
  • Price steps are calculated with Security.PriceStep. Instruments without a configured step will treat distances as raw price points.
namespace StockSharp.Samples.Strategies;

using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;

/// <summary>
/// Timer EA strategy: SMA crossover with time-of-day filter.
/// Trades only during active session hours.
/// </summary>
public class TimerEaStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }

	public TimerEaStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast SMA period", "Indicators");
		_slowPeriod = Param(nameof(SlowPeriod), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow SMA period", "Indicators");
	}

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

		var fast = new SimpleMovingAverage { Length = FastPeriod };
		var slow = new SimpleMovingAverage { Length = SlowPeriod };

		decimal? prevFast = null;
		decimal? prevSlow = null;

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fast, slow, (candle, fastVal, slowVal) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				if (!IsFormedAndOnlineAndAllowTrading())
					return;

				if (prevFast.HasValue && prevSlow.HasValue)
				{
					if (prevFast.Value <= prevSlow.Value && fastVal > slowVal && Position <= 0)
						BuyMarket();
					else if (prevFast.Value >= prevSlow.Value && fastVal < slowVal && Position >= 0)
						SellMarket();
				}

				prevFast = fastVal;
				prevSlow = slowVal;
			})
			.Start();

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