Options Strategy V1.3
该策略利用 EMA 金叉/死叉配合 RSI 与基于 ATR 的止损和止盈,并加入成交量均线过滤。可选地要求突破开盘区间,并在纽约时间 15:55 自动平仓。策略在特定时段和用户自定义的禁止交易区间内不会开仓。
详情
- 入场条件:
- 多头:短期 EMA 上穿长期 EMA,RSI ≥
RsiLongThreshold,成交量 ≥ 成交量均线,可选收盘价 > 开盘区间高点。 - 空头:短期 EMA 下穿长期 EMA,RSI ≤
RsiShortThreshold,成交量 ≥ 成交量均线,可选收盘价 < 开盘区间低点。
- 多头:短期 EMA 上穿长期 EMA,RSI ≥
- 方向:可做多或做空。
- 出场条件:
- 基于 ATR 的止损与止盈。
- 反向 EMA 交叉。
- 纽约时间 15:55 自动平仓。
- 止损:有。
- 默认值:
EmaShortLength = 8EmaLongLength = 28RsiLength = 12AtrLength = 14SlMultiplier = 1.4TpSlRatio = 4VolumeMaLength = 20
- 过滤:
- 类别:趋势跟随
- 方向:可配置
- 指标:EMA、RSI、ATR、SMA
- 止损:是
- 周期:日内
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
public class OptionsV13Strategy : Strategy
{
private readonly StrategyParam<int> _emaShortLength;
private readonly StrategyParam<int> _emaLongLength;
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<DataType> _candleType;
public int EmaShortLength { get => _emaShortLength.Value; set => _emaShortLength.Value = value; }
public int EmaLongLength { get => _emaLongLength.Value; set => _emaLongLength.Value = value; }
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public OptionsV13Strategy()
{
_emaShortLength = Param(nameof(EmaShortLength), 14).SetGreaterThanZero();
_emaLongLength = Param(nameof(EmaLongLength), 40).SetGreaterThanZero();
_rsiLength = Param(nameof(RsiLength), 14).SetGreaterThanZero();
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var emaShort = new ExponentialMovingAverage { Length = EmaShortLength };
var emaLong = new ExponentialMovingAverage { Length = EmaLongLength };
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var prevF = 0m;
var prevS = 0m;
var init = false;
var lastSignal = DateTimeOffset.MinValue;
var cooldown = TimeSpan.FromMinutes(360);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(emaShort, emaLong, rsi, (candle, f, s, r) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!emaShort.IsFormed || !emaLong.IsFormed || !rsi.IsFormed)
return;
if (!init)
{
prevF = f;
prevS = s;
init = true;
return;
}
if (candle.OpenTime - lastSignal >= cooldown)
{
if (prevF <= prevS && f > s && r > 50 && Position <= 0)
{
BuyMarket();
lastSignal = candle.OpenTime;
}
else if (prevF >= prevS && f < s && r < 50 && Position >= 0)
{
SellMarket();
lastSignal = candle.OpenTime;
}
}
prevF = f;
prevS = s;
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, emaShort);
DrawIndicator(area, emaLong);
DrawOwnTrades(area);
}
}
}
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, RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class options_v13_strategy(Strategy):
def __init__(self):
super(options_v13_strategy, self).__init__()
self._ema_short_length = self.Param("EmaShortLength", 14) \
.SetGreaterThanZero()
self._ema_long_length = self.Param("EmaLongLength", 40) \
.SetGreaterThanZero()
self._rsi_length = self.Param("RsiLength", 14) \
.SetGreaterThanZero()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5)))
self._prev_fast = 0.0
self._prev_slow = 0.0
self._initialized = False
self._last_signal_ticks = 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(options_v13_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._initialized = False
self._last_signal_ticks = 0
def OnStarted2(self, time):
super(options_v13_strategy, self).OnStarted2(time)
self._prev_fast = 0.0
self._prev_slow = 0.0
self._initialized = False
self._last_signal_ticks = 0
self._ema_short = ExponentialMovingAverage()
self._ema_short.Length = self._ema_short_length.Value
self._ema_long = ExponentialMovingAverage()
self._ema_long.Length = self._ema_long_length.Value
self._rsi = RelativeStrengthIndex()
self._rsi.Length = self._rsi_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._ema_short, self._ema_long, self._rsi, self.OnProcess).Start()
def OnProcess(self, candle, f, s, r):
if candle.State != CandleStates.Finished:
return
if not self._ema_short.IsFormed or not self._ema_long.IsFormed or not self._rsi.IsFormed:
return
fv = float(f)
sv = float(s)
rv = float(r)
if not self._initialized:
self._prev_fast = fv
self._prev_slow = sv
self._initialized = True
return
cooldown_ticks = TimeSpan.FromMinutes(360).Ticks
current_ticks = candle.OpenTime.Ticks
if current_ticks - self._last_signal_ticks >= cooldown_ticks:
if self._prev_fast <= self._prev_slow and fv > sv and rv > 50 and self.Position <= 0:
self.BuyMarket()
self._last_signal_ticks = current_ticks
elif self._prev_fast >= self._prev_slow and fv < sv and rv < 50 and self.Position >= 0:
self.SellMarket()
self._last_signal_ticks = current_ticks
self._prev_fast = fv
self._prev_slow = sv
def CreateClone(self):
return options_v13_strategy()