Skip to content

Commit

Permalink
Add setCompatibility function
Browse files Browse the repository at this point in the history
  • Loading branch information
shixiongfei committed Jun 8, 2024
1 parent 0756914 commit df5616c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

export * from "./types.js";
import { FuncUnstId } from "./types.js";
import { Compatibility, FuncUnstId } from "./types.js";

export type InputParameterType = "price" | "real" | "integer";

Expand Down Expand Up @@ -103,6 +103,9 @@ export declare function setUnstablePeriod(
period: number
): void;

/** Set compatibility function */
export declare function setCompatibility(value: Compatibility): void;

/** Get function infomation */
export declare function explain(funcName: string): FuncInfo;

Expand Down
33 changes: 33 additions & 0 deletions src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* https://github.com/shixiongfei/napi-talib
*/

#include "ta_func.h"
#include "ta_abstract.h"
#include "ta_utility.h"
#include <vector>
Expand Down Expand Up @@ -320,6 +321,37 @@ static napi_value setUnstablePeriod(napi_env env, napi_callback_info info) {
return undefined;
}

static napi_value setCompatibility(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value argv, undefined;
napi_valuetype valuetype;
TA_RetCode retCode;
TA_Compatibility value;

CHECK(napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr));
CHECK(napi_get_undefined(env, &undefined));

CHECK(napi_typeof(env, argv, &valuetype));

if (valuetype != napi_number) {
napi_throw_type_error(env, nullptr, "The argument must be a Integer");
return undefined;
}

CHECK(napi_get_value_int32(env, argv, (int *)&value));

if (TA_SUCCESS != (retCode = TA_SetCompatibility(value))) {
napi_value error;

CHECK(createTAError(env, retCode, &error));
CHECK(napi_throw(env, error));

return undefined;
}

return undefined;
}

static napi_value explain(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value argv, undefined, object, inputs, optInputs, outputs;
Expand Down Expand Up @@ -994,6 +1026,7 @@ static napi_value init(napi_env env, napi_value exports) {
DECLARE_NAPI_METHOD(getFunctionGroups),
DECLARE_NAPI_METHOD(getFunctions),
DECLARE_NAPI_METHOD(setUnstablePeriod),
DECLARE_NAPI_METHOD(setCompatibility),
DECLARE_NAPI_METHOD(explain),
DECLARE_NAPI_METHOD(execute),
DECLARE_NAPI_METHOD(version),
Expand Down
31 changes: 24 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
const fs = require("node:fs");
const talib = require(".");
const { ADX } = require("./functions");
const { SMA } = require("./promises.js");
const { SMA, EMA } = require("./promises.js");

/*
* In real projects, it can be written like this
Expand All @@ -33,11 +33,14 @@ console.log(talib.getFunctionGroups());

console.log(talib.getFunctions());

console.log(talib.setUnstablePeriod(talib.FuncUnstId.EMA, 14));
console.log(talib.setUnstablePeriod(talib.FuncUnstId.MAMA, 14));

console.log(talib.setCompatibility(talib.Compatibility.METASTOCK));

console.log(talib.explain("ADX"));

console.log(
// Synchronous call
talib.execute({
name: "ADX",
startIdx: 0,
Expand All @@ -52,23 +55,37 @@ console.log(
);

console.log(
// Synchronous call
ADX(marketData.high, marketData.low, marketData.close, {
optInTimePeriod: 9,
})
);

console.log(
talib.execute({
// Asynchronous call
talib.execute(
{
name: "SMA",
startIdx: 0,
endIdx: marketData.close.length - 1,
params: {
inReal: marketData.close,
optInTimePeriod: 180,
},
})
},
(error, results) => {
if (error) {
console.error(error.message);
} else {
console.log(results);
}
}
);

SMA(marketData.close, { optInTimePeriod: 180 }).then((sma) => {
console.log(sma);
// Parallel computing
Promise.all([
SMA(marketData.close, { optInTimePeriod: 5 }),
EMA(marketData.close, { optInTimePeriod: 5 }),
]).then(([sma, ema]) => {
console.log("SMA:", sma);
console.log("EMA:", ema);
});
5 changes: 5 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ export enum FuncUnstId {
T3,
ALL,
}

export enum Compatibility {
DEFAULT,
METASTOCK,
}

0 comments on commit df5616c

Please sign in to comment.