This strategy is a StockSharp port of the MetaTrader expert Expert_AH_HM_RSI. It looks for hammer or hanging man candlestick patterns and requires a confirming signal from the Relative Strength Index (RSI) before trading. The approach mirrors the original Expert Advisor, including its risk management philosophy of reversing positions when a fresh signal appears.
Trading Logic
Trend Filter – A short Simple Moving Average (default length 2) is used to determine whether the market is in a micro downtrend or uptrend.
Candlestick Pattern – The strategy analyses the most recent completed candle:
A hammer is detected when the body sits in the upper third of the range, the candle gaps lower than the previous bar, and the midpoint of the candle is below the moving-average trend.
A hanging man is detected when the body sits in the upper third, the candle gaps higher than the previous bar, and the midpoint of the candle is above the moving-average trend.
RSI Filter –
Long trades require the RSI to be below the configurable hammer threshold (default 40).
Short trades require the RSI to be above the hanging-man threshold (default 60).
Trade Execution – On a valid signal the strategy enters with Volume + |Position|, so open positions are reversed immediately when the opposite setup arrives.
Exit Rules – Positions are flattened when the RSI crosses the configurable lower (default 30) or upper (default 70) boundaries in the opposite direction, replicating the exit votes in the original code.
Indicators
RelativeStrengthIndex (length 33 by default).
SimpleMovingAverage (length 2 by default) applied to candle closes.
Parameters
Name
Description
Default
Volume
Order size used for entries.
1
RsiPeriod
RSI lookback period.
33
MaPeriod
Moving-average period for the trend filter.
2
HammerRsiThreshold
Maximum RSI value that allows a hammer long entry.
40
HangingManRsiThreshold
Minimum RSI value that allows a hanging-man short entry.
60
LowerExitLevel
RSI boundary used to close shorts after an upward cross.
30
UpperExitLevel
RSI boundary used to close longs after a downward cross.
70
CandleType
Timeframe processed by the strategy.
1 hour candles
All parameters can be optimised via the StockSharp parameter UI.
Usage Notes
The logic works exclusively on finished candles. Ensure the selected timeframe and data feed produce complete bars.
Because the reversal logic always trades Volume + |Position|, positions flip direction instantly on the opposite signal, matching the Expert Advisor.
Start the built-in risk management once at launch (StartProtection() is called in OnStarted).
Files
CS/AhHmRsiStrategy.cs – Strategy implementation.
README.md – English documentation.
README_zh.md – Chinese documentation.
README_ru.md – Russian documentation.
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Hammer/Hanging Man + RSI strategy.
/// Buys on hammer with low RSI, sells on hanging man with high RSI.
/// </summary>
public class AhHmRsiStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<decimal> _rsiLow;
private readonly StrategyParam<decimal> _rsiHigh;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public decimal RsiLow { get => _rsiLow.Value; set => _rsiLow.Value = value; }
public decimal RsiHigh { get => _rsiHigh.Value; set => _rsiHigh.Value = value; }
public AhHmRsiStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI period", "Indicators");
_rsiLow = Param(nameof(RsiLow), 40m)
.SetDisplay("RSI Low", "RSI oversold threshold for buy", "Signals");
_rsiHigh = Param(nameof(RsiHigh), 60m)
.SetDisplay("RSI High", "RSI overbought threshold for sell", "Signals");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(rsi, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsiValue)
{
if (candle.State != CandleStates.Finished) return;
var body = Math.Abs(candle.ClosePrice - candle.OpenPrice);
var range = candle.HighPrice - candle.LowPrice;
if (range <= 0 || body <= 0) return;
var upperShadow = candle.HighPrice - Math.Max(candle.OpenPrice, candle.ClosePrice);
var lowerShadow = Math.Min(candle.OpenPrice, candle.ClosePrice) - candle.LowPrice;
var isHammer = lowerShadow > body * 2 && upperShadow < body;
var isHangingMan = upperShadow > body * 2 && lowerShadow < body;
if (isHammer && rsiValue < RsiLow && Position <= 0)
BuyMarket();
else if (isHangingMan && rsiValue > RsiHigh && 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
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class ah_hm_rsi_strategy(Strategy):
def __init__(self):
super(ah_hm_rsi_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5)))
self._rsi_period = self.Param("RsiPeriod", 14)
self._rsi_low = self.Param("RsiLow", 40.0)
self._rsi_high = self.Param("RsiHigh", 60.0)
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def RsiPeriod(self):
return self._rsi_period.Value
@RsiPeriod.setter
def RsiPeriod(self, value):
self._rsi_period.Value = value
@property
def RsiLow(self):
return self._rsi_low.Value
@RsiLow.setter
def RsiLow(self, value):
self._rsi_low.Value = value
@property
def RsiHigh(self):
return self._rsi_high.Value
@RsiHigh.setter
def RsiHigh(self, value):
self._rsi_high.Value = value
def OnStarted2(self, time):
super(ah_hm_rsi_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.RsiPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(rsi, self._process_candle).Start()
def _process_candle(self, candle, rsi_value):
if candle.State != CandleStates.Finished:
return
rsi_val = float(rsi_value)
body = abs(float(candle.ClosePrice) - float(candle.OpenPrice))
rng = float(candle.HighPrice) - float(candle.LowPrice)
if rng <= 0 or body <= 0:
return
upper_shadow = float(candle.HighPrice) - max(float(candle.OpenPrice), float(candle.ClosePrice))
lower_shadow = min(float(candle.OpenPrice), float(candle.ClosePrice)) - float(candle.LowPrice)
is_hammer = lower_shadow > body * 2 and upper_shadow < body
is_hanging_man = upper_shadow > body * 2 and lower_shadow < body
if is_hammer and rsi_val < self.RsiLow and self.Position <= 0:
self.BuyMarket()
elif is_hanging_man and rsi_val > self.RsiHigh and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return ah_hm_rsi_strategy()