Initial commit: A-share stock analysis project with screening, backtesting, and multi-factor analysis tools
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { tool } from "@opencode-ai/plugin";
|
||||
import path from "path";
|
||||
|
||||
const SCRIPTS_DIR = (ctx: { worktree: string }) =>
|
||||
path.join(ctx.worktree, "scripts");
|
||||
|
||||
async function runPython(
|
||||
ctx: { worktree: string },
|
||||
fn: string,
|
||||
args: Record<string, unknown>,
|
||||
): Promise<string> {
|
||||
const scriptPath = path.join(SCRIPTS_DIR(ctx), "backtest_engine.py");
|
||||
const argsJson = JSON.stringify(args);
|
||||
const result = await Bun.$`python3 ${scriptPath} ${fn} ${argsJson}`.text();
|
||||
return result.trim();
|
||||
}
|
||||
|
||||
export const run = tool({
|
||||
description: "Run a backtest for a trading strategy on specified stock universe and period",
|
||||
args: {
|
||||
strategy_name: tool.schema.string().optional().describe("Name of a predefined strategy, or omit to use custom rules"),
|
||||
entry_rule: tool.schema.string().optional().describe("Custom entry condition (Python expression using df columns like close, ma20, ma60, volume, etc.)"),
|
||||
exit_rule: tool.schema.string().optional().describe("Custom exit condition (Python expression) or 'stop_loss:0.05,take_profit:0.15,max_hold:20'"),
|
||||
universe: tool.schema.enum(["hs300", "zz500", "all", "custom"]).default("hs300").describe("Stock universe to test on"),
|
||||
symbols: tool.schema.string().optional().describe("Comma-separated stock codes for custom universe"),
|
||||
start_date: tool.schema.string().default("20210101").describe("Start date YYYYMMDD"),
|
||||
end_date: tool.schema.string().default("20251231").describe("End date YYYYMMDD"),
|
||||
},
|
||||
async execute(args, context) {
|
||||
return runPython(context, "run", args);
|
||||
},
|
||||
});
|
||||
|
||||
export const predefined = tool({
|
||||
description: "List all predefined backtest strategies with descriptions",
|
||||
args: {},
|
||||
async execute(args, context) {
|
||||
return runPython(context, "predefined", args);
|
||||
},
|
||||
});
|
||||
|
||||
export const compare = tool({
|
||||
description: "Compare multiple predefined strategies on the same universe and period",
|
||||
args: {
|
||||
strategies: tool.schema.string().describe("Comma-separated strategy names to compare"),
|
||||
universe: tool.schema.enum(["hs300", "zz500", "all"]).default("hs300").describe("Stock universe"),
|
||||
start_date: tool.schema.string().default("20210101").describe("Start date YYYYMMDD"),
|
||||
end_date: tool.schema.string().default("20251231").describe("End date YYYYMMDD"),
|
||||
},
|
||||
async execute(args, context) {
|
||||
return runPython(context, "compare", args);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user