-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
239 lines (212 loc) · 6.69 KB
/
index.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//require('dotenv').config()
const bittrex = require('node-bittrex-api')
const tech = require('technicalindicators')
const Slimbot = require('slimbot');
const slimbot = new Slimbot(process.env['TELEGRAM_BOT_TOKEN']);
//slimbot.startPolling()
//const SMA = require('technicalindicators').SMA;
//const SMA = require('technicalindicators').SMA;
tech.setConfig('precision', 8)
bittrex.options({
'verbose' : false,
'baseUrlv2' : 'https://bittrex.com/Api/v2.0'
});
let candles = []
let rounds = 1
let targetVol = 250
let interval = 'thirtyMin'
let found = false
//let _pairs = pairs(2500)
const RVOL = (vol, period) => {
let relVol = vol.map((cur, i, arr) => {
if(i > period) {
let lookback = vol.slice(i - period, i)
let avgVol = lookback.reduce((acc, val, index) => {
acc += val
if(index === lookback.length - 1)
return acc / lookback.length
return acc
})
//avgVol = (avgVol / lookback.length)
return Math.round(cur / avgVol)
}
})
return relVol
}
const getCandles = (pair) => {
return new Promise ((resolve, reject) => {
bittrex.getcandles({
marketName: pair,
tickInterval: interval
}, (data, err) => {
if(err) {
reject(err)
}
if (data) {
let output = {pair: pair, data: data.result}
resolve(output)
}
})
})
}
const pairs = (minVol) => {
return new Promise((resolve, reject) => {
bittrex.getmarketsummaries((data, err) => {
let result = data.result.filter(cur => {
return cur.BaseVolume > minVol && cur.MarketName.match(/^(BTC)/g)
})
.map(cur => {
return cur.MarketName
})
resolve(result)
})
})
}
const tradePair = (arr) => {
return new Promise((resolve, reject) => {
let input = arr.data.map(x => x.C).slice(-52)
let output
let low = arr.data.map(x => x.L).slice(-3)
let high = arr.data.map(x => x.H).slice(-3)
let open = arr.data.map(x => x.O).slice(-3)
let close = arr.data.map(x => x.C).slice(-3)
let v = arr.data.map(x => x.V)
let ema10 = tech.EMA.calculate({period: 10, values: input}).reverse()
let ema30 = tech.EMA.calculate({period: 30, values: input}).reverse()
let rsi = tech.RSI.calculate({values: input, period: 14}).reverse()
let fromBellow = rsi[1] < rsi[0]
//let threePeriodBull = tech.bullish({open, close, high, low})
//let psar = tech.PSAR.calculate({high: arr.data.map(x => x.H), low: arr.data.map(x => x.L), step: 0.02, max: 0.2}).reverse()
let volume = close[2] > open[2] ? RVOL(v.slice(-52), 20).reverse() : 0
//threePeriodBull ? console.log(arr.pair, 'as bullish pattern') : null
//let bull = tech.threewhitesoldiers({open, close, high, low})
if(volume[0] > 1 && rsi[0] > 45 && rsi[0] < 70 && fromBellow && ema10[0] > ema30[0]){
//if( volume[0] > 1 && input > ema30[0] /*&& rsi[0] > 60 && rsi[0] < 80 && rsi[1] < rsi[0]*/){
console.log('Trade : ', arr.pair, 'vol = ', volume[0])
let msg =
'Trade [' + arr.pair +'](https://bittrex.com/Market/Index?MarketName=' + arr.pair + ') *Vol: *' + volume[0] + ' *RSI: *' + rsi[0];
slimbot.sendMessage('@trexMarketScan', msg, {parse_mode: 'Markdown'})
//found = true
let output = arr.pair
}
resolve(output)
})
}
// const getMarkets = () => {
// return new Promise((resolve, reject) => {
// let seq = Promise.resolve()
// let _pairs = pairs(1000).then(res => {
// res.forEach(pair => {
// seq = seq.then(getCandles(pair))
// .then(res => tradePair)
// .catch(err => console.log(err, 'Failed to fetch!'))
// })
// })
// .then(res => {
// console.log('Get markets round ' + rounds + ' finished!')
// rounds++
// })
// .then(res => resolve(res))
// .catch(err => console.log(err, 'Failed to fetch!'))
// })
// }
// const getMarkets = () => {
// return new Promise((resolve, reject) => {
// let _pairs = pairs(100).then(res => {
// let marketPromises = res.map(getCandles)
// Promise.all(marketPromises)
// .then(res => res.map(tradePair))
// .then(res => resolve(res))
// .then(res => {
// console.log('Get markets round ' + rounds + ' finished!')
// rounds++
// })
// .catch(res => console.log('Error', res))
// })
// })
// }
// const getMarkets = () => {
// return new Promise((resolve, reject) => {
// let _pairs = pairs(targetVol).then(res => {
// //let marketPromises = res.map(getCandles)
// res.reduce((acc, cur) => {
// return getCandles(cur)
// //.then(market)
// .then(res => tradePair(res))
// .catch(res => console.log('Error', res.message))
// }, Promise.resolve())
// })
// })
// }
const getMarkets = () => {
let out = []
return pairs(targetVol).then(res => {
res.reduce( (promise, res) => {
return promise.then( () => {
out.push(getCandles(res)
.then(res => tradePair(res))
.catch(res => console.log('Error', res.message))
);
})
}, Promise.resolve())
})
.then( () => Promise.all(out))
.then(res => {
if(!found)
setTimeout(y, 900000)
})
}
const y = () => {
Promise.resolve(getMarkets())
.then(res => {
console.log('Get markets round ' + rounds + ' finished!', new Date())
rounds++
})
}
y()
//getMarkets()
// Promise.resolve(getMarkets())
// .then(res => {
// console.log('Get markets round ' + rounds + ' finished!', new Date())
// rounds++
// })
// JSON.stringify(data.result.reverse(), null, ' ')
// fs.writeFile('candleData.json', _data, (err) => {
// if(err) throw err
// console.log('Saved!')
// })
/*bittrex.getcandles({
marketName: 'BTC-ETC',
tickInterval: 'thirtyMin' // 'oneMin', 'fiveMin', thirtyMin, 'hour', 'day'
}, (data, err) => {
if(err) {
return console.log(err)
}
let _data = JSON.stringify(data.result.reverse(), null, ' ')
fs.writeFile('candleData.json', _data, (err) => {
if(err) throw err
console.log('Saved!')
})
//console.log('Data for : ' + pair, data.result);
//let _data = data.result.reverse()
//.slice(0, 14)
//JSON.stringify(_data)
//console.log(_data)
//let _data = data.result.reverse()
})*/
//const pairs = crypto.bittrex.pairs()
// .then(res => res.filter(cur => cur.match(/(BTC)$/g)))
// .then(r => crypto.bittrex.ticker(r))
// .then(console.log)
// const calc = (initialInv, days, targetProfit) => {
// result = ''
// i = 0
// targetProfit = (targetProfit / 100) + 1
// do {
// i++
// initialInv *= targetProfit
// console.log('In ' + i + ' days: ', initialInv.toFixed(8))
// } while (i < days);
// }
//
// calc(0.02, 180, 3)