Relative Strength Comparison
See original GitHub issueRelative Strength Comparison
Relative Strength Comparison compares a security’s price change with that of a “base” security or index. When the Relative Strength Comparison indicator is moving up, it shows that the security’s price is performing better than the base security/index in relative terms. When the indicator is moving sideways, it shows that both securities prices are rising and falling by the same percentages. When the indicator is moving down, it shows that the security’s price is performing worse than the base security/index in relative terms. Relative Strength Comparison is often used to compare a security’s performance with a market index.
And another read from Investopedia.com
I’ve built this in my own system but I’d like to migrate my system to use your library. It shouldn’t be too hard to port to your formatting and I wouldn’t mind making an attempt PR on this, though I’m not sure when I’ll have time. Also your chops are definitely better than mine so perhaps you can improve upon my version.
Here’s my code:
public class RelativeStrengthComparison : IndicatorBase
{
public int AvgLength = 9;
public decimal[] Result { get; set; }
public decimal[] Average { get; set; }
private bool _computeAverage = false;
private int _collectionLength = 0;
public RelativeStrengthComparison(IEnumerable<Candle> focus, IEnumerable<Candle> compare, int length, bool computeAverage = false)
: base(focus, compare)
{
AvgLength = length;
_computeAverage = computeAverage;
}
protected override void Initialize()
{
_collectionLength = Math.Min(Series.Length, SeriesCompare.Length);
Result = new decimal[_collectionLength];
Average = new decimal[_collectionLength];
}
public override void Compute(int startIndex = 0, int? endIndex = null)
{
if (endIndex == null)
endIndex = _collectionLength;
for (int index = startIndex; index < endIndex; index++)
{
decimal focusClose = Series.Close[index];
decimal comprClose = SeriesCompare.Close[index];
decimal result = focusClose / comprClose;
Result.SetValue(result, index);
if (_computeAverage)
{
Average[index] = Avg(Result, index, AvgLength);
}
}
}
public decimal Avg(decimal[] input, int index, int period)
{
var pos = Math.Max(index - period + 1, 0);
if (pos == 0)
return 0;
var sum = input.Sum(index, period);
return sum / period;
}
}
Additional context or information
My use case for this is building watchlists for my algorithm to watch.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top GitHub Comments
This is similar to our existing indicator: Price Relative Strength. I’m adding it to the backlog and will likely take a look at it this weekend.
This Issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new Issue for related bugs.