aider: implement simple cli in ink for running backtests
This commit is contained in:
+6
-13
@@ -3,7 +3,7 @@ import { database as calendarDatabase } from "./AggregateDatabase/Calendar/optio
|
||||
import type { CalendarKey } from "./AggregateDatabase/Calendar/interfaces.js";
|
||||
import { nextDate } from "./lib/utils/nextDate.js";
|
||||
|
||||
type BacktestInput = {
|
||||
export type BacktestInput = {
|
||||
symbol: string;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
@@ -11,6 +11,7 @@ type BacktestInput = {
|
||||
historicalProbabilityOfSuccess?: number;
|
||||
initialBuyingPower?: number;
|
||||
};
|
||||
|
||||
export async function backtest({
|
||||
symbol,
|
||||
startDate,
|
||||
@@ -138,16 +139,8 @@ export async function backtest({
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Ending Buying Power:", buyingPower);
|
||||
console.log("Portfolio:", portfolio.values());
|
||||
}
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length > 0) {
|
||||
if (args.length < 3) {
|
||||
console.error("Please provide at least 3 command line arguments.");
|
||||
process.exit(1);
|
||||
} else {
|
||||
await backtest({ symbol: args[0], startDate: args[1], endDate: args[2] });
|
||||
}
|
||||
return {
|
||||
endingBuyingPower: buyingPower,
|
||||
portfolio: Array.from(portfolio.values()),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import { render, Box, Text, useInput, useApp } from "ink";
|
||||
import TextInput from "ink-text-input";
|
||||
import type { BacktestInput } from "./backtest.js";
|
||||
import { backtest } from "./backtest.js";
|
||||
|
||||
const App = () => {
|
||||
const [step, setStep] = useState(0);
|
||||
const [input, setInput] = useState<Partial<BacktestInput>>({});
|
||||
const [result, setResult] = useState<string>("");
|
||||
const { exit } = useApp();
|
||||
|
||||
const steps = [
|
||||
{ prompt: "Enter symbol:", key: "symbol" },
|
||||
{ prompt: "Enter start date (YYYY-MM-DD):", key: "startDate" },
|
||||
{ prompt: "Enter end date (YYYY-MM-DD):", key: "endDate" },
|
||||
{
|
||||
prompt: "Enter historical probability of success (0-1, default 0.8):",
|
||||
key: "historicalProbabilityOfSuccess",
|
||||
},
|
||||
{
|
||||
prompt: "Enter initial buying power (default 2000):",
|
||||
key: "initialBuyingPower",
|
||||
},
|
||||
];
|
||||
|
||||
useInput((input, key) => {
|
||||
if (key.escape) {
|
||||
exit();
|
||||
}
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (step === steps.length) {
|
||||
const runBacktest = async () => {
|
||||
try {
|
||||
const backtestResult = await backtest(input as BacktestInput);
|
||||
setResult(JSON.stringify(backtestResult, null, 2));
|
||||
} catch (error) {
|
||||
setResult(`Error: ${error.message}`);
|
||||
}
|
||||
};
|
||||
runBacktest();
|
||||
}
|
||||
}, [step, input]);
|
||||
|
||||
const handleSubmit = (value: string) => {
|
||||
const currentStep = steps[step];
|
||||
let parsedValue: string | number = value;
|
||||
|
||||
if (
|
||||
currentStep.key === "historicalProbabilityOfSuccess" ||
|
||||
currentStep.key === "initialBuyingPower"
|
||||
) {
|
||||
parsedValue = Number.parseFloat(value) || undefined;
|
||||
}
|
||||
|
||||
setInput({ ...input, [currentStep.key]: parsedValue });
|
||||
setStep(step + 1);
|
||||
};
|
||||
|
||||
if (step < steps.length) {
|
||||
return (
|
||||
<Box flexDirection="column">
|
||||
<Text>{steps[step].prompt}</Text>
|
||||
<TextInput
|
||||
value={(input[steps[step].key] as string) || ""}
|
||||
onChange={(value) =>
|
||||
setInput((prev) => ({ ...prev, [steps[step].key]: value }))
|
||||
}
|
||||
onSubmit={handleSubmit}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Box flexDirection="column">
|
||||
<Text>Backtest Results:</Text>
|
||||
<Text>{result}</Text>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
render(<App />);
|
||||
Reference in New Issue
Block a user