forked from yonie/npm-kraken-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
952 lines (814 loc) · 26 KB
/
server.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
// @ts-check
// logging including date/time
let log = require("./log.js");
log("initializing..");
// eslint-disable-next-line no-unused-vars
let dns = require("dns"),
dnscache = require("dnscache")({ enable: true, ttl: 1800, cachesize: 1000 });
// get the secrets
require("dotenv").config();
const krakenKey = process.env.KRAKEN_KEY;
const krakenPasscode = process.env.KRAKEN_PASSCODE;
// get the settings
let settings = require("./settings.js");
const percentageDrop = settings.percentageDrop;
const stopLossPercentage = settings.stopLossPercentage;
const minTradeVolume = settings.minTradeVolume;
const maxSharePerAssetPercent = settings.maxSharePerAssetPercent;
const fixedBuyAmount = settings.fixedBuyAmount;
const maxGreedPercentage = settings.maxGreedPercentage;
if (
!percentageDrop ||
!stopLossPercentage ||
!minTradeVolume ||
!maxSharePerAssetPercent ||
!fixedBuyAmount ||
!maxGreedPercentage
) {
console.error("Critical error: missing (part of) settings.js file.");
process.exit(1);
}
// how often does the engine refresh data, in seconds
// note anything lower than about 30 will get you into API rate limiting
const ENGINE_TICK = 31;
// minimum trade amount before trying a sell order (in eur)
// note values lower than 10 can lead to API errors from Kraken
const MIN_SELL_AMOUNT = 10;
// set to false to disable any trading actions, rendering the bot passive
const TRADING_ENABLED = true;
// how many trades to show in the history list of the web client
const NUM_TRADE_HISTORY = 150;
// determines how much % near low the lasttrade needs to be to consider buying
// eg. set to 0 to only buy assets that are at their lowest observed point
const BUY_TOLERANCE = 20;
// the maximum amount an asset can crash before we begin to ignore it
const MAX_DROP = 40;
// internally used to check if we are in stoploss mode
let STOP_LOSS_MODE = false;
// internal flag used to keep the engine aware whether orders have been updated
let ORDERS_DIRTY = true;
// set up kraken api
let KrakenClient = require("kraken-api");
let kraken = new KrakenClient(krakenKey, krakenPasscode);
const http = require("http");
const url = require("url");
// port where the internal web interface is available. set to -1 to disable internal web server
const HTTP_PORT = 8000;
const server = http.createServer();
if (HTTP_PORT > -1) server.listen(HTTP_PORT);
server.on("listening", function () {
log("listening on port " + HTTP_PORT);
});
let tradeBalance;
// internal web server, can be used for inspection of operation
server.on("request", (request, response) => {
var contenttype;
// json array of wallet data
if (request.url?.includes("/wallet")) {
contenttype = "application/json";
response.writeHead(200, {
"Content-Type": contenttype,
});
response.write(JSON.stringify(wallet));
response.end();
return;
}
// endpoint for external value trackers
if (request.url?.includes("/balance/btc")) {
contenttype = "application/json";
response.writeHead(200, {
"Content-Type": contenttype,
});
if (tradeBalance && ticker && ticker["XXBTZEUR"]) {
var result = tradeBalance / parseInt(ticker["XXBTZEUR"].split(" ")[0]);
response.write(JSON.stringify(result));
}
response.end();
return;
}
// endpoint for external value trackers
if (request.url?.includes("/balance/eur")) {
contenttype = "application/json";
response.writeHead(200, {
"Content-Type": contenttype,
});
if (tradeBalance) {
response.write(JSON.stringify(parseFloat(tradeBalance)));
}
response.end();
return;
}
// json array of known trade history
if (request.url?.includes("/trades")) {
contenttype = "application/json";
response.writeHead(200, {
"Content-Type": contenttype,
});
response.write(JSON.stringify(trades));
response.end();
return;
}
// json array of current orders
if (request.url?.includes("/orders")) {
contenttype = "application/json";
response.writeHead(200, {
"Content-Type": contenttype,
});
response.write(JSON.stringify(orders));
response.end();
return;
}
// custom formatted price information
if (request.url?.includes("/ticker")) {
contenttype = "application/json";
response.writeHead(200, {
"Content-Type": contenttype,
});
response.write(JSON.stringify(ticker));
response.end();
return;
}
contenttype = "text/html";
response.writeHead(200, {
"Content-Type": contenttype,
});
response.write(
"<!doctype HTML><html><head><title>kraken</title></head><body>"
);
response.write('<h1><a href="/">kraken</a></h1>');
if (tradeBalance && wallet["ZEUR"] && wallet["ZEUR"].value)
response.write(
"<h2>latest balance: " +
tradeBalance +
" (" +
((wallet["ZEUR"].value / tradeBalance) * 100).toFixed(0) +
"% free)</h2>"
);
// try to show btc value next to eur value
if (tradeBalance && ticker && ticker["XXBTZEUR"]) {
response.write(
`<h3>${(
tradeBalance / parseInt(ticker["XXBTZEUR"].split(" ")[0])
).toPrecision(4)} btc`
);
if (greedValue) response.write(` (greed index: ${greedValue}%)`);
response.write(`</h3>`);
if (STOP_LOSS_MODE) response.write(`<h4>note: stop loss mode active!</h4>`)
}
response.write('<a href="/wallet">wallet</a><br/>');
response.write('<a href="/trades">trades</a><br/>');
if (orders)
response.write(
'<a href="/orders">orders (' + Object.keys(orders).length + ")</a><br/>"
);
response.write('<a href="/ticker">ticker</a><br/>');
// we support selecting a single assets to see data for eg. ?pair=btceur
let requestedPair = request.url
? getPrimaryNameForAsset(url.parse(request.url, true).query["pair"])
: null;
if (requestedPair) {
response.write(
"<h3>" +
requestedPair +
": " +
(ticker && ticker[requestedPair] ? ticker[requestedPair] : "") +
"</h3>"
);
const asset = requestedPair.slice(0, -3);
if (wallet[asset].amount && wallet[asset].value)
response.write(
"<h4>Current holdings: " +
" $" +
asset +
" " +
wallet[asset].amount +
" (eur " +
wallet[asset].value.toFixed(2) +
")</h4>"
);
}
if (trades) {
response.write("<p>latest trades:</p>");
response.write("<ul>");
for (let i = 0; i < Math.min(trades.length, NUM_TRADE_HISTORY); i++) {
if (!trades[i]) continue;
// filter for desired pair if provided
if (
!requestedPair ||
trades[i].pair === requestedPair ||
trades[i].pair == pairs[requestedPair]?.altname
) {
response.write("<li>");
response.write(
"" + new Date(trades[i]["time"] * 1000).toLocaleString("nl-NL")
);
response.write(" ");
response.write(trades[i]["type"]);
response.write(" ");
response.write(trades[i]["vol"]);
response.write(' <a href="?pair=' + trades[i]["pair"] + '">');
response.write(trades[i]["pair"]);
response.write("</a> ");
response.write(" @ ");
response.write(trades[i]["price"]);
response.write(" = ");
response.write(trades[i]["cost"]);
response.write("</li>");
}
}
response.write("</ul>");
}
if (orders) {
response.write("<p>latest orders:</p>");
response.write("<ul>");
Object.keys(orders).forEach(function (orderId) {
const order = orders[orderId];
// filter for desired pair if provided
if (
!requestedPair ||
order.descr.pair === requestedPair ||
order.descr.pair === pairs[requestedPair]?.altname
) {
response.write("<li>");
response.write(
"" + new Date(order.opentm * 1000).toLocaleString("nl-NL")
);
response.write(" ");
response.write(order.descr.type);
response.write(" ");
response.write(order.vol);
response.write(' <a href="?pair=' + order.descr.pair + '">');
response.write(order.descr.pair);
response.write("</a> ");
response.write(" @ ");
response.write(order.descr.price);
response.write(" = ");
response.write("" + order.descr.price * order.vol);
response.write("</li>");
}
});
response.write("</ul>");
}
// basic history showing recent balance changes over time
if (balanceHistory) {
for (let i in balanceHistory) {
response.write("<p>" + i + " " + balanceHistory[i] + "</p>");
}
}
response.write("</body></html>");
response.end();
});
let pairs;
let pairsExploded;
// determine pairs
kraken.api("AssetPairs", null, function (error, pairdata) {
if (error) {
console.error("Critical error fetching asset pairs:", error);
process.exit(1);
}
// push all pairs into an object
pairs = {};
for (let assetpair in pairdata["result"]) {
if (assetpair.endsWith("EUR"))
pairs[assetpair] = pairdata["result"][assetpair];
}
// get the exploded variant
pairsExploded = Object.keys(pairs).join();
TRADING_ENABLED
? log("trading on " + Object.keys(pairs).length + " pairs.")
: log("NOT trading!");
// do initial requests
setTimeout(updateBalance, 1000);
setTimeout(getTradeHistory, 4000);
setTimeout(updateOpenOrders, 2000);
setTimeout(getTicker, 3000);
});
var ticker = {};
// main routine
setInterval(getTicker, 1000 * ENGINE_TICK);
function getTicker() {
// get ticker info for all pairs
kraken.api(
"Ticker",
{
pair: pairsExploded,
},
function (error, tickerdata) {
if (error) {
console.error("Critical error fetching ticker data:", error);
process.exit(1);
}
let balanceSum = 0;
// loop through the pairs in a random order
Object.keys(tickerdata.result).forEach(function (pair) {
const asset = pairs[pair].base;
// for each pair see if we need to trade
var lasttrade = trimToPrecision(
pair,
parseFloat(tickerdata.result[pair].c[0])
);
var daylow = trimToPrecision(
pair,
parseFloat(tickerdata.result[pair].l[1])
);
var tradevolume = Math.round(tickerdata.result[pair].v[1] * lasttrade);
var dayhi = trimToPrecision(
pair,
parseFloat(tickerdata.result[pair].h[1])
);
if (!daylow || !dayhi || !lasttrade) {
console.error("Missing ticker data.", daylow, dayhi, lasttrade);
return;
}
// update wallet
if (wallet[asset]) {
wallet[asset]["price"] = parseFloat(lasttrade);
if (wallet[asset]["amount"]) {
wallet[asset]["value"] =
wallet[asset]["amount"] * wallet[asset]["price"];
// add to our total observed balance
balanceSum += wallet[asset].value;
}
}
// do some basic intepretation of the ticker data
var distancefromlow = Math.round(
((lasttrade - daylow) / (dayhi - daylow)) * 100
);
var move = Math.round(((dayhi - daylow) / dayhi) * 100);
// build ticker
ticker[pair] =
lasttrade +
" (" +
distancefromlow +
"%/" +
move +
"%) lo:" +
daylow +
"/hi:" +
dayhi +
" vol(EUR): " +
tradevolume;
// check if we have hard set to skip trading
if (!TRADING_ENABLED) return;
// if we don't know our balance we should not trade
if (!tradeBalance) return;
// make sure we have order info before we start trading
if (ORDERS_DIRTY) return;
// check if we want to buy
if (
!STOP_LOSS_MODE &&
move >= percentageDrop &&
move < MAX_DROP &&
distancefromlow <= BUY_TOLERANCE
)
considerBuy(pair, tradevolume, lasttrade, asset);
// check if we want to sell
if (wallet && wallet[asset] && wallet[asset]["amount"] > 0)
considerSell(move, lasttrade, pair, asset);
});
// add our non-asset balance to complete the sum
balanceSum += wallet["ZEUR"].value;
tradeBalance = balanceSum.toFixed(2);
}
);
}
function considerBuy(pair, tradevolume, lasttrade, asset) {
console.debug("Potentially interesting asset:", pair, ticker[pair]);
const shareOfWallet = getShareOfWallet();
if (shareOfWallet == null) return;
// make sure stable coins don't count toward "share of wallet"
const stablestuff =
wallet["PAXG"] && wallet["PAXG"].value ? wallet["PAXG"].value : 0;
// also make sure we don't buy stuff below minimum trade volume
if (
wallet["ZEUR"] &&
wallet["ZEUR"].amount + stablestuff > tradeBalance * shareOfWallet &&
tradevolume > minTradeVolume
) {
let buyPrice = lasttrade;
let buyVolume = fixedBuyAmount / buyPrice;
// clean up if we can
if (pairs[pair].lot_decimals)
buyVolume = Number(buyVolume.toFixed(pairs[pair].lot_decimals));
// make sure the minimum order size works with the API
if (pairs[pair].ordermin)
buyVolume = Math.max(buyVolume, pairs[pair].ordermin);
// if we have too much of one asset (including orders!), don't buy more
const buyOrderValue = sumOpenBuyOrderValue(pair);
const ownedAssetValue =
wallet && wallet[asset] && wallet[asset].value
? wallet[asset]["value"]
: 0;
if (
buyVolume * buyPrice + (buyOrderValue ?? 0) + ownedAssetValue <
(maxSharePerAssetPercent / 100) * tradeBalance
) {
// buy stuff
buy(pair, buyVolume);
// make the order book "dirty" again (otherwise we keep ordering until next order book update)
ORDERS_DIRTY = true;
setTimeout(updateOpenOrders, 5000);
}
}
}
function getShareOfWallet() {
// adjust how much we buy based on btc price
let btcPrice = ticker["XXBTZEUR"]
? parseFloat(ticker["XXBTZEUR"].split(" ")[0])
: null;
// if btc price is unknown we're not proceeding
if (!btcPrice) {
console.warn("BTC price not yet known, aborting potential trade.");
return;
}
if (btcPrice < 15000) return 0.19;
if (btcPrice < 20000) return 0.24;
if (btcPrice < 25000) return 0.31;
if (btcPrice < 30000) return 0.4;
if (btcPrice < 35000) return 0.52;
if (btcPrice < 40000) return 0.67;
if (btcPrice < 45000) return 0.86;
return 0.86;
}
function considerSell(move, lasttrade, pair, asset) {
// determine the sell price based on observed movement and a magic value: ex. (10-3) * 0.61 = 4.27%
const SELL_RATIO = 0.61;
var sellmod =
(Math.max(move, percentageDrop) - BUY_TOLERANCE / 10) * SELL_RATIO * 0.01 +
1;
var sellPrice = lasttrade * sellmod;
// quick hack for API
sellPrice = trimToPrecision(pair, sellPrice);
if (!sellPrice) {
console.error("Error calculating sell price.", pair, sellPrice);
return;
}
// check open orders to see if a sell order is even still possible
const openSellOrderVolume = getSellOrderVolume(pair);
const notYetForSale = wallet[asset]["amount"] - (openSellOrderVolume ?? 0);
if (notYetForSale * lasttrade > MIN_SELL_AMOUNT)
console.info(
"Found asset somehow not yet for sale:",
pair,
notYetForSale,
notYetForSale * lasttrade
);
const walletAmount = wallet[asset].amount;
// sell volume is what remains decucing open orders from the held amount
let sellVolume = walletAmount - (openSellOrderVolume ?? 0);
// clean up if we can
if (pairs[pair].lot_decimals)
sellVolume = Number(sellVolume.toFixed(pairs[pair].lot_decimals));
// don't trade if have too little to sell
if (sellVolume * sellPrice > MIN_SELL_AMOUNT) {
if (!STOP_LOSS_MODE) sell("limit", pair, sellVolume, sellPrice);
if (STOP_LOSS_MODE)
sell(
"stop-loss",
pair,
sellVolume,
trimToPrecision(pair, lasttrade * ((100 - stopLossPercentage) / 100))
);
// make the order book "dirty" again otherwise we keep ordering until next update
//ordersDirty = true;
setTimeout(updateOpenOrders, 5000);
}
}
// helper function to easily calculate the total amount of open order value for a given pair
function sumOpenBuyOrderValue(pair) {
if (!pair) return;
if (!orders) return;
var sum = 0;
for (var i in orders) {
// we need to use altnames lookup because of shitty kraken implementation
if (
orders[i].descr.pair == pairs[pair].altname &&
orders[i].descr.type == "buy"
) {
sum = sum + orders[i].descr.price * orders[i].vol;
}
}
return sum;
}
// helper function to easily calculate the total amount of open order value for a given pair
function getSellOrderVolume(pair) {
if (!pair) return;
if (!orders) return;
var sum = 0;
for (var i in orders) {
// we need to use altnames lookup because of shitty kraken implementation
if (
orders[i].descr.pair == pairs[pair].altname &&
orders[i].descr.type == "sell"
) {
sum = sum + parseFloat(orders[i].vol);
}
}
return sum;
}
let orders = {};
setInterval(updateOpenOrders, (1000 * ENGINE_TICK) / 2);
// update open orders
function updateOpenOrders() {
kraken.api("OpenOrders", null, function (error, openOrders) {
// reinitialize orders
orders = {};
if (error) {
console.error("Error fetching open orders: ", error);
ORDERS_DIRTY = true;
return;
}
// we're not going to cancel orders if we are not trading
if (!TRADING_ENABLED) return;
// iterate through all the open orders
for (var order in openOrders.result.open) {
// fill the orders storage
orders[order] = openOrders.result.open[order];
// get the order information
const orderBuySell = openOrders.result.open[order].descr.type;
const orderLimitMarket = openOrders.result.open[order].descr.ordertype;
const currentOrderPrice = openOrders.result.open[order].descr.price;
const orderPair = getPrimaryNameForAsset(
openOrders.result.open[order].descr.pair
);
const lastTradePrice =
ticker && orderPair && ticker[orderPair]
? ticker[orderPair].split(" ")[0]
: null;
// in stoploss mode, cancel all existing limit sell orders so they can be replaced
if (
STOP_LOSS_MODE &&
orderBuySell == "sell" &&
orderLimitMarket == "limit"
) {
log("Cancelling limit order as we are now in stoploss mode: " + orders[order].descr.order);
cancelOrder(order);
}
// in normal mode, cancel limit orders that are too far out so they can be resent
if (
!STOP_LOSS_MODE &&
orderBuySell == "sell" &&
orderLimitMarket == "limit" &&
lastTradePrice &&
currentOrderPrice > lastTradePrice * 10
) {
log(
"Cancelling limit order because it has become unattainable: " + orders[order].descr.order
);
cancelOrder(order);
}
// if we have a stop loss order that we should replan, cancel it so a new one can be made
const desiredStopLossPrice = lastTradePrice
? trimToPrecision(
orderPair,
lastTradePrice * ((100 - stopLossPercentage) / 100)
)
: null;
if (
orderBuySell == "sell" &&
orderLimitMarket == "stop-loss" &&
desiredStopLossPrice &&
currentOrderPrice < desiredStopLossPrice
) {
log(
"Updating stop loss order with higher price: " +
orderPair +
" " +
currentOrderPrice +
" --> " +
desiredStopLossPrice +
" (+" +
((1 - desiredStopLossPrice / currentOrderPrice) * 100).toFixed(1) +
"%)"
);
editOrder(order, orderPair, desiredStopLossPrice);
}
}
ORDERS_DIRTY = false;
});
}
// returns the primary name for an asset if an altname is provided
function getPrimaryNameForAsset(altname) {
let found = null;
if (!altname || !pairs) return null;
Object.keys(pairs).forEach(function (pair) {
if (pairs[pair].altname == altname || pair == altname) {
found = pair;
}
});
return found;
}
var trades = [];
// get trade history info
setInterval(getTradeHistory, 1000 * ENGINE_TICK * 2);
// cancels order for given order id
function cancelOrder(orderId) {
if (!orderId) return false;
console.debug("Cancelling order:", orderId);
kraken.api(
"CancelOrder",
{
txid: orderId,
},
function (error) {
if (error) {
console.error("Error cancelling order:", error);
return false;
}
return true;
}
);
}
// fetch trade history in batches. this call is batched to prevent rate limiting by kraken.
function getTradeHistory() {
trades = [];
// hardcoded max size in kraken api
const sample = 50;
const delayms = 105;
// how much history do we want
const max = 500;
for (let i = 0; i < max; i += sample) {
setTimeout(function () {
kraken.api(
"TradesHistory",
{ ofs: i },
function (error, tradesHistoryData) {
if (error) {
console.error("Error updating trades history:", error);
return;
}
for (var trade in tradesHistoryData.result.trades) {
trades.push(tradesHistoryData.result.trades[trade]);
}
}
);
}, delayms * i);
}
}
var wallet = {};
var balanceHistory = [];
var greedValue = null;
var greedValueClassification = null;
// update greed
setInterval(getGreedStatistics, 1000 * ENGINE_TICK);
// we are getting greed stats from an external source, which informs us if
// we need to enter stop loss mode.
function getGreedStatistics() {
const https = require("https");
const apiUrl = "https://api.alternative.me/fng/";
https
.get(apiUrl, (response) => {
let data = "";
response.on("data", (chunk) => {
data += chunk;
});
response.on("end", () => {
try {
const apiResponse = JSON.parse(data);
greedValue = apiResponse.data[0].value;
greedValueClassification = apiResponse.data[0].value_classification;
} catch (error) {
console.error("Error parsing greed data:", error);
}
if (greedValue && greedValueClassification) {
log(
"Current greed index: " +
greedValueClassification +
" (" +
greedValue +
"%)"
);
// if greed is too high, we should exit positions
STOP_LOSS_MODE = greedValue >= maxGreedPercentage;
if (STOP_LOSS_MODE)
console.warn(
"Warning: Stop loss mode active due to high detected greed!"
);
}
});
})
.on("error", (error) => {
console.error("Error fetching greed data:", error);
});
}
// get trade balance info
setInterval(updateBalance, 1000 * ENGINE_TICK);
function updateBalance() {
const btcValue = ticker["XXBTZEUR"]
? (tradeBalance / parseFloat(ticker["XXBTZEUR"])).toFixed(3)
: null;
if (tradeBalance)
log("Trade balance: " + tradeBalance + " (" + btcValue + " btc)");
balanceHistory[new Date().toISOString().substring(0, 13)] = tradeBalance;
// get asset balance
kraken.api("Balance", null, function (error, balanceData) {
if (error) {
console.error("Error getting balance:", error);
return;
}
// check the items in our current wallet if they are still there
for (var walletAsset in wallet) {
if (balanceData.result[walletAsset] == null) {
wallet[walletAsset] = null;
}
}
for (var assetName in balanceData.result) {
var amount = parseFloat(balanceData.result[assetName]);
if (!wallet[assetName]) wallet[assetName] = {};
wallet[assetName]["asset"] = assetName;
wallet[assetName]["amount"] = amount;
// ensure base currency balance also has value
if (assetName == "ZEUR") wallet[assetName]["value"] = amount;
}
});
}
// buy for a given price
function buy(pair, volume) {
if (volume > 0) {
log("Adding order: buy " + volume + " " + pair + " @ market...");
return kraken.api(
"AddOrder",
{
pair: pair,
type: "buy",
ordertype: "market", // NOTE we are market buying effectively ignoring price
volume: volume,
},
function (error, buydata) {
if (error) {
console.error("Error adding buy order:", error.message);
return;
}
if (buydata) {
log("[ORDER] " + buydata["result"]["descr"]["order"], pair);
}
}
);
}
}
// sell order
function sell(type, pair, volume, price) {
if (type != "limit" && type != "stop-loss") {
log("Invalid sell order type.");
return;
}
if (volume * price > 0) {
log("Adding order: sell " + volume + " " + pair + " @ " + price + "...");
return kraken.api(
"AddOrder",
{
pair: pair,
type: "sell",
ordertype: type,
volume: volume,
price: price,
},
function (error, selldata) {
if (error) {
console.error(
pair,
volume,
price,
"Error adding sell order",
error.message
);
return;
}
if (selldata) {
log("[ORDER] " + selldata["result"]["descr"]["order"], pair);
}
}
);
}
}
// cancels order for given order id
function editOrder(orderId, orderPair, newPrice) {
if (!orderId || !orderPair || !newPrice) return false;
console.debug("Edit order:", orderId, orderPair, newPrice);
kraken.api(
"EditOrder",
{
txid: orderId,
pair: orderPair,
price: newPrice,
},
function (error) {
if (error) {
console.error("Error editing order:", error);
return false;
}
return true;
}
);
}
// clean up price to deal with specifications imposed by kraken API
function trimToPrecision(pair, price) {
if (!pair || !price) {
console.error("Invalid arguments.", pair, price);
return;
}
var precision = pairs[pair].pair_decimals;
if (precision > -1) return price.toFixed(precision);
console.error("Unknown price format for pair.", pair);
return price;
}