public static void CalculateAverage (PriceVolumeSeries series, int start, int count, out double average, out double max, out int maxIdx, out double min, out int minIdx) { Candle candle = (Candle)series.Data[start]; double close = candle.close; average = close; max = close; maxIdx = 0; min = max; minIdx = 0; int end = start + count - 1; for (int i = start + 1; i <= end; i++) { candle = (Candle)series.Data[i]; close = candle.close; if (max < close) { max = close; maxIdx = i; } if (min > close) { min = close; minIdx = i; } average += close; } average = average / count; }
/// <summary> /// find ascending triangel in the series /// </summary> /// <param name="series"> /// A <see cref="PriceVolumeSeries"/> /// The data to analyse /// </param> public AscendingTriangle(PriceVolumeSeries series, double stdDeviations) { this.series = series; this.shapes = new System.Collections.Generic.List<TA.Shape>(1); StdDeviations = stdDeviations; }
public static bool Write(string filename, PriceVolumeSeries series) { System.IO.FileStream fileStream = null; bool shouldClose = false; try { fileStream = new System.IO.FileStream(filename, FileMode.CreateNew, FileAccess.Write, FileShare.Read); shouldClose = true; StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.Write(series.ToString(TA.PriceVolumeSeries.Format.CSV)); streamWriter.Flush(); fileStream.Close(); shouldClose = false; fileStream = null; } catch (IOException e) { Console.WriteLine(e.ToString()); } if (shouldClose) { fileStream.Close(); } return(false); }
public Shape(int start, int end, PriceVolumeSeries series) { this.start = start; this.end = end; this.series = series; }
/// <summary> /// find ascending triangel in the series /// </summary> /// <param name="series"> /// A <see cref="PriceVolumeSeries"/> /// The data to analyse /// </param> public AscendingTriangle(PriceVolumeSeries series, double stdDeviations) { this.series = series; this.shapes = new System.Collections.Generic.List <TA.Shape>(1); StdDeviations = stdDeviations; }
public static double[] Normalize (PriceVolumeSeries series) { double max = series.Max; double min = series.Min; double[] result = Normalize(series, max, min); return(result); }
public static double[] GetClose(PriceVolumeSeries series) { int count = series.Data.Count; double[] result = new double[count]; int idx = 0; foreach (Candle candle in series.Data) { result[idx] = candle.close; idx++; } return(result); }
/// <summary> /// the method does not calculate series std deviation, but a measurement of the single /// candle variance or a single candle volatility /// Welford's Algorithm for single pass calculation /// long n = 0; /// double mu = 0.0; /// double sq = 0.0; /// /// void update(double x) { /// ++n; /// double muNew = mu + (x - mu)/n; /// sq += (x - mu) * (x - muNew) /// mu = muNew; /// } /// double mean() { return mu; } /// double var() { return n > 1 ? sq/n : 0.0; } /// More can be found here http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance /// Example in Java for online calculaiton http://www.johndcook.com/standard_deviation.html /// </summary> public static void CalculateAverageStdDeviation (PriceVolumeSeries series, int start, int count, out double average, out double max, out double min, out double stdDeviation) { Candle candle; double close; average = 0; min = Int32.MaxValue; max = Int32.MinValue; stdDeviation = 0; double candleSize = 0; int end = start + count - 1; for (int i = start; i <= end; i++) { candle = (Candle)series.Data[i]; close = candle.close; average += close; max = Math.Max(max, close); min = Math.Min(min, close); double d = Math.Abs(candle.open - close); // calculate "variance" candleSize += d; } average = average / count; double candleSizeAverage = candleSize / count; for (int i = start; i <= end; i++) { candle = (Candle)series.Data[i]; close = candle.close; candleSize = Math.Abs(candle.open - close); double d = candleSize - candleSizeAverage; // calculate "candle variance" stdDeviation += d * d; } stdDeviation = Math.Sqrt(stdDeviation / (count - 1)); }
/// <summary> /// Method will normalize (bring to range from -1 to 1 the open, high, low, close prices /// Performance warning: created a new candle for every candle in the /// series /// </summary> /// <param name="series"> /// A <see cref="PriceVolumeSeries"/> /// </param> /// <param name="start"> /// A <see cref="System.Int32"/> /// </param> /// <param name="count"> /// A <see cref="System.Int32"/> /// </param> /// A <see cref="System.Double"/> /// <param name="max"> /// </param> /// <param name="min"> /// A <see cref="System.Double"/> /// </param> public static double[] Normalize (PriceVolumeSeries series, double max, double min) { int count = series.Data.Count; double[] result = new double[count]; double max_min = max - min; for (int i = 0; i < count; i++) { Candle candle = (Candle)series.Data[i]; double close = candle.close; close = 2 * ((close - min) / max_min - 0.5); result[i] = close; } return(result); }
public static void CalculateAverage(PriceVolumeSeries series, int start, int count, out double average, out double max, out int maxIdx, out double min, out int minIdx) { Candle candle = (Candle)series.Data[start]; double close = candle.close; average = close; max = close; maxIdx = 0; min = max; minIdx = 0; int end = start + count - 1; for (int i = start + 1; i <= end; i++) { candle = (Candle)series.Data[i]; close = candle.close; if (max < close) { max = close; maxIdx = i; } if (min > close) { min = close; minIdx = i; } average += close; } average = average / count; }
public static bool Write(string filename, PriceVolumeSeries series) { System.IO.FileStream fileStream = null; bool shouldClose = false; try { fileStream = new System.IO.FileStream(filename, FileMode.CreateNew, FileAccess.Write, FileShare.Read); shouldClose = true; StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.Write(series.ToString(TA.PriceVolumeSeries.Format.CSV)); streamWriter.Flush(); fileStream.Close(); shouldClose = false; fileStream = null; } catch (IOException e) { Console.WriteLine(e.ToString()); } if (shouldClose) { fileStream.Close(); } return false; }
/// <summary> /// divide the series in two (or three) parts, calculate maximum and minimum in each part parts /// maximums should be "close" enough and subsequent low should be higher than the previous /// check if i have at least three points luying on the same ascending line and at least two /// points around the maximum /// /// When dividing the range in two or three segments two different approaches are used /// - equal time /// - fib (0.38, 0.618, 1) time /// </summary> public bool isTriangle(int start, int end, PriceVolumeSeries series) { bool result = false; do { // no enough points for triangle if ((end - start) < 6) break; int halfPoint = (end - start) / 2; double average, min1, max1, min2, max2; int min1Idx, max1Idx, min2Idx, max2Idx; double stdDeviation = StdDeviations * series.StdDeviation; if ((series.Max - series.Min) < 2 * stdDeviation) { break; } PriceVolumeSeries.CalculateAverage(series, start, halfPoint - start + 1, out average, out min1, out min1Idx, out max1, out max1Idx); PriceVolumeSeries.CalculateAverage(series, halfPoint, end - halfPoint + 1, out average, out min2, out min2Idx, out max2, out max2Idx); // max2-stdDeviation < max1 < max2+stdDeviation if ((max1 <= (max2 - stdDeviation)) || (max1 >= (max2 + stdDeviation))) { break; } if (min2 < min1 + stdDeviation) { break; } // i have two conditions - maxs are "close" and second low is higher than the first // do i have two highs and three lows ? two highs (two maxs) are in place. now lows double tangent = (min2 - min1) / (min2Idx - min1Idx); double target = min1 - tangent * (min1Idx - start); bool thirdPoint = false; bool closeUnder = false; // look for 3rd close on line described by the tangent // on the way I make sure that all closes are above the ascending line for (int i = start; i <= end; i++) { Candle candle = (Candle)series.Data[i]; double close = candle.close; if ((i != min1Idx) && (i != min2Idx) && (close < target - stdDeviation)) { closeUnder = true; break; } thirdPoint = thirdPoint | ((i != min1Idx) && (i != min2Idx) && (close > target - stdDeviation) && (close < target + stdDeviation)); target = target + tangent; } result = !closeUnder && thirdPoint; } while (false); return result; }
/// <summary> /// Method will normalize (bring to range from -1 to 1 the open, high, low, close prices /// Performance warning: created a new candle for every candle in the /// series /// </summary> /// <param name="series"> /// A <see cref="PriceVolumeSeries"/> /// </param> /// <param name="start"> /// A <see cref="System.Int32"/> /// </param> /// <param name="count"> /// A <see cref="System.Int32"/> /// </param> /// A <see cref="System.Double"/> /// <param name="max"> /// </param> /// <param name="min"> /// A <see cref="System.Double"/> /// </param> public static double[] Normalize(PriceVolumeSeries series, double max, double min) { int count = series.Data.Count; double[] result = new double[count]; double max_min = max - min; for (int i = 0; i < count; i++) { Candle candle = (Candle)series.Data[i]; double close = candle.close; close = 2 * ((close - min) / max_min - 0.5); result[i] = close; } return result; }
/// <summary> /// the method does not calculate series std deviation, but a measurement of the single /// candle variance or a single candle volatility /// Welford's Algorithm for single pass calculation /// long n = 0; /// double mu = 0.0; /// double sq = 0.0; /// /// void update(double x) { /// ++n; /// double muNew = mu + (x - mu)/n; /// sq += (x - mu) * (x - muNew) /// mu = muNew; /// } /// double mean() { return mu; } /// double var() { return n > 1 ? sq/n : 0.0; } /// More can be found here http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance /// Example in Java for online calculaiton http://www.johndcook.com/standard_deviation.html /// </summary> public static void CalculateAverageStdDeviation(PriceVolumeSeries series, int start, int count, out double average, out double max, out double min, out double stdDeviation) { Candle candle; double close; average = 0; min = Int32.MaxValue; max = Int32.MinValue; stdDeviation = 0; double candleSize = 0; int end = start + count - 1; for (int i = start; i <= end; i++) { candle = (Candle)series.Data[i]; close = candle.close; average += close; max = Math.Max(max, close); min = Math.Min(min, close); double d = Math.Abs(candle.open - close); // calculate "variance" candleSize += d; } average = average / count; double candleSizeAverage = candleSize / count; for (int i = start; i <= end; i++) { candle = (Candle)series.Data[i]; close = candle.close; candleSize = Math.Abs(candle.open - close); double d = candleSize - candleSizeAverage; // calculate "candle variance" stdDeviation += d * d; } stdDeviation = Math.Sqrt(stdDeviation / (count - 1)); }
public static bool Read(string filename, out PriceVolumeSeries series) { series = null; return(false); }
/// <summary> /// find ascending triangel in the series /// </summary> /// <param name="series"> /// A <see cref="PriceVolumeSeries"/> /// The data to analyse /// </param> public AscendingTriangle(PriceVolumeSeries series) : this(series, 0.2) { }
public static void CalculateAverageStdDeviation(PriceVolumeSeries series, out double average, out double max, out double min, out double stdDeviation) { CalculateAverageStdDeviation(series, 0, series.Data.Count, out average, out max, out min, out stdDeviation); }
public static double[] GetClose(PriceVolumeSeries series) { int count = series.Data.Count; double[] result = new double[count]; int idx = 0; foreach (Candle candle in series.Data) { result[idx] = candle.close; idx++; } return result; }
/// <summary> /// divide the series in two (or three) parts, calculate maximum and minimum in each part parts /// maximums should be "close" enough and subsequent low should be higher than the previous /// check if i have at least three points luying on the same ascending line and at least two /// points around the maximum /// /// When dividing the range in two or three segments two different approaches are used /// - equal time /// - fib (0.38, 0.618, 1) time /// </summary> public bool isTriangle(int start, int end, PriceVolumeSeries series) { bool result = false; do { // no enough points for triangle if ((end - start) < 6) { break; } int halfPoint = (end - start) / 2; double average, min1, max1, min2, max2; int min1Idx, max1Idx, min2Idx, max2Idx; double stdDeviation = StdDeviations * series.StdDeviation; if ((series.Max - series.Min) < 2 * stdDeviation) { break; } PriceVolumeSeries.CalculateAverage(series, start, halfPoint - start + 1, out average, out min1, out min1Idx, out max1, out max1Idx); PriceVolumeSeries.CalculateAverage(series, halfPoint, end - halfPoint + 1, out average, out min2, out min2Idx, out max2, out max2Idx); // max2-stdDeviation < max1 < max2+stdDeviation if ((max1 <= (max2 - stdDeviation)) || (max1 >= (max2 + stdDeviation))) { break; } if (min2 < min1 + stdDeviation) { break; } // i have two conditions - maxs are "close" and second low is higher than the first // do i have two highs and three lows ? two highs (two maxs) are in place. now lows double tangent = (min2 - min1) / (min2Idx - min1Idx); double target = min1 - tangent * (min1Idx - start); bool thirdPoint = false; bool closeUnder = false; // look for 3rd close on line described by the tangent // on the way I make sure that all closes are above the ascending line for (int i = start; i <= end; i++) { Candle candle = (Candle)series.Data[i]; double close = candle.close; if ((i != min1Idx) && (i != min2Idx) && (close < target - stdDeviation)) { closeUnder = true; break; } thirdPoint = thirdPoint | ((i != min1Idx) && (i != min2Idx) && (close > target - stdDeviation) && (close < target + stdDeviation)); target = target + tangent; } result = !closeUnder && thirdPoint; }while (false); return(result); }
public static double[] Normalize(PriceVolumeSeries series) { double max = series.Max; double min = series.Min; double[] result = Normalize(series, max, min); return result; }
public static bool Read(string filename, out PriceVolumeSeries series) { series = null; return false; }
public static void CalculateAverageStdDeviation (PriceVolumeSeries series, out double average, out double max, out double min, out double stdDeviation) { CalculateAverageStdDeviation(series, 0, series.Data.Count, out average, out max, out min, out stdDeviation); }