ao.src.js
6.22 KB
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
/* *
*
* License: www.highcharts.com/license
*
* */
'use strict';
import H from '../parts/Globals.js';
import U from '../parts/Utilities.js';
var isArray = U.isArray;
var correctFloat = H.correctFloat,
noop = H.noop;
/**
* The AO series type
*
* @private
* @class
* @name Highcharts.seriesTypes.ao
*
* @augments Highcharts.Series
*/
H.seriesType(
'ao',
'sma',
/**
* Awesome Oscillator. This series requires the `linkedTo` option to
* be set and should be loaded after the `stock/indicators/indicators.js`
*
* @sample {highstock} stock/indicators/ao
* Awesome
*
* @extends plotOptions.sma
* @since 7.0.0
* @product highstock
* @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
* params, pointInterval, pointIntervalUnit, pointPlacement,
* pointRange, pointStart, showInNavigator, stacking
* @optionparent plotOptions.ao
*/
{
/**
* Color of the Awesome oscillator series bar that is greater than the
* previous one. Note that if a `color` is defined, the `color`
* takes precedence and the `greaterBarColor` is ignored.
*
* @sample {highstock} stock/indicators/ao/
* greaterBarColor
*
* @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
* @since 7.0.0
*/
greaterBarColor: '#06B535',
/**
* Color of the Awesome oscillator series bar that is lower than the
* previous one. Note that if a `color` is defined, the `color`
* takes precedence and the `lowerBarColor` is ignored.
*
* @sample {highstock} stock/indicators/ao/
* lowerBarColor
*
* @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
* @since 7.0.0
*/
lowerBarColor: '#F21313',
threshold: 0,
groupPadding: 0.2,
pointPadding: 0.2,
states: {
hover: {
halo: {
size: 0
}
}
}
},
/**
* @lends Highcharts.Series#
*/
{
nameBase: 'AO',
nameComponents: false,
// Columns support:
markerAttribs: noop,
getColumnMetrics: H.seriesTypes.column.prototype.getColumnMetrics,
crispCol: H.seriesTypes.column.prototype.crispCol,
translate: H.seriesTypes.column.prototype.translate,
drawPoints: H.seriesTypes.column.prototype.drawPoints,
drawGraph: function () {
var indicator = this,
options = indicator.options,
points = indicator.points,
userColor = indicator.userOptions.color,
positiveColor = options.greaterBarColor,
negativeColor = options.lowerBarColor,
firstPoint = points[0],
i;
if (!userColor && firstPoint) {
firstPoint.color = positiveColor;
for (i = 1; i < points.length; i++) {
if (points[i].y > points[i - 1].y) {
points[i].color = positiveColor;
} else if (points[i].y < points[i - 1].y) {
points[i].color = negativeColor;
} else {
points[i].color = points[i - 1].color;
}
}
}
},
getValues: function (series) {
var shortPeriod = 5,
longPeriod = 34,
xVal = series.xData || [],
yVal = series.yData || [],
yValLen = yVal.length,
AO = [], // 0- date, 1- Awesome Oscillator
xData = [],
yData = [],
high = 1,
low = 2,
shortSum = 0,
longSum = 0,
shortSMA, // Shorter Period SMA
longSMA, // Longer Period SMA
awesome,
shortLastIndex,
longLastIndex,
price,
i,
j;
if (
xVal.length <= longPeriod ||
!isArray(yVal[0]) ||
yVal[0].length !== 4
) {
return false;
}
for (i = 0; i < longPeriod - 1; i++) {
price = (yVal[i][high] + yVal[i][low]) / 2;
if (i >= longPeriod - shortPeriod) {
shortSum = correctFloat(shortSum + price);
}
longSum = correctFloat(longSum + price);
}
for (j = longPeriod - 1; j < yValLen; j++) {
price = (yVal[j][high] + yVal[j][low]) / 2;
shortSum = correctFloat(shortSum + price);
longSum = correctFloat(longSum + price);
shortSMA = shortSum / shortPeriod;
longSMA = longSum / longPeriod;
awesome = correctFloat(shortSMA - longSMA);
AO.push([xVal[j], awesome]);
xData.push(xVal[j]);
yData.push(awesome);
shortLastIndex = j + 1 - shortPeriod;
longLastIndex = j + 1 - longPeriod;
shortSum = correctFloat(
shortSum -
(yVal[shortLastIndex][high] + yVal[shortLastIndex][low]) / 2
);
longSum = correctFloat(
longSum -
(yVal[longLastIndex][high] + yVal[longLastIndex][low]) / 2
);
}
return {
values: AO,
xData: xData,
yData: yData
};
}
}
);
/**
* An `AO` series. If the [type](#series.ao.type)
* option is not specified, it is inherited from [chart.type](#chart.type).
*
* @extends series,plotOptions.ao
* @since 7.0.0
* @product highstock
* @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
* navigatorOptions, pointInterval, pointIntervalUnit,
* pointPlacement, pointRange, pointStart, showInNavigator, stacking
* @apioption series.ao
*/