Skip to content

Commit

Permalink
Merge pull request #114 from cinar:cinar/issue111
Browse files Browse the repository at this point in the history
Volume Weighted Moving Average (VWMA)
  • Loading branch information
cinar authored Jul 3, 2022
2 parents 22fa1dd + 2d10204 commit cea5a66
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 118 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The following list of indicators are currently supported by this package:
- [Triangular Moving Average (TRIMA)](src/indicator/trend/index.md#triangular-moving-average-trima)
- [Triple Exponential Average (TRIX)](#triple-exponential-average-trix)
- [Typical Price](src/indicator/trend/index.md#typical-price)
- [Volume Weighted Moving Average (VWMA)](src/indicator/trend/index.md#volume-weighted-moving-average-vwma)
- [Vortex Indicator](src/indicator/trend/index.md#vortex-indicator)

### Momentum Indicators
Expand Down
Empty file added npm
Empty file.
119 changes: 2 additions & 117 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "indicatorts",
"version": "1.0.9",
"version": "1.0.10",
"description": "Stock technical indicators and strategies in TypeScript for browser and server programs.",
"main": "dist/index.js",
"module": "dist/index.es.js",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export * from './indicator/trend/tema';
export * from './indicator/trend/trima';
export * from './indicator/trend/trix';
export * from './indicator/trend/typicalPrice';
export * from './indicator/trend/vwma';
export * from './indicator/trend/vortex';
export * from './indicator/volatility/accelerationBands';
export * from './indicator/volatility/atr';
Expand Down Expand Up @@ -83,6 +84,7 @@ export * from './strategy/trend/kdjStrategy';
export * from './strategy/trend/macdStrategy';
export * from './strategy/trend/parabolicSarStrategy';
export * from './strategy/trend/typicalPriceStrategy';
export * from './strategy/trend/vwmaStrategy';
export * from './strategy/trend/vortexStrategy';
export * from './strategy/volatility/accelerationBandsStrategy';
export * from './strategy/volatility/bollingerBandsStrategy';
Expand Down
15 changes: 15 additions & 0 deletions src/indicator/trend/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Trend indicators measure the direction and strength of a trend.
- [Triangular Moving Average (TRIMA)](#triangular-moving-average-trima)
- [Triple Exponential Average (TRIX)](#triple-exponential-average-trix)
- [Typical Price](#typical-price)
- [Volume Weighted Moving Average (VWMA)](#volume-weighted-moving-average-vwma)
- [Vortex Indicator](#vortex-indicator)

#### Absolute Price Oscillator (APO)
Expand Down Expand Up @@ -384,6 +385,20 @@ import {typicalPrice} from 'indicatorts';
const result = typicalPrice(highs, lows, closings);
```

#### Volume Weighted Moving Average (VWMA)

The [vwma](./vwma.ts) function calculates the Volume Weighted Moving Average (VWMA) averaging the price data with an emphasis on volume, meaning areas with higher volume will have a greater weight.

```
VWMA = Sum(Price * Volume) / Sum(Volume) for a given Period.
```

```TypeScript
import {vwma} from 'indicatorts';

const result = vwma(period, closings, volumes);
```

#### Vortex Indicator

The [vortex](./vortex.ts) function provides two oscillators that capture positive and negative trend movement. A bullish signal triggers when the positive trend indicator crosses above the negative trend indicator or a key level. A bearish signal triggers when the negative trend indicator crosses above the positive trend indicator or a key level.
Expand Down
27 changes: 27 additions & 0 deletions src/indicator/trend/vwma.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts

import { deepStrictEqual } from 'assert';
import { roundDigitsAll } from '../../helper/numArray';
import { defaultVwma, vwma } from './vwma';

describe('Volume Weighted Moving Average', () => {
it('should be able to compute VWMA', () => {
const closings = [20, 21, 21, 19, 16];
const volumes = [100, 50, 40, 50, 100];
const expected = [20, 20.33, 20.47, 20.29, 17.84];
const period = 3;

const actual = vwma(period, closings, volumes);
deepStrictEqual(roundDigitsAll(2, actual), expected);
});

it('should be able to compute default', () => {
const closings = [20, 21, 21, 19, 16];
const volumes = [100, 50, 40, 50, 100];
const expected = [20, 20.33, 20.47, 20.17, 18.94];

const actual = defaultVwma(closings, volumes);
deepStrictEqual(roundDigitsAll(2, actual), expected);
});
});
42 changes: 42 additions & 0 deletions src/indicator/trend/vwma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts

import { divide, multiply } from '../../helper/numArray';
import { msum } from './msum';

/** Default VWMA period value. */
export const DEFAULT_VWMA_PERIOD = 20;

/**
* The vwma function calculates the Volume Weighted Moving Average (VWMA)
* averaging the price data with an emphasis on volume, meaning areas
* with higher volume will have a greater weight.
*
* VWMA = Sum(Price * Volume) / Sum(Volume) for a given Period.
*
* @param period period value.
* @param closings asset closings.
* @param volumes asset volumes.
* @returns vwma values.
*/
export function vwma(
period: number,
closings: number[],
volumes: number[]
): number[] {
return divide(
msum(period, multiply(closings, volumes)),
msum(period, volumes)
);
}

/**
* The defaultVwma function calculates VWMA with a period of 20.
*
* @param closings asset closings.
* @param volumes asset volumes.
* @returns vwma values.
*/
export function defaultVwma(closings: number[], volumes: number[]): number[] {
return vwma(DEFAULT_VWMA_PERIOD, closings, volumes);
}
18 changes: 18 additions & 0 deletions src/strategy/trend/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ import {typicalPriceStrategy} from 'indicatorts';
const actions = typicalPriceStrategy(asset);
```

#### Volume Weighted Moving Average (VWMA) Strategy

The [vwmaStrategy](./vwmaStrategy.ts) function uses SMA and VWMA indicators to provide a _BUY_ action when VWMA is above SMA, and a _SELL_ signal when VWMA is below SMA, a _HOLD_ signal otherwse.

```TypeScript
import { vwmaStrategy } from 'indicatorts';

const actions = vwmaStrategy(period, asset);
```

The [defaultVwmaStrategy](./vwmaStrategy.ts) function can be used with the default period of 20.

```TypeScript
import { defaultVwmaStrategy } from 'indicatorts';

const actions = defaultVwmaStrategy(asset);
```

#### Vortex Strategy

The [vortexStrategy](./vortexStrategy.ts) uses the values that are generated by the [Vortex Indicator](../../indicator/trend/index.md#vortex-indicator) indicator function to provide a _BUY_ action when the _plusVi_ is greather than the _minusVi_, and _SELL_ action when the _plusVi_ is less than the _minusVi_, and _HOLD_ action when the _plusVi_ is equal to the _minusVi_.
Expand Down
31 changes: 31 additions & 0 deletions src/strategy/trend/vwmaStrategy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts

import { deepStrictEqual } from 'assert';
import { Asset } from '../asset';
import { Action } from '../action';
import { wvmaStrategy } from './vwmaStrategy';

describe('VWMA stategy', () => {
it('should be able to compute strategy', () => {
const asset: Asset = {
dates: [],
openings: [],
highs: [],
lows: [],
closings: [20, 21, 21, 19, 16],
volumes: [100, 50, 40, 50, 100],
};
const expected = [
Action.HOLD,
Action.SELL,
Action.SELL,
Action.SELL,
Action.SELL,
];
const period = 3;

const actual = wvmaStrategy(period, asset);
deepStrictEqual(actual, expected);
});
});
Loading

0 comments on commit cea5a66

Please sign in to comment.