GitHub で見る

Cronex AC 戦略

Cronex AC戦略は、StockSharpの高レベルAPIを使用してクラシックなCronex Acceleration/Deceleration(AC)エキスパートアドバイザーを再現します。2つの連続移動平均でAcceleratorオシレーターを平滑化し、ファストラインがスローラインをクロスしたときに反応します。強気クロスはロングポジションを開いてショートを閉じ、弱気クロスはショートを開いてロングを閉じます。

トレードロジック

  1. 選択したローソク足シリーズからAcceleratorオシレーター(AO-AC)値を構築する。
  2. 選択した移動平均タイプでACを2回平滑化する:最初の平滑化が「ファスト」ラインを生成し、2回目の平滑化が「シグナル」ラインを生成する。
  3. SignalBarパラメーターで定義されたバーで2本のラインを評価する。戦略はクロスオーバーを確認するために1本さらに前のバーも確認する。
  4. ファストラインがシグナルラインを上回ってクロスしたとき、戦略は既存のショートポジションを閉じ(有効な場合)、新しいロングポジションを開く(有効な場合)。
  5. ファストラインがシグナルラインを下回ってクロスしたとき、戦略は既存のロングポジションを閉じ(有効な場合)、新しいショートポジションを開く(有効な場合)。
  6. ポジションサイズは設定したVolumeと現在のポジションの絶対値を合計したものに等しく、1つの成行注文での反転を可能にする。

このロジックは、完全に完成したローソク足のみで動作し、両方向のエントリーとエグジットの権限を分離することでMQL5エキスパートを反映します。

パラメーター

名前 デフォルト 説明
SmoothingType CronexMovingAverageType Simple Acceleratorオシレーターに適用する移動平均アルゴリズム。オプション:Simple、Exponential、Smoothed、Weighted。
FastPeriod int 14 第1平滑化(ファストライン)のルックバック。
SlowPeriod int 25 第2平滑化(シグナルライン)のルックバック。
SignalBar int 1 シグナルを読み取る際に過去を参照する完成バーの数。値1はデフォルトのCronex動作を再現する。
CandleType DataType TimeFrame(8h) 計算に使用するローソク足シリーズ。
EnableLongEntry bool true 強気クロスオーバー後のロングポジション開設を許可。
EnableShortEntry bool true 弱気クロスオーバー後のショートポジション開設を許可。
EnableLongExit bool true ファストラインがスローラインを下回ったときのロングポジション決済を許可。
EnableShortExit bool true ファストラインがスローラインを上回ったときのショートポジション決済を許可。
Volume decimal 戦略デフォルト エントリーに使用する注文サイズ。戦略は1つの取引で反転するために開かれたポジションの絶対値を自動的に追加する。

チャート

チャートエリアが利用可能なとき、戦略は以下をプロットします:

  • 選択した時間軸のソースローソク足、
  • Acceleratorオシレーター値、
  • ファストとシグナルの移動平均、
  • 視覚的検証のための戦略自身の取引。

注意事項

  • すべての計算は再描画を避けるため完成したローソク足(CandleStates.Finished)に依存する。
  • 平滑化バッファは元のMQLエキスパートと一致するよう、要求したSignalBarシフトを評価するのに必要な履歴値だけを保持する。
  • MQLバージョンの資金管理機能(ストップロス、テイクプロフィット、偏差)は、ポジション管理をStockSharpのリスクコントロールを通じて外部で処理できるよう意図的に省略されている。
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;

public class CronexAcStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public CronexAcStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}