Skip to content

Instantly share code, notes, and snippets.

@kiyoaki
Created March 28, 2017 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiyoaki/74663e2b819fd5e43a2942d2e197b0b2 to your computer and use it in GitHub Desktop.
Save kiyoaki/74663e2b819fd5e43a2942d2e197b0b2 to your computer and use it in GitHub Desktop.
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