Ultimate Trading Bot
只做多策略,结合RSI、均线、MACD和随机指标的交叉来确定进出场。
详情
- 入场条件:RSI上穿超卖且价格高于均线,同时MACD和随机指标上穿。
- 多空方向:仅多头。
- 出场条件:相反交叉条件。
- 止损:无显式止损。
- 默认值:
RsiLength= 14RsiOverbought= 70RsiOversold= 30MaLength= 50StochLength= 14MacdFast= 12MacdSlow= 26MacdSignal= 9
- 筛选:
- 类型:动量
- 方向:多头
- 指标:RSI, MA, MACD, Stochastic
- 止损:无
- 复杂度:中等
- 时间框架:中期
- 季节性:否
- 神经网络:否
- 背离:否
- 风险级别:中
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>
/// Ultimate Trading Bot strategy using RSI momentum with EMA trend filter.
/// </summary>
public class UltimateTradingBotStrategy : Strategy
{
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<decimal> _rsiOverbought;
private readonly StrategyParam<decimal> _rsiOversold;
private readonly StrategyParam<int> _fastEmaLength;
private readonly StrategyParam<int> _slowEmaLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevRsi;
private decimal _prevFast;
private decimal _prevSlow;
private int _cooldown;
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public decimal RsiOverbought { get => _rsiOverbought.Value; set => _rsiOverbought.Value = value; }
public decimal RsiOversold { get => _rsiOversold.Value; set => _rsiOversold.Value = value; }
public int FastEmaLength { get => _fastEmaLength.Value; set => _fastEmaLength.Value = value; }
public int SlowEmaLength { get => _slowEmaLength.Value; set => _slowEmaLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public UltimateTradingBotStrategy()
{
_rsiLength = Param(nameof(RsiLength), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "Period of RSI", "General");
_rsiOverbought = Param(nameof(RsiOverbought), 70m)
.SetDisplay("RSI Overbought", "Overbought level", "General");
_rsiOversold = Param(nameof(RsiOversold), 30m)
.SetDisplay("RSI Oversold", "Oversold level", "General");
_fastEmaLength = Param(nameof(FastEmaLength), 8)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Fast EMA period", "General");
_slowEmaLength = Param(nameof(SlowEmaLength), 21)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Slow EMA period", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for analysis", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
protected override void OnReseted()
{
base.OnReseted();
_prevRsi = 0;
_prevFast = 0;
_prevSlow = 0;
_cooldown = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var emaFast = new ExponentialMovingAverage { Length = FastEmaLength };
var emaSlow = new ExponentialMovingAverage { Length = SlowEmaLength };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(rsi, emaFast, emaSlow, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, emaFast);
DrawIndicator(area, emaSlow);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsiVal, decimal emaFast, decimal emaSlow)
{
if (candle.State != CandleStates.Finished)
return;
if (_prevRsi == 0 || _prevFast == 0 || _prevSlow == 0)
{
_prevRsi = rsiVal;
_prevFast = emaFast;
_prevSlow = emaSlow;
return;
}
if (_cooldown > 0)
{
_cooldown--;
_prevRsi = rsiVal;
_prevFast = emaFast;
_prevSlow = emaSlow;
return;
}
var hist = emaFast - emaSlow;
var histUp = hist > 0m;
var histDown = hist < 0m;
var rsiCrossUp = _prevRsi <= 50m && rsiVal > 50m;
var rsiCrossDown = _prevRsi >= 50m && rsiVal < 50m;
// Exit
if (Position > 0 && rsiCrossDown)
{
SellMarket();
_cooldown = 80;
}
else if (Position < 0 && rsiCrossUp)
{
BuyMarket();
_cooldown = 80;
}
// Entry
if (Position == 0)
{
if (rsiCrossUp && histUp)
{
BuyMarket();
_cooldown = 80;
}
else if (rsiCrossDown && histDown)
{
SellMarket();
_cooldown = 80;
}
}
_prevRsi = rsiVal;
_prevFast = emaFast;
_prevSlow = emaSlow;
}
}
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 ultimate_trading_bot_strategy(Strategy):
def __init__(self):
super(ultimate_trading_bot_strategy, self).__init__()
self._rsi_length = self.Param("RsiLength", 14) \
.SetDisplay("RSI Length", "Period of RSI", "General")
self._rsi_overbought = self.Param("RsiOverbought", 70) \
.SetDisplay("RSI Overbought", "Overbought level", "General")
self._rsi_oversold = self.Param("RsiOversold", 30) \
.SetDisplay("RSI Oversold", "Oversold level", "General")
self._fast_ema_length = self.Param("FastEmaLength", 8) \
.SetDisplay("Fast EMA", "Fast EMA period", "General")
self._slow_ema_length = self.Param("SlowEmaLength", 21) \
.SetDisplay("Slow EMA", "Slow EMA period", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Timeframe for analysis", "General")
self._prev_rsi = 0.0
self._prev_fast = 0.0
self._prev_slow = 0.0
self._cooldown = 0
@property
def rsi_length(self):
return self._rsi_length.Value
@property
def rsi_overbought(self):
return self._rsi_overbought.Value
@property
def rsi_oversold(self):
return self._rsi_oversold.Value
@property
def fast_ema_length(self):
return self._fast_ema_length.Value
@property
def slow_ema_length(self):
return self._slow_ema_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(ultimate_trading_bot_strategy, self).OnReseted()
self._prev_rsi = 0.0
self._prev_fast = 0.0
self._prev_slow = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(ultimate_trading_bot_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_length
ema_fast = ExponentialMovingAverage()
ema_fast.Length = self.fast_ema_length
ema_slow = ExponentialMovingAverage()
ema_slow.Length = self.slow_ema_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(rsi, ema_fast, ema_slow, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema_fast)
self.DrawIndicator(area, ema_slow)
self.DrawOwnTrades(area)
def on_process(self, candle, rsi_val, ema_fast, ema_slow):
if candle.State != CandleStates.Finished:
return
rsi_val = float(rsi_val)
ema_fast = float(ema_fast)
ema_slow = float(ema_slow)
if self._prev_rsi == 0 or self._prev_fast == 0 or self._prev_slow == 0:
self._prev_rsi = rsi_val
self._prev_fast = ema_fast
self._prev_slow = ema_slow
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_rsi = rsi_val
self._prev_fast = ema_fast
self._prev_slow = ema_slow
return
hist = ema_fast - ema_slow
hist_up = hist > 0
hist_down = hist < 0
rsi_cross_up = self._prev_rsi <= 50 and rsi_val > 50
rsi_cross_down = self._prev_rsi >= 50 and rsi_val < 50
if self.Position > 0 and rsi_cross_down:
self.SellMarket()
self._cooldown = 80
elif self.Position < 0 and rsi_cross_up:
self.BuyMarket()
self._cooldown = 80
if self.Position == 0:
if rsi_cross_up and hist_up:
self.BuyMarket()
self._cooldown = 80
elif rsi_cross_down and hist_down:
self.SellMarket()
self._cooldown = 80
self._prev_rsi = rsi_val
self._prev_fast = ema_fast
self._prev_slow = ema_slow
def CreateClone(self):
return ultimate_trading_bot_strategy()