From c01cae412f7516357fd03fff3618a17c626aa776 Mon Sep 17 00:00:00 2001 From: KienVT9 Date: Wed, 16 Jul 2025 23:12:09 +0700 Subject: [PATCH] update --- src/schedule/index.ts | 12 ++++++----- src/services/indicatorService.ts | 4 ++-- test.ts | 35 +++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/schedule/index.ts b/src/schedule/index.ts index 197d18c..5957d57 100644 --- a/src/schedule/index.ts +++ b/src/schedule/index.ts @@ -4,6 +4,7 @@ import { Order } from "../dao/order"; import { sendLarkMessage } from "../services/messageService"; import { BybitService } from "../services/bybitService"; import { Analysis } from "../dao/analysis"; +import { Candle } from "../dao/candles"; function sendMessage(message: string) { sendLarkMessage("oc_f9b2e8f0309ecab0c94e3e134b0ddd29", message); @@ -62,9 +63,10 @@ export const eventHandlerFuture: EventHandler = { `Sell ${order.symbol} ${order.interval}M ${reason} ${order.entry}` ); }, - onEvent: async (eventType: EventType, analysis: Analysis) => { + onEvent: async (eventType: EventType, {candle, analysis}: {candle: Candle, analysis: Analysis}) => { if (eventType === "HighVolatility") { const positions = await bybitService.listPositions({ + category: "linear", symbol: analysis.symbol, }); if (positions.length > 0) { @@ -76,7 +78,7 @@ export const eventHandlerFuture: EventHandler = { symbol: analysis.symbol, side: "Sell", orderType: "Limit", - price: Number(analysis.currentBB.upper).toFixed(2), + price: candle.close > analysis.currentBB.upper ? Number(candle.close).toFixed(2) : Number(analysis.currentBB.upper).toFixed(2), qty: halfSize, }); sendMessage(`Future Căt nửa ${analysis.symbol} ${analysis.interval}M ${analysis.currentBB.upper} ${halfSize}`); @@ -88,7 +90,7 @@ export const eventHandlerFuture: EventHandler = { symbol: analysis.symbol, side: "Buy", orderType: "Limit", - price: Number(analysis.currentBB.lower).toFixed(2), + price: candle.close < analysis.currentBB.lower ? Number(candle.close).toFixed(2) : Number(analysis.currentBB.lower).toFixed(2), qty: halfSize, }); sendMessage(`Future Căt nửa ${analysis.symbol} ${analysis.interval}M ${analysis.currentBB.lower} ${halfSize}`); @@ -132,7 +134,7 @@ const eventHandlerSpot: EventHandler = { reason: ${reason} `); }, - onEvent: async (eventType: EventType, analysis: Analysis) => { + onEvent: async (eventType: EventType, {candle, analysis}: {candle: Candle, analysis: Analysis}) => { }, }; @@ -161,7 +163,7 @@ const eventHandlerNotification: EventHandler = { reason: ${reason} `); }, - onEvent: async (eventType: EventType, analysis: Analysis) => { + onEvent: async (eventType: EventType, {candle, analysis}: {candle: Candle, analysis: Analysis}) => { }, }; diff --git a/src/services/indicatorService.ts b/src/services/indicatorService.ts index 62983b1..a769a05 100644 --- a/src/services/indicatorService.ts +++ b/src/services/indicatorService.ts @@ -16,7 +16,7 @@ export type EventType = "HighVolatility" | "PinBar" | "EmaCross" | "MacdCross" | export interface EventHandler { onBuy: (candle: Order, reason: string) => void; onSell: (candle: Order, reason: string) => void; - onEvent: (eventType: EventType, analysis: Analysis) => void; + onEvent: (eventType: EventType, {candle, analysis}: {candle: Candle, analysis: Analysis}) => void; } export class IndicatorService { @@ -302,7 +302,7 @@ export class IndicatorService { } if (analysis.isHighVolatility) { - eventHandler.onEvent("HighVolatility", analysis); + eventHandler.onEvent("HighVolatility", {candle: candles[0], analysis}); } } diff --git a/test.ts b/test.ts index d039474..4e06656 100644 --- a/test.ts +++ b/test.ts @@ -22,8 +22,41 @@ function toTimestamp(strTime: string): number { (async () => { // await analyzeCandlesJob("ETHUSDT", "15", toTimestamp("2025-07-08 23:59:59")); - await analyzeCandlesJob("BTCUSDT", "15", undefined, toTimestamp("2025-07-16 22:14:59")); + // await analyzeCandlesJob("BTCUSDT", "15", undefined, toTimestamp("2025-07-16 22:14:59")); + const positions = await bybitService.listPositions({ + category: "linear", + symbol: "BTCUSDT", + }); + console.log(positions); + if (positions.length > 0) { + const position = positions[0]; + console.log(position); + if (position.side === "Buy") { + const halfSize = (Number(position.size) / 2).toFixed(2); + console.log(halfSize); + const res = await bybitService.submitOrder({ + category: "linear", + symbol: "BTCUSDT", + side: "Sell", + orderType: "Limit", + price: "119310.2", + qty: halfSize, + }); + console.log(res); + } + if (position.side === "Sell") { + const halfSize = (Number(position.size) / 2).toFixed(2); + await bybitService.submitOrder({ + category: "linear", + symbol: "BTCUSDT", + side: "Buy", + orderType: "Limit", + price: "119310.2", + qty: halfSize, + }); + } + } // const balance = await bybitService.getBalance(); // console.log(balance.result.list[0].totalEquity); })();