Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
public static class TechnicalAnalysisFormula
{
public static double Rsi(double[] source, int period)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (source.Length <= period) throw new ArgumentException(nameof(source) + ".Length <= period");
var totalProfit = 0d;
var totalLoss = 0d;
for (var i = source.Length - period; i < source.Length; i++)
{
var profitOrLoss = source[i] - source[i - 1];
if (profitOrLoss > 0)
{
totalProfit += profitOrLoss;
}
else if (profitOrLoss < 0)
{
totalLoss += profitOrLoss;
}
}
return 100 * totalProfit / (totalProfit + -totalLoss);
}
public static List<double> RsiList(double[] source, int period)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (source.Length <= period) throw new ArgumentException(nameof(source) + ".Length <= period");
var list = new List<double>();
for (var offset = 0; offset < source.Length - period; offset++)
{
var targets = source.Skip(offset).Take(period + 1).ToArray();
list.Add(Rsi(targets, period));
}
return list;
}
public static double Ema(double[] source, int period)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var alpha = 2d / (1d + period);
var result = 0d;
var first = true;
foreach (var price in source)
{
if (first)
{
first = false;
result = price;
continue;
}
result = price * alpha + result * (1 - alpha);
}
return result;
}
public static List<double> EmaList(double[] source, int period)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var alpha = 2d / (1d + period);
var result = 0d;
var first = true;
var list = new List<double>(source.Length);
foreach (var price in source)
{
if (first)
{
first = false;
result = price;
list.Add(result);
continue;
}
result = price * alpha + result * (1 - alpha);
list.Add(result);
}
return list;
}
public static Macd Macd(double[] source, int period, int signalPeriod)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var macdSlow = 0d;
var macd = 0d;
var macdSignal = 0d;
var first = true;
var alpha = 2d / (1d + period);
var signalAlpha = 2d / (1d + signalPeriod);
foreach (var price in source)
{
if (first)
{
first = false;
macdSlow = price;
macdSignal = 0d;
continue;
}
macdSlow = price * alpha + macdSlow * (1 - alpha);
macd = price - macdSlow;
macdSignal = macd * signalAlpha + macdSignal * (1 - signalAlpha);
}
return new Macd { Slow = macdSlow, Value = macd, Signal = macdSignal };
}
public static List<Macd> MacdList(double[] source, int period, int signalPeriod)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var macdSlow = 0d;
var macd = 0d;
var macdSignal = 0d;
var first = true;
var list = new List<Macd>(source.Length);
var alpha = 2d / (1d + period);
var signalAlpha = 2d / (1d + signalPeriod);
foreach (var price in source)
{
if (first)
{
first = false;
macdSlow = price;
macdSignal = 0d;
list.Add(new Macd { Slow = macdSlow, Value = macd, Signal = macdSignal });
continue;
}
macdSlow = price * alpha + macdSlow * (1 - alpha);
macd = price - macdSlow;
macdSignal = macd * signalAlpha + macdSignal * (1 - signalAlpha);
list.Add(new Macd { Slow = macdSlow, Value = macd, Signal = macdSignal });
}
return list;
}
}
public struct Macd
{
public double Slow { get; set; }
public double Value { get; set; }
public double Signal { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment