GitHub で見る

Alligator 追跡戦略

Ride Alligator 戦略の実装。この手法はAlligatorインジケーターとして知られる3本の移動平均線を使用します。TeethラインがJawsの下にある状態でLipsラインがJawsラインを上抜けするとロングポジションを開きます。LipsがJawsを下抜けし、TeethラインがJawsの上にある場合はショートポジションを開きます。オープンポジションはJawsラインに追従するトレーリングストップで保護されます。

詳細

  • エントリー条件:
    • ロング: Lips > Jaws && Teeth < Jaws && previous Lips < previous Jaws
    • ショート: Lips < Jaws && Teeth > Jaws && previous Lips > previous Jaws
  • ロング/ショート: 両方
  • エグジット条件:
    • ロング: price <= Jaws
    • ショート: price >= Jaws
  • ストップ: Alligator Jaws でのトレーリングストップ
  • デフォルト値:
    • AlligatorPeriod = 5
    • MaType = MovingAverageTypeEnum.Weighted
    • CandleType = TimeSpan.FromMinutes(1).TimeFrame()
  • フィルター:
    • カテゴリ: トレンドフォロー
    • 方向: 両方
    • インジケーター: Alligator
    • ストップ: はい
    • 複雑さ: 基本
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Ride Alligator strategy using SMMA jaw/teeth/lips crossover.
/// </summary>
public class RideAlligatorStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevJaw;
	private decimal _prevLips;
	private bool _hasPrev;

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

	public RideAlligatorStrategy()
	{
		_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();
		_prevJaw = 0;
		_prevLips = 0;
		_hasPrev = false;
	}

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

		var jaw = new SmoothedMovingAverage { Length = 13 };
		var teeth = new SmoothedMovingAverage { Length = 8 };
		var lips = new SmoothedMovingAverage { Length = 5 };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(jaw, teeth, lips, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevJaw = jaw;
			_prevLips = lips;
			_hasPrev = true;
			return;
		}

		// Lips crosses above jaw -> buy
		if (_prevLips <= _prevJaw && lips > jaw && teeth < jaw)
		{
			if (Position < 0)
				BuyMarket();
			if (Position <= 0)
				BuyMarket();
		}
		// Lips crosses below jaw -> sell
		else if (_prevLips >= _prevJaw && lips < jaw && teeth > jaw)
		{
			if (Position > 0)
				SellMarket();
			if (Position >= 0)
				SellMarket();
		}

		// Exit on price crossing jaw
		if (Position > 0 && candle.ClosePrice < jaw)
			SellMarket();
		else if (Position < 0 && candle.ClosePrice > jaw)
			BuyMarket();

		_prevJaw = jaw;
		_prevLips = lips;
	}
}