williams-r.src.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  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/ReduceArray.js', [], function () {
  32. /**
  33. *
  34. * (c) 2010-2020 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 reduceArrayMixin = {
  42. /**
  43. * Get min value of array filled by OHLC data.
  44. * @private
  45. * @param {Array<*>} arr Array of OHLC points (arrays).
  46. * @param {string} index Index of "low" value in point array.
  47. * @return {number} Returns min value.
  48. */
  49. minInArray: function (arr,
  50. index) {
  51. return arr.reduce(function (min,
  52. target) {
  53. return Math.min(min,
  54. target[index]);
  55. }, Number.MAX_VALUE);
  56. },
  57. /**
  58. * Get max value of array filled by OHLC data.
  59. * @private
  60. * @param {Array<*>} arr Array of OHLC points (arrays).
  61. * @param {string} index Index of "high" value in point array.
  62. * @return {number} Returns max value.
  63. */
  64. maxInArray: function (arr, index) {
  65. return arr.reduce(function (max, target) {
  66. return Math.max(max, target[index]);
  67. }, -Number.MAX_VALUE);
  68. },
  69. /**
  70. * Get extremes of array filled by OHLC data.
  71. * @private
  72. * @param {Array<*>} arr Array of OHLC points (arrays).
  73. * @param {string} minIndex Index of "low" value in point array.
  74. * @param {string} maxIndex Index of "high" value in point array.
  75. * @return {Array<number,number>} Returns array with min and max value.
  76. */
  77. getArrayExtremes: function (arr, minIndex, maxIndex) {
  78. return arr.reduce(function (prev, target) {
  79. return [
  80. Math.min(prev[0], target[minIndex]),
  81. Math.max(prev[1], target[maxIndex])
  82. ];
  83. }, [Number.MAX_VALUE, -Number.MAX_VALUE]);
  84. }
  85. };
  86. return reduceArrayMixin;
  87. });
  88. _registerModule(_modules, 'Stock/Indicators/WilliamsRIndicator.js', [_modules['Core/Utilities.js'], _modules['Mixins/ReduceArray.js']], function (U, reduceArrayMixin) {
  89. /* *
  90. *
  91. * License: www.highcharts.com/license
  92. *
  93. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  94. *
  95. * */
  96. var isArray = U.isArray,
  97. seriesType = U.seriesType;
  98. var getArrayExtremes = reduceArrayMixin.getArrayExtremes;
  99. /**
  100. * The Williams %R series type.
  101. *
  102. * @private
  103. * @class
  104. * @name Highcharts.seriesTypes.williamsr
  105. *
  106. * @augments Highcharts.Series
  107. */
  108. seriesType('williamsr', 'sma',
  109. /**
  110. * Williams %R. This series requires the `linkedTo` option to be
  111. * set and should be loaded after the `stock/indicators/indicators.js`.
  112. *
  113. * @sample {highstock} stock/indicators/williams-r
  114. * Williams %R
  115. *
  116. * @extends plotOptions.sma
  117. * @since 7.0.0
  118. * @product highstock
  119. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  120. * pointInterval, pointIntervalUnit, pointPlacement,
  121. * pointRange, pointStart, showInNavigator, stacking
  122. * @requires stock/indicators/indicators
  123. * @requires stock/indicators/williams-r
  124. * @optionparent plotOptions.williamsr
  125. */
  126. {
  127. /**
  128. * Paramters used in calculation of Williams %R series points.
  129. * @excluding index
  130. */
  131. params: {
  132. /**
  133. * Period for Williams %R oscillator
  134. */
  135. period: 14
  136. }
  137. },
  138. /**
  139. * @lends Highcharts.Series#
  140. */
  141. {
  142. nameBase: 'Williams %R',
  143. getValues: function (series, params) {
  144. var period = params.period,
  145. xVal = series.xData,
  146. yVal = series.yData,
  147. yValLen = yVal ? yVal.length : 0,
  148. WR = [], // 0- date, 1- Williams %R
  149. xData = [],
  150. yData = [],
  151. slicedY,
  152. close = 3,
  153. low = 2,
  154. high = 1,
  155. extremes,
  156. R,
  157. HH, // Highest high value in period
  158. LL, // Lowest low value in period
  159. CC, // Current close value
  160. i;
  161. // Williams %R requires close value
  162. if (xVal.length < period ||
  163. !isArray(yVal[0]) ||
  164. yVal[0].length !== 4) {
  165. return;
  166. }
  167. // For a N-period, we start from N-1 point, to calculate Nth point
  168. // That is why we later need to comprehend slice() elements list
  169. // with (+1)
  170. for (i = period - 1; i < yValLen; i++) {
  171. slicedY = yVal.slice(i - period + 1, i + 1);
  172. extremes = getArrayExtremes(slicedY, low, high);
  173. LL = extremes[0];
  174. HH = extremes[1];
  175. CC = yVal[i][close];
  176. R = ((HH - CC) / (HH - LL)) * -100;
  177. if (xVal[i]) {
  178. WR.push([xVal[i], R]);
  179. xData.push(xVal[i]);
  180. yData.push(R);
  181. }
  182. }
  183. return {
  184. values: WR,
  185. xData: xData,
  186. yData: yData
  187. };
  188. }
  189. });
  190. /**
  191. * A `Williams %R Oscillator` series. If the [type](#series.williamsr.type)
  192. * option is not specified, it is inherited from [chart.type](#chart.type).
  193. *
  194. * @extends series,plotOptions.williamsr
  195. * @since 7.0.0
  196. * @product highstock
  197. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  198. * navigatorOptions, pointInterval, pointIntervalUnit,
  199. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  200. * @requires stock/indicators/indicators
  201. * @requires stock/indicators/williams-r
  202. * @apioption series.williamsr
  203. */
  204. ''; // adds doclets above to the transpiled file
  205. });
  206. _registerModule(_modules, 'masters/indicators/williams-r.src.js', [], function () {
  207. });
  208. }));