Skip to content

Commit 44b5b3d

Browse files
Patrick BanksPatrick Banks
authored andcommitted
Generate a more random trade ID
1 parent d0c711c commit 44b5b3d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@types/telebot": "1.2.2",
4848
"@typescript-eslint/eslint-plugin": "4.22.0",
4949
"@typescript-eslint/parser": "4.22.0",
50+
"@types/uuid": "^8.3.1",
5051
"axios": "0.21.1",
5152
"bignumber.js": "9.0.1",
5253
"binance-api-node": "0.10.41",

src/trader/trader.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import BigNumber from "bignumber.js"
22
import { Balances, Dictionary, Market, Order } from "ccxt"
33
import PQueue from "p-queue"
4+
import crypto from "crypto"
5+
import { v4 as uuidv4 } from 'uuid'
46

57
import logger from "../logger"
68
import {
@@ -1949,7 +1951,7 @@ async function createTradeOpen(tradingData: TradingData): Promise<TradeOpen> {
19491951

19501952
// Create the new trade
19511953
return Promise.resolve({
1952-
id: "T" + Date.now(), // Generate a temporary internal ID, because we only get one from the NBT Hub when reloading the payload
1954+
id: newTradeID(), // Generate a temporary internal ID, because we only get one from the NBT Hub when reloading the payload
19531955
isStopped: false,
19541956
positionType: tradingData.signal.positionType!,
19551957
tradingType: tradingData.strategy.tradingType,
@@ -1969,6 +1971,14 @@ async function createTradeOpen(tradingData: TradingData): Promise<TradeOpen> {
19691971
})
19701972
}
19711973

1974+
// Generate a shortened MD5 hash of a UUID
1975+
function newTradeID(): string {
1976+
var md5sum = crypto.createHash('md5');
1977+
md5sum.update(uuidv4());
1978+
// 12 characters should still be unique enough for our purposes
1979+
return md5sum.digest('hex').substr(0, 12)
1980+
}
1981+
19721982
// Get the list of wallets that are applicable for the market and trade position, sorted by priority
19731983
function getPreferredWallets(market: Market, positionType = PositionType.LONG) {
19741984
const primary = env().PRIMARY_WALLET.toLowerCase() as WalletType // Primary wallet for reference balance

0 commit comments

Comments
 (0)