From 288b6ca5baa28014ee0e6907c2bc6271ebfe9756 Mon Sep 17 00:00:00 2001 From: bludnic Date: Sat, 11 May 2024 22:50:16 +0100 Subject: [PATCH] chore(templates): add debug strategy --- package.json | 3 +- packages/bot-templates/package.json | 1 + packages/bot-templates/src/templates/debug.ts | 47 +++++++++++++++++++ packages/bot-templates/src/templates/index.ts | 1 + 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 packages/bot-templates/src/templates/debug.ts diff --git a/package.json b/package.json index 4f187109..3ba824ed 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "lint:fix": "turbo run lint:fix", "format": "prettier --write \"**/*.{ts,tsx,md}\"", "front": "turbo run dev --filter=frontend", - "postinstall": "pnpm link --global" + "postinstall": "pnpm link --global", + "debug": "ts-node --transpile-only packages/cli/src/index.ts trade debug" }, "bin": { "opentrader": "./bin/opentrader.sh" diff --git a/packages/bot-templates/package.json b/packages/bot-templates/package.json index f372fdaa..48a4293a 100644 --- a/packages/bot-templates/package.json +++ b/packages/bot-templates/package.json @@ -22,6 +22,7 @@ "@opentrader/bot-processor": "workspace:*", "@opentrader/exchanges": "workspace:*", "@opentrader/indicators": "workspace:*", + "@opentrader/logger": "workspace:*", "@opentrader/tools": "workspace:*", "zod": "3.22.4" } diff --git a/packages/bot-templates/src/templates/debug.ts b/packages/bot-templates/src/templates/debug.ts new file mode 100644 index 00000000..26fc4b3c --- /dev/null +++ b/packages/bot-templates/src/templates/debug.ts @@ -0,0 +1,47 @@ +import { z } from "zod"; +import type { ISymbolInfo } from "@opentrader/types"; +import type { IExchange } from "@opentrader/exchanges"; +import type { IBotConfiguration, TBotContext } from "@opentrader/bot-processor"; +import { useExchange } from "@opentrader/bot-processor"; +import { logger } from "@opentrader/logger"; + +export function* debug(ctx: TBotContext) { + const { config: bot, onStart, onStop } = ctx; + const exchange: IExchange = yield useExchange(); + + logger.debug("Hello world"); + + if (onStart) { + logger.info( + `[DebugStrategy] Bot started. Using ${exchange.exchangeCode} exchange`, + ); + + if (bot.settings.fetchSymbols) { + const symbols: ISymbolInfo[] = yield exchange.getSymbols(); + logger.info(`[DebugStrategy] Fetched ${symbols.length} symbols`); + } else { + logger.info( + `[DebugStrategy] Skipping fetching symbols (fetchSymbols=false)`, + ); + } + + return; + } + if (onStop) { + logger.info(`[DebugStrategy] Bot stopped`); + return; + } + + logger.info(`[DebugStrategy] Run bot template`); + + logger.info(`[DebugStrategy] Bot template executed successfully`); +} + +debug.displayName = "Debug Strategy"; +debug.schema = z.object({ + fetchSymbols: z.boolean().optional(), +}); + +export type DebugStrategyConfig = IBotConfiguration< + z.infer +>; diff --git a/packages/bot-templates/src/templates/index.ts b/packages/bot-templates/src/templates/index.ts index 8a569cb5..81b81aee 100644 --- a/packages/bot-templates/src/templates/index.ts +++ b/packages/bot-templates/src/templates/index.ts @@ -1,3 +1,4 @@ export * from "./grid-bot"; export * from "./grid-bot-lite"; export * from "./low-cap"; +export * from "./debug";