williams-r.src.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @license Highstock JS v8.0.0 (2019-12-10)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Wojciech Chmiel
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/indicators/williams-r', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'mixins/reduce-array.js', [_modules['parts/Globals.js']], function (H) {
  32. /**
  33. *
  34. * (c) 2010-2019 Pawel Fus & Daniel Studencki
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var reduce = H.reduce;
  42. var reduceArrayMixin = {
  43. /**
  44. * Get min value of array filled by OHLC data.
  45. * @private
  46. * @param {Array<*>} arr Array of OHLC points (arrays).
  47. * @param {string} index Index of "low" value in point array.
  48. * @return {number} Returns min value.
  49. */
  50. minInArray: function (arr, index) {
  51. return reduce(arr, function (min, target) {
  52. return Math.min(min, target[index]);
  53. }, Number.MAX_VALUE);
  54. },
  55. /**
  56. * Get max value of array filled by OHLC data.
  57. * @private
  58. * @param {Array<*>} arr Array of OHLC points (arrays).
  59. * @param {string} index Index of "high" value in point array.
  60. * @return {number} Returns max value.
  61. */
  62. maxInArray: function (arr, index) {
  63. return reduce(arr, function (max, target) {
  64. return Math.max(max, target[index]);
  65. }, -Number.MAX_VALUE);
  66. },
  67. /**
  68. * Get extremes of array filled by OHLC data.
  69. * @private
  70. * @param {Array<*>} arr Array of OHLC points (arrays).
  71. * @param {string} minIndex Index of "low" value in point array.
  72. * @param {string} maxIndex Index of "high" value in point array.
  73. * @return {Array<number,number>} Returns array with min and max value.
  74. */
  75. getArrayExtremes: function (arr, minIndex, maxIndex) {
  76. return reduce(arr, function (prev, target) {
  77. return [
  78. Math.min(prev[0], target[minIndex]),
  79. Math.max(prev[1], target[maxIndex])
  80. ];
  81. }, [Number.MAX_VALUE, -Number.MAX_VALUE]);
  82. }
  83. };
  84. return reduceArrayMixin;
  85. });
  86. _registerModule(_modules, 'indicators/williams-r.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['mixins/reduce-array.js']], function (H, U, reduceArrayMixin) {
  87. /* *
  88. *
  89. * License: www.highcharts.com/license
  90. *
  91. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  92. *
  93. * */
  94. var isArray = U.isArray;
  95. var getArrayExtremes = reduceArrayMixin.getArrayExtremes;
  96. /**
  97. * The Williams %R series type.
  98. *
  99. * @private
  100. * @class
  101. * @name Highcharts.seriesTypes.williamsr
  102. *
  103. * @augments Highcharts.Series
  104. */
  105. H.seriesType('williamsr', 'sma',
  106. /**
  107. * Williams %R. This series requires the `linkedTo` option to be
  108. * set and should be loaded after the `stock/indicators/indicators.js`.
  109. *
  110. * @sample {highstock} stock/indicators/williams-r
  111. * Williams %R
  112. *
  113. * @extends plotOptions.sma
  114. * @since 7.0.0
  115. * @product highstock
  116. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  117. * pointInterval, pointIntervalUnit, pointPlacement,
  118. * pointRange, pointStart, showInNavigator, stacking
  119. * @requires stock/indicators/indicators
  120. * @requires stock/indicators/williams-r
  121. * @optionparent plotOptions.williamsr
  122. */
  123. {
  124. /**
  125. * Paramters used in calculation of Williams %R series points.
  126. * @excluding index
  127. */
  128. params: {
  129. /**
  130. * Period for Williams %R oscillator
  131. */
  132. period: 14
  133. }
  134. },
  135. /**
  136. * @lends Highcharts.Series#
  137. */
  138. {
  139. nameBase: 'Williams %R',
  140. getValues: function (series, params) {
  141. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, WR = [], // 0- date, 1- Williams %R
  142. xData = [], yData = [], slicedY, close = 3, low = 2, high = 1, extremes, R, HH, // Highest high value in period
  143. LL, // Lowest low value in period
  144. CC, // Current close value
  145. i;
  146. // Williams %R requires close value
  147. if (xVal.length < period ||
  148. !isArray(yVal[0]) ||
  149. yVal[0].length !== 4) {
  150. return;
  151. }
  152. // For a N-period, we start from N-1 point, to calculate Nth point
  153. // That is why we later need to comprehend slice() elements list
  154. // with (+1)
  155. for (i = period - 1; i < yValLen; i++) {
  156. slicedY = yVal.slice(i - period + 1, i + 1);
  157. extremes = getArrayExtremes(slicedY, low, high);
  158. LL = extremes[0];
  159. HH = extremes[1];
  160. CC = yVal[i][close];
  161. R = ((HH - CC) / (HH - LL)) * -100;
  162. if (xVal[i]) {
  163. WR.push([xVal[i], R]);
  164. xData.push(xVal[i]);
  165. yData.push(R);
  166. }
  167. }
  168. return {
  169. values: WR,
  170. xData: xData,
  171. yData: yData
  172. };
  173. }
  174. });
  175. /**
  176. * A `Williams %R Oscillator` series. If the [type](#series.williamsr.type)
  177. * option is not specified, it is inherited from [chart.type](#chart.type).
  178. *
  179. * @extends series,plotOptions.williamsr
  180. * @since 7.0.0
  181. * @product highstock
  182. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  183. * navigatorOptions, pointInterval, pointIntervalUnit,
  184. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  185. * @requires stock/indicators/indicators
  186. * @requires stock/indicators/williams-r
  187. * @apioption series.williamsr
  188. */
  189. ''; // adds doclets above to the transpiled file
  190. });
  191. _registerModule(_modules, 'masters/indicators/williams-r.src.js', [], function () {
  192. });
  193. }));