Skip to content

Commit

Permalink
feat: add useParam* hooks and encodePath, decodePath (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
zccz14 authored Dec 21, 2023
1 parent 837ab3c commit 4bb6ee1
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 59 deletions.
4 changes: 2 additions & 2 deletions @libs/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export const global = {
useMemoAsync,
useAccountInfo,
useLog,
useParamProduct,
useParamOHLC,
useProduct,
useOHLC,
decodePath,
encodePath,
useRecordTable,
useExchange,
useSeries,
Expand Down
2 changes: 0 additions & 2 deletions @libs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const useMemo = global.useMemo;
export const useMemoAsync = global.useMemoAsync;
export const useAccountInfo = global.useAccountInfo;
export const useLog = global.useLog;
export const useParamProduct = global.useParamProduct;
export const useParamOHLC = global.useParamOHLC;
export const useProduct = global.useProduct;
export const useOHLC = global.useOHLC;
export const useRecordTable = global.useRecordTable;
Expand Down
2 changes: 2 additions & 0 deletions @libs/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export * from "./useCounterParty";
export * from "./useDelay";
export * from "./useParamBoolean";
export * from "./useParamNumber";
export * from "./useParamOHLC";
export * from "./useParamProduct";
export * from "./useParamString";
export * from "./usePositionLimit";
export * from "./useRuleEffect";
Expand Down
19 changes: 19 additions & 0 deletions @libs/utils/useParamOHLC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Use OHLC with parameter
*/
export const useParamOHLC = (key: string) => {
const OHLCKey = useParamSchema<string>(key, {
type: "string",
format: "OHLC-key",
});
const { datasource_id, product_id, period_in_sec } = useMemo(() => {
const [datasource_id = "", product_id = "", _period_in_sec] = decodePath(
OHLCKey || ""
);
const period_in_sec = +_period_in_sec;
return { datasource_id, product_id, period_in_sec };
}, []);

const periods = useOHLC(datasource_id, product_id, period_in_sec);
return { datasource_id, product_id, period_in_sec, ...periods };
};
16 changes: 16 additions & 0 deletions @libs/utils/useParamProduct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Use parameter as Product
* @param key - parameter's name
*/
export const useParamProduct = (key: string): IProduct => {
const productKey = useParamSchema<string>(key, {
type: "string",
format: "product-key",
});

const { datasource_id, product_id } = useMemo(() => {
const [datasource_id = "", product_id = ""] = decodePath(productKey || "");
return { datasource_id, product_id };
}, []);
return useProduct(datasource_id, product_id);
};
1 change: 1 addition & 0 deletions @libs/utils/usePositionLimit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useSinglePosition } from "@libs";
/**
* Derive a PositionLimit account from the source account.
*
Expand Down
8 changes: 7 additions & 1 deletion @models/CCI-trending.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useATR, useCCI, useRef, useSimplePositionManager } from "@libs";
import {
useATR,
useCCI,
useParamOHLC,
useRef,
useSimplePositionManager,
} from "@libs";

/**
* CCI 顺势交易策略
Expand Down
2 changes: 1 addition & 1 deletion @models/R-Breaker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// R-Breaker 策略
// 高低周期的回转策略,根据高周期计算几个枢轴价位,然后在低周期上进行交易

import { useParamOHLC, useSinglePosition } from "@libs";
export default () => {
// 设定参数
const { product_id, close: C } = useParamOHLC("低周期"); // e.g. 1min
Expand Down
7 changes: 6 additions & 1 deletion @models/consecutive-up-down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
// 进入多头时平掉空头,进入空头时平掉多头
// X 和 Y 输入在策略设置中进行管理
// 并在附图中绘制连续上涨/下跌的信号的直方图
import { useSeriesMap } from "@libs";
import {
useParamNumber,
useSeriesMap,
useSinglePosition,
useParamOHLC,
} from "@libs";

export default () => {
const { product_id, close } = useParamOHLC("SomeKey");
Expand Down
2 changes: 1 addition & 1 deletion @models/double-ma.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 双均线策略 (Double Moving Average)
// 当短期均线由下向上穿越长期均线时做多 (金叉)
// 当短期均线由上向下穿越长期均线时做空 (死叉)
import { useSMA, useSimplePositionManager } from "@libs";
import { useParamOHLC, useSMA, useSimplePositionManager } from "@libs";

export default () => {
// 使用收盘价序列
Expand Down
1 change: 1 addition & 0 deletions @models/dual-thrust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useMAX,
useMIN,
useParamNumber,
useParamOHLC,
useSimplePositionManager,
} from "@libs";

Expand Down
1 change: 1 addition & 0 deletions @models/greedy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useParamOHLC, useSinglePosition } from "@libs";
// 贪心策略
// 如果当前开盘价与前一根K线的高点或低点之间存在差距,贪婪策略会打开一个初始订单。如果开盘价大于前一个高点,则策略做多,如果开盘价低于前一个K线的低点,则开空仓。
// 开仓后,只要蜡烛的颜色与开仓一致,就会继续向同一方向填单。如果当前仓位是多头,将为每个后续的绿色蜡烛创建新的多头订单,反之亦然。这将继续进行,直到出现不同颜色的蜡烛
Expand Down
1 change: 1 addition & 0 deletions @models/hp-sma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
useHPFilter,
useParamNumber,
useParamOHLC,
useSMA,
useSimplePositionManager,
} from "@libs";
Expand Down
8 changes: 7 additions & 1 deletion @models/super-trend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
// 下轨为上一根K线的(close+open)/2 - A * atr
// 当价格上穿到上轨时,进入突破模式,当下穿上轨时,进入下跌模式
// 并画出上轨和下轨
import { useATR, useParamNumber, useSeriesMap } from "@libs";
import {
useATR,
useParamNumber,
useParamOHLC,
useSeriesMap,
useSinglePosition,
} from "@libs";

export default () => {
const { product_id, close, open, high, low } = useParamOHLC("SomeKey");
Expand Down
1 change: 1 addition & 0 deletions @models/turtle-long.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useMAX,
useMIN,
useParamNumber,
useParamOHLC,
useRuleEffect,
useSeriesMap,
useSinglePosition,
Expand Down
68 changes: 18 additions & 50 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,33 +472,6 @@ declare const useMemoAsync: <T>(
* @see https://json-schema.org/ for JSON Schema Specification
*/
declare const useParamSchema: <T>(key: string, schema: any) => T;
/**
* Use an application parameter (number)
*
* @param key - The key of the parameter
* @param defaultValue - The default value of the parameter
* @returns The value of the parameter
* @deprecated import from "@libs" instead
*/
declare const useParamNumber: (key: string, defaultValue?: number) => number;
/**
* Use an application parameter (boolean)
*
* @param key - The key of the parameter
* @param defaultValue - The default value of the parameter
* @returns The value of the parameter
* @deprecated import from "@libs" instead
*/
declare const useParamBoolean: (key: string, defaultValue?: boolean) => boolean;
/**
* Use an application parameter (string)
*
* @param key - The key of the parameter
* @param defaultValue - The default value of the parameter
* @returns The value of the parameter
* @deprecated import from "@libs" instead
*/
declare const useParamString: (key: string, defaultValue?: string) => string;

// Series Hook
/** A series is a number[] with some additional fields */
Expand All @@ -521,8 +494,7 @@ declare const useProduct: (
datasource_id: string,
product_id: string
) => IProduct;
/** Use product parameters */
declare const useParamProduct: (key: string) => IProduct;

/**
* Use OHLC Period Data
* @param datasource_id - Data source ID
Expand All @@ -541,34 +513,14 @@ declare const useOHLC: (
close: Series;
volume: Series;
};
/** Use OHLC data */
declare const useParamOHLC: (key: string) => ReturnType<typeof useOHLC> & {
datasource_id: string;
product_id: string;
period_in_sec: number;
};

/** Use Account Info */
declare const useAccountInfo: (options?: {
account_id?: string;
currency?: string;
leverage?: number;
initial_balance?: number;
}) => IAccountInfo;
/**
* Use a position manager for a specified product and direction
*/
declare const useSinglePosition: (
product_id: string,
variant: PositionVariant,
account_id?: string
) => {
targetVolume: number;
takeProfitPrice: number;
stopLossPrice: number;
setTargetVolume: (v: number) => void;
setTakeProfitPrice: (v: number) => void;
setStopLossPrice: (v: number) => void;
} & IPosition;

/** Use Exchange */
declare const useExchange: () => {
Expand Down Expand Up @@ -600,6 +552,22 @@ declare const roundToStep: (
step: number,
roundFn?: ((x: number) => number) | undefined
) => number;

/**
* convert params to path.
* Path is splitted by `/`.
* Escape to `\/` if a param including `/`.
*/
declare const encodePath: (...params: any[]) => string;

/**
* convert path to params.
* Path is splitted by `/`.
* Escape to `\/` if a param including `/`.
* @public
*/
declare const decodePath: (path: string) => string[];

declare const getProfit: (
product: IProduct,
openPrice: number,
Expand Down

0 comments on commit 4bb6ee1

Please sign in to comment.