Auf GitHub ansehen

VisualTrader Simulator Edition

Diese Strategie ist eine vereinfachte Portierung der VisualTrader-Skripte aus MetaTrader.

Sie eröffnet eine einzelne Marktposition in der gewählten Richtung und fügt schützende Stop-Loss- und Take-Profit-Orders hinzu. Die Parameter ermöglichen die Konfiguration von Richtung, Take-Profit und Stop-Loss in absoluten Preiswerten. Die Strategie demonstriert, wie manuelle Trade-Management-Skripte mit der High-Level-API von StockSharp nachgebaut werden können.

Parameter

  • Trade Direction – Buy oder Sell für die initiale Order wählen.
  • Take Profit – optionaler Take-Profit-Wert in absolutem Preis. Auf 0 setzen zum Deaktivieren.
  • Stop Loss – optionaler Stop-Loss-Wert in absolutem Preis. Auf 0 setzen zum Deaktivieren.
  • Volume – Basis-Strategie-Volumen für die Marktorder.

Handelslogik

Beim Start führt die Strategie folgendes aus:

  1. Erstellt Schutzorders mit StartProtection.
  2. Sendet eine Marktorder basierend auf der gewählten Handelsrichtung.

Das Beispiel ist nicht auf Indikatoren oder Marktdaten angewiesen und dient ausschließlich Demonstrationszwecken.

using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Visual Trader strategy with EMA crossover and stop/take protection.
/// </summary>
public class VisualTraderSimulatorEditionStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

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

	public VisualTraderSimulatorEditionStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

		var fastEma = new ExponentialMovingAverage { Length = FastPeriod };
		var slowEma = new ExponentialMovingAverage { Length = SlowPeriod };

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

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

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

		if (_hasPrev)
		{
			var crossUp = _prevFast <= _prevSlow && fast > slow;
			var crossDown = _prevFast >= _prevSlow && fast < slow;

			if (crossUp && Position <= 0)
				BuyMarket();
			else if (crossDown && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
		_hasPrev = true;
	}
}