@quantbrasil/cli 0.1.0-beta.10 → 0.1.0-beta.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/cli/index.d.ts +4 -2
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +10 -2
- package/dist/commands/cointegration.d.ts +52 -0
- package/dist/commands/cointegration.d.ts.map +1 -0
- package/dist/commands/cointegration.js +118 -0
- package/dist/commands/rankings.d.ts +65 -0
- package/dist/commands/rankings.d.ts.map +1 -1
- package/dist/commands/rankings.js +149 -0
- package/dist/vendor/core/capabilities/cointegration.d.ts +52 -0
- package/dist/vendor/core/capabilities/cointegration.d.ts.map +1 -0
- package/dist/vendor/core/capabilities/cointegration.js +63 -0
- package/dist/vendor/core/capabilities/index.d.ts +1 -0
- package/dist/vendor/core/capabilities/index.d.ts.map +1 -1
- package/dist/vendor/core/capabilities/index.js +1 -0
- package/dist/vendor/core/capabilities/rankings.d.ts +94 -2
- package/dist/vendor/core/capabilities/rankings.d.ts.map +1 -1
- package/dist/vendor/core/capabilities/rankings.js +118 -1
- package/dist/vendor/core/capabilities/registry.d.ts +282 -4
- package/dist/vendor/core/capabilities/registry.d.ts.map +1 -1
- package/dist/vendor/core/capabilities/registry.js +2 -0
- package/dist/vendor/core/capabilities/types.d.ts +1 -1
- package/dist/vendor/core/capabilities/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/skills/quantbrasil/SKILL.md +10 -7
- package/skills/quantbrasil/references/cli.md +28 -6
- package/skills/quantbrasil/references/cointegration.md +40 -0
- package/skills/quantbrasil/references/costs.md +4 -1
- package/skills/quantbrasil/references/quality-eval-queries.json +20 -0
- package/skills/quantbrasil/references/rankings.md +31 -3
- package/skills/quantbrasil/references/unsupported.md +2 -0
- package/skills/quantbrasil/references/workflows.md +30 -3
package/README.md
CHANGED
|
@@ -76,7 +76,9 @@ quantbrasil holdings var 182 --years 1 --confidence 95
|
|
|
76
76
|
quantbrasil rankings list
|
|
77
77
|
quantbrasil rankings current --system momentum-90d --top 20
|
|
78
78
|
quantbrasil rankings current --system magic-formula --top 10
|
|
79
|
+
quantbrasil rankings current --system low-risk --top 30
|
|
79
80
|
quantbrasil rankings current --id 123 --top 20
|
|
81
|
+
quantbrasil rankings return --system magic-formula --from 2024-01-18 --to 2026-05-25 --top 20 --rebalance quarterly
|
|
80
82
|
quantbrasil screening universes
|
|
81
83
|
quantbrasil screening indicators
|
|
82
84
|
quantbrasil screening run --system acoes-mais-liquidas --query-file ./screening.json
|
package/dist/cli/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import { type CapabilitiesCommandIO, runCapabilitiesCommand } from "../commands/capabilities.js";
|
|
3
|
+
import { runCointegrationPairCommand } from "../commands/cointegration.js";
|
|
3
4
|
import { runMarketAssetsCommand, runMarketPriceCommand } from "../commands/market.js";
|
|
4
5
|
import { runInitCommand } from "../commands/init.js";
|
|
5
6
|
import { runHoldingCreateCommand, runHoldingBetaCommand, runHoldingGetCommand, runHoldingHistoricalReturnCommand, runHoldingListCommand, runHoldingRenameCommand, runHoldingSetPositionsCommand, runHoldingSetTargetsCommand, runHoldingVarCommand, runWatchlistAddAssetsCommand, runWatchlistCreateCommand, runWatchlistGetCommand, runWatchlistListCommand, runWatchlistRemoveAssetsCommand, runWatchlistRenameCommand } from "../commands/portfolios.js";
|
|
6
|
-
import { runRankingsCurrentCommand, runRankingsListCommand } from "../commands/rankings.js";
|
|
7
|
+
import { runRankingsCurrentCommand, runRankingsListCommand, runRankingsReturnCommand } from "../commands/rankings.js";
|
|
7
8
|
import { runScreeningIndicatorsCommand, runScreeningRunCommand, runScreeningUniversesCommand } from "../commands/screening.js";
|
|
8
9
|
import { runSkillsInstallCommand } from "../commands/skills.js";
|
|
9
10
|
import { runStatusCommand } from "../commands/status.js";
|
|
@@ -30,7 +31,8 @@ export { runAssetOverviewCommand } from "../commands/assets.js";
|
|
|
30
31
|
export { runCapabilitiesCommand, runStatusCommand };
|
|
31
32
|
export { runInitCommand };
|
|
32
33
|
export { runMarketAssetsCommand, runMarketPriceCommand };
|
|
33
|
-
export { runRankingsCurrentCommand, runRankingsListCommand };
|
|
34
|
+
export { runRankingsCurrentCommand, runRankingsListCommand, runRankingsReturnCommand, };
|
|
35
|
+
export { runCointegrationPairCommand };
|
|
34
36
|
export { runScreeningIndicatorsCommand, runScreeningRunCommand, runScreeningUniversesCommand, };
|
|
35
37
|
export { runHoldingBetaCommand, runHoldingCreateCommand, runHoldingGetCommand, runHoldingHistoricalReturnCommand, runHoldingListCommand, runHoldingRenameCommand, runHoldingSetPositionsCommand, runHoldingSetTargetsCommand, runHoldingVarCommand, runWatchlistAddAssetsCommand, runWatchlistCreateCommand, runWatchlistGetCommand, runWatchlistListCommand, runWatchlistRemoveAssetsCommand, runWatchlistRenameCommand, };
|
|
36
38
|
export { runSkillsInstallCommand };
|
package/dist/cli/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAUpC,OAAO,EACL,KAAK,qBAAqB,EAE1B,sBAAsB,EACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAGL,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAGL,cAAc,EACf,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAGL,uBAAuB,EACvB,qBAAqB,EACrB,oBAAoB,EACpB,iCAAiC,EACjC,qBAAqB,EACrB,uBAAuB,EACvB,6BAA6B,EAC7B,2BAA2B,EAC3B,oBAAoB,EACpB,4BAA4B,EAC5B,yBAAyB,EACzB,sBAAsB,EACtB,uBAAuB,EACvB,+BAA+B,EAC/B,yBAAyB,EAC1B,MAAM,2BAA2B,CAAC;AACnC,O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AO,EAAE,uBAAuB,EAAE,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
package/dist/cli/index.js
CHANGED
|
@@ -3,10 +3,11 @@ import { Command } from "commander";
|
|
|
3
3
|
import { registerAssetsCommands, } from "../commands/assets.js";
|
|
4
4
|
import { registerAuthCommands, runAuthWhoamiCommand, } from "../commands/auth.js";
|
|
5
5
|
import { registerCapabilitiesCommand, runCapabilitiesCommand, } from "../commands/capabilities.js";
|
|
6
|
+
import { registerCointegrationCommands, runCointegrationPairCommand, } from "../commands/cointegration.js";
|
|
6
7
|
import { registerMarketCommands, runMarketAssetsCommand, runMarketPriceCommand, } from "../commands/market.js";
|
|
7
8
|
import { registerInitCommand, runInitCommand, } from "../commands/init.js";
|
|
8
9
|
import { registerPortfoliosCommands, runHoldingCreateCommand, runHoldingBetaCommand, runHoldingGetCommand, runHoldingHistoricalReturnCommand, runHoldingListCommand, runHoldingRenameCommand, runHoldingSetPositionsCommand, runHoldingSetTargetsCommand, runHoldingVarCommand, runWatchlistAddAssetsCommand, runWatchlistCreateCommand, runWatchlistGetCommand, runWatchlistListCommand, runWatchlistRemoveAssetsCommand, runWatchlistRenameCommand, } from "../commands/portfolios.js";
|
|
9
|
-
import { registerRankingsCommands, runRankingsCurrentCommand, runRankingsListCommand, } from "../commands/rankings.js";
|
|
10
|
+
import { registerRankingsCommands, runRankingsCurrentCommand, runRankingsListCommand, runRankingsReturnCommand, } from "../commands/rankings.js";
|
|
10
11
|
import { registerScreeningCommands, runScreeningIndicatorsCommand, runScreeningRunCommand, runScreeningUniversesCommand, } from "../commands/screening.js";
|
|
11
12
|
import { registerSkillsCommands, runSkillsInstallCommand, } from "../commands/skills.js";
|
|
12
13
|
import { registerStatusFlag, runStatusCommand, } from "../commands/status.js";
|
|
@@ -50,6 +51,11 @@ export function createCliProgram(options = {}) {
|
|
|
50
51
|
env: options.env,
|
|
51
52
|
fetch: options.fetch,
|
|
52
53
|
};
|
|
54
|
+
const cointegrationContext = {
|
|
55
|
+
io: options.io,
|
|
56
|
+
env: options.env,
|
|
57
|
+
fetch: options.fetch,
|
|
58
|
+
};
|
|
53
59
|
const screeningContext = {
|
|
54
60
|
io: options.io,
|
|
55
61
|
env: options.env,
|
|
@@ -87,6 +93,7 @@ export function createCliProgram(options = {}) {
|
|
|
87
93
|
registerMarketCommands(program, marketContext);
|
|
88
94
|
registerPortfoliosCommands(program, portfoliosContext);
|
|
89
95
|
registerRankingsCommands(program, rankingsContext);
|
|
96
|
+
registerCointegrationCommands(program, cointegrationContext);
|
|
90
97
|
registerScreeningCommands(program, screeningContext);
|
|
91
98
|
registerSkillsCommands(program, skillsContext);
|
|
92
99
|
registerUpdateCommand(program, updateContext);
|
|
@@ -155,7 +162,8 @@ export { runAssetOverviewCommand } from "../commands/assets.js";
|
|
|
155
162
|
export { runCapabilitiesCommand, runStatusCommand };
|
|
156
163
|
export { runInitCommand };
|
|
157
164
|
export { runMarketAssetsCommand, runMarketPriceCommand };
|
|
158
|
-
export { runRankingsCurrentCommand, runRankingsListCommand };
|
|
165
|
+
export { runRankingsCurrentCommand, runRankingsListCommand, runRankingsReturnCommand, };
|
|
166
|
+
export { runCointegrationPairCommand };
|
|
159
167
|
export { runScreeningIndicatorsCommand, runScreeningRunCommand, runScreeningUniversesCommand, };
|
|
160
168
|
export { runHoldingBetaCommand, runHoldingCreateCommand, runHoldingGetCommand, runHoldingHistoricalReturnCommand, runHoldingListCommand, runHoldingRenameCommand, runHoldingSetPositionsCommand, runHoldingSetTargetsCommand, runHoldingVarCommand, runWatchlistAddAssetsCommand, runWatchlistCreateCommand, runWatchlistGetCommand, runWatchlistListCommand, runWatchlistRemoveAssetsCommand, runWatchlistRenameCommand, };
|
|
161
169
|
export { runSkillsInstallCommand };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import type { JsonValue } from "../vendor/core/index.js";
|
|
3
|
+
import { type CliInvokeContext } from "../cli/client.js";
|
|
4
|
+
import { type TerminalWriter } from "../cli/terminal.js";
|
|
5
|
+
export interface CointegrationCommandIO {
|
|
6
|
+
stdout: TerminalWriter;
|
|
7
|
+
}
|
|
8
|
+
export interface CointegrationPairCommandOptions {
|
|
9
|
+
window?: string;
|
|
10
|
+
json?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface CointegrationZScorePoint {
|
|
13
|
+
[key: string]: JsonValue;
|
|
14
|
+
date: string;
|
|
15
|
+
zscore: number | null;
|
|
16
|
+
}
|
|
17
|
+
export interface CointegrationBetaPoint {
|
|
18
|
+
[key: string]: JsonValue;
|
|
19
|
+
date: string;
|
|
20
|
+
slope: number | null;
|
|
21
|
+
}
|
|
22
|
+
export interface CointegrationPairResponse {
|
|
23
|
+
[key: string]: JsonValue;
|
|
24
|
+
ok: boolean;
|
|
25
|
+
summary_markdown: string;
|
|
26
|
+
base_asset: string;
|
|
27
|
+
comparison_asset: string;
|
|
28
|
+
window: number;
|
|
29
|
+
beta: number | null;
|
|
30
|
+
beta_std: number | null;
|
|
31
|
+
beta_mean: number | null;
|
|
32
|
+
beta_volatility: number | null;
|
|
33
|
+
adf: number | null;
|
|
34
|
+
p_value: number | null;
|
|
35
|
+
confidence: number | null;
|
|
36
|
+
halflife: number | null;
|
|
37
|
+
critical_values: Record<string, number>;
|
|
38
|
+
zscore_series: CointegrationZScorePoint[];
|
|
39
|
+
beta_series: CointegrationBetaPoint[];
|
|
40
|
+
}
|
|
41
|
+
export interface CointegrationCommandContext extends CliInvokeContext {
|
|
42
|
+
io?: CointegrationCommandIO;
|
|
43
|
+
}
|
|
44
|
+
type JsonRecord = {
|
|
45
|
+
[key: string]: JsonValue;
|
|
46
|
+
};
|
|
47
|
+
export declare function registerCointegrationCommands(program: Command, context?: CointegrationCommandContext): void;
|
|
48
|
+
export declare function runCointegrationPairCommand(baseAsset: string, comparisonAsset: string, options: CointegrationPairCommandOptions, context?: CointegrationCommandContext): Promise<void>;
|
|
49
|
+
export declare function buildCointegrationPairInput(baseAsset: string, comparisonAsset: string, options: CointegrationPairCommandOptions): JsonRecord;
|
|
50
|
+
export declare function formatCointegrationPairHuman(data: CointegrationPairResponse, theme?: import("../cli/terminal.js").TerminalTheme): string;
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=cointegration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cointegration.d.ts","sourceRoot":"","sources":["../../src/commands/cointegration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAuB,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE9E,OAAO,EAAuB,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE9E,MAAM,WAAW,sBAAsB;IACrC,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,MAAM,WAAW,+BAA+B;IAC9C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,WAAW,wBAAwB;IACvC,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB;AAED,MAAM,WAAW,sBAAsB;IACrC,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,MAAM,WAAW,yBAAyB;IACxC,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,EAAE,EAAE,OAAO,CAAC;IACZ,gBAAgB,EAAE,MAAM,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,MAAM,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,aAAa,EAAE,wBAAwB,EAAE,CAAC;IAC1C,WAAW,EAAE,sBAAsB,EAAE,CAAC;CACvC;AAED,MAAM,WAAW,2BAA4B,SAAQ,gBAAgB;IACnE,EAAE,CAAC,EAAE,sBAAsB,CAAC;CAC7B;AAED,KAAK,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAE/C,wBAAgB,6BAA6B,CAC3C,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,2BAAgC,GACxC,IAAI,CA6BN;AAED,wBAAsB,2BAA2B,CAC/C,SAAS,EAAE,MAAM,EACjB,eAAe,EAAE,MAAM,EACvB,OAAO,EAAE,+BAA+B,EACxC,OAAO,GAAE,2BAAgC,GACxC,OAAO,CAAC,IAAI,CAAC,CAiBf;AAED,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,MAAM,EACjB,eAAe,EAAE,MAAM,EACvB,OAAO,EAAE,+BAA+B,GACvC,UAAU,CAkBZ;AAED,wBAAgB,4BAA4B,CAC1C,IAAI,EAAE,yBAAyB,EAC/B,KAAK,6CAAsC,GAC1C,MAAM,CAiDR"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { invokeCliCapability } from "../cli/client.js";
|
|
2
|
+
import { createCliValidationError } from "../cli/errors.js";
|
|
3
|
+
import { createTerminalTheme } from "../cli/terminal.js";
|
|
4
|
+
export function registerCointegrationCommands(program, context = {}) {
|
|
5
|
+
const cointegrationCommand = program
|
|
6
|
+
.command("cointegration")
|
|
7
|
+
.description("Calcula cointegração para análise de Long & Short");
|
|
8
|
+
cointegrationCommand
|
|
9
|
+
.command("pair")
|
|
10
|
+
.description("Calcula cointegração para análise de Long & Short")
|
|
11
|
+
.argument("<base_asset>", "Ticker do ativo base, por exemplo PETR4")
|
|
12
|
+
.argument("<comparison_asset>", "Ticker do ativo de comparação, por exemplo VALE3")
|
|
13
|
+
.option("--window <n>", "Janela de candles (padrão 250)")
|
|
14
|
+
.option("--json", "Exibe saída JSON")
|
|
15
|
+
.action(async (baseAsset, comparisonAsset, options) => {
|
|
16
|
+
await runCointegrationPairCommand(baseAsset, comparisonAsset, options, context);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export async function runCointegrationPairCommand(baseAsset, comparisonAsset, options, context = {}) {
|
|
20
|
+
const stdout = context.io?.stdout ?? process.stdout;
|
|
21
|
+
const theme = createTerminalTheme(stdout, context.env ?? process.env);
|
|
22
|
+
const response = await invokeCliCapability({
|
|
23
|
+
capability: "cointegration.pair",
|
|
24
|
+
input: buildCointegrationPairInput(baseAsset, comparisonAsset, options),
|
|
25
|
+
env: context.env,
|
|
26
|
+
fetch: context.fetch,
|
|
27
|
+
});
|
|
28
|
+
if (options.json) {
|
|
29
|
+
stdout.write(`${JSON.stringify(response.data, null, 2)}\n`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
stdout.write(`${formatCointegrationPairHuman(response.data, theme)}\n`);
|
|
33
|
+
}
|
|
34
|
+
export function buildCointegrationPairInput(baseAsset, comparisonAsset, options) {
|
|
35
|
+
const normalizedBase = normalizeTicker(baseAsset, "base_asset");
|
|
36
|
+
const normalizedComparison = normalizeTicker(comparisonAsset, "comparison_asset");
|
|
37
|
+
if (normalizedBase === normalizedComparison) {
|
|
38
|
+
throw createCliValidationError("Os ativos base e de comparação devem ser diferentes.");
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
base_asset: normalizedBase,
|
|
42
|
+
comparison_asset: normalizedComparison,
|
|
43
|
+
window: parseWindowOption(options.window),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function formatCointegrationPairHuman(data, theme = createTerminalTheme(process.stdout)) {
|
|
47
|
+
const lines = [
|
|
48
|
+
theme.label("Cointegração / Long & Short entre ativos"),
|
|
49
|
+
"",
|
|
50
|
+
theme.bold(`${data.base_asset} x ${data.comparison_asset}`),
|
|
51
|
+
`${theme.label("Janela:")} ${formatInteger(data.window)} candles`,
|
|
52
|
+
"",
|
|
53
|
+
];
|
|
54
|
+
lines.push(`${theme.label("Beta:")} ${formatOptionalNumber(data.beta)}`);
|
|
55
|
+
if (data.beta_mean !== null) {
|
|
56
|
+
lines.push(`${theme.label("Beta médio:")} ${formatNumber(data.beta_mean)}`);
|
|
57
|
+
}
|
|
58
|
+
if (data.beta_std !== null) {
|
|
59
|
+
lines.push(`${theme.label("Beta std:")} ${formatNumber(data.beta_std)}`);
|
|
60
|
+
}
|
|
61
|
+
if (data.beta_volatility !== null) {
|
|
62
|
+
lines.push(`${theme.label("Volatilidade beta:")} ${formatNumber(data.beta_volatility)}`);
|
|
63
|
+
}
|
|
64
|
+
lines.push(`${theme.label("ADF:")} ${formatOptionalNumber(data.adf)}`);
|
|
65
|
+
lines.push(`${theme.label("P-valor:")} ${formatOptionalNumber(data.p_value)}`);
|
|
66
|
+
lines.push(`${theme.label("Confiança:")} ${data.confidence === null ? "n/d" : formatPercent(data.confidence * 100)}`);
|
|
67
|
+
lines.push(`${theme.label("Meia-vida:")} ${formatOptionalNumber(data.halflife)}`);
|
|
68
|
+
const latest = data.zscore_series[data.zscore_series.length - 1];
|
|
69
|
+
if (latest && latest.zscore !== null) {
|
|
70
|
+
lines.push(`${theme.label("Z-score atual:")} ${formatNumber(latest.zscore)} ${theme.dim(`(${latest.date})`)}`);
|
|
71
|
+
}
|
|
72
|
+
lines.push(`${theme.label("Pontos da série de z-score:")} ${formatInteger(data.zscore_series.length)}`);
|
|
73
|
+
lines.push(`${theme.label("Pontos da série de beta:")} ${formatInteger(data.beta_series.length)}`);
|
|
74
|
+
return lines.join("\n");
|
|
75
|
+
}
|
|
76
|
+
function normalizeTicker(rawTicker, label) {
|
|
77
|
+
const normalized = rawTicker.trim().toUpperCase();
|
|
78
|
+
if (!normalized) {
|
|
79
|
+
throw createCliValidationError(`${label} é obrigatório.`);
|
|
80
|
+
}
|
|
81
|
+
return normalized;
|
|
82
|
+
}
|
|
83
|
+
function parseWindowOption(rawWindow) {
|
|
84
|
+
if (rawWindow === undefined) {
|
|
85
|
+
return 250;
|
|
86
|
+
}
|
|
87
|
+
const trimmed = rawWindow.trim();
|
|
88
|
+
if (!trimmed) {
|
|
89
|
+
throw createCliValidationError("--window não pode ser vazio.");
|
|
90
|
+
}
|
|
91
|
+
const parsed = Number.parseInt(trimmed, 10);
|
|
92
|
+
if (!Number.isInteger(parsed) || parsed <= 0 || String(parsed) !== trimmed) {
|
|
93
|
+
throw createCliValidationError("--window deve ser um inteiro positivo.");
|
|
94
|
+
}
|
|
95
|
+
if (parsed < 20 || parsed > 2000) {
|
|
96
|
+
throw createCliValidationError("--window deve estar entre 20 e 2000 candles.");
|
|
97
|
+
}
|
|
98
|
+
return parsed;
|
|
99
|
+
}
|
|
100
|
+
function formatInteger(value) {
|
|
101
|
+
return new Intl.NumberFormat("pt-BR", {
|
|
102
|
+
maximumFractionDigits: 0,
|
|
103
|
+
}).format(value);
|
|
104
|
+
}
|
|
105
|
+
function formatNumber(value) {
|
|
106
|
+
return new Intl.NumberFormat("pt-BR", {
|
|
107
|
+
maximumFractionDigits: 4,
|
|
108
|
+
}).format(value);
|
|
109
|
+
}
|
|
110
|
+
function formatOptionalNumber(value) {
|
|
111
|
+
return value === null ? "n/d" : formatNumber(value);
|
|
112
|
+
}
|
|
113
|
+
function formatPercent(value) {
|
|
114
|
+
return `${new Intl.NumberFormat("pt-BR", {
|
|
115
|
+
maximumFractionDigits: 2,
|
|
116
|
+
minimumFractionDigits: 2,
|
|
117
|
+
}).format(value)}%`;
|
|
118
|
+
}
|
|
@@ -14,6 +14,22 @@ export interface RankingsCurrentCommandOptions {
|
|
|
14
14
|
top?: string;
|
|
15
15
|
json?: boolean;
|
|
16
16
|
}
|
|
17
|
+
export interface RankingsReturnCommandOptions {
|
|
18
|
+
system?: string;
|
|
19
|
+
from?: string;
|
|
20
|
+
to?: string;
|
|
21
|
+
top?: string;
|
|
22
|
+
rebalance?: string;
|
|
23
|
+
weight?: string;
|
|
24
|
+
indexFilter?: string;
|
|
25
|
+
json?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface RankingReturnSimulationOut {
|
|
28
|
+
[key: string]: JsonValue;
|
|
29
|
+
supported: boolean;
|
|
30
|
+
earliest_start_date: string | null;
|
|
31
|
+
max_top_n: number | null;
|
|
32
|
+
}
|
|
17
33
|
export interface RankingMetricOut {
|
|
18
34
|
[key: string]: JsonValue;
|
|
19
35
|
key: string;
|
|
@@ -31,6 +47,7 @@ export interface RankingDefinitionOut {
|
|
|
31
47
|
metrics: RankingMetricOut[];
|
|
32
48
|
latest_as_of: string | null;
|
|
33
49
|
total_assets: number | null;
|
|
50
|
+
return_simulation: RankingReturnSimulationOut;
|
|
34
51
|
}
|
|
35
52
|
export interface RankingsListResponse {
|
|
36
53
|
[key: string]: JsonValue;
|
|
@@ -66,6 +83,51 @@ export interface RankingCurrentResponse {
|
|
|
66
83
|
count: number;
|
|
67
84
|
rows: RankingCurrentRowOut[];
|
|
68
85
|
}
|
|
86
|
+
export interface RankingReturnAssetAllocation {
|
|
87
|
+
[key: string]: JsonValue;
|
|
88
|
+
ticker: string;
|
|
89
|
+
weight: number;
|
|
90
|
+
ranking_position: number;
|
|
91
|
+
}
|
|
92
|
+
export interface RankingReturnRebalance {
|
|
93
|
+
[key: string]: JsonValue;
|
|
94
|
+
date: string;
|
|
95
|
+
portfolio: RankingReturnAssetAllocation[];
|
|
96
|
+
}
|
|
97
|
+
export interface RankingReturnAssetReturn {
|
|
98
|
+
[key: string]: JsonValue;
|
|
99
|
+
ticker: string;
|
|
100
|
+
return_value: number;
|
|
101
|
+
weight: number;
|
|
102
|
+
contribution: number;
|
|
103
|
+
}
|
|
104
|
+
export interface RankingReturnPeriodReturn {
|
|
105
|
+
[key: string]: JsonValue;
|
|
106
|
+
start_date: string;
|
|
107
|
+
end_date: string;
|
|
108
|
+
total_return: number;
|
|
109
|
+
asset_returns: RankingReturnAssetReturn[];
|
|
110
|
+
}
|
|
111
|
+
export interface RankingReturnResult {
|
|
112
|
+
[key: string]: JsonValue;
|
|
113
|
+
total_return: number;
|
|
114
|
+
annualized_return: number;
|
|
115
|
+
start_date: string;
|
|
116
|
+
end_date: string;
|
|
117
|
+
top_n: number;
|
|
118
|
+
rebalance_frequency: string;
|
|
119
|
+
rebalance_dates: RankingReturnRebalance[];
|
|
120
|
+
period_returns: RankingReturnPeriodReturn[];
|
|
121
|
+
ibov_return: number;
|
|
122
|
+
ibov_annualized_return: number;
|
|
123
|
+
}
|
|
124
|
+
export interface RankingReturnResponse {
|
|
125
|
+
[key: string]: JsonValue;
|
|
126
|
+
ok: boolean;
|
|
127
|
+
summary_markdown: string;
|
|
128
|
+
ranking: RankingDefinitionOut;
|
|
129
|
+
result: RankingReturnResult;
|
|
130
|
+
}
|
|
69
131
|
export interface RankingsCommandContext extends CliInvokeContext {
|
|
70
132
|
io?: RankingsCommandIO;
|
|
71
133
|
}
|
|
@@ -75,8 +137,11 @@ type JsonRecord = {
|
|
|
75
137
|
export declare function registerRankingsCommands(program: Command, context?: RankingsCommandContext): void;
|
|
76
138
|
export declare function runRankingsListCommand(options: RankingsListCommandOptions, context?: RankingsCommandContext): Promise<void>;
|
|
77
139
|
export declare function runRankingsCurrentCommand(options: RankingsCurrentCommandOptions, context?: RankingsCommandContext): Promise<void>;
|
|
140
|
+
export declare function runRankingsReturnCommand(options: RankingsReturnCommandOptions, context?: RankingsCommandContext): Promise<void>;
|
|
78
141
|
export declare function buildRankingsCurrentInput(options: RankingsCurrentCommandOptions): JsonRecord;
|
|
142
|
+
export declare function buildRankingsReturnInput(options: RankingsReturnCommandOptions): JsonRecord;
|
|
79
143
|
export declare function formatRankingsListHuman(data: RankingsListResponse, theme?: import("../cli/terminal.js").TerminalTheme): string;
|
|
80
144
|
export declare function formatRankingCurrentHuman(data: RankingCurrentResponse, theme?: import("../cli/terminal.js").TerminalTheme): string;
|
|
145
|
+
export declare function formatRankingReturnHuman(data: RankingReturnResponse, theme?: import("../cli/terminal.js").TerminalTheme): string;
|
|
81
146
|
export {};
|
|
82
147
|
//# sourceMappingURL=rankings.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rankings.d.ts","sourceRoot":"","sources":["../../src/commands/rankings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"rankings.d.ts","sourceRoot":"","sources":["../../src/commands/rankings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAM5C,OAAO,EAAuB,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE9E,OAAO,EAAuB,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE9E,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,MAAM,WAAW,0BAA0B;IACzC,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,WAAW,6BAA6B;IAC5C,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,C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,CAAC;CAC7B;AAED,MAAM,WAAW,sBAAuB,SAAQ,gBAAgB;IAC9D,EAAE,CAAC,EAAE,iBAAiB,CAAC;CACxB;AAED,KAAK,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAE/C,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,sBAA2B,GACnC,IAAI,CA6CN;AAED,wBAAsB,sBAAsB,CAC1C,OAAO,EAAE,0BAA0B,EACnC,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,IAAI,CAAC,CAef;AAED,wBAAsB,yBAAyB,CAC7C,OAAO,EAAE,6BAA6B,EACtC,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,IAAI,CAAC,CAgBf;AAED,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,4BAA4B,EACrC,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,IAAI,CAAC,CAgBf;AAED,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,6BAA6B,GACrC,UAAU,CAuBZ;AAED,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,4BAA4B,GACpC,UAAU,CA6BZ;AAED,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,oBAAoB,EAC1B,KAAK,6CAAsC,GAC1C,MAAM,CAaR;AAED,wBAAgB,yBAAyB,CACvC,IAAI,EAAE,sBAAsB,EAC5B,KAAK,6CAAsC,GAC1C,MAAM,CAwCR;AAED,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,qBAAqB,EAC3B,KAAK,6CAAsC,GAC1C,MAAM,CA4BR"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { rankingReturnIndexFilterValues, rankingReturnRebalanceValues, rankingReturnWeightingValues, } from "../vendor/core/capabilities/index.js";
|
|
1
2
|
import { invokeCliCapability } from "../cli/client.js";
|
|
2
3
|
import { createCliValidationError } from "../cli/errors.js";
|
|
3
4
|
import { createTerminalTheme } from "../cli/terminal.js";
|
|
@@ -22,6 +23,20 @@ export function registerRankingsCommands(program, context = {}) {
|
|
|
22
23
|
.action(async (options) => {
|
|
23
24
|
await runRankingsCurrentCommand(options, context);
|
|
24
25
|
});
|
|
26
|
+
rankingsCommand
|
|
27
|
+
.command("return")
|
|
28
|
+
.description("Calcula o retorno histórico de um ranking do sistema")
|
|
29
|
+
.requiredOption("--system <slug>", "Slug do ranking do sistema")
|
|
30
|
+
.requiredOption("--from <date>", "Data inicial em AAAA-MM-DD")
|
|
31
|
+
.requiredOption("--to <date>", "Data final em AAAA-MM-DD")
|
|
32
|
+
.option("--top <n>", "Número de ativos em cada rebalanceamento", "20")
|
|
33
|
+
.requiredOption("--rebalance <frequency>", "Frequência: weekly, monthly, bimonthly, quarterly, semiannually ou annually")
|
|
34
|
+
.option("--weight <method>", "Método de ponderação", "equal")
|
|
35
|
+
.option("--index-filter <filter>", "Filtro: NONE, IBOV, IBX100 ou SMLL", "NONE")
|
|
36
|
+
.option("--json", "Exibe saída JSON")
|
|
37
|
+
.action(async (options) => {
|
|
38
|
+
await runRankingsReturnCommand(options, context);
|
|
39
|
+
});
|
|
25
40
|
}
|
|
26
41
|
export async function runRankingsListCommand(options, context = {}) {
|
|
27
42
|
const stdout = context.io?.stdout ?? process.stdout;
|
|
@@ -52,6 +67,21 @@ export async function runRankingsCurrentCommand(options, context = {}) {
|
|
|
52
67
|
}
|
|
53
68
|
stdout.write(`${formatRankingCurrentHuman(response.data, theme)}\n`);
|
|
54
69
|
}
|
|
70
|
+
export async function runRankingsReturnCommand(options, context = {}) {
|
|
71
|
+
const stdout = context.io?.stdout ?? process.stdout;
|
|
72
|
+
const theme = createTerminalTheme(stdout, context.env ?? process.env);
|
|
73
|
+
const response = await invokeCliCapability({
|
|
74
|
+
capability: "rankings.return",
|
|
75
|
+
input: buildRankingsReturnInput(options),
|
|
76
|
+
env: context.env,
|
|
77
|
+
fetch: context.fetch,
|
|
78
|
+
});
|
|
79
|
+
if (options.json) {
|
|
80
|
+
stdout.write(`${JSON.stringify(response.data, null, 2)}\n`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
stdout.write(`${formatRankingReturnHuman(response.data, theme)}\n`);
|
|
84
|
+
}
|
|
55
85
|
export function buildRankingsCurrentInput(options) {
|
|
56
86
|
const hasId = options.id !== undefined;
|
|
57
87
|
const hasSystem = options.system !== undefined;
|
|
@@ -69,6 +99,22 @@ export function buildRankingsCurrentInput(options) {
|
|
|
69
99
|
}
|
|
70
100
|
return input;
|
|
71
101
|
}
|
|
102
|
+
export function buildRankingsReturnInput(options) {
|
|
103
|
+
const startDate = parseIsoDateOption(options.from, "--from");
|
|
104
|
+
const endDate = parseIsoDateOption(options.to, "--to");
|
|
105
|
+
if (startDate >= endDate) {
|
|
106
|
+
throw createCliValidationError("--from deve ser anterior a --to.");
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
system_id: normalizeRequiredString(options.system, "--system"),
|
|
110
|
+
start_date: startDate,
|
|
111
|
+
end_date: endDate,
|
|
112
|
+
top_n: parseRankingReturnTopOption(options.top),
|
|
113
|
+
rebalance_frequency: parseEnumOption(options.rebalance, "--rebalance", rankingReturnRebalanceValues),
|
|
114
|
+
weighting_method: parseEnumOption(options.weight ?? "equal", "--weight", rankingReturnWeightingValues),
|
|
115
|
+
index_filter: parseEnumOption(options.indexFilter ?? "NONE", "--index-filter", rankingReturnIndexFilterValues),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
72
118
|
export function formatRankingsListHuman(data, theme = createTerminalTheme(process.stdout)) {
|
|
73
119
|
const lines = [
|
|
74
120
|
theme.label("Rankings"),
|
|
@@ -108,6 +154,33 @@ export function formatRankingCurrentHuman(data, theme = createTerminalTheme(proc
|
|
|
108
154
|
lines.push(...formatRankingRows(data.rows, data.ranking.order_metric.label));
|
|
109
155
|
return lines.join("\n");
|
|
110
156
|
}
|
|
157
|
+
export function formatRankingReturnHuman(data, theme = createTerminalTheme(process.stdout)) {
|
|
158
|
+
const result = data.result;
|
|
159
|
+
const alpha = result.total_return - result.ibov_return;
|
|
160
|
+
const lines = [
|
|
161
|
+
theme.label("Retorno de ranking"),
|
|
162
|
+
"",
|
|
163
|
+
`${theme.bold(data.ranking.name)} ${theme.dim("(sistema)")}`,
|
|
164
|
+
`${theme.label("Sistema:")} ${data.ranking.id}`,
|
|
165
|
+
`${theme.label("Período:")} ${formatDatePtBr(result.start_date)} a ${formatDatePtBr(result.end_date)}`,
|
|
166
|
+
`${theme.label("Top:")} ${formatInteger(result.top_n)}`,
|
|
167
|
+
`${theme.label("Rebalanceamento:")} ${formatRebalanceFrequency(result.rebalance_frequency)}`,
|
|
168
|
+
"",
|
|
169
|
+
`${theme.label("Retorno total:")} ${formatPercentage(result.total_return)}`,
|
|
170
|
+
`${theme.label("Retorno anualizado:")} ${formatPercentage(result.annualized_return)}`,
|
|
171
|
+
`${theme.label("IBOV no período:")} ${formatPercentage(result.ibov_return)}`,
|
|
172
|
+
`${theme.label("IBOV anualizado:")} ${formatPercentage(result.ibov_annualized_return)}`,
|
|
173
|
+
`${theme.label("Alfa vs IBOV:")} ${formatPercentagePoints(alpha)}`,
|
|
174
|
+
"",
|
|
175
|
+
`${theme.label("Rebalanceamentos:")} ${formatInteger(result.rebalance_dates.length)}`,
|
|
176
|
+
`${theme.label("Períodos calculados:")} ${formatInteger(result.period_returns.length)}`,
|
|
177
|
+
];
|
|
178
|
+
if (data.summary_markdown.trim()) {
|
|
179
|
+
lines.push("");
|
|
180
|
+
lines.push(data.summary_markdown);
|
|
181
|
+
}
|
|
182
|
+
return lines.join("\n");
|
|
183
|
+
}
|
|
111
184
|
function formatRankingGroup(title, rankings, theme) {
|
|
112
185
|
const lines = [
|
|
113
186
|
`${theme.label(title)} ${theme.dim(`(${formatInteger(rankings.length)})`)}`,
|
|
@@ -139,6 +212,9 @@ function formatRankingGroup(title, rankings, theme) {
|
|
|
139
212
|
lines.push(` ${theme.label("Ativos:")} ${formatInteger(ranking.total_assets)}`);
|
|
140
213
|
}
|
|
141
214
|
lines.push(` ${theme.label("Uso:")} ${formatRankingUsage(ranking)}`);
|
|
215
|
+
if (ranking.kind === "SYSTEM") {
|
|
216
|
+
lines.push(` ${theme.label("Retorno:")} ${formatRankingReturnUsage(ranking)}`);
|
|
217
|
+
}
|
|
142
218
|
}
|
|
143
219
|
return lines;
|
|
144
220
|
}
|
|
@@ -158,6 +234,15 @@ function formatRankingUsage(ranking) {
|
|
|
158
234
|
}
|
|
159
235
|
return `quantbrasil rankings current --system ${ranking.id} --top 20`;
|
|
160
236
|
}
|
|
237
|
+
function formatRankingReturnUsage(ranking) {
|
|
238
|
+
if (!ranking.return_simulation.supported) {
|
|
239
|
+
return "indisponível";
|
|
240
|
+
}
|
|
241
|
+
return `quantbrasil rankings return --system ${ranking.id} --from ${formatRankingReturnUsageStartDate(ranking)} --to AAAA-MM-DD --top 20 --rebalance quarterly`;
|
|
242
|
+
}
|
|
243
|
+
function formatRankingReturnUsageStartDate(ranking) {
|
|
244
|
+
return ranking.return_simulation.earliest_start_date ?? "AAAA-MM-DD";
|
|
245
|
+
}
|
|
161
246
|
function formatRankingKind(kind) {
|
|
162
247
|
return kind === "SYSTEM" ? "sistema" : "usuário";
|
|
163
248
|
}
|
|
@@ -194,12 +279,76 @@ function formatMetricValue(metric) {
|
|
|
194
279
|
}
|
|
195
280
|
return String(metric.value);
|
|
196
281
|
}
|
|
282
|
+
function formatPercentage(value) {
|
|
283
|
+
return `${formatNumber(value * 100)}%`;
|
|
284
|
+
}
|
|
285
|
+
function formatPercentagePoints(value) {
|
|
286
|
+
const prefix = value > 0 ? "+" : value < 0 ? "-" : "";
|
|
287
|
+
return `${prefix}${formatNumber(Math.abs(value * 100))} p.p.`;
|
|
288
|
+
}
|
|
289
|
+
function formatDatePtBr(value) {
|
|
290
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})/.exec(value);
|
|
291
|
+
if (!match) {
|
|
292
|
+
return value;
|
|
293
|
+
}
|
|
294
|
+
return `${match[3]}/${match[2]}/${match[1]}`;
|
|
295
|
+
}
|
|
296
|
+
function formatRebalanceFrequency(value) {
|
|
297
|
+
switch (value) {
|
|
298
|
+
case "weekly":
|
|
299
|
+
return "semanal";
|
|
300
|
+
case "monthly":
|
|
301
|
+
return "mensal";
|
|
302
|
+
case "bimonthly":
|
|
303
|
+
return "bimestral";
|
|
304
|
+
case "quarterly":
|
|
305
|
+
return "trimestral";
|
|
306
|
+
case "semiannually":
|
|
307
|
+
return "semestral";
|
|
308
|
+
case "annually":
|
|
309
|
+
return "anual";
|
|
310
|
+
default:
|
|
311
|
+
return value;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
197
314
|
function parseTopOption(rawTop) {
|
|
198
315
|
if (rawTop === undefined) {
|
|
199
316
|
return 20;
|
|
200
317
|
}
|
|
201
318
|
return parsePositiveInteger(rawTop, "--top");
|
|
202
319
|
}
|
|
320
|
+
function parseRankingReturnTopOption(rawTop) {
|
|
321
|
+
const parsed = parseTopOption(rawTop);
|
|
322
|
+
if (parsed > 30) {
|
|
323
|
+
throw createCliValidationError("--top deve ser menor ou igual a 30.");
|
|
324
|
+
}
|
|
325
|
+
return parsed;
|
|
326
|
+
}
|
|
327
|
+
function parseIsoDateOption(rawValue, label) {
|
|
328
|
+
const normalized = normalizeRequiredString(rawValue, label);
|
|
329
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(normalized)) {
|
|
330
|
+
throw createCliValidationError(`${label} deve usar o formato AAAA-MM-DD.`);
|
|
331
|
+
}
|
|
332
|
+
const parsed = new Date(`${normalized}T00:00:00.000Z`);
|
|
333
|
+
if (Number.isNaN(parsed.getTime())) {
|
|
334
|
+
throw createCliValidationError(`${label} deve ser uma data válida.`);
|
|
335
|
+
}
|
|
336
|
+
const [year, month, day] = normalized.split("-").map(Number);
|
|
337
|
+
if (parsed.getUTCFullYear() !== year ||
|
|
338
|
+
parsed.getUTCMonth() + 1 !== month ||
|
|
339
|
+
parsed.getUTCDate() !== day) {
|
|
340
|
+
throw createCliValidationError(`${label} deve ser uma data válida.`);
|
|
341
|
+
}
|
|
342
|
+
return normalized;
|
|
343
|
+
}
|
|
344
|
+
function parseEnumOption(rawValue, label, allowedValues) {
|
|
345
|
+
const normalized = normalizeRequiredString(rawValue, label);
|
|
346
|
+
const match = allowedValues.find(value => value === normalized);
|
|
347
|
+
if (!match) {
|
|
348
|
+
throw createCliValidationError(`${label} deve ser um destes valores: ${allowedValues.join(", ")}.`);
|
|
349
|
+
}
|
|
350
|
+
return match;
|
|
351
|
+
}
|
|
203
352
|
function parsePositiveInteger(rawValue, label) {
|
|
204
353
|
const normalized = normalizeRequiredString(rawValue, label);
|
|
205
354
|
const parsed = Number.parseInt(normalized, 10);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export declare const cointegrationCapabilities: readonly [{
|
|
2
|
+
readonly id: "cointegration.pair";
|
|
3
|
+
readonly kind: "read";
|
|
4
|
+
readonly visibility: readonly ["cli", "opencode"];
|
|
5
|
+
readonly description: "Calcula cointegração para análise de Long & Short entre dois ativos: z-score, beta, p-valor, meia-vida e intervalos de confiança.";
|
|
6
|
+
readonly http: {
|
|
7
|
+
readonly method: "POST";
|
|
8
|
+
readonly path: "/api/desk/tools/cointegration/pair";
|
|
9
|
+
readonly inputMode: "json_body";
|
|
10
|
+
readonly schemas: {
|
|
11
|
+
readonly request: "CointegrationPairRequest";
|
|
12
|
+
readonly response: "CointegrationPairResponse";
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
readonly cli: {
|
|
16
|
+
readonly group: "cointegration";
|
|
17
|
+
readonly command: "pair";
|
|
18
|
+
readonly summary: "Calcula cointegração para análise de Long & Short.";
|
|
19
|
+
readonly positional: readonly [{
|
|
20
|
+
readonly kind: "positional";
|
|
21
|
+
readonly name: "base_asset";
|
|
22
|
+
readonly type: "string";
|
|
23
|
+
readonly required: true;
|
|
24
|
+
readonly placeholder: "TICKER";
|
|
25
|
+
readonly description: "Ticker do ativo base, por exemplo PETR4.";
|
|
26
|
+
}, {
|
|
27
|
+
readonly kind: "positional";
|
|
28
|
+
readonly name: "comparison_asset";
|
|
29
|
+
readonly type: "string";
|
|
30
|
+
readonly required: true;
|
|
31
|
+
readonly placeholder: "TICKER";
|
|
32
|
+
readonly description: "Ticker do ativo de comparação, por exemplo VALE3.";
|
|
33
|
+
}];
|
|
34
|
+
readonly options: readonly [{
|
|
35
|
+
readonly kind: "option";
|
|
36
|
+
readonly flag: "--window";
|
|
37
|
+
readonly name: "window";
|
|
38
|
+
readonly type: "integer";
|
|
39
|
+
readonly required: false;
|
|
40
|
+
readonly placeholder: "N";
|
|
41
|
+
readonly defaultValue: 250;
|
|
42
|
+
readonly description: "Janela de candles para a regressão. Padrão 250 dias úteis.";
|
|
43
|
+
}];
|
|
44
|
+
readonly examples: readonly ["quantbrasil cointegration pair PETR4 VALE3", "quantbrasil cointegration pair PETR4 VALE3 --window 120", "quantbrasil cointegration pair PETR4 VALE3 --json"];
|
|
45
|
+
};
|
|
46
|
+
readonly tool: {
|
|
47
|
+
readonly name: "qb_cointegration_pair";
|
|
48
|
+
readonly title: "Calcular cointegração para Long & Short";
|
|
49
|
+
};
|
|
50
|
+
readonly outputModes: readonly ["json", "human"];
|
|
51
|
+
}];
|
|
52
|
+
//# sourceMappingURL=cointegration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cointegration.d.ts","sourceRoot":"","sources":["../../src/capabilities/cointegration.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA+Dc,CAAC"}
|