using System;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// NeuroNirvaman: Perceptron-inspired strategy using RSI momentum
/// with EMA trend filter and weighted signal scoring.
/// </summary>
public class NeuroNirvamanMq4Strategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<int> _fastEmaLength;
private readonly StrategyParam<int> _slowEmaLength;
private readonly StrategyParam<int> _atrLength;
private decimal _prevRsi;
private decimal _prevFast;
private decimal _prevSlow;
private decimal _entryPrice;
public NeuroNirvamanMq4Strategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Timeframe.", "General");
_rsiLength = Param(nameof(RsiLength), 14)
.SetDisplay("RSI Length", "RSI period.", "Indicators");
_fastEmaLength = Param(nameof(FastEmaLength), 10)
.SetDisplay("Fast EMA", "Fast EMA period.", "Indicators");
_slowEmaLength = Param(nameof(SlowEmaLength), 30)
.SetDisplay("Slow EMA", "Slow EMA period.", "Indicators");
_atrLength = Param(nameof(AtrLength), 14)
.SetDisplay("ATR Length", "ATR period.", "Indicators");
}
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int RsiLength
{
get => _rsiLength.Value;
set => _rsiLength.Value = value;
}
public int FastEmaLength
{
get => _fastEmaLength.Value;
set => _fastEmaLength.Value = value;
}
public int SlowEmaLength
{
get => _slowEmaLength.Value;
set => _slowEmaLength.Value = value;
}
public int AtrLength
{
get => _atrLength.Value;
set => _atrLength.Value = value;
}
/// <inheritdoc />
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevRsi = 0;
_prevFast = 0;
_prevSlow = 0;
_entryPrice = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevRsi = 0;
_prevFast = 0;
_prevSlow = 0;
_entryPrice = 0;
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var fast = new ExponentialMovingAverage { Length = FastEmaLength };
var slow = new ExponentialMovingAverage { Length = SlowEmaLength };
var atr = new AverageTrueRange { Length = AtrLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsi, fast, slow, atr, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fast);
DrawIndicator(area, slow);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsiVal, decimal fastVal, decimal slowVal, decimal atrVal)
{
if (candle.State != CandleStates.Finished)
return;
if (_prevFast == 0 || _prevSlow == 0 || _prevRsi == 0 || atrVal <= 0)
{
_prevRsi = rsiVal;
_prevFast = fastVal;
_prevSlow = slowVal;
return;
}
var close = candle.ClosePrice;
// Perceptron score: weighted RSI momentum + EMA trend alignment
var rsiSignal = rsiVal > 50 ? 1m : -1m;
var trendSignal = fastVal > slowVal ? 1m : -1m;
var rsiAccel = rsiVal - _prevRsi > 0 ? 1m : -1m;
var score = rsiSignal * 0.4m + trendSignal * 0.4m + rsiAccel * 0.2m;
// Exit management
if (Position > 0)
{
if (close <= _entryPrice - atrVal * 2m || close >= _entryPrice + atrVal * 3m || score < -0.5m)
{
SellMarket();
_entryPrice = 0;
}
}
else if (Position < 0)
{
if (close >= _entryPrice + atrVal * 2m || close <= _entryPrice - atrVal * 3m || score > 0.5m)
{
BuyMarket();
_entryPrice = 0;
}
}
// Entry: strong perceptron score with RSI crossover confirmation
if (Position == 0)
{
if (score > 0.5m && _prevRsi <= 50 && rsiVal > 50)
{
_entryPrice = close;
BuyMarket();
}
else if (score < -0.5m && _prevRsi >= 50 && rsiVal < 50)
{
_entryPrice = close;
SellMarket();
}
}
_prevRsi = rsiVal;
_prevFast = fastVal;
_prevSlow = slowVal;
}
}
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, ExponentialMovingAverage, AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
class neuro_nirvaman_mq4_strategy(Strategy):
def __init__(self):
super(neuro_nirvaman_mq4_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Timeframe.", "General")
self._rsi_length = self.Param("RsiLength", 14).SetDisplay("RSI Length", "RSI period.", "Indicators")
self._fast_ema_length = self.Param("FastEmaLength", 10).SetDisplay("Fast EMA", "Fast EMA period.", "Indicators")
self._slow_ema_length = self.Param("SlowEmaLength", 30).SetDisplay("Slow EMA", "Slow EMA period.", "Indicators")
self._atr_length = self.Param("AtrLength", 14).SetDisplay("ATR Length", "ATR period.", "Indicators")
@property
def CandleType(self): return self._candle_type.Value
@CandleType.setter
def CandleType(self, value): self._candle_type.Value = value
def OnReseted(self):
super(neuro_nirvaman_mq4_strategy, self).OnReseted()
self._prev_rsi = 0
self._prev_fast = 0
self._prev_slow = 0
self._entry_price = 0
def OnStarted2(self, time):
super(neuro_nirvaman_mq4_strategy, self).OnStarted2(time)
self._prev_rsi = 0
self._prev_fast = 0
self._prev_slow = 0
self._entry_price = 0
rsi = RelativeStrengthIndex()
rsi.Length = self._rsi_length.Value
fast = ExponentialMovingAverage()
fast.Length = self._fast_ema_length.Value
slow = ExponentialMovingAverage()
slow.Length = self._slow_ema_length.Value
atr = AverageTrueRange()
atr.Length = self._atr_length.Value
sub = self.SubscribeCandles(self.CandleType)
sub.Bind(rsi, fast, slow, atr, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, sub)
self.DrawIndicator(area, fast)
self.DrawIndicator(area, slow)
self.DrawOwnTrades(area)
def OnProcess(self, candle, rsi_val, fast_val, slow_val, atr_val):
if candle.State != CandleStates.Finished:
return
if self._prev_fast == 0 or self._prev_slow == 0 or self._prev_rsi == 0 or atr_val <= 0:
self._prev_rsi = rsi_val
self._prev_fast = fast_val
self._prev_slow = slow_val
return
close = candle.ClosePrice
rsi_signal = 1 if rsi_val > 50 else -1
trend_signal = 1 if fast_val > slow_val else -1
rsi_accel = 1 if (rsi_val - self._prev_rsi) > 0 else -1
score = rsi_signal * 0.4 + trend_signal * 0.4 + rsi_accel * 0.2
if self.Position > 0:
if close <= self._entry_price - atr_val * 2 or close >= self._entry_price + atr_val * 3 or score < -0.5:
self.SellMarket()
self._entry_price = 0
elif self.Position < 0:
if close >= self._entry_price + atr_val * 2 or close <= self._entry_price - atr_val * 3 or score > 0.5:
self.BuyMarket()
self._entry_price = 0
if self.Position == 0:
if score > 0.5 and self._prev_rsi <= 50 and rsi_val > 50:
self._entry_price = close
self.BuyMarket()
elif score < -0.5 and self._prev_rsi >= 50 and rsi_val < 50:
self._entry_price = close
self.SellMarket()
self._prev_rsi = rsi_val
self._prev_fast = fast_val
self._prev_slow = slow_val
def CreateClone(self):
return neuro_nirvaman_mq4_strategy()