2
0

williams-r.src.js 7.5 KB

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