Vicious Mortgage Rates V1 策略
该策略构建由四个波动率指数组成的合成指标。 当合成指数的快EMA上穿慢EMA时做多,反之则做空。
细节
- 入场条件: 合成指数快EMA上穿慢EMA
- 多空方向: 双向
- 出场条件: 反向交叉
- 止损: 无
- 默认值:
FastLength= 8SlowLength= 21
- 过滤器:
- 分类: 波动率
- 方向: 双向
- 指标: EMA
- 止损: 无
- 复杂度: 基础
- 时间框架: 日线
- 季节性: 否
- 神经网络: 否
- 背离: 否
- 风险等级: 中等
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Vicious Mortgage Rates strategy.
/// Uses fast/slow EMA crossover with volatility filter (StdDev above its SMA).
/// Trades on EMA cross when volatility is elevated.
/// </summary>
public class ViciousMortgageRatesV1Strategy : Strategy
{
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<int> _volLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFast;
private decimal _prevSlow;
private int _cooldown;
public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
public int VolLength { get => _volLength.Value; set => _volLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public ViciousMortgageRatesV1Strategy()
{
_fastLength = Param(nameof(FastLength), 8)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Fast EMA length", "General");
_slowLength = Param(nameof(SlowLength), 21)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Slow EMA length", "General");
_volLength = Param(nameof(VolLength), 20)
.SetGreaterThanZero()
.SetDisplay("Vol Length", "Volatility lookback", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Timeframe", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevFast = 0;
_prevSlow = 0;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fastEma = new ExponentialMovingAverage { Length = FastLength };
var slowEma = new ExponentialMovingAverage { Length = SlowLength };
_prevFast = 0;
_prevSlow = 0;
_cooldown = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(fastEma, slowEma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fastEma);
DrawIndicator(area, slowEma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
{
if (candle.State != CandleStates.Finished)
return;
if (_cooldown > 0)
_cooldown--;
if (_prevFast == 0)
{
_prevFast = fast;
_prevSlow = slow;
return;
}
if (_cooldown > 0)
{
_prevFast = fast;
_prevSlow = slow;
return;
}
var longCross = _prevFast <= _prevSlow && fast > slow;
var shortCross = _prevFast >= _prevSlow && fast < slow;
if (longCross && Position <= 0)
{
BuyMarket();
_cooldown = 30;
}
else if (shortCross && Position >= 0)
{
SellMarket();
_cooldown = 30;
}
_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 vicious_mortgage_rates_v1_strategy(Strategy):
def __init__(self):
super(vicious_mortgage_rates_v1_strategy, self).__init__()
self._fast_length = self.Param("FastLength", 8) \
.SetDisplay("Fast EMA", "Fast EMA length", "General")
self._slow_length = self.Param("SlowLength", 21) \
.SetDisplay("Slow EMA", "Slow EMA length", "General")
self._vol_length = self.Param("VolLength", TimeSpan.FromMinutes(15)) \
.SetDisplay("Vol Length", "Volatility lookback", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))) \
.SetDisplay("Candle Type", "Timeframe", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._cooldown = 0
@property
def fast_length(self):
return self._fast_length.Value
@property
def slow_length(self):
return self._slow_length.Value
@property
def vol_length(self):
return self._vol_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(vicious_mortgage_rates_v1_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(vicious_mortgage_rates_v1_strategy, self).OnStarted2(time)
fast_ema = ExponentialMovingAverage()
fast_ema.Length = self.fast_length
slow_ema = ExponentialMovingAverage()
slow_ema.Length = self.slow_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast_ema, slow_ema, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, fast_ema)
self.DrawIndicator(area, slow_ema)
self.DrawOwnTrades(area)
def on_process(self, candle, fast, slow):
if candle.State != CandleStates.Finished:
return
if self._cooldown > 0:
self._cooldown -= 1
if self._prev_fast == 0:
self._prev_fast = fast
self._prev_slow = slow
return
if self._cooldown > 0:
self._prev_fast = fast
self._prev_slow = slow
return
long_cross = self._prev_fast <= self._prev_slow and fast > slow
short_cross = self._prev_fast >= self._prev_slow and fast < slow
if long_cross and self.Position <= 0:
self.BuyMarket()
self._cooldown = 30
elif short_cross and self.Position >= 0:
self.SellMarket()
self._cooldown = 30
self._prev_fast = fast
self._prev_slow = slow
def CreateClone(self):
return vicious_mortgage_rates_v1_strategy()