GitHub で見る

AMMA Trend 戦略

概要

この戦略は Modified Moving Average (AMMA) インジケーターを使用して短期的なトレンド変化を捕捉します。直近の足における AMMA の傾きの方向を分析し、新興トレンドの方向にポジションを開きながら反対方向のポジションをクローズします。

仕組み

  1. 選択した時間軸で設定可能な期間の ModifiedMovingAverage が計算されます。
  2. 完成した足ごとに、戦略は直近 3 つの AMMA 値を比較します。
  3. インジケーター値が上昇シーケンスを形成し、最新値が前の値より大きい場合、ロングポジションが開かれます。ショートポジションはクローズされます。
  4. インジケーター値が下降シーケンスを形成し、最新値が前の値より小さい場合、ショートポジションが開かれます。ロングポジションはクローズされます。

パラメーター

  • CandleType – 計算に使用する足の時間軸。
  • MaPeriod – 修正移動平均の期間。
  • AllowLongEntry – ロングポジションのオープンを有効にする。
  • AllowShortEntry – ショートポジションのオープンを有効にする。
  • AllowLongExit – ロングポジションのクローズを有効にする。
  • AllowShortExit – ショートポジションのクローズを有効にする。

注意事項

この戦略は完成した足のみで動作し、注文実行には組み込みの BuyMarket および SellMarket メソッドを使用します。リスク管理機能は標準的な Strategy プロパティを使用して外部から追加できます。

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>
/// AMMA Trend strategy using Modified Moving Average direction changes.
/// Buys when MMA turns up, sells when MMA turns down.
/// </summary>
public class AmmaTrendStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _maPeriod;

	private decimal? _mma0;
	private decimal? _mma1;
	private decimal? _mma2;
	private decimal? _mma3;

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

	public AmmaTrendStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use for analysis", "General");

		_maPeriod = Param(nameof(MaPeriod), 25)
			.SetGreaterThanZero()
			.SetDisplay("AMMA Period", "Period of the modified moving average", "Indicator");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_mma0 = _mma1 = _mma2 = _mma3 = null;
	}

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

		var mma = new SmoothedMovingAverage { Length = MaPeriod };

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

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

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

		_mma3 = _mma2;
		_mma2 = _mma1;
		_mma1 = _mma0;
		_mma0 = mmaValue;

		if (_mma1 is null || _mma2 is null || _mma3 is null)
			return;

		// Upward movement detected: MMA turned from falling to rising
		if (_mma2 < _mma3 && _mma1 > _mma2 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Downward movement detected: MMA turned from rising to falling
		else if (_mma2 > _mma3 && _mma1 < _mma2 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
	}
}