Blame view

src/main/webapp/libs/Highstock-7.2.0/code/es-modules/indicators/zigzag.src.js 6.7 KB
caiyongsong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/* *
 *
 *  (c) 2010-2019 Kacper Madej
 *
 *  License: www.highcharts.com/license
 *
 * */

'use strict';

import H from '../parts/Globals.js';
import '../parts/Utilities.js';

var seriesType = H.seriesType,
    UNDEFINED;

/**
 * The Zig Zag series type.
 *
 * @private
 * @class
 * @name Highcharts.seriesTypes.zigzag
 *
 * @augments Highcharts.Series
 */
seriesType(
    'zigzag',
    'sma',
    /**
     * Zig Zag indicator.
     *
     * This series requires `linkedTo` option to be set.
     *
     * @sample stock/indicators/zigzag
     *         Zig Zag indicator
     *
     * @extends      plotOptions.sma
     * @since        6.0.0
     * @product      highstock
     * @optionparent plotOptions.zigzag
     */
    {
        /**
         * @excluding index, period
         */
        params: {
            /**
             * The point index which indicator calculations will base - low
             * value.
             *
             * For example using OHLC data, index=2 means the indicator will be
             * calculated using Low values.
             */
            lowIndex: 2,
            /**
             * The point index which indicator calculations will base - high
             * value.
             *
             * For example using OHLC data, index=1 means the indicator will be
             * calculated using High values.
             */
            highIndex: 1,
            /**
             * The threshold for the value change.
             *
             * For example deviation=1 means the indicator will ignore all price
             * movements less than 1%.
             */
            deviation: 1
        }
    },
    /**
     * @lends Highcharts.Series#
     */
    {
        nameComponents: ['deviation'],
        nameSuffixes: ['%'],
        nameBase: 'Zig Zag',
        getValues: function (series, params) {
            var lowIndex = params.lowIndex,
                highIndex = params.highIndex,
                deviation = params.deviation / 100,
                deviations = {
                    'low': 1 + deviation,
                    'high': 1 - deviation
                },
                xVal = series.xData,
                yVal = series.yData,
                yValLen = yVal ? yVal.length : 0,
                Zigzag = [],
                xData = [],
                yData = [],
                i, j,
                ZigzagPoint,
                firstZigzagLow,
                firstZigzagHigh,
                directionUp,
                zigZagLen,
                exitLoop = false,
                yIndex = false;

            // Exit if not enught points or no low or high values
            if (
                xVal.length <= 1 ||
                (
                    yValLen &&
                    (
                        yVal[0][lowIndex] === UNDEFINED ||
                        yVal[0][highIndex] === UNDEFINED
                    )
                )
            ) {
                return false;
            }

            // Set first zigzag point candidate
            firstZigzagLow = yVal[0][lowIndex];
            firstZigzagHigh = yVal[0][highIndex];

            // Search for a second zigzag point candidate,
            // this will also set first zigzag point
            for (i = 1; i < yValLen; i++) {
                // requried change to go down
                if (yVal[i][lowIndex] <= firstZigzagHigh * deviations.high) {
                    Zigzag.push([xVal[0], firstZigzagHigh]);
                    // second zigzag point candidate
                    ZigzagPoint = [xVal[i], yVal[i][lowIndex]];
                    // next line will be going up
                    directionUp = true;
                    exitLoop = true;

                    // requried change to go up
                } else if (
                    yVal[i][highIndex] >= firstZigzagLow * deviations.low
                ) {
                    Zigzag.push([xVal[0], firstZigzagLow]);
                    // second zigzag point candidate
                    ZigzagPoint = [xVal[i], yVal[i][highIndex]];
                    // next line will be going down
                    directionUp = false;
                    exitLoop = true;

                }
                if (exitLoop) {
                    xData.push(Zigzag[0][0]);
                    yData.push(Zigzag[0][1]);
                    j = i++;
                    i = yValLen;
                }
            }

            // Search for next zigzags
            for (i = j; i < yValLen; i++) {
                if (directionUp) { // next line up

                    // lower when going down -> change zigzag candidate
                    if (yVal[i][lowIndex] <= ZigzagPoint[1]) {
                        ZigzagPoint = [xVal[i], yVal[i][lowIndex]];
                    }

                    // requried change to go down -> new zigzagpoint and
                    // direction change
                    if (yVal[i][highIndex] >= ZigzagPoint[1] * deviations.low) {
                        yIndex = highIndex;
                    }

                } else { // next line down

                    // higher when going up -> change zigzag candidate
                    if (yVal[i][highIndex] >= ZigzagPoint[1]) {
                        ZigzagPoint = [xVal[i], yVal[i][highIndex]];
                    }

                    // requried change to go down -> new zigzagpoint and
                    // direction change
                    if (yVal[i][lowIndex] <= ZigzagPoint[1] * deviations.high) {
                        yIndex = lowIndex;
                    }
                }
                if (yIndex !== false) { // new zigzag point and direction change
                    Zigzag.push(ZigzagPoint);
                    xData.push(ZigzagPoint[0]);
                    yData.push(ZigzagPoint[1]);
                    ZigzagPoint = [xVal[i], yVal[i][yIndex]];
                    directionUp = !directionUp;

                    yIndex = false;
                }
            }

            zigZagLen = Zigzag.length;

            // no zigzag for last point
            if (
                zigZagLen !== 0 &&
                Zigzag[zigZagLen - 1][0] < xVal[yValLen - 1]
            ) {
                // set last point from zigzag candidate
                Zigzag.push(ZigzagPoint);
                xData.push(ZigzagPoint[0]);
                yData.push(ZigzagPoint[1]);
            }
            return {
                values: Zigzag,
                xData: xData,
                yData: yData
            };
        }
    }
);

/**
 * A `Zig Zag` series. If the [type](#series.zigzag.type) option is not
 * specified, it is inherited from [chart.type](#chart.type).
 *
 * @extends   series,plotOptions.zigzag
 * @since     6.0.0
 * @product   highstock
 * @excluding dataParser, dataURL
 * @apioption series.zigzag
 */