Skip to content

Commit

Permalink
Fix On Balance Volume (OBV) calculation. (#428)
Browse files Browse the repository at this point in the history
# Describe Request

Fix On Balance Volume (OBV)  calculation.

Fixed #426 

# Change Type

Bug Fix
  • Loading branch information
cinar authored Mar 3, 2024
1 parent 9ea77f5 commit 726ed61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/indicator/volume/onBalanceVolume.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts

import { roundDigitsAll } from '../../index';
import { onBalanceVolume } from './onBalanceVolume';

describe('On Balance Volume (OBV)', () => {
it('should be able to compute OBV', () => {
const closings = [9, 11, 7, 10, 8];
const volumes = [100, 110, 80, 120, 90];
const expected = [0, 110, 30, 150, 60];

const actual = onBalanceVolume(closings, volumes);
expect(roundDigitsAll(2, actual)).toStrictEqual(expected);
});
});
16 changes: 7 additions & 9 deletions src/indicator/volume/onBalanceVolume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ export function onBalanceVolume(

const result = new Array<number>(closings.length);

result[0] = 0;

for (let i = 1; i < result.length; i++) {
if (i === 0) {
result[i] = 0;
} else {
result[i] = result[i - 1];
result[i] = result[i - 1];

if (closings[i] > closings[i - 1]) {
result[i] += volumes[i];
} else if (closings[i] < closings[i - 1]) {
result[i] -= volumes[i];
}
if (closings[i] > closings[i - 1]) {
result[i] += volumes[i];
} else if (closings[i] < closings[i - 1]) {
result[i] -= volumes[i];
}
}

Expand Down

0 comments on commit 726ed61

Please sign in to comment.