GitHub で見る

Charles 1.3.7 戦略

この戦略は現在価格の上下に対称的なストップ注文を配置し、トレーリングエグジットを使用してブレイクアウトを捉えます。

パラメーター

  • Anchor – ストップ注文を配置する価格ステップでの距離。
  • XFactor – 注文量の乗数。
  • Trailing Stop – 価格ステップでのトレーリングストップ距離。
  • Trailing Profit – ポジション決済の利益閾値。
  • Stop Loss – 価格ステップでの固定ストップロス(0で無効化)。
  • Volume – 基本注文量。
  • Candle Type – 処理するローソク足の時間軸。

取引ロジック

  1. ポジションがない場合、既存の注文をキャンセルし、最後のローソク足の終値からAnchorステップ離れた位置にBuy StopとSell Stopの両方を配置します。
  2. ポジションが開かれると、反対のストップ注文はキャンセルされます。エントリー価格は決済計算のために記録されます。
  3. ロングポジションの場合、利益がTrailing Profitに達するか価格がStop Loss分下落した場合にポジションが決済されます。ショートポジションの場合はロジックが反転します。

この戦略はシンプルなリスク管理を伴うブレイクアウト取引の例として設計されています。

using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Charles 1.3.7 breakout strategy using symmetric price levels.
/// </summary>
public class Charles137Strategy : Strategy
{
	private readonly StrategyParam<decimal> _anchor;
	private readonly StrategyParam<decimal> _trailingProfit;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;
	private decimal _buyLevel;
	private decimal _sellLevel;
	private bool _levelsSet;

	public decimal Anchor { get => _anchor.Value; set => _anchor.Value = value; }
	public decimal TrailingProfit { get => _trailingProfit.Value; set => _trailingProfit.Value = value; }
	public decimal StopLossVal { get => _stopLoss.Value; set => _stopLoss.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Charles137Strategy()
	{
		_anchor = Param(nameof(Anchor), 200m)
			.SetGreaterThanZero()
			.SetDisplay("Anchor", "Distance for breakout levels", "General");

		_trailingProfit = Param(nameof(TrailingProfit), 500m)
			.SetGreaterThanZero()
			.SetDisplay("Trailing Profit", "Profit target distance", "General");

		_stopLoss = Param(nameof(StopLossVal), 300m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss", "Stop loss distance", "General");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_entryPrice = 0;
		_buyLevel = 0;
		_sellLevel = 0;
		_levelsSet = false;
	}

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

		SubscribeCandles(CandleType)
			.Bind(ProcessCandle)
			.Start();
	}

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

		var price = candle.ClosePrice;

		if (Position == 0)
		{
			if (!_levelsSet)
			{
				_buyLevel = price + Anchor;
				_sellLevel = price - Anchor;
				_levelsSet = true;
				return;
			}

			if (price >= _buyLevel)
			{
				BuyMarket();
				_entryPrice = price;
				_levelsSet = false;
			}
			else if (price <= _sellLevel)
			{
				SellMarket();
				_entryPrice = price;
				_levelsSet = false;
			}
			else
			{
				_buyLevel = price + Anchor;
				_sellLevel = price - Anchor;
			}
		}
		else if (Position > 0)
		{
			var profit = price - _entryPrice;
			if (profit >= TrailingProfit || profit <= -StopLossVal)
			{
				SellMarket();
				_levelsSet = false;
			}
		}
		else if (Position < 0)
		{
			var profit = _entryPrice - price;
			if (profit >= TrailingProfit || profit <= -StopLossVal)
			{
				BuyMarket();
				_levelsSet = false;
			}
		}
	}
}