Skip to content

Commit c11d8b3

Browse files
committed
doc(readme): more detailed example
1 parent 8379a52 commit c11d8b3

3 files changed

Lines changed: 108 additions & 67 deletions

File tree

README.md

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# talib-binding
22

3-
The [TA-Lib](http://ta-lib.org/) sync bindings.
3+
A synchronous [TA-Lib](http://ta-lib.org/) bindings for Node.js & TypeScript.
44

55
## Install
66

@@ -10,73 +10,68 @@ yarn add talib-binding
1010

1111
## Usage
1212

13-
All the functions are exported, you can call it in the same form to [c api](https://ta-lib.org/d_api/d_api.html), but
14-
there are some difference:
15-
16-
- the `startIdx` and `endIdx` is placed at the end of the function signature, and is optional.
17-
- the return value is the computed result, if result is one array, will be it, else will be a nested array to contains all output arrays.
18-
- if the ta-lib function returns a error code, will throw a error.
19-
20-
Additionally, you can pass a `Record[]` to extract its fields rather than input double array such as `inHigh`, `inLow`, etc.
21-
And if the input field is a implicit field of the records, you need to input a string to specifying which one field will be extract
22-
as it.
13+
1. All the functions are exported, you can call it in the same form to [c api](https://ta-lib.org/d_api/d_api.html), but there are some difference:
14+
- the `startIdx` and `endIdx` is placed at the end of the function signature, and is optional.
15+
- the return value is the computed result, if result is one array, will be it, else will be a nested array to contains all output arrays.
16+
- if the ta-lib function returns a error code, will throw a error.
17+
2. Additionally, you can pass a `Record[]` to extract its fields rather than input double array such as `inHigh`, `inLow`, etc. And if the input field is a implicit field of the records, you need to input a string to specifying which one field will be extract as it.
18+
3. The types for TypeScript is generated, you can use this it in TypeScript projects.
2319

2420
## Example
2521

26-
```typescript
27-
import * as talib from './src/talib-binding.generated'
28-
29-
// Input double array directly:
30-
let output = talib.SAR(
31-
[2, 3, 4, 5], /* inHigh */
32-
[1, 2, 3, 4], /* inLow */
33-
0.02, /* optAcceleration_Factor, optional */
34-
0.2, /* optAF_Maximum, optional */
35-
0, /* startIdx, optional */
36-
3 /* endIdx, optional */
37-
)
38-
console.log(output)
39-
40-
// Input a records array:
41-
output = talib.SAR([
42-
{
43-
Time: 0,
44-
Open: 1,
45-
High: 2,
46-
Low: 1,
47-
Close: 2,
48-
Volume: 1,
49-
},
50-
{
51-
Time: 0,
52-
Open: 2,
53-
High: 3,
54-
Low: 2,
55-
Close: 3,
56-
Volume: 1,
57-
},
58-
])
59-
60-
// Input implicit field name with Record[]
61-
output = talib.COS([
62-
{
63-
Time: 0,
64-
Open: 1,
65-
High: 2,
66-
Low: 1,
67-
Close: 2,
68-
Volume: 1,
69-
},
70-
{
71-
Time: 0,
72-
Open: 2,
73-
High: 3,
74-
Low: 2,
75-
Close: 3,
76-
Volume: 1,
77-
},
78-
], 'Volume')
79-
```
22+
- call in the same form of `TA-Lib`:
23+
```typescript
24+
// import * as talib from 'talib-binding'
25+
import * as talib from './src/talib-binding.generated'
26+
27+
talib.SAR(
28+
[2, 3, 4, 5], /* inHigh */
29+
[1, 2, 3, 4], /* inLow */
30+
0.02, /* optAcceleration_Factor, optional */
31+
0.2, /* optAF_Maximum, optional */
32+
0, /* startIdx, optional */
33+
3 /* endIdx, optional */
34+
)
35+
```
36+
- pass a `Record` array as the first parameter, the library will extract the field value automatically, if the function contains some implicit parameter name, you need to pass the name string to extract it. The implicit parameter means that the param is not one of `High`, `Low`, `Open`, `Close`, and `Volume`, just like `inReal`, more detailed information could be found in the TypeScript function signatures.
37+
```typescript
38+
// import * as talib from 'talib-binding'
39+
import * as talib from './src/talib-binding.generated'
40+
talib.SAR([
41+
{Time: 0, Open: 1, High: 2, Low: 1, Close: 2, Volume: 1},
42+
{Time: 0, Open: 2, High: 3, Low: 2, Close: 3, Volume: 1},
43+
{Time: 0, Open: 3, High: 4, Low: 3, Close: 4, Volume: 1},
44+
{Time: 0, Open: 4, High: 5, Low: 4, Close: 5, Volume: 1},
45+
])
46+
// The COS function contains implicit parameter name, you need to call it as follow:
47+
talib.COS([
48+
{Time: 0, Open: 1, High: 2, Low: 1, Close: 2, Volume: 1},
49+
{Time: 0, Open: 2, High: 3, Low: 2, Close: 3, Volume: 1},
50+
{Time: 0, Open: 3, High: 4, Low: 3, Close: 4, Volume: 1},
51+
{Time: 0, Open: 4, High: 5, Low: 4, Close: 5, Volume: 1},
52+
], 'Volume')
53+
```
54+
- if function return a single array, the return value will be it, else will be a nested array.
55+
```typescript
56+
// import * as talib from 'talib-binding'
57+
import * as talib from './src/talib-binding.generated'
58+
const outReal = talib.SAR([2, 3, 4, 5], [1, 2, 3, 4])
59+
console.log(outReal)
60+
// [ 1, 1.04, 1.1584 ]
61+
62+
const [outUp, outMid, outLow] = talib.ACCBANDS([2, 3, 4, 5], [1, 2, 3, 4], [2, 3, 4, 5], 3)
63+
console.log(outUp, outMid, outLow)
64+
// [ 5.45079365079365, 6.302645502645503 ]
65+
// [ 3, 4 ]
66+
// [ 0.4507936507936508, 1.3026455026455028 ]
67+
```
68+
- **if TA function returns a error, OR startIdx/endIdx out of range, will throw it**:
69+
```typescript
70+
// import * as talib from 'talib-binding'
71+
import * as talib from './src/talib-binding.generated'
72+
talib.ACCBANDS([2, 3, 4, 5], [1, 2, 3, 4], [2, 3, 4, 5], 3)
73+
// throw RangeError: `startIdx` or `endIdx` out of range
74+
```
8075

8176
## Notes
8277

src/generate.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {parseString} from "xml2js"
1414
import {OptionalInputArgument, OutputArgument, RequiredInputArgument, TaFuncApiXml} from "./ta_func_api.generated"
1515
import {createMap, IMap} from "known-types"
1616
import G = require("glob")
17+
import {Arguments} from "yargs"
1718

1819
interface Json2ts {
1920
convertObjectToTsInterfaces<T>(content: T, name?: string): string;
@@ -227,7 +228,7 @@ export const TA_MATypes: IMap<string> = createMap({
227228
8: 'TA_MAType_T3',
228229
})
229230

230-
export function generateBindings() {
231+
export function generateBindings({_}: Arguments) {
231232
const body = new ContentBuilder()
232233
body.normal(
233234
'/*!',
@@ -248,7 +249,9 @@ export function generateBindings() {
248249
.normal('return;')
249250
.undent('}')
250251
const config: TaFuncApiXml = require('./ta_func_api.generated.json')
251-
config.FinancialFunctions.FinancialFunction.filter((func) => func.Abbreviation[0] === 'SAR' || 1).forEach((func) => {
252+
config.FinancialFunctions.FinancialFunction.filter(
253+
(func) => _.length === 0 || _.indexOf(func.Abbreviation[0]) > -1
254+
).forEach((func) => {
252255
const name = func.Abbreviation[0]
253256
const required: RequiredInputArgument[] = func.RequiredInputArguments
254257
? func.RequiredInputArguments.reduce((prev, curr) => {
@@ -270,6 +273,7 @@ export function generateBindings() {
270273
if (output.length === 1) {
271274
outInitJS.push(`outAll_JS = ${jsOutName(output[0])};`)
272275
} else {
276+
outInitJS.push(`outAll_JS = Nan::New<v8::Array>(${output.length});`)
273277
output.forEach((argv, index) => {
274278
outInitJS.push(`outAll_JS->Set(${index}, ${jsOutName(argv)});`)
275279
})

0 commit comments

Comments
 (0)