IMA专家策略
该策略利用价格相对于其移动平均的相对速度进行交易。
指标 Close / SMA - 1 在连续两根K线之间比较。指标快速上升开多头,快速下降开空头。
细节
- 入场条件:
- 多头:
(IMA_now - IMA_prev) / abs(IMA_prev) >= SignalLevel - 空头:
(IMA_now - IMA_prev) / abs(IMA_prev) <= -SignalLevel
- 多头:
- 出场条件:反向信号
- 头寸大小:
RiskLevel与StopLossTicks决定交易量,并受MaxVolume限制 - 多空:双向
- 止损:无
- 默认值:
SmaPeriod= 5TakeProfitTicks= 50StopLossTicks= 1000SignalLevel= 0.5RiskLevel= 0.01MaxVolume= 1CandleType= TimeSpan.FromMinutes(1).TimeFrame()
- 筛选:
- 类别:趋势跟随
- 方向:双向
- 指标:SMA
- 止损:否
- 复杂度:基础
- 时间框架:日内
- 季节性:否
- 神经网络:否
- 背离:否
- 风险级别:中等
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy based on relative change of price to its SMA.
/// </summary>
public class ImaExpertStrategy : Strategy
{
private readonly StrategyParam<int> _smaPeriod;
private readonly StrategyParam<decimal> _signalLevel;
private readonly StrategyParam<DataType> _candleType;
private decimal? _previousIma;
/// <summary>
/// Period of the SMA indicator.
/// </summary>
public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
/// <summary>
/// Threshold for signal generation.
/// </summary>
public decimal SignalLevel { get => _signalLevel.Value; set => _signalLevel.Value = value; }
/// <summary>
/// Candle type for indicator calculations.
/// </summary>
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
/// <summary>
/// Initializes a new instance of the strategy.
/// </summary>
public ImaExpertStrategy()
{
_smaPeriod = Param(nameof(SmaPeriod), 5)
.SetGreaterThanZero()
.SetDisplay("SMA Period", "Length of moving average", "Parameters");
_signalLevel = Param(nameof(SignalLevel), 0.5m)
.SetDisplay("Signal Level", "IMA change threshold", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for calculations", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_previousIma = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sma = new ExponentialMovingAverage { Length = SmaPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(sma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal smaValue)
{
if (candle.State != CandleStates.Finished)
return;
if (smaValue == 0)
return;
var price = candle.ClosePrice;
var ima = price / smaValue - 1m;
if (_previousIma is null || _previousIma.Value == 0)
{
_previousIma = ima;
return;
}
var k1 = (ima - _previousIma.Value) / Math.Abs(_previousIma.Value);
_previousIma = ima;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (Position == 0)
{
if (k1 >= SignalLevel)
BuyMarket();
else if (k1 <= -SignalLevel)
SellMarket();
}
else if (Position > 0 && k1 <= -SignalLevel)
{
SellMarket();
}
else if (Position < 0 && k1 >= SignalLevel)
{
BuyMarket();
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class ima_expert_strategy(Strategy):
def __init__(self):
super(ima_expert_strategy, self).__init__()
self._sma_period = self.Param("SmaPeriod", 5).SetDisplay("SMA Period", "Length of moving average", "Parameters")
self._signal_level = self.Param("SignalLevel", 0.5).SetDisplay("Signal Level", "IMA change threshold", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))).SetDisplay("Candle Type", "Timeframe for calculations", "General")
self._previous_ima = None
@property
def sma_period(self): return self._sma_period.Value
@property
def signal_level(self): return self._signal_level.Value
@property
def candle_type(self): return self._candle_type.Value
def OnReseted(self):
super(ima_expert_strategy, self).OnReseted()
self._previous_ima = None
def OnStarted2(self, time):
super(ima_expert_strategy, self).OnStarted2(time)
sma = ExponentialMovingAverage()
sma.Length = self.sma_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(sma, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawOwnTrades(area)
def process_candle(self, candle, sma_value):
if candle.State != CandleStates.Finished: return
sv = float(sma_value)
if sv == 0.0: return
price = float(candle.ClosePrice)
ima = price / sv - 1.0
if self._previous_ima is None or self._previous_ima == 0.0:
self._previous_ima = ima
return
k1 = (ima - self._previous_ima) / abs(self._previous_ima)
self._previous_ima = ima
if not self.IsFormedAndOnlineAndAllowTrading(): return
sl = float(self.signal_level)
if self.Position == 0:
if k1 >= sl: self.BuyMarket()
elif k1 <= -sl: self.SellMarket()
elif self.Position > 0 and k1 <= -sl:
self.SellMarket()
elif self.Position < 0 and k1 >= sl:
self.BuyMarket()
def CreateClone(self): return ima_expert_strategy()