MACFibo戦略
この戦略はMACFiboトレードシステムを実装しています。5期間EMAと20期間SMAのクロスオーバーを待ちます。クロス後、アルゴリズムはクロスバーの終値(点A)から直近の極値(点B)までのスウィングを測定し、フィボナッチ拡張レベルを構築します。ポジションはこれらのレベルから導出されたテイクプロフィットとストップロスで成行価格で建てられます。オプションの退出は、速いEMAが反対方向に中間SMAをクロスすると損失中のトレードを閉じます。
詳細
- エントリー条件:
- ロング: 5 EMAが20 SMAを上抜け。点Bは下降動作が始まって以来の最安値。
- ショート: 5 EMAが20 SMAを下抜け。点Bは上昇動作が始まって以来の最高値。
- エグジット条件:
- フィボナッチ161.8%レベルまたは最小テイクプロフィット距離でテイクプロフィット。
- フィボナッチ38.2%レベルまたは最大ストップロス距離でストップロス。
- 5 EMAがポジションに逆らって8 SMAをクロスし、かつトレードが損失中の場合はオプションで決済。
- フィルター:
- 設定された開始時間と終了時間の間のみ取引。
- 月曜日または金曜日の取引を無効にできます。
- パラメーター:
FastLength– 速いEMAの長さ。MidLength– 保護的退出のための中間SMAの長さ。SlowLength– トレンド検出のための遅いSMAの長さ。MinTakeProfit– 価格単位での最小テイクプロフィット。MaxStopLoss– 価格単位での最大ストップロス。StartHour/EndHour– 許可された取引時間窓。FridayTrade/MondayTrade– これらの曜日の取引を有効にする。CloseAtFastMid– 速い/中間クロスで損失トレードを決済。CandleType– 計算用のローソク足の種類。
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>
/// EMA crossover strategy with Fibonacci-inspired targets.
/// </summary>
public class MacfiboStrategy : Strategy
{
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFast;
private decimal _prevSlow;
private bool _hasPrev;
public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public MacfiboStrategy()
{
_fastLength = Param(nameof(FastLength), 10)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");
_slowLength = Param(nameof(SlowLength), 20)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");
_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();
_prevFast = 0;
_prevSlow = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fast = new ExponentialMovingAverage { Length = FastLength };
var slow = new ExponentialMovingAverage { Length = SlowLength };
SubscribeCandles(CandleType).Bind(fast, slow, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevFast = fastValue;
_prevSlow = slowValue;
_hasPrev = true;
return;
}
var crossUp = _prevFast <= _prevSlow && fastValue > slowValue;
var crossDown = _prevFast >= _prevSlow && fastValue < slowValue;
if (crossUp && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (crossDown && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevFast = fastValue;
_prevSlow = slowValue;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class macfibo_strategy(Strategy):
def __init__(self):
super(macfibo_strategy, self).__init__()
self._fast_length = self.Param("FastLength", 10) \
.SetDisplay("Fast EMA", "Fast EMA period", "Indicators")
self._slow_length = self.Param("SlowLength", 20) \
.SetDisplay("Slow EMA", "Slow EMA period", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
@property
def fast_length(self):
return self._fast_length.Value
@property
def slow_length(self):
return self._slow_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(macfibo_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(macfibo_strategy, self).OnStarted2(time)
fast = ExponentialMovingAverage()
fast.Length = self.fast_length
slow = ExponentialMovingAverage()
slow.Length = self.slow_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast, slow, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
if not self._has_prev:
self._prev_fast = fast_value
self._prev_slow = slow_value
self._has_prev = True
return
cross_up = self._prev_fast <= self._prev_slow and fast_value > slow_value
cross_down = self._prev_fast >= self._prev_slow and fast_value < slow_value
if cross_up and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif cross_down and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_fast = fast_value
self._prev_slow = slow_value
def CreateClone(self):
return macfibo_strategy()