Reflex 与 Trendflex
该策略使用 Reflex 与 Trendflex EMA 的交叉信号进行交易。
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>
/// Simplified Reflex and Trendflex crossover strategy.
/// </summary>
public class ReflexTrendflexStrategy : Strategy
{
private readonly StrategyParam<int> _reflexLen;
private readonly StrategyParam<int> _trendflexLen;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevReflex;
private decimal _prevTrend;
public int ReflexLength { get => _reflexLen.Value; set => _reflexLen.Value = value; }
public int TrendflexLength { get => _trendflexLen.Value; set => _trendflexLen.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public ReflexTrendflexStrategy()
{
_reflexLen = Param(nameof(ReflexLength), 10)
.SetGreaterThanZero()
.SetDisplay("Reflex Length", "Reflex EMA length", "General");
_trendflexLen = Param(nameof(TrendflexLength), 30)
.SetGreaterThanZero()
.SetDisplay("Trendflex Length", "Trendflex EMA length", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to process", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevReflex = 0m;
_prevTrend = 0m;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var reflex = new ExponentialMovingAverage { Length = ReflexLength };
var trend = new ExponentialMovingAverage { Length = TrendflexLength };
_prevReflex = 0m;
_prevTrend = 0m;
var subscription = SubscribeCandles(CandleType);
subscription.Bind(reflex, trend, Process).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, reflex);
DrawIndicator(area, trend);
DrawOwnTrades(area);
}
}
private void Process(ICandleMessage candle, decimal reflexVal, decimal trendVal)
{
if (candle.State != CandleStates.Finished)
return;
if (_prevReflex == 0)
{
_prevReflex = reflexVal;
_prevTrend = trendVal;
return;
}
var prevDiff = _prevReflex - _prevTrend;
var currDiff = reflexVal - trendVal;
if (prevDiff <= 0 && currDiff > 0 && Position <= 0)
{
BuyMarket();
}
else if (prevDiff >= 0 && currDiff < 0 && Position >= 0)
{
SellMarket();
}
_prevReflex = reflexVal;
_prevTrend = trendVal;
}
}
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 reflex_trendflex_strategy(Strategy):
def __init__(self):
super(reflex_trendflex_strategy, self).__init__()
self._reflex_len = self.Param("ReflexLength", 10) \
.SetDisplay("Reflex Length", "Reflex EMA length", "General")
self._trendflex_len = self.Param("TrendflexLength", 30) \
.SetDisplay("Trendflex Length", "Trendflex EMA length", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Type of candles to process", "General")
self._prev_reflex = 0.0
self._prev_trend = 0.0
@property
def reflex_len(self):
return self._reflex_len.Value
@property
def trendflex_len(self):
return self._trendflex_len.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(reflex_trendflex_strategy, self).OnReseted()
self._prev_reflex = 0.0
self._prev_trend = 0.0
def OnStarted2(self, time):
super(reflex_trendflex_strategy, self).OnStarted2(time)
reflex = ExponentialMovingAverage()
reflex.Length = self.reflex_len
trend = ExponentialMovingAverage()
trend.Length = self.trendflex_len
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(reflex, trend, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, reflex)
self.DrawIndicator(area, trend)
self.DrawOwnTrades(area)
def on_process(self, candle, reflex_val, trend_val):
if candle.State != CandleStates.Finished:
return
if self._prev_reflex == 0:
self._prev_reflex = reflex_val
self._prev_trend = trend_val
return
prev_diff = self._prev_reflex - self._prev_trend
curr_diff = reflex_val - trend_val
if prev_diff <= 0 and curr_diff > 0 and self.Position <= 0:
self.BuyMarket()
elif prev_diff >= 0 and curr_diff < 0 and self.Position >= 0:
self.SellMarket()
self._prev_reflex = reflex_val
self._prev_trend = trend_val
def CreateClone(self):
return reflex_trendflex_strategy()