-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters