-
Notifications
You must be signed in to change notification settings - Fork 5
/
ui.js
158 lines (147 loc) · 4.72 KB
/
ui.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
const blessed = require('blessed')
const contrib = require('blessed-contrib')
const DEBUG = false
exports = module.exports = {}
let screen
const ui = {}
const init = exports.init = () => {
if( DEBUG ) return
init_screen()
init_keystrokes()
}
const init_screen = exports.init_screen = () => {
screen = blessed.screen()
ui.overview_table = contrib.table({
top: '2%',
left: '2%',
width: '96%',
height: '13%',
label: 'Overview',
border: {type: 'line', fg: 'yellow'},
fg: 'yellow',
interactive: false,
columnSpacing: 4, //in chars
columnWidth: [12, 8, 12, 12, 12, 8, 12, 12, 8, 8, 8, 8], // in chars
})
ui.trade_table = contrib.table({
keys: true,
fg: 'yellow',
interactive: false,
label: 'Trade Buckets',
width: '48%',
height: '82%',
top: '16%',
left: '50%',
border: {type: "line", fg: "yellow"},
columnSpacing: 4, //in chars
columnWidth: [4, 12, 12, 4, 17], /*in chars*/
})
ui.gdax_log = contrib.log({
fg: "yellow",
selectedFg: "yellow",
label: 'GDAX Log',
left: '2%',
top: '16%',
width: '46%',
height: '28%',
border: {type: "line", fg: "yellow"}
})
ui.sys_log = contrib.log({
fg: "yellow",
selectedFg: "yellow",
label: 'System Log',
left: '2%',
top: '47%',
width: '46%',
height: '51%',
border: {type: "line", fg: "yellow"}
})
screen.append(ui.overview_table)
screen.append(ui.trade_table)
screen.append(ui.gdax_log)
screen.append(ui.sys_log)
setInterval( () => {
refresh_overview_table()
refresh_trade_table()
screen.render()
}, 300 )
}
const init_keystrokes = exports.init_keystrokes = () => {
screen.key(['escape', 'q', 'C-c'], (ch, key) => {
exit_gracefully()
})
// toggle buys
screen.key(['b'], (ch, key) => {
trade_data.buys.enabled = !trade_data.buys.enabled
if( trade_data.buys.enabled ) {
logger('sys_log', 'Buying is now enabled.')
} else {
logger('sys_log', 'Buying is now disabled.')
}
})
// toggle sells
screen.key(['s'], (ch, key) => {
trade_data.sells.enabled = !trade_data.sells.enabled
if( trade_data.sells.enabled ) {
logger('sys_log', 'Selling is now enabled.')
} else {
logger('sys_log', 'Selling is now disabled.')
}
})
// cancel buys
screen.key(['c'], (ch, key) => {
trades.cancel_all_buys()
})
// Reinitialize the app
screen.key(['r'], (ch, key) => {
reload_config()
})
}
const refresh_trade_table = exports.refresh_trade_table = () => {
let table_data = []
for( let trade of _.sortBy(_.values(trades.trades), (t) => -t.buy.price) ) {
let buy_price = (trade.buy.price) ? `$${trade.buy.price.toFixed(2)}` : '-'
let sell_price = (trade.sell.executed_price) ? `$${trade.sell.executed_price.toFixed(2)}` : '-'
let row = [trade.side, buy_price, sell_price, trade.size, trade.state]
table_data.push( row )
}
ui.trade_table.setData({
headers: ['Side', 'Buy @', 'Sell @', 'Size', 'State'],
data: table_data
})
}
const refresh_overview_table = exports.refresh_overview_table = () => {
if( !account.account || !settings ) return
let current_price = 'Loading'
if( gdax.orderbook_synced ) current_price = `$${gdax.midmarket_price.current.toFixed(3)}`
ui.overview_table.setData({
headers: [`P (${(settings) ? settings.product_id : '-'})`, 'dP/dt', 'Initial Cash', 'Cash on Hand', 'Profit', 'Fees', 'Net Change', 'Buys', 'Sells', 'Min', 'Max', 'Buckets'],
data: [[ current_price,
`$${gdax.midmarket_price.velocity.toPrecision(4)}/s`,
`$${(settings) ? settings.multipong.initial_cash.toFixed(2) : '-'}`,
`$${trades.figures.current_cash.toFixed(2)}`,
`$${(trades.figures.net_gain+trades.figures.unrealized_buys).toFixed(2)}`, // profit
`$${account.account.fees.toFixed(2)}`, // fees
`$${trades.figures.net_gain.toPrecision(4)}`, // net
//`$${trades.figures.max_profit.toPrecision(4)}`, // max
`${(trade_data.buys.enabled ? 'On' : 'Off')} (${account.account.buy_count})`,
`${(trade_data.sells.enabled ? 'On' : 'Off')} (${account.account.sell_count})`,
`$${(settings) ? settings.multipong.min_price.toFixed(2) : '-'}`,
`$${(settings) ? settings.multipong.max_price.toFixed(2) : '-'}`,
settings.multipong.num_buckets]]
})
}
const logger = exports.logger = (target, content) => {
if( typeof content !== 'string' ) {
content = JSON.stringify(content)
}
if( log.file ) {
log.file.write( content )
log.file.write('\n')
}
if( DEBUG ) {
console.log(content)
return
}
ui[target].log(`${moment().tz(settings.tz).format('HH:mm:ss')} ${content}`)
}