// Copyright (c) 2026 fxtradingguide.com // SPDX-License-Identifier: MIT using FxTradingGuide.Studies; namespace FxTradingGuide.Strategies.RsiMeanReversion; /// /// Wilder RSI. The first average is a simple mean of the first period /// changes. Later averages use (previous * (period - 1) + current) / period. /// public static class RsiMeanReversion { public const int Period = 14; public const double Lower = 30.0; public const double Upper = 70.0; public const double Midline = 50.0; public static double RsiValue(double averageGain, double averageLoss) { if (averageGain == 0.0 && averageLoss == 0.0) { return 50.0; } if (averageLoss == 0.0) { return 100.0; } if (averageGain == 0.0) { return 0.0; } var relative = averageGain / averageLoss; return 100.0 - (100.0 / (1.0 + relative)); } public static double?[] WilderRsi(IReadOnlyList close, int period) { ArgumentOutOfRangeException.ThrowIfNegativeOrZero(period); var gains = new double[close.Count]; var losses = new double[close.Count]; for (var index = 1; index < close.Count; index++) { var change = close[index] - close[index - 1]; if (change > 0) { gains[index] = change; } else if (change < 0) { losses[index] = -change; } } var result = new double?[close.Count]; if (close.Count <= period) { return result; } double averageGain = 0; double averageLoss = 0; for (var index = 1; index <= period; index++) { averageGain += gains[index]; averageLoss += losses[index]; } averageGain /= period; averageLoss /= period; result[period] = RsiValue(averageGain, averageLoss); for (var index = period + 1; index < close.Count; index++) { averageGain = ((averageGain * (period - 1)) + gains[index]) / period; averageLoss = ((averageLoss * (period - 1)) + losses[index]) / period; result[index] = RsiValue(averageGain, averageLoss); } return result; } public static int?[] Signals( IReadOnlyList close, int period = Period, double lower = Lower, double upper = Upper, double midline = Midline) { if (!(0 < lower && lower < midline && midline < upper && upper < 100)) { throw new ArgumentOutOfRangeException(nameof(lower), "RSI levels must sit in order between 0 and 100"); } var rsi = WilderRsi(close, period); var position = 0; var desired = new int?[close.Count]; for (var index = 0; index < rsi.Length; index++) { if (rsi[index] is not double value) { desired[index] = null; continue; } if (value <= lower) { position = 1; } else if (value >= upper) { position = -1; } else if (position == 1 && value >= midline) { position = 0; } else if (position == -1 && value <= midline) { position = 0; } desired[index] = position; } return desired; } public static void AssertFixture() { var rsi = WilderRsi(new[] { 10d, 11d, 12d, 11d, 13d }, 3); if (rsi[0] is not null || rsi[1] is not null || rsi[2] is not null) { throw new InvalidOperationException("RSI warmup failed"); } if (rsi[3] is not double seed || Math.Abs(seed - (100.0 - (100.0 / 3.0))) > 1e-9) { throw new InvalidOperationException("RSI seed failed"); } if (rsi[4] is not double next || Math.Abs(next - (100.0 - (100.0 / 6.0))) > 1e-9) { throw new InvalidOperationException("RSI smoothing failed"); } } } public static class Program { public static void Main() { RsiMeanReversion.AssertFixture(); var observations = SampleSeries.Load(); var rates = observations.Select(item => item.Rate).ToArray(); StudyReport.AssertCausal(rates.Length, end => { var slice = new double[end]; Array.Copy(rates, slice, end); return RsiMeanReversion.Signals(slice); }); StudyReport.Write("rsi-mean-reversion", observations, RsiMeanReversion.Signals(rates)); } }