GitHub で見る

ユニバーサル・インベスター戦略

概要

ユニバーサル・インベスター戦略は、指数移動平均 (EMA) と線形加重移動平均 (LWMA) のクロスオーバーを使用して市場の方向性を決定します。両平均が同じ方向に動いていることを確認することでトレンドの強さを確認します。

ロジック

  • 買いエントリー: LWMAがEMAより上にあり、両平均とも上昇している。
  • 売りエントリー: LWMAがEMAより下にあり、両平均とも下落している。
  • 買い決済: LWMAがEMAを下抜け。
  • 売り決済: LWMAがEMAを上抜け。

減少係数が有効な場合、連続した負けトレードの後にポジションサイズを縮小します。

パラメーター

名前 説明
MovingPeriod EMAおよびLWMA計算の長さ。
DecreaseFactor 損失後のロット削減係数(0で削減を無効化)。
CandleType 計算用のローソク足データタイプ。
Volume 戦略設定からの基本取引量。

注意事項

  • 完成したローソク足のみで動作。
  • インジケーターバインディングを使用したStockSharpのハイレベルAPI使用。
  • Pythonバージョンは提供されていません。
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>
/// Strategy based on EMA and WMA crossover with trend confirmation.
/// </summary>
public class UniversalInvestorStrategy : Strategy
{
	private readonly StrategyParam<int> _movingPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevEma;
	private decimal _prevLwma;
	private bool _hasPrev;

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

	public UniversalInvestorStrategy()
	{
		_movingPeriod = Param(nameof(MovingPeriod), 23)
			.SetGreaterThanZero()
			.SetDisplay("Moving Period", "Smoothing period for EMA and WMA", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for calculations", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevEma = 0;
		_prevLwma = 0;
		_hasPrev = false;
	}

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

		var ema = new ExponentialMovingAverage { Length = MovingPeriod };
		var lwma = new WeightedMovingAverage { Length = MovingPeriod };

		SubscribeCandles(CandleType)
			.Bind(ema, lwma, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevEma = emaValue;
			_prevLwma = lwmaValue;
			_hasPrev = true;
			return;
		}

		var openBuy = lwmaValue > emaValue && lwmaValue > _prevLwma && emaValue > _prevEma;
		var openSell = lwmaValue < emaValue && lwmaValue < _prevLwma && emaValue < _prevEma;
		var closeBuy = lwmaValue < emaValue;
		var closeSell = lwmaValue > emaValue;

		if (Position > 0 && closeBuy)
		{
			SellMarket();
		}
		else if (Position < 0 && closeSell)
		{
			BuyMarket();
		}
		else if (Position == 0)
		{
			if (openBuy && !closeBuy)
				BuyMarket();
			else if (openSell && !closeSell)
				SellMarket();
		}

		_prevEma = emaValue;
		_prevLwma = lwmaValue;
	}
}