GitHub で見る

スムージング平均戦略

概要

この戦略は、追加のスムージングオフセットを伴う単純移動平均(SMA)を中心にトレードします。終値が平均からオフセット距離を超えてクロスしたときにポジションを開くことで、移動平均からの価格乖離を活用しようとします。

仕組み

  • 選択したローソク足タイプのSMAを計算する。
  • オープンポジションがない場合:
    • 終値が SMA + Smoothing を下回ったときにショートポジションを開く。
    • 終値が SMA - Smoothing を上回ったときにロングポジションを開く。
  • ショートポジションがオープンの場合:
    • 終値が SMA + Smoothing を上回ったときにポジションをクローズする。
  • ロングポジションがオープンの場合:
    • 終値が SMA - Smoothing を下回ったときにポジションをクローズする。

この戦略は成行注文を使用し、完成済みのローソク足のみで動作します。

パラメーター

  • MA Period – SMAのルックバック期間。
  • Smoothing – シグナル生成時にSMAに加算または減算する価格オフセット。
  • Candle Type – 計算に使用するローソク足の時間軸。

注意事項

この変換は、元のMQL4スクリプト smoothingaverage.mq4 に基づいています。

using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Moving average strategy with smoothing offset.
/// </summary>
public class SmoothingAverageStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<decimal> _smoothing;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private int _cooldown;

	public int MaPeriod
	{
		get => _maPeriod.Value;
		set => _maPeriod.Value = value;
	}

	public decimal Smoothing
	{
		get => _smoothing.Value;
		set => _smoothing.Value = value;
	}

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

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

	public SmoothingAverageStrategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 200)
			.SetDisplay("MA Period", "Moving average period", "MA")
			;
		_smoothing = Param(nameof(Smoothing), 1400m)
			.SetDisplay("Smoothing", "Price offset from moving average", "General")
			;
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
		_cooldownBars = Param(nameof(CooldownBars), 36)
			.SetDisplay("Cooldown Bars", "Bars to wait between new signals", "General")
			;
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_cooldown = 0;
	}

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

		// create moving average indicator
		var sma = new SimpleMovingAverage { Length = MaPeriod };

		// subscribe to candles
		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(sma, ProcessCandle)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_cooldown > 0)
		{
			_cooldown--;
			return;
		}

		var price = candle.ClosePrice;
		var offset = (Security.PriceStep ?? 1m) * Smoothing;

		if (Position == 0)
		{
			if (price >= maValue + offset)
			{
				SellMarket();
				_cooldown = CooldownBars;
			}
			else if (price <= maValue - offset)
			{
				BuyMarket();
				_cooldown = CooldownBars;
			}
		}
		else if (Position < 0 && price <= maValue)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
		else if (Position > 0 && price >= maValue)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
	}
}