GitHub で見る

Blonde Trader戦略

Blonde TraderはMQLから変換されたグリッドベースのトレーディング戦略です。直近の極値から価格が離れる動きを探し、グリッド状の指値注文で建玉します。

コンセプト

  • 直近 Period X 本のローソク足における最高値と最安値を計算する。
  • 現在価格が直近高値を Limit ティック以上下回っている場合、ロングポジションを建て、グリッドを形成する一連の買い指値注文を出す。
  • 現在価格が直近安値を Limit ティック以上上回っている場合、ショートポジションを建て、グリッドを形成する一連の売り指値注文を出す。
  • 累積利益が Amount に達したとき、全ポジションを決済する。
  • オプションとして、価格が LockDown ティック分利益方向に動いた後、損益分岐点にストップ注文を置いてポジションを保護する。

パラメーター

名前 説明
PeriodX 最高値・最安値を計算するルックバック期間。
Limit 現在価格から極値までの最小ティック距離。
Grid グリッド指値注文間のティック幅。
Amount 口座通貨での利益目標。
LockDown ストップを損益分岐点に動かすまでのティック距離。
CandleType 分析に使用するローソク足の種類。

インジケーター

  • Highest – ルックバック期間中の最高値を追跡する。
  • Lowest – ルックバック期間中の最安値を追跡する。

注文ロジック

  1. ロングセットアップが出現した場合:
    • デフォルト戦略ボリュームで成行買い。
    • エントリー価格の下方に Grid ティック間隔で4つの追加買い指値注文を出し、各注文のボリュームを倍増させる。
  2. ショートセットアップが出現した場合:
    • デフォルト戦略ボリュームで成行売り。
    • エントリー価格の上方に同じグリッドとボリューム倍増ルールで4つの追加売り指値注文を出す。
  3. PnLAmount に達した場合、全オープンポジションと指値注文を決済する。
  4. LockDown がゼロより大きく、価格がポジションに有利な方向へ指定ティック数動いた場合、エントリー価格の1ティック先に保護ストップ注文を置く。

備考

この戦略は基本的なグリッドトレーディングロジックを示しています。SubscribeCandles、インジケーター・バインディング、BuyMarketSellLimitSellStopなどのシンプルな注文ヘルパーといった高レベルAPIのみを使用しています。

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>
/// Blonde Trader strategy. Buys when price pulls back from recent high,
/// sells when price bounces from recent low. Uses Highest/Lowest indicators.
/// </summary>
public class BlondeTraderStrategy : Strategy
{
	private readonly StrategyParam<int> _lookback;
	private readonly StrategyParam<decimal> _threshold;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;

	public int Lookback { get => _lookback.Value; set => _lookback.Value = value; }
	public decimal Threshold { get => _threshold.Value; set => _threshold.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BlondeTraderStrategy()
	{
		_lookback = Param(nameof(Lookback), 20)
			.SetGreaterThanZero()
			.SetDisplay("Lookback", "Period for Highest/Lowest", "General");

		_threshold = Param(nameof(Threshold), 0.002m)
			.SetDisplay("Threshold", "Min distance ratio from extreme", "General");

		_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();
		_entryPrice = 0;
	}

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

		var highest = new Highest { Length = Lookback };
		var lowest = new Lowest { Length = Lookback };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(highest, lowest, ProcessCandle)
			.Start();

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

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

		var price = candle.ClosePrice;
		var range = high - low;

		if (range <= 0 || high == 0)
			return;

		var distFromHigh = (high - price) / high;
		var distFromLow = (price - low) / price;

		// Buy signal: price pulled back from high by at least threshold
		if (distFromHigh > Threshold && Position <= 0)
		{
			BuyMarket();
			_entryPrice = price;
		}
		// Sell signal: price bounced up from low by at least threshold
		else if (distFromLow > Threshold && Position >= 0)
		{
			SellMarket();
			_entryPrice = price;
		}
	}
}