Skip to content

Commit 87839c6

Browse files
committed
fix(TradeManager): trade not destroyed after bot stopped
1 parent 80d038c commit 87839c6

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

packages/bot/src/bot.manager.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { logger } from "@opentrader/logger";
33
import { Bot } from "./bot.js";
44
import { MarketsStream } from "./streams/markets.stream.js";
55
import { OrdersStream } from "./streams/orders.stream.js";
6+
import { TradeManager } from "./trade.manager.js";
67

78
export class BotManager {
89
bots: Bot[] = [];
910

1011
constructor(
1112
private ordersStream: OrdersStream,
1213
private marketsStream: MarketsStream,
14+
private tradeManager: TradeManager,
1315
) {}
1416

1517
async start(id: number) {
@@ -22,7 +24,7 @@ export class BotManager {
2224
include: { exchangeAccount: true },
2325
});
2426

25-
const bot = new Bot(data, this.ordersStream, this.marketsStream);
27+
const bot = new Bot(data, this.ordersStream, this.marketsStream, this.tradeManager);
2628

2729
try {
2830
await bot.start();
@@ -47,7 +49,7 @@ export class BotManager {
4749
where: { id },
4850
include: { exchangeAccount: true },
4951
});
50-
const bot = new Bot(data, this.ordersStream, this.marketsStream);
52+
const bot = new Bot(data, this.ordersStream, this.marketsStream, this.tradeManager);
5153
await bot.stop();
5254

5355
return;

packages/bot/src/bot.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { BotProcessing, getWatchers, shouldRunStrategy } from "@opentrader/proce
88
import { MarketEvent, MarketId, MarketEventType } from "@opentrader/types";
99
import { MarketsStream } from "./streams/markets.stream.js";
1010
import { OrderEvent, OrdersStream } from "./streams/orders.stream.js";
11+
import { TradeManager } from "./trade.manager.js";
1112

1213
type OrderFilledEvent = {
1314
type: typeof MarketEventType.onOrderFilled;
@@ -33,6 +34,7 @@ export class Bot {
3334
public bot: TBotWithExchangeAccount,
3435
private ordersStream: OrdersStream,
3536
private marketsStream: MarketsStream,
37+
private tradeManager: TradeManager,
3638
) {
3739
this.strategy = findStrategy(this.bot.template);
3840
this.queue = cargoQueue<QueueEvent>(this.queueHandler);
@@ -178,6 +180,12 @@ export class Bot {
178180
this.queue.kill();
179181
this.stopped = true;
180182

183+
// Stop all trades executors
184+
const trades = this.tradeManager.trades.filter((trade) => trade.smartTrade.botId === this.bot.id);
185+
for (const trade of trades) {
186+
trade.destroy();
187+
}
188+
181189
// Mark the bot as disabled
182190
this.bot = await xprisma.bot.custom.update({
183191
where: { id: this.bot.id },

packages/bot/src/platform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export class Platform {
3333
this.marketStream = new MarketsStream([]);
3434
this.marketStream.on("market", this.handleMarketEvent);
3535

36-
this.botManager = new BotManager(this.ordersStream, this.marketStream);
3736
this.tradeManager = new TradeManager(this.ordersStream);
37+
this.botManager = new BotManager(this.ordersStream, this.marketStream, this.tradeManager);
3838
}
3939

4040
async bootstrap() {

0 commit comments

Comments
 (0)