williams-r.src.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 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-2021 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/WilliamsR/WilliamsRIndicator.js', [_modules['Mixins/ReduceArray.js'], _modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (ReduceArrayMixin, SeriesRegistry, U) {
  89. /* *
  90. *
  91. * License: www.highcharts.com/license
  92. *
  93. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  94. *
  95. * */
  96. var __extends = (this && this.__extends) || (function () {
  97. var extendStatics = function (d,
  98. b) {
  99. extendStatics = Object.setPrototypeOf ||
  100. ({ __proto__: [] } instanceof Array && function (d,
  101. b) { d.__proto__ = b; }) ||
  102. function (d,
  103. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  104. return extendStatics(d, b);
  105. };
  106. return function (d, b) {
  107. extendStatics(d, b);
  108. function __() { this.constructor = d; }
  109. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  110. };
  111. })();
  112. var getArrayExtremes = ReduceArrayMixin.getArrayExtremes;
  113. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  114. var extend = U.extend,
  115. isArray = U.isArray,
  116. merge = U.merge;
  117. /**
  118. * The Williams %R series type.
  119. *
  120. * @private
  121. * @class
  122. * @name Highcharts.seriesTypes.williamsr
  123. *
  124. * @augments Highcharts.Series
  125. */
  126. var WilliamsRIndicator = /** @class */ (function (_super) {
  127. __extends(WilliamsRIndicator, _super);
  128. function WilliamsRIndicator() {
  129. var _this = _super !== null && _super.apply(this,
  130. arguments) || this;
  131. _this.data = void 0;
  132. _this.options = void 0;
  133. _this.points = void 0;
  134. return _this;
  135. }
  136. WilliamsRIndicator.prototype.getValues = function (series, params) {
  137. var period = params.period,
  138. xVal = series.xData,
  139. yVal = series.yData,
  140. yValLen = yVal ? yVal.length : 0,
  141. WR = [], // 0- date, 1- Williams %R
  142. xData = [],
  143. yData = [],
  144. slicedY,
  145. close = 3,
  146. low = 2,
  147. high = 1,
  148. extremes,
  149. R,
  150. HH, // Highest high value in period
  151. LL, // Lowest low value in period
  152. CC, // Current close value
  153. i;
  154. // Williams %R requires close value
  155. if (xVal.length < period ||
  156. !isArray(yVal[0]) ||
  157. yVal[0].length !== 4) {
  158. return;
  159. }
  160. // For a N-period, we start from N-1 point, to calculate Nth point
  161. // That is why we later need to comprehend slice() elements list
  162. // with (+1)
  163. for (i = period - 1; i < yValLen; i++) {
  164. slicedY = yVal.slice(i - period + 1, i + 1);
  165. extremes = getArrayExtremes(slicedY, low, high);
  166. LL = extremes[0];
  167. HH = extremes[1];
  168. CC = yVal[i][close];
  169. R = ((HH - CC) / (HH - LL)) * -100;
  170. if (xVal[i]) {
  171. WR.push([xVal[i], R]);
  172. xData.push(xVal[i]);
  173. yData.push(R);
  174. }
  175. }
  176. return {
  177. values: WR,
  178. xData: xData,
  179. yData: yData
  180. };
  181. };
  182. /**
  183. * Williams %R. This series requires the `linkedTo` option to be
  184. * set and should be loaded after the `stock/indicators/indicators.js`.
  185. *
  186. * @sample {highstock} stock/indicators/williams-r
  187. * Williams %R
  188. *
  189. * @extends plotOptions.sma
  190. * @since 7.0.0
  191. * @product highstock
  192. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  193. * pointInterval, pointIntervalUnit, pointPlacement,
  194. * pointRange, pointStart, showInNavigator, stacking
  195. * @requires stock/indicators/indicators
  196. * @requires stock/indicators/williams-r
  197. * @optionparent plotOptions.williamsr
  198. */
  199. WilliamsRIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  200. /**
  201. * Paramters used in calculation of Williams %R series points.
  202. * @excluding index
  203. */
  204. params: {
  205. index: void 0,
  206. /**
  207. * Period for Williams %R oscillator
  208. */
  209. period: 14
  210. }
  211. });
  212. return WilliamsRIndicator;
  213. }(SMAIndicator));
  214. extend(WilliamsRIndicator.prototype, {
  215. nameBase: 'Williams %R'
  216. });
  217. SeriesRegistry.registerSeriesType('williamsr', WilliamsRIndicator);
  218. /* *
  219. *
  220. * Default Export
  221. *
  222. * */
  223. /**
  224. * A `Williams %R Oscillator` series. If the [type](#series.williamsr.type)
  225. * option is not specified, it is inherited from [chart.type](#chart.type).
  226. *
  227. * @extends series,plotOptions.williamsr
  228. * @since 7.0.0
  229. * @product highstock
  230. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  231. * navigatorOptions, pointInterval, pointIntervalUnit,
  232. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  233. * @requires stock/indicators/indicators
  234. * @requires stock/indicators/williams-r
  235. * @apioption series.williamsr
  236. */
  237. ''; // adds doclets above to the transpiled file
  238. return WilliamsRIndicator;
  239. });
  240. _registerModule(_modules, 'masters/indicators/williams-r.src.js', [], function () {
  241. });
  242. }));