Skip to content

Commit

Permalink
feat(cli): add stop command
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed May 10, 2024
1 parent fea3acb commit 0def64d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./grid-lines";
export * from "./run-backtest";
export * from "./run-trading";
export * from "./stop-command";
70 changes: 70 additions & 0 deletions packages/cli/src/api/stop-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { templates } from "@opentrader/bot-templates";
import { xprisma } from "@opentrader/db/dist";
import { logger } from "@opentrader/logger";
import { BotProcessing } from "@opentrader/processing";
import type { CommandResult, ConfigName } from "../types";
import { readBotConfig, readExchangesConfig } from "../config";

export async function stopCommand(
options: {
config: ConfigName;
},
): Promise<CommandResult> {
const config = readBotConfig(options.config);
logger.debug(config, "Parsed bot config");

const exchangesConfig = readExchangesConfig(options.config);
logger.debug(exchangesConfig, "Parsed exchanges config");

const bot = await xprisma.bot.custom.findUnique({
where: {
label: config.label,
}
});

if (!bot) {
logger.info(`Bot "${config.label}" does not exists. Nothing to stop`);

return {
result: undefined,
};
}

const strategyExists = bot.template in templates;
if (!strategyExists) {
const availableStrategies = Object.keys(templates).join(", ");
logger.info(
`Strategy "${bot.template}" does not exists. Available strategies: ${availableStrategies}`,
);

return {
result: undefined,
};
}

logger.info(`Processing stop command for bot "${bot.label}"...`);
await stopBot(bot.id);
logger.info(`Command stop processed successfully for bot "${bot.label}"`);

return {
result: undefined,
};
}

async function stopBot(botId: number) {
const botProcessor = await BotProcessing.fromId(botId);
await botProcessor.processStopCommand();

await disableBot(botId);
}

async function disableBot(botId: number) {
await xprisma.bot.custom.update({
where: {
id: botId,
},
data: {
enabled: false,
},
});
}
16 changes: 16 additions & 0 deletions packages/cli/src/commands/stop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Command, Option } from "commander";
import { DEFAULT_CONFIG_NAME } from "../config";
import { handle } from "../utils/command";
import * as api from "../api";

export function addStopCommand(program: Command) {
program
.command("stop")
.description("Process stop command")
.addOption(
new Option("-c, --config <config>", "Config file").default(
DEFAULT_CONFIG_NAME,
),
)
.action(handle(api.stopCommand));
}
2 changes: 2 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Command } from "commander";
import { addStopCommand } from "./commands/stop";
import { addBacktestCommand } from "./commands/backtest";
import { addGridLinesCommand } from "./commands/grid-lines";
import { addTradeCommand } from "./commands/trade";
Expand All @@ -13,5 +14,6 @@ program
addBacktestCommand(program);
addGridLinesCommand(program);
addTradeCommand(program);
addStopCommand(program);

program.parse();

0 comments on commit 0def64d

Please sign in to comment.