J-Lines Ribbon 4-Cycle Engine 策略
J-Lines Ribbon 4-Cycle Engine 策略通过EMA带和平均方向指数将市场划分为 CHOP、LONG 和 SHORT 周期。策略在新的周期出现以及价格从关键 EMA 反弹时入场,在相反的交叉或摆动突破时平仓。
细节
- 入场条件:
- 多头:出现新的 LONG 周期或在 EMA72/EMA126 上方反弹且 EMA72 > EMA89。
- 空头:出现新的 SHORT 周期或在 EMA72/EMA126 下方反弹且 EMA72 < EMA89。
- 止损:最近摆动高/低点。
- 默认参数:
DmiLength= 8AdxFloor= 12
- 过滤器:
- 类型:趋势
- 方向:双向
- 指标:EMA, ADX
- 止损:是
- 复杂度:中等
- 时间框架:任意
- 季节性:无
- 神经网络:无
- 背离:无
- 风险等级:中等
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 signal cooldown to keep trade frequency stable.
/// </summary>
public class JLinesRibbon4CycleEngineStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<int> _cooldownBars;
private ExponentialMovingAverage _fastEma;
private ExponentialMovingAverage _slowEma;
private decimal _prevFast;
private decimal _prevSlow;
private bool _initialized;
private int _barsSinceSignal;
/// <summary>
/// Candle type for strategy calculation.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Fast EMA length.
/// </summary>
public int FastLength
{
get => _fastLength.Value;
set => _fastLength.Value = value;
}
/// <summary>
/// Slow EMA length.
/// </summary>
public int SlowLength
{
get => _slowLength.Value;
set => _slowLength.Value = value;
}
/// <summary>
/// Minimum finished candles between two signals.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Initialize <see cref="JLinesRibbon4CycleEngineStrategy"/>.
/// </summary>
public JLinesRibbon4CycleEngineStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_fastLength = Param(nameof(FastLength), 72)
.SetGreaterThanZero()
.SetDisplay("Fast Length", "Fast EMA length", "Indicators");
_slowLength = Param(nameof(SlowLength), 89)
.SetGreaterThanZero()
.SetDisplay("Slow Length", "Slow EMA length", "Indicators");
_cooldownBars = Param(nameof(CooldownBars), 200)
.SetGreaterThanZero()
.SetDisplay("Cooldown Bars", "Minimum bars between signals", "Risk");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_fastEma = null;
_slowEma = null;
_prevFast = 0m;
_prevSlow = 0m;
_initialized = false;
_barsSinceSignal = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_fastEma = new ExponentialMovingAverage { Length = FastLength };
_slowEma = new ExponentialMovingAverage { Length = SlowLength };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(_fastEma, _slowEma, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
{
if (candle.State != CandleStates.Finished)
return;
if (!_fastEma.IsFormed || !_slowEma.IsFormed)
return;
if (!_initialized)
{
_prevFast = fast;
_prevSlow = slow;
_initialized = true;
_barsSinceSignal = CooldownBars;
return;
}
_barsSinceSignal++;
if (_barsSinceSignal >= CooldownBars)
{
var crossUp = _prevFast <= _prevSlow && fast > slow;
var crossDown = _prevFast >= _prevSlow && fast < slow;
if (crossUp)
{
if (Position < 0)
BuyMarket(Math.Abs(Position));
else if (Position == 0)
BuyMarket();
_barsSinceSignal = 0;
}
else if (crossDown)
{
if (Position > 0)
SellMarket(Math.Abs(Position));
else if (Position == 0)
SellMarket();
_barsSinceSignal = 0;
}
}
_prevFast = fast;
_prevSlow = slow;
}
}
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 j_lines_ribbon_4_cycle_engine_strategy(Strategy):
def __init__(self):
super(j_lines_ribbon_4_cycle_engine_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._fast_length = self.Param("FastLength", 72) \
.SetGreaterThanZero() \
.SetDisplay("Fast Length", "Fast EMA length", "Indicators")
self._slow_length = self.Param("SlowLength", 89) \
.SetGreaterThanZero() \
.SetDisplay("Slow Length", "Slow EMA length", "Indicators")
self._cooldown_bars = self.Param("CooldownBars", 200) \
.SetGreaterThanZero() \
.SetDisplay("Cooldown Bars", "Minimum bars between signals", "Risk")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._initialized = False
self._bars_since_signal = 0
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(j_lines_ribbon_4_cycle_engine_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._initialized = False
self._bars_since_signal = 0
def OnStarted2(self, time):
super(j_lines_ribbon_4_cycle_engine_strategy, self).OnStarted2(time)
fast_ema = ExponentialMovingAverage()
fast_ema.Length = self._fast_length.Value
slow_ema = ExponentialMovingAverage()
slow_ema.Length = self._slow_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast_ema, slow_ema, self.OnProcess).Start()
def OnProcess(self, candle, fast_val, slow_val):
if candle.State != CandleStates.Finished:
return
fast = float(fast_val)
slow = float(slow_val)
if not self._initialized:
self._prev_fast = fast
self._prev_slow = slow
self._initialized = True
self._bars_since_signal = self._cooldown_bars.Value
return
self._bars_since_signal += 1
cd = self._cooldown_bars.Value
if self._bars_since_signal >= cd:
cross_up = self._prev_fast <= self._prev_slow and fast > slow
cross_down = self._prev_fast >= self._prev_slow and fast < slow
if cross_up:
if self.Position < 0:
self.BuyMarket()
elif self.Position == 0:
self.BuyMarket()
self._bars_since_signal = 0
elif cross_down:
if self.Position > 0:
self.SellMarket()
elif self.Position == 0:
self.SellMarket()
self._bars_since_signal = 0
self._prev_fast = fast
self._prev_slow = slow
def CreateClone(self):
return j_lines_ribbon_4_cycle_engine_strategy()