@@ -189,8 +189,10 @@ export function getChartType(index: number): 'log' | 'linear' {
189
189
* @param {Indicator[] } baseSeries - The base series of data for comparison.
190
190
* @param {number } smoothValue - The value to be used for smoothing the data.
191
191
* @param {number[] } stepRange - The range of steps to be considered for trimming.
192
+ * @param {boolean } trimSmoothEnds - Whether to trim the smoothed ends of the series.
192
193
*/
193
- export function smoothAndTrimAllCharts ( series : Indicator [ ] , baseSeries : Indicator [ ] , smoothValue : number , stepRange : number [ ] ) {
194
+ export function smoothAndTrimAllCharts ( series : Indicator [ ] , baseSeries : Indicator [ ] , smoothValue : number ,
195
+ stepRange : number [ ] , trimSmoothEnds : boolean = true ) {
194
196
let [ smoothWindow , smoothRange ] = getSmoothWindow ( series ?? [ ] ,
195
197
baseSeries ?? [ ] , smoothValue )
196
198
@@ -199,46 +201,52 @@ export function smoothAndTrimAllCharts(series: Indicator[], baseSeries: Indicato
199
201
s . series = smoothSeries ( s . series , smoothWindow [ 0 ] [ i ] )
200
202
return s
201
203
} )
202
- trimSteps ( series , stepRange [ 0 ] , stepRange [ 1 ] , smoothRange )
204
+ trimSteps ( series , stepRange [ 0 ] , stepRange [ 1 ] , smoothWindow [ 0 ] , trimSmoothEnds )
203
205
}
204
206
205
207
if ( baseSeries != null ) {
206
208
baseSeries . map ( ( s , i ) => {
207
209
s . series = smoothSeries ( s . series , smoothWindow [ 1 ] [ i ] )
208
210
return s
209
211
} )
210
- trimSteps ( baseSeries , stepRange [ 0 ] , stepRange [ 1 ] , smoothRange )
212
+ trimSteps ( baseSeries , stepRange [ 0 ] , stepRange [ 1 ] , smoothWindow [ 1 ] , trimSmoothEnds )
211
213
}
212
214
}
213
215
214
- export function trimSteps ( series : Indicator [ ] , min : number , max : number , smoothRange : number = 0 ) {
215
- smoothRange /= 2 // remove half from each end
216
- series . forEach ( s => {
217
- let localSmoothRange = smoothRange
218
- if ( s . series . length <= 2 ) {
219
- localSmoothRange = 0
220
- } else {
221
- let trimStepCount = localSmoothRange / ( s . series [ 1 ] . step - s . series [ 0 ] . step )
222
- if ( trimStepCount < 1 ) { // not going to trim anything - or else will filter out single steps
223
- localSmoothRange = 0
224
- }
216
+ function trimSteps ( series : Indicator [ ] , min : number , max : number , smoothWindow : number [ ] , trimSmoothEnds : boolean = true ) {
217
+ series . forEach ( ( s , i ) => {
218
+ let localSmoothWindow = smoothWindow [ i ] / 2 // remove half from each end
219
+ localSmoothWindow = Math . floor ( localSmoothWindow )
220
+ if ( localSmoothWindow < 0 ) {
221
+ localSmoothWindow = 0
222
+ }
223
+ if ( s . series . length <= 1 ) {
224
+ localSmoothWindow = 0
225
+ } else if ( smoothWindow [ i ] >= s . series . length ) {
226
+ localSmoothWindow = Math . floor ( s . series . length / 2 )
225
227
}
226
228
229
+
227
230
let localMin = min
228
231
let localMax = max
229
232
230
233
if ( localMin == - 1 ) {
231
234
localMin = s . series [ 0 ] . step
232
235
}
233
- localMin = Math . max ( localMin , s . series [ 0 ] . step + localSmoothRange )
236
+ if ( trimSmoothEnds ) {
237
+ localMin = Math . max ( localMin , s . series [ localSmoothWindow ] . step )
238
+ }
234
239
235
240
if ( localMax == - 1 ) {
236
241
localMax = s . series [ s . series . length - 1 ] . step
237
242
}
238
- localMax = Math . min ( localMax , s . series [ s . series . length - 1 ] . step - localSmoothRange )
243
+ if ( trimSmoothEnds ) {
244
+ localMax = Math . min ( localMax , s . series [ s . series . length - 1 - localSmoothWindow +
245
+ ( s . series . length % 2 == 0 && localSmoothWindow != 0 ? 1 : 0 ) ] . step ) // get the mid value for even length series
246
+ }
239
247
240
- localMin = Math . floor ( localMin )
241
- localMax = Math . ceil ( localMax )
248
+ localMin = Math . floor ( localMin - 1 )
249
+ localMax = Math . ceil ( localMax + 1 )
242
250
243
251
let minIndex = s . series . length - 1
244
252
let maxIndex = 0
@@ -253,25 +261,6 @@ export function trimSteps(series: Indicator[], min: number, max: number, smoothR
253
261
254
262
s . lowTrimIndex = minIndex
255
263
s . highTrimIndex = maxIndex
256
-
257
- if ( s . lowTrimIndex > s . highTrimIndex ) { // if trim covers all just give the middle value
258
- localMax = max == - 1 ? s . series [ s . series . length - 1 ] . step : max
259
- localMin = min == - 1 ? s . series [ 0 ] . step : min
260
-
261
- minIndex = s . series . length - 1
262
- maxIndex = 0
263
-
264
- for ( let i = 0 ; i < s . series . length ; i ++ ) {
265
- let p = s . series [ i ]
266
- if ( p . step >= localMin && p . step <= localMax ) {
267
- minIndex = Math . min ( i , minIndex )
268
- maxIndex = Math . max ( i , maxIndex )
269
- }
270
- }
271
-
272
- s . lowTrimIndex = ( minIndex + maxIndex ) / 2
273
- s . highTrimIndex = ( minIndex + maxIndex ) / 2
274
- }
275
264
} )
276
265
}
277
266
@@ -312,7 +301,9 @@ export function getSmoothWindow(currentSeries: Indicator[], baseSeries: Indicato
312
301
313
302
let stepRange = [ [ ] , [ ] ]
314
303
for ( let s of currentSeries ) {
315
- if ( s . series . length >= 2 && ! s . is_summary ) {
304
+ if ( smoothValue == 100 ) { // hardcode to max range in case not range due to step inconsistencies
305
+ stepRange [ 0 ] . push ( s . series . length )
306
+ } else if ( s . series . length >= 2 && ! s . is_summary ) {
316
307
let stepGap = s . series [ 1 ] . step - s . series [ 0 ] . step
317
308
let numSteps = Math . max ( 1 , Math . floor ( smoothRange / stepGap ) )
318
309
stepRange [ 0 ] . push ( numSteps )
@@ -321,7 +312,9 @@ export function getSmoothWindow(currentSeries: Indicator[], baseSeries: Indicato
321
312
}
322
313
}
323
314
for ( let s of baseSeries ) {
324
- if ( s . series . length >= 2 && ! s . is_summary ) {
315
+ if ( smoothValue == 100 ) {
316
+ stepRange [ 1 ] . push ( s . series . length )
317
+ } else if ( s . series . length >= 2 && ! s . is_summary ) {
325
318
let stepGap = s . series [ 1 ] . step - s . series [ 0 ] . step
326
319
let numSteps = Math . max ( 1 , Math . floor ( smoothRange / stepGap ) )
327
320
stepRange [ 1 ] . push ( numSteps )
0 commit comments