Ehlers SwamiCharts RSI 指标
汇总周期 2–48 的 RSI 值形成颜色图。平均颜色为绿色时做多,红色时做空。
细节
- 入场条件:平均颜色为绿色(
Color1Avg== 255 且Color2Avg>LongColor)做多;平均颜色为红色(Color1Avg>ShortColor且Color2Avg== 255)做空。 - 多空:双向。
- 出场条件:相反信号。
- 止损:否。
- 默认值:
LongColor= 50ShortColor= 50CandleType= 5 分钟
- 过滤器:
- 类别: Oscillator
- 方向: 双向
- 指标: RSI
- 止损: 否
- 复杂度: 高级
- 时间框架: 日内
- 季节性: 否
- 神经网络: 否
- 背离: 否
- 风险等级: 中等
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>
/// Ehlers SwamiCharts RSI based strategy generating signals from averaged RSI colors.
/// </summary>
public class EhlersSwamiChartsRsiStrategy : Strategy
{
private readonly StrategyParam<int> _longColor;
private readonly StrategyParam<int> _shortColor;
private readonly StrategyParam<DataType> _candleType;
public int LongColor { get => _longColor.Value; set => _longColor.Value = value; }
public int ShortColor { get => _shortColor.Value; set => _shortColor.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public EhlersSwamiChartsRsiStrategy()
{
_longColor = Param(nameof(LongColor), 50)
.SetDisplay("LongColor", "Long color threshold", "General");
_shortColor = Param(nameof(ShortColor), 50)
.SetDisplay("ShortColor", "Short color threshold", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsis = new RelativeStrengthIndex[24];
for (var i = 0; i < 24; i++)
{
rsis[i] = new RelativeStrengthIndex { Length = i + 10 };
}
var subscription = SubscribeCandles(CandleType);
subscription.BindEx(rsis, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue[] values)
{
if (candle.State != CandleStates.Finished)
return;
int color1Tot = 0;
int color2Tot = 0;
int count = 0;
foreach (var val in values)
{
if (val.IsEmpty)
continue;
var rsi = val.ToDecimal() / 100m;
int c1;
int c2;
if (rsi >= 0.5m)
{
c1 = (int)Math.Ceiling(255m * (2m - 2m * rsi));
c2 = 255;
}
else
{
c1 = 255;
c2 = (int)Math.Ceiling(255m * 2m * rsi);
}
color1Tot += c1;
color2Tot += c2;
count++;
}
if (count == 0)
return;
var color1Avg = (int)Math.Ceiling(color1Tot / (decimal)count);
var color2Avg = (int)Math.Ceiling(color2Tot / (decimal)count);
var longSignal = color1Avg == 255 && color2Avg > LongColor;
var shortSignal = color1Avg > ShortColor && color2Avg == 255;
if (longSignal && Position <= 0)
{
BuyMarket();
}
else if (shortSignal && Position >= 0)
{
SellMarket();
}
}
}
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, Array
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import RelativeStrengthIndex, IIndicator
from StockSharp.Algo.Strategies import Strategy
class ehlers_swami_charts_rsi_strategy(Strategy):
def __init__(self):
super(ehlers_swami_charts_rsi_strategy, self).__init__()
self._long_color = self.Param("LongColor", 50) \
.SetDisplay("LongColor", "Long color threshold", "General")
self._short_color = self.Param("ShortColor", 50) \
.SetDisplay("ShortColor", "Short color threshold", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for candles", "General")
@property
def long_color(self):
return self._long_color.Value
@property
def short_color(self):
return self._short_color.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(ehlers_swami_charts_rsi_strategy, self).OnStarted2(time)
rsis = []
for i in range(24):
ind = RelativeStrengthIndex()
ind.Length = i + 10
rsis.append(ind)
arr = Array[IIndicator](rsis)
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(arr, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, values):
if candle.State != CandleStates.Finished:
return
color1_tot = 0
color2_tot = 0
count = 0
for val in values:
if val.IsEmpty:
continue
rsi = float(val) / 100.0
if rsi >= 0.5:
c1 = int(Math.Ceiling(255.0 * (2.0 - 2.0 * rsi)))
c2 = 255
else:
c1 = 255
c2 = int(Math.Ceiling(255.0 * 2.0 * rsi))
color1_tot += c1
color2_tot += c2
count += 1
if count == 0:
return
color1_avg = int(Math.Ceiling(float(color1_tot) / float(count)))
color2_avg = int(Math.Ceiling(float(color2_tot) / float(count)))
long_signal = color1_avg == 255 and color2_avg > int(self.long_color)
short_signal = color1_avg > int(self.short_color) and color2_avg == 255
if long_signal and self.Position <= 0:
self.BuyMarket()
elif short_signal and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return ehlers_swami_charts_rsi_strategy()