-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
97 lines (84 loc) · 3.46 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
const redis = require('redis')
const { promisify } = require('util')
const Exchange = require('./exchange')
class TradingBot {
constructor(exchange, config) {
if (!(exchange instanceof Exchange)) throw new Error('Invalid exchange instance')
this.exchange = exchange
this.config = config
this.redisClient = redis.createClient(config.redis)
this.getAsync = promisify(this.redisClient.get).bind(this.redisClient)
this.setAsync = promisify(this.redisClient.set).bind(this.redisClient)
}
async isTradeOpen() {
const tradeState = await this.getAsync(this.config.tradeStateKey)
return tradeState === 'open'
}
async setTradeState(state) {
await this.setAsync(this.config.tradeStateKey, state)
}
async runSwingTradingStrategy() {
// Fetch OHLCV data
const ohlcv = await this.exchange.fetchOHLCV(this.config.symbol, this.config.timeframe)
if (!ohlcv || ohlcv.length === 0) {
console.log('No OHLCV data available.')
return
}
// Calculate Fibonacci levels
const high = Math.max(...ohlcv.map(candle => candle[2]))
const low = Math.min(...ohlcv.map(candle => candle[3]))
const closingPrices = ohlcv.map(candle => candle[4])
const currentPrice = closingPrices[closingPrices.length - 1]
const fibonacciLevels = this.calculateFibonacciLevels(high, low)
// Check for buy or sell signals
if (currentPrice > fibonacciLevels[fibonacciLevels.length - 1]) {
console.log('Buy signal triggered at Fibonacci level')
// Execute buy order logic here
} else if (currentPrice < fibonacciLevels[0]) {
console.log('Sell signal triggered at Fibonacci level')
// Execute sell order logic here
}
}
async runScalpingStrategy() {
// Fetch OHLCV data
const ohlcv = await this.exchange.fetchOHLCV(this.config.symbol, this.config.timeframe)
if (!ohlcv || ohlcv.length === 0) {
console.log('No OHLCV data available.')
return
}
// Calculate scalping thresholds
const closingPrices = ohlcv.map(candle => candle[4])
const currentPrice = closingPrices[closingPrices.length - 1]
const buyDrop = currentPrice * (1 - this.config.scalping.buyDropPercent)
const sellRise = currentPrice * (1 + this.config.scalping.sellRisePercent)
// Check for buy or sell signals
if (currentPrice < buyDrop) {
console.log('Buy signal triggered at scalping strategy')
// Execute buy order logic here
} else if (currentPrice > sellRise) {
console.log('Sell signal triggered at scalping strategy')
// Execute sell order logic here
}
}
async run() {
if (await this.isTradeOpen()) {
console.log('A trade is currently open, waiting for it to close before starting a new one.')
return
}
try {
await this.runSwingTradingStrategy()
await this.runScalpingStrategy()
} finally {
await this.setTradeState('closed')
}
}
calculateFibonacciLevels(high, low) {
const levels = this.config.swingTrading.fibonacciLevels
return levels.map(level => {
const diff = high - low
const levelPrice = high - diff * level
return levelPrice
})
}
}
module.exports = TradingBot