Skip to content

Commit

Permalink
fix(TradeManager): trade not destroyed after bot stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Jan 29, 2025
1 parent 80d038c commit 87839c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/bot/src/bot.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { logger } from "@opentrader/logger";
import { Bot } from "./bot.js";
import { MarketsStream } from "./streams/markets.stream.js";
import { OrdersStream } from "./streams/orders.stream.js";
import { TradeManager } from "./trade.manager.js";

export class BotManager {
bots: Bot[] = [];

constructor(
private ordersStream: OrdersStream,
private marketsStream: MarketsStream,
private tradeManager: TradeManager,
) {}

async start(id: number) {
Expand All @@ -22,7 +24,7 @@ export class BotManager {
include: { exchangeAccount: true },
});

const bot = new Bot(data, this.ordersStream, this.marketsStream);
const bot = new Bot(data, this.ordersStream, this.marketsStream, this.tradeManager);

try {
await bot.start();
Expand All @@ -47,7 +49,7 @@ export class BotManager {
where: { id },
include: { exchangeAccount: true },
});
const bot = new Bot(data, this.ordersStream, this.marketsStream);
const bot = new Bot(data, this.ordersStream, this.marketsStream, this.tradeManager);
await bot.stop();

return;
Expand Down
8 changes: 8 additions & 0 deletions packages/bot/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BotProcessing, getWatchers, shouldRunStrategy } from "@opentrader/proce
import { MarketEvent, MarketId, MarketEventType } from "@opentrader/types";
import { MarketsStream } from "./streams/markets.stream.js";
import { OrderEvent, OrdersStream } from "./streams/orders.stream.js";
import { TradeManager } from "./trade.manager.js";

type OrderFilledEvent = {
type: typeof MarketEventType.onOrderFilled;
Expand All @@ -33,6 +34,7 @@ export class Bot {
public bot: TBotWithExchangeAccount,
private ordersStream: OrdersStream,
private marketsStream: MarketsStream,
private tradeManager: TradeManager,
) {
this.strategy = findStrategy(this.bot.template);
this.queue = cargoQueue<QueueEvent>(this.queueHandler);
Expand Down Expand Up @@ -178,6 +180,12 @@ export class Bot {
this.queue.kill();
this.stopped = true;

// Stop all trades executors
const trades = this.tradeManager.trades.filter((trade) => trade.smartTrade.botId === this.bot.id);
for (const trade of trades) {
trade.destroy();
}

// Mark the bot as disabled
this.bot = await xprisma.bot.custom.update({
where: { id: this.bot.id },
Expand Down
2 changes: 1 addition & 1 deletion packages/bot/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class Platform {
this.marketStream = new MarketsStream([]);
this.marketStream.on("market", this.handleMarketEvent);

this.botManager = new BotManager(this.ordersStream, this.marketStream);
this.tradeManager = new TradeManager(this.ordersStream);
this.botManager = new BotManager(this.ordersStream, this.marketStream, this.tradeManager);
}

async bootstrap() {
Expand Down

0 comments on commit 87839c6

Please sign in to comment.