public static AvgGainLossPair GetAGL(int period, int index, List <double> prices) { if (index < period) { throw new Exception("no enough data"); } AvgGainLossPair agl = GetFirstAGL(period, prices); if (index == period) { return(agl); } for (int i = period + 1; i <= index; i++) { double prevPrice = prices[i - 1]; double curPrice = prices[i]; double delta = curPrice - prevPrice; double gain = delta > 0? delta: 0; double loss = delta < 0? Math.Abs(delta): 0; agl.AvgGain = (agl.AvgGain * 13 + gain) / 14; agl.AvgLoss = (agl.AvgLoss * 13 + loss) / 14; } return(agl); }
public void CanGetFirstAGL() { AvgGainLossPair value = RSICalculator.GetFirstAGL(_period, _shortPrices); Assert.IsTrue(value.AvgGain.AlmostEqual(0.24)); Assert.IsTrue(value.AvgLoss.AlmostEqual(0.10)); }
public void CanGetAGL() { AvgGainLossPair value = RSICalculator.GetAGL(_period, 15, _shortPrices); Assert.IsTrue(value.AvgGain.AlmostEqual(0.22)); Assert.IsTrue(value.AvgLoss.AlmostEqual(0.11)); value = RSICalculator.GetAGL(_period, 32, _shortPrices); Assert.IsTrue(value.AvgGain.AlmostEqual(0.18)); Assert.IsTrue(value.AvgLoss.AlmostEqual(0.30)); }
public static double PredictPrice(int period, double targetRSI, List <double> prices) { double currentPrice = prices.Last(); double currentRSI = RSICalculator.CalculateLastRsi(period, prices.ToArray()); if (currentRSI == 0) { return(0); } if (targetRSI == currentRSI) { return(currentPrice); } double targetRS = GetRS(targetRSI); AvgGainLossPair aglp = GetAGL(period, prices.Count() - 1, prices); double delta = (targetRSI > currentRSI) ? targetRS * aglp.AvgLoss * 13 - aglp.AvgGain * 13 : (aglp.AvgGain * 13 / targetRS - aglp.AvgLoss * 13) * -1; return(currentPrice + delta); }