This commit is contained in:
KienVT9 2025-07-16 23:12:09 +07:00
parent 8588eb9720
commit c01cae412f
3 changed files with 43 additions and 8 deletions

View file

@ -4,6 +4,7 @@ import { Order } from "../dao/order";
import { sendLarkMessage } from "../services/messageService"; import { sendLarkMessage } from "../services/messageService";
import { BybitService } from "../services/bybitService"; import { BybitService } from "../services/bybitService";
import { Analysis } from "../dao/analysis"; import { Analysis } from "../dao/analysis";
import { Candle } from "../dao/candles";
function sendMessage(message: string) { function sendMessage(message: string) {
sendLarkMessage("oc_f9b2e8f0309ecab0c94e3e134b0ddd29", message); sendLarkMessage("oc_f9b2e8f0309ecab0c94e3e134b0ddd29", message);
@ -62,9 +63,10 @@ export const eventHandlerFuture: EventHandler = {
`Sell ${order.symbol} ${order.interval}M ${reason} ${order.entry}` `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") { if (eventType === "HighVolatility") {
const positions = await bybitService.listPositions({ const positions = await bybitService.listPositions({
category: "linear",
symbol: analysis.symbol, symbol: analysis.symbol,
}); });
if (positions.length > 0) { if (positions.length > 0) {
@ -76,7 +78,7 @@ export const eventHandlerFuture: EventHandler = {
symbol: analysis.symbol, symbol: analysis.symbol,
side: "Sell", side: "Sell",
orderType: "Limit", 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, qty: halfSize,
}); });
sendMessage(`Future Căt nửa ${analysis.symbol} ${analysis.interval}M ${analysis.currentBB.upper} ${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, symbol: analysis.symbol,
side: "Buy", side: "Buy",
orderType: "Limit", 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, qty: halfSize,
}); });
sendMessage(`Future Căt nửa ${analysis.symbol} ${analysis.interval}M ${analysis.currentBB.lower} ${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} 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} reason: ${reason}
`); `);
}, },
onEvent: async (eventType: EventType, analysis: Analysis) => { onEvent: async (eventType: EventType, {candle, analysis}: {candle: Candle, analysis: Analysis}) => {
}, },
}; };

View file

@ -16,7 +16,7 @@ export type EventType = "HighVolatility" | "PinBar" | "EmaCross" | "MacdCross" |
export interface EventHandler { export interface EventHandler {
onBuy: (candle: Order, reason: string) => void; onBuy: (candle: Order, reason: string) => void;
onSell: (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 { export class IndicatorService {
@ -302,7 +302,7 @@ export class IndicatorService {
} }
if (analysis.isHighVolatility) { if (analysis.isHighVolatility) {
eventHandler.onEvent("HighVolatility", analysis); eventHandler.onEvent("HighVolatility", {candle: candles[0], analysis});
} }
} }

35
test.ts
View file

@ -22,8 +22,41 @@ function toTimestamp(strTime: string): number {
(async () => { (async () => {
// await analyzeCandlesJob("ETHUSDT", "15", toTimestamp("2025-07-08 23:59:59")); // 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(); // const balance = await bybitService.getBalance();
// console.log(balance.result.list[0].totalEquity); // console.log(balance.result.list[0].totalEquity);
})(); })();