-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
144 lines (132 loc) ยท 5.43 KB
/
bot.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const { parseEther } = require("@ethersproject/units")
const sleep = require('util').promisify(setTimeout)
const { getStats, predictionContract, getBNBPrice, checkBalance, reduceWaitingTimeByTwoBlocks, saveRound } = require("./lib")
const { TradingViewScan, SCREENERS_ENUM, EXCHANGES_ENUM, INTERVALS_ENUM } = require("trading-view-recommends-parser-nodejs")
// Global Config
const GLOBAL_CONFIG = {
BET_AMOUNT: 0.2, // in USD
DAILY_GOAL: 5, // in USD,
WAITING_TIME: 265000, // in Miliseconds (4.3 Minutes)
THRESHOLD: 60 // Minimum % of certainty of signals (50 - 100)
}
//Bet UP
const betUp = async (amount, epoch) => {
try {
const tx = await predictionContract.betBull(epoch, {
value: parseEther(amount.toFixed(18).toString()),
})
await tx.wait()
console.log(`๐ค Successful bet of ${amount} BNB to UP ๐`)
} catch (error) {
console.log("Transaction Error", error)
GLOBAL_CONFIG.WAITING_TIME = reduceWaitingTimeByTwoBlocks(GLOBAL_CONFIG.WAITING_TIME)
}
}
//Bet DOWN
const betDown = async (amount, epoch) => {
try {
const tx = await predictionContract.betBear(epoch, {
value: parseEther(amount.toFixed(18).toString()),
})
await tx.wait()
console.log(`๐ค Successful bet of ${amount} BNB to DOWN ๐`)
} catch (error) {
console.log("Transaction Error", error)
GLOBAL_CONFIG.WAITING_TIME = reduceWaitingTimeByTwoBlocks(GLOBAL_CONFIG.WAITING_TIME)
}
}
//Check Signals
const getSignals = async () => {
//1 Minute signals
let resultMin = await new TradingViewScan(
SCREENERS_ENUM["crypto"],
EXCHANGES_ENUM["BINANCE"],
"BNBUSDT",
INTERVALS_ENUM["1m"]
).analyze()
let minObj = JSON.stringify(resultMin.summary)
let minRecomendation = JSON.parse(minObj)
//5 Minute signals
let resultMed = await new TradingViewScan(
SCREENERS_ENUM["crypto"],
EXCHANGES_ENUM["BINANCE"],
"BNBUSDT",
INTERVALS_ENUM["5m"]
).analyze()
let medObj = JSON.stringify(resultMed.summary)
let medRecomendation = JSON.parse(medObj)
//Average signals
if (minRecomendation && medRecomendation) {
let averageBuy = (parseInt(minRecomendation.BUY) + parseInt(medRecomendation.BUY)) / 2
let averageSell = (parseInt(minRecomendation.SELL) + parseInt(medRecomendation.SELL)) / 2
let averageNeutral = (parseInt(minRecomendation.NEUTRAL) + parseInt(medRecomendation.NEUTRAL)) / 2
return {
buy: averageBuy,
sell: averageSell,
neutral: averageNeutral
}
} else {
return false
}
}
//Percentage difference
const percentage = (a, b) => {
return parseInt(100 * a / (a + b))
}
//Strategy of betting
const strategy = async (minAcurracy, epoch) => {
let BNBPrice
let earnings = await getStats()
if (earnings.profit_USD >= GLOBAL_CONFIG.DAILY_GOAL) {
console.log("๐ง Daily goal reached. Shuting down... โจ ")
process.exit()
}
try {
BNBPrice = await getBNBPrice()
} catch (err) {
return
}
let signals = await getSignals()
if (signals) {
if (signals.buy > signals.sell && percentage(signals.buy, signals.sell) > minAcurracy) {
console.log(`${epoch.toString()} ๐ฎ Prediction: UP ๐ข ${percentage(signals.buy, signals.sell)}%`)
await betUp((GLOBAL_CONFIG.BET_AMOUNT / BNBPrice), epoch)
await saveRound(epoch.toString(), [{ round: epoch.toString(), betAmount: (GLOBAL_CONFIG.BET_AMOUNT / BNBPrice).toString(), bet: "bull" }])
} else if (signals.sell > signals.buy && percentage(signals.sell, signals.buy) > minAcurracy) {
console.log(`${epoch.toString()} ๐ฎ Prediction: DOWN ๐ด ${percentage(signals.sell, signals.buy)}%`)
await betDown((GLOBAL_CONFIG.BET_AMOUNT / BNBPrice), epoch)
await saveRound(epoch.toString(), [{ round: epoch.toString(), betAmount: (GLOBAL_CONFIG.BET_AMOUNT / BNBPrice).toString(), bet: "bear" }])
} else {
let lowPercentage
if (signals.buy > signals.sell) {
lowPercentage = percentage(signals.buy, signals.sell)
} else {
lowPercentage = percentage(signals.sell, signals.buy)
}
console.log("Waiting for next round ๐", lowPercentage + "%")
}
} else {
console.log("Error obtaining signals")
}
}
//Check balance
checkBalance(GLOBAL_CONFIG.AMOUNT_TO_BET)
console.log('๐ค Welcome! Waiting for next round...')
// fixed event listening on 29/11/22
//Betting
predictionContract.on("StartRound", async (epoch) => {
console.log("๐ฅ Starting round " + epoch.toString())
console.log("๐ Waiting " + (GLOBAL_CONFIG.WAITING_TIME / 60000).toFixed(1) + " minutes")
await sleep(GLOBAL_CONFIG.WAITING_TIME)
await strategy(GLOBAL_CONFIG.THRESHOLD, epoch)
})
//Show stats
predictionContract.on("EndRound", async (epoch) => {
await saveRound(epoch)
let stats = await getStats()
console.log('--------------------------------')
console.log(`๐ Fortune: ${stats.percentage}`)
console.log(`๐ ${stats.win}|${stats.loss} ๐ `)
console.log(`๐ฐ Profit: ${stats.profit_USD.toFixed(3)} USD`)
console.log('--------------------------------')
})