在 GitHub 上查看
JK 同步策略
概述
JK 同步策略是 MetaTrader 5 专家顾问 "JK synchro"(MQL 编号 2415)的 StockSharp 版本。原始 EA 统计最近若干根 K 线中收盘价低于开盘价以及收盘价高于开盘价的数量,并在优势方向开仓。本移植版本保留了该逻辑,同时提供类型安全的参数、内置风控以及 StockSharp 框架的日志工具。
交易逻辑
- 按照
CandleType 参数订阅蜡烛数据,并只在蜡烛收盘后处理。
- 维护长度为
AnalysisPeriod 的滑动窗口,对每根蜡烛执行:
- 当
Open > Close 时增加看跌计数。
- 当
Open < Close 时增加看涨计数。
- 若
Open == Close(十字星)则忽略。
- 当窗口填满后判断:
- 如果看跌数量大于看涨数量,准备开多单。
- 如果看涨数量大于看跌数量,准备开空单。
- 在发出订单前必须满足以下条件:
- 策略已连接并允许交易(
IsFormedAndOnlineAndAllowTrading)。
- 当前小时位于
StartHour 与 EndHour(包含)之间。
- 距离上一次进场已超过
PauseBetweenTradesSeconds 指定的冷却时间。
- 新增的手数不会使净头寸超过
MaxPositions * OrderVolume。
- 当出现反向信号且当前持有相反仓位时,策略会先平仓并等待下一根蜡烛,再考虑反向开仓。
- 止损、止盈与移动止损以点数(pip)表示,并根据品种的最小报价步长自动换算成价格距离。
风险控制
- 止损 / 止盈:以点数配置,可选。头寸变化时重新计算,在每根收盘蜡烛上检查触发条件。
- 移动止损:当
TrailingStopPips 与 TrailingStepPips 均大于 0 时启用。盈利超过 TrailingStop + TrailingStep 后,止损按照设定步长向盈利方向移动。
- 仓位上限:净头寸绝对值不得超过
MaxPositions * OrderVolume。
- 入场冷却:在
OnPositionChanged 中记录每次成交时间,确保在冷却时间结束前不会再次开仓。
参数
| 参数 |
默认值 |
说明 |
OrderVolume |
0.1 |
每次市价单的交易量。 |
MaxPositions |
10 |
单方向最多允许的手数。 |
AnalysisPeriod |
18 |
统计看涨/看跌蜡烛的数量。 |
PauseBetweenTradesSeconds |
540 |
入场后的冷却时间(秒)。 |
StartHour |
3 |
交易窗口起始小时(包含)。 |
EndHour |
6 |
交易窗口结束小时(包含)。 |
StopLossPips |
50 |
止损距离(点)。设为 0 关闭。 |
TakeProfitPips |
150 |
止盈距离(点)。设为 0 关闭。 |
TrailingStopPips |
15 |
移动止损距离(点)。设为 0 关闭移动止损。 |
TrailingStepPips |
5 |
更新移动止损前所需的额外盈利(点)。启用移动止损时必须为正。 |
CandleType |
15 分钟 |
用于计算的蜡烛数据源。 |
实现要点
- 全程使用 StockSharp 高级 API(
SubscribeCandles、.Bind、BuyMarket、SellMarket)。
- 在
OnPositionChanged 中记录成交时间以实现与原 EA 相同的冷却机制。
- 根据
Security.PriceStep 与 Security.Decimals 自动计算点值,对 3 位或 5 位报价品种自动乘以 10。
- 在蜡烛收盘时根据最高价与最低价检查止损与止盈触发。
- 移动止损完全复刻原始逻辑:只有当盈利超过
TrailingStop + TrailingStep 时才开始移动,并且不会反向调整。
使用建议
- 根据合约大小调整
OrderVolume 与 MaxPositions,以符合账户风险要求。
AnalysisPeriod 应与所选时间框架匹配,时间框架越短通常需要更长的窗口以降低噪声。
- 调整
StartHour 与 EndHour,使其覆盖目标市场的活跃时段,例如欧盘时段的欧元货币对。
- 回测不同的止损、止盈与移动止损组合,原 EA 在不同市场环境下会切换不同的风控配置。
与 MQL 版本的差异
- StockSharp 采用净头寸模型,切换方向时先平掉原有仓位;而 MetaTrader 版本允许同时持有多方向头寸(套保)。
- 参数管理与日志系统依赖 StockSharp,便于优化与界面展示。
- 移动止损在蜡烛收盘时评估,与其他 StockSharp 策略移植保持一致,避免对未完成蜡烛产生过早反应。
通过以上设置,您可以在 StockSharp 生态内直接运行、分析并优化 JK 同步策略。
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 JkSynchroStrategy : 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 JkSynchroStrategy()
{
_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;
}
}
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 jk_synchro_strategy(Strategy):
def __init__(self):
super(jk_synchro_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 14) \
.SetDisplay("Fast Period", "Fast MA period", "Indicator")
self._slow_period = self.Param("SlowPeriod", 50) \
.SetDisplay("Slow Period", "Slow MA period", "Indicator")
self._stop_loss_points = self.Param("StopLossPoints", 200) \
.SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 400) \
.SetDisplay("Take Profit", "Take-profit in price steps", "Risk")
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def stop_loss_points(self):
return self._stop_loss_points.Value
@property
def take_profit_points(self):
return self._take_profit_points.Value
def OnReseted(self):
super(jk_synchro_strategy, self).OnReseted()
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(jk_synchro_strategy, self).OnStarted2(time)
self._fast = ExponentialMovingAverage()
self._fast.Length = self.fast_period
self._slow = ExponentialMovingAverage()
self._slow.Length = self.slow_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(5)))
subscription.Bind(self._fast, self._slow, self._process_candle)
subscription.Start()
def _process_candle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fast_val = float(fast_value)
slow_val = float(slow_value)
if not self._fast.IsFormed or not self._slow.IsFormed:
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_fast = fast_val
self._prev_slow = slow_val
return
close = float(candle.ClosePrice)
step = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 1.0
if self.Position > 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close <= self._entry_price - self.stop_loss_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close >= self._entry_price + self.take_profit_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
elif self.Position < 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close >= self._entry_price + self.stop_loss_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close <= self._entry_price - self.take_profit_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._prev_fast <= self._prev_slow and fast_val > slow_val and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = close
self._cooldown = 100
elif self._prev_fast >= self._prev_slow and fast_val < slow_val and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = close
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
def CreateClone(self):
return jk_synchro_strategy()