GitHub で見る

戦略ベーステンプレート

このフォルダーはカスタムトレーディングアイデアを構築するための最小限のスキャフォールド を提供します。この戦略は単一の指数移動平均のみを計算し、一般的なパラメーターの幅広い 範囲を公開します:ロングまたはショートトレードの有効化、オプションのテイクプロフィット とストップロス、および最適化範囲。開発者は独自のエントリーとエグジットロジックをプレース ホルダー内に挿入して、新しいシステムを迅速にプロトタイプ化できます。

このテンプレートは、パーセンテージベースのターゲットで組み込みの保護モジュールを起動 する方法も示しており、異なるリスク設定を実験しやすくしています。実際のシグナルは含まれて いないため、このスクリプトはそのまま取引することを意図しておらず、さらなる研究の出発点 として機能することを目的としています。

詳細

  • エントリー条件: 未実装 – カスタムルールに置き換えてください。
  • ロング/ショート: パラメーターで設定可能。
  • エグジット条件: 未実装 – カスタムルールに置き換えてください。
  • ストップ: 保護モジュールで処理されるオプションのパーセンテージテイクプロフィットとストップロス。
  • デフォルト値:
    • EMAの長さ = 10。
    • テイクプロフィット = 1.2%、ストップロス = 1.8%(デフォルトでは無効)。
  • フィルター:
    • カテゴリ: テンプレート
    • 方向: 設定可能
    • インジケーター: EMA
    • ストップ: オプション
    • 複雑さ: 低
    • 時間軸: 任意
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: ユーザー定義
namespace StockSharp.Samples.Strategies;

using System;
using System.Collections.Generic;

using Ecng.Common;

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

/// <summary>
/// Strategy Base - EMA trend following strategy.
/// Buys when price crosses above EMA, sells when price crosses below EMA.
/// </summary>
public class StratBaseStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _cooldownBars;

	private ExponentialMovingAverage _ema;

	private decimal _prevClose;
	private decimal _prevEma;
	private int _cooldownRemaining;

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

	public int EmaLength
	{
		get => _emaLength.Value;
		set => _emaLength.Value = value;
	}

	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	public StratBaseStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_emaLength = Param(nameof(EmaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Length", "EMA period", "Moving Averages");

		_cooldownBars = Param(nameof(CooldownBars), 10)
			.SetDisplay("Cooldown Bars", "Bars to wait between trades", "Risk");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_ema = null;
		_prevClose = 0;
		_prevEma = 0;
		_cooldownRemaining = 0;
	}

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

		_ema = new ExponentialMovingAverage { Length = EmaLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_ema, OnProcess)
			.Start();

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

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

		if (!_ema.IsFormed)
		{
			_prevClose = candle.ClosePrice;
			_prevEma = emaVal;
			return;
		}

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevClose = candle.ClosePrice;
			_prevEma = emaVal;
			return;
		}

		if (_cooldownRemaining > 0)
		{
			_cooldownRemaining--;
			_prevClose = candle.ClosePrice;
			_prevEma = emaVal;
			return;
		}

		if (_prevClose == 0 || _prevEma == 0)
		{
			_prevClose = candle.ClosePrice;
			_prevEma = emaVal;
			return;
		}

		// Price crosses above EMA
		var crossUp = candle.ClosePrice > emaVal && _prevClose <= _prevEma;
		// Price crosses below EMA
		var crossDown = candle.ClosePrice < emaVal && _prevClose >= _prevEma;

		if (crossUp && Position <= 0)
		{
			if (Position < 0)
				BuyMarket(Math.Abs(Position));
			BuyMarket(Volume);
			_cooldownRemaining = CooldownBars;
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0)
				SellMarket(Math.Abs(Position));
			SellMarket(Volume);
			_cooldownRemaining = CooldownBars;
		}

		_prevClose = candle.ClosePrice;
		_prevEma = emaVal;
	}
}