JSatl Digit System 策略
JSatl Digit System 使用 Jurik 移动平均线 (JMA) 判断趋势方向。 策略检测 JMA 的斜率,并在价格与斜率方向一致时开仓。
当 JMA 上升且收盘价高于平均线时,开多头; 当 JMA 下降且收盘价低于平均线时,开空头。 相反信号平仓。
细节
- 入场条件:JMA 斜率与价格确认。
- 多/空:双向。
- 离场条件:反向信号。
- 止损:无。
- 默认参数:
JmaLength= 14CandleType= TimeSpan.FromHours(4)
- 过滤器:
- 分类:趋势
- 方向:双向
- 指标:JMA
- 止损:无
- 复杂度:基础
- 时间框架:波段 (4h)
- 季节性:否
- 神经网络:否
- 背离:否
- 风险等级:中等
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>
/// JSatl Digit System based on Jurik Moving Average slope.
/// Opens long when JMA rises and price is above the average.
/// Opens short when JMA falls and price is below the average.
/// </summary>
public class JsAtlDigitSystemStrategy : Strategy
{
private readonly StrategyParam<int> _jmaLength;
private readonly StrategyParam<DataType> _candleType;
private bool _isFirstValue = true;
private decimal _prevJma;
/// <summary>
/// Period length for Jurik Moving Average.
/// </summary>
public int JmaLength
{
get => _jmaLength.Value;
set => _jmaLength.Value = value;
}
/// <summary>
/// Candle type used for analysis.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="JsAtlDigitSystemStrategy"/>.
/// </summary>
public JsAtlDigitSystemStrategy()
{
_jmaLength = Param(nameof(JmaLength), 14)
.SetGreaterThanZero()
.SetDisplay("JMA Length", "Period of Jurik moving average", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_isFirstValue = true;
_prevJma = 0m;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_isFirstValue = true;
_prevJma = 0m;
var jma = new JurikMovingAverage { Length = JmaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(jma, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal jmaValue)
{
if (candle.State != CandleStates.Finished)
return;
if (_isFirstValue)
{
_prevJma = jmaValue;
_isFirstValue = false;
return;
}
var price = candle.ClosePrice;
var slope = jmaValue - _prevJma;
if (slope > 0m && price > jmaValue)
{
// JMA rising and price above average -> open long or close short
if (Position <= 0m)
BuyMarket();
}
else if (slope < 0m && price < jmaValue)
{
// JMA falling and price below average -> open short or close long
if (Position >= 0m)
SellMarket();
}
_prevJma = jmaValue;
}
}
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 JurikMovingAverage
from StockSharp.Algo.Strategies import Strategy
class js_atl_digit_system_strategy(Strategy):
def __init__(self):
super(js_atl_digit_system_strategy, self).__init__()
self._jma_length = self.Param("JmaLength", 14) \
.SetDisplay("JMA Length", "Period of Jurik moving average", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._is_first_value = True
self._prev_jma = 0.0
@property
def jma_length(self):
return self._jma_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(js_atl_digit_system_strategy, self).OnReseted()
self._is_first_value = True
self._prev_jma = 0.0
def OnStarted2(self, time):
super(js_atl_digit_system_strategy, self).OnStarted2(time)
self._is_first_value = True
self._prev_jma = 0.0
jma = JurikMovingAverage()
jma.Length = int(self.jma_length)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(jma, self.process_candle).Start()
def process_candle(self, candle, jma_value):
if candle.State != CandleStates.Finished:
return
jma_value = float(jma_value)
if self._is_first_value:
self._prev_jma = jma_value
self._is_first_value = False
return
price = float(candle.ClosePrice)
slope = jma_value - self._prev_jma
if slope > 0 and price > jma_value:
if self.Position <= 0:
self.BuyMarket()
elif slope < 0 and price < jma_value:
if self.Position >= 0:
self.SellMarket()
self._prev_jma = jma_value
def CreateClone(self):
return js_atl_digit_system_strategy()