Exemplo n.º 1
0
Arquivo: TA.cs Projeto: larytet/JQuant
        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;
        }
Exemplo n.º 2
0
        /// <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;
        }
Exemplo n.º 3
0
Arquivo: TA.cs Projeto: larytet/JQuant
        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);
        }
Exemplo n.º 4
0
Arquivo: TA.cs Projeto: larytet/JQuant
        public Shape(int start, int end, PriceVolumeSeries series)
        {
            this.start = start;
            this.end   = end;

            this.series = series;
        }
Exemplo n.º 5
0
Arquivo: TA.cs Projeto: larytet/JQuant
        /// <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;
        }
Exemplo n.º 6
0
Arquivo: TA.cs Projeto: larytet/JQuant
        public static double[] Normalize
            (PriceVolumeSeries series)
        {
            double max = series.Max;
            double min = series.Min;

            double[] result = Normalize(series, max, min);
            return(result);
        }
Exemplo n.º 7
0
Arquivo: TA.cs Projeto: larytet/JQuant
        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);
        }
Exemplo n.º 8
0
Arquivo: TA.cs Projeto: larytet/JQuant
        /// <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));
        }
Exemplo n.º 9
0
Arquivo: TA.cs Projeto: larytet/JQuant
        /// <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);
        }
Exemplo n.º 10
0
        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;
        }
Exemplo n.º 11
0
        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;
        }
Exemplo n.º 12
0
        /// <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;
        }
Exemplo n.º 13
0
        /// <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;
        }
Exemplo n.º 14
0
        /// <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));
        }
Exemplo n.º 15
0
Arquivo: TA.cs Projeto: larytet/JQuant
 public static bool Read(string filename, out PriceVolumeSeries series)
 {
     series = null;
     return(false);
 }
Exemplo n.º 16
0
 /// <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)
 {
 }
Exemplo n.º 17
0
 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);
 }
Exemplo n.º 18
0
Arquivo: TA.cs Projeto: larytet/JQuant
 /// <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)
 {
 }
Exemplo n.º 19
0
        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;
        }
Exemplo n.º 20
0
Arquivo: TA.cs Projeto: larytet/JQuant
        /// <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);
        }
Exemplo n.º 21
0
 public static double[] Normalize(PriceVolumeSeries series)
 {
     double max = series.Max;
     double min = series.Min;
     double[] result = Normalize(series, max, min);
     return result;
 }
Exemplo n.º 22
0
 public static bool Read(string filename, out PriceVolumeSeries series)
 {
     series = null;
     return false;
 }
Exemplo n.º 23
0
        public Shape(int start, int end, PriceVolumeSeries series)
        {
            this.start = start;
            this.end = end;

            this.series = series;
        }
Exemplo n.º 24
0
Arquivo: TA.cs Projeto: larytet/JQuant
 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);
 }