-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
279 lines (240 loc) · 8.47 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import binance from 'node-binance-api';
import { Container, Row, Col } from 'reactstrap';
import './styles.css';
import AlertMessage from '../../components/AlertMessage';
import Header from '../../components/Header';
import HorizontalTabList from '../../components/HorizontalTabList';
import InfoPanel from '../../components/InfoPanel';
import InputPanel from '../../components/InputPanel';
import OptionPanel from '../../components/OptionPanel';
import Chart from '../../components/Chart';
export default class TradeHub extends Component {
static propTypes = {
opts: PropTypes.shape({
binance: PropTypes.shape({
key: PropTypes.string.isRequired,
secret: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
};
state = {
cryptoList: [],
selectedCrypto: { symbol: 'ETHBTC', baseAsset: 'ETH', quoteAsset: 'BTC' },
quantity: 0,
currentPrice: 0,
boughtPrice: 0,
diffPercentage: 0.01,
highestPrice: 0,
sellEnabled: false,
sold: false,
alert: { isOpen: true, message: 'Welcome, happy trading!', color: 'primary' },
};
componentDidMount = () => {
binance.options({
APIKEY: this.props.opts.binance.key,
APISECRET: this.props.opts.binance.secret,
reconnect: false,
test: true,
});
this.getCryptoList();
this.bindSocket(this.state.selectedCrypto.symbol);
};
onDismiss = () => {
const alert = Object.assign({}, this.state.alert);
alert.isOpen = false;
this.setState({ alert });
}
getPercentageChange = (oldNumber, newNumber) => {
const decreaseValue = oldNumber - newNumber;
return (decreaseValue / oldNumber) * 100;
}
getCryptoList = () => {
fetch('https://api.binance.com/api/v1/exchangeInfo').then(res => res.json()).then((data) => {
const cryptoList = data.symbols;
if (cryptoList.length > 0) {
this.setState({
cryptoList,
selectedCrypto: cryptoList[0],
});
}
});
};
setBoughtPrice = price => this.setState({ boughtPrice: parseFloat(price) });
setQuantity = quantity => this.setState({ quantity: parseFloat(quantity) });
setDiffPercentage = percentage => this.setState({ diffPercentage: parseFloat(percentage) / 100 });
setSellEnabled = value => this.setState({ sellEnabled: value });
setHighestPrice = price => this.setState({ highestPrice: parseFloat(price) });
isHighestPrice = price => price > this.state.highestPrice;
shouldSell = (price) => {
const { sellEnabled, sold, highestPrice, diffPercentage } = this.state;
if (sellEnabled && !sold) {
return price <= highestPrice - (highestPrice * diffPercentage);
}
return false;
};
checkPrice = (price) => {
if (price < this.state.boughtPrice) return;
if (this.isHighestPrice(price)) {
this.setHighestPrice(price);
return;
}
if (this.shouldSell(price)) this.sell(price);
};
sell = (price) => {
binance.sell(this.state.selectedCrypto.symbol, this.state.quantity, price);
const alert = Object.assign({}, this.state.alert);
alert.isOpen = true;
alert.message = `Trading bot sold ${this.state.quantity} ${this.state.selectedCrypto.baseAsset}
at ${price} ${this.state.selectedCrypto.symbol}. Refresh the page for a new strategy.`
alert.color = 'success';
this.setState({
sold: true,
alert,
});
};
changeSelectedCrypto = (symbol) => {
const crypto = this.state.cryptoList.find(obj => obj.symbol === symbol);
if (crypto === null) return;
binance.prices((error, ticker) => {
const currentPrice = parseFloat(ticker[crypto.symbol]);
this.setState({
currentPrice,
sellEnabled: false,
highestPrice: currentPrice,
});
});
this.setState({
selectedCrypto: crypto,
}, () => this.rebindSocket());
};
bindSocket = (symbol) => {
binance.websockets.candlesticks([symbol], '1m', (candlesticks) => {
const { k: ticks } = candlesticks;
const { c: close } = ticks;
const currentPrice = parseFloat(close);
this.setState({ currentPrice });
this.checkPrice(currentPrice);
});
};
removeSocket = endpoint => binance.websockets.terminate(endpoint);
rebindSocket = () => {
const newCrypto = this.state.selectedCrypto.symbol;
const newEndpoint = `${newCrypto.toLowerCase()} @kline_1m`;
const subscriptions = binance.websockets.subscriptions();
Object.keys(subscriptions).forEach((endpoint) => {
if (endpoint !== newEndpoint) this.removeSocket(endpoint);
});
this.bindSocket(newCrypto);
};
render = () => {
const {
sellEnabled, selectedCrypto, cryptoList, boughtPrice, quantity, currentPrice, highestPrice,
} = this.state;
const diffPercentage = this.state.diffPercentage * 100;
const sellPrice = (highestPrice - (highestPrice * this.state.diffPercentage)).toFixed(6);
const highestPriceChange = this.getPercentageChange(highestPrice, currentPrice).toFixed(2);
const sellPriceChange = this.getPercentageChange(sellPrice, boughtPrice).toFixed(2);
const sellOptions = [
{
label: 'Enable',
value: true,
color: 'success',
},
{
label: 'Disable',
value: false,
color: 'danger',
},
];
return (
<div>
<Header />
<Container>
<AlertMessage {...this.state} onDismiss={this.onDismiss} />
<HorizontalTabList
list={cryptoList}
selectedValue={this.state.selectedCrypto.symbol}
changeSelected={this.changeSelectedCrypto}
/>
<Row>
<Col>
<InputPanel
name="quantity"
value={quantity}
step="0.01"
onChange={this.setQuantity}
title="Quantity"
description={`How many ${selectedCrypto.baseAsset} you give the bot to sell.`}
placeholder="Quantity"
/>
</Col>
<Col>
<InputPanel
name="bought_price"
value={boughtPrice}
step="0.01"
onChange={this.setBoughtPrice}
title="Bought"
description={`Price in ${selectedCrypto.quoteAsset} at which you bought ${selectedCrypto.baseAsset}.`}
placeholder="Bought price"
/>
</Col>
<Col>
<InputPanel
name="diffpercentage"
value={diffPercentage}
step="0.01"
onChange={this.setDiffPercentage}
title="Difference"
description="% between highest price and sell price."
placeholder="Difference %"
/>
</Col>
<Col>
<OptionPanel
options={sellOptions}
value={sellEnabled}
onChange={this.setSellEnabled}
title="Bot strategy"
description="Should the bot start trading? (note: only sells with profit)."
/>
</Col>
</Row>
</Container>
<Container className="mt-3">
<Row>
<Col><Chart symbol={this.state.selectedCrypto.symbol} /></Col>
</Row>
</Container>
<Container className="mt-3">
<Row>
<Col>
<InfoPanel
title={` ${currentPrice} ${selectedCrypto.baseAsset}/${selectedCrypto.quoteAsset}`}
description="Current price."
/>
</Col>
<Col>
<InfoPanel
title={`${highestPrice} ${selectedCrypto.baseAsset}/${selectedCrypto.quoteAsset}`}
description="Highest price since bot is running."
subtitle={`${highestPriceChange}%`}
subtitleClass={highestPriceChange >= 0 ? 'text-success' : 'text-danger'}
/>
</Col>
<Col>
<InfoPanel
title={`${sellPrice} ${selectedCrypto.baseAsset}/${selectedCrypto.quoteAsset} `}
description={`${sellPrice > boughtPrice ? 'Price at which the bot will sell' : 'Bot will not sell. Lower than bought price'}.`}
subtitle={`${sellPriceChange}%`}
subtitleClass={sellPriceChange >= 0 ? 'text-success' : 'text-danger'}
/>
</Col>
</Row>
</Container>
</div>
);
};
}