-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (66 loc) · 2.23 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import schedule from "node-schedule";
import { nodeCache } from "./src/cache.js";
import { errorHandler, sendLineNotify } from "./src/common.js";
import { getAvailableBalance, getPositionType } from "./src/helpers.js";
import { closePosition, openPosition } from "./src/trade.js";
import { getCachedKlineData } from "./src/cached-data.js";
import { rsi } from "technicalindicators";
import { getSignal } from "./src/signal.js";
import {
RSI_PERIOD,
RSI_LONG_LEVEL,
RSI_SHORT_LEVEL,
LEVERAGE
} from "./configs/trade-config.js";
const logBalance = async () => {
const availableBalance = await getAvailableBalance();
await sendLineNotify(`Balance: ${availableBalance}`);
};
const setTradeConfigs = async () => {
console.log(new Date().toLocaleString());
nodeCache.mset([
{ key: "rsiPeriod", val: RSI_PERIOD, ttl: 0 },
{ key: "rsiLongLevel", val: RSI_LONG_LEVEL, ttl: 0 },
{ key: "rsiShortLevel", val: RSI_SHORT_LEVEL, ttl: 0 },
{ key: "leverage", val: LEVERAGE, ttl: 0 }
]);
await logBalance();
console.log("============================================================");
};
await setTradeConfigs();
const getTradeSignal = async () => {
const positionType = await getPositionType();
const cachedKlineData = await getCachedKlineData();
const values = cachedKlineData.map((kline) => kline.closePrice);
const rsiPeriod = nodeCache.get("rsiPeriod");
const rsiLongLevel = nodeCache.get("rsiLongLevel");
const rsiShortLevel = nodeCache.get("rsiShortLevel");
const rsiData = rsi({ period: rsiPeriod, values });
const preRsi = rsiData[rsiData.length - 2];
return getSignal({
positionType,
preRsi,
rsiLongLevel,
rsiShortLevel
});
};
const executeStrategy = async () => {
try {
console.log(new Date().toLocaleString());
const tradeSignal = await getTradeSignal();
if (tradeSignal === "NONE") {
console.log("NONE");
}
if (tradeSignal === "OPEN_LONG") {
await openPosition("BUY");
}
if (tradeSignal === "CLOSE_LONG") {
await closePosition("SELL");
await logBalance();
}
console.log("============================================================");
} catch (error) {
await errorHandler(error);
}
};
schedule.scheduleJob("1 * * * *", executeStrategy);