-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathircu.c
1854 lines (1541 loc) · 46.5 KB
/
ircu.c
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define WM_IRCUC
#include "ircu.h"
void
ircu_identify(void)
{
char *pass = irc[global.is].args;
if(!strcmp(global.conf.adminpass, pass))
{
int x = irc_addAdmin(irc[global.is].from);
if(x != -1)
irc_broadcast("%s will be recognized as an admin for the next %d seconds", irc[global.is].admin[x].nick, IRC_ADMINTIME);
else
irc_reply("Maximum admins reached. Denied :~(?");
return;
}
irc_reply("Incorrect. This attempt has been logged.");
logger(C_IRCU, CRIT, "ircu_identify",
"admin access attempt by %s on %s:%s with pw %s", irc[global.is].from,
irc[global.is].server, irc[global.is].port, pass);
}
void
ircu_math(void)
{
double res = te_interp(irc[global.is].args, 0);
irc_reply("%f", res);
}
void
ircu_diff(void)
{
char *str = irc[global.is].args;
const char *argv[20];
int argc = 0;
double x, y;
for(; (argv[argc] = strsep(&str, " ")) != NULL && argc < 20; argc++);
if(argc == 2)
{
x = strtod(argv[0], NULL);
y = strtod(argv[1], NULL);
if(x == 0 || y == 0)
irc_reply("no.");
else
{
double z = (x - y) / y * 100;
irc_reply("%.11f", z);
}
}
else
irc_reply("usage: diff num1 num2");
}
void
ircu_ping(void)
{
char *args = irc[global.is].args;
irc_reply("PONG %s", (args) ? args : " ");
}
void
ircu_version(void)
{
irc_reply("%s", global.conf.verstring);
}
void
ircu_help(void)
{
int x = 0;
char buf[1000], *t = buf, *args = irc[global.is].args;
if(args != NULL)
{
while(user_cmds[x].cmd != NULL && strcasecmp(user_cmds[x].cmd, args))
x++;
if(user_cmds[x].cmd != NULL)
irc_reply("Usage: %s", user_cmds[x].usage);
else
irc_reply("Unknown command :~(?");
return;
}
buf[0] = '\0';
while(user_cmds[x].cmd != NULL)
{
strcat(t, user_cmds[x].cmd);
if(user_cmds[x].alias != NULL)
{
strcat(t, " [");
strcat(t, user_cmds[x].alias);
strcat(t, "]");
}
x++;
if(user_cmds[x].cmd != NULL)
strcat(t, ", ");
}
irc_reply(buf);
}
void
ircu_join(void)
{
int x = global.is;
char *args = irc[x].args;
if(irc_admin() == false)
irc_reply("No. :~(?");
else if(args == NULL || args[0] == '\0')
irc_reply("Usage: join #channel [key]");
else
{
char *temp = args, *chan;
chan = strsep(&temp, " ");
if(irc_find_chan(x, chan) != -1)
irc_reply("I think I'm already on channel %s", chan);
else
{
int y = 0;
for(; y < IRC_MAXCHANS; y++)
{
if(irc[x].chan[y][0] == '\0')
{
strncpy(irc[x].chan[y], chan, 39);
if(temp && *temp != '\0')
sock_write(irc[x].sock, "JOIN %s :%s", chan, temp);
else
sock_write(irc[x].sock, "JOIN %s", chan);
break;
}
}
}
}
}
void
ircu_part(void)
{
int x = global.is, y;
char *args = irc[x].args;
if(irc_admin() == false)
irc_reply("No. :~(?");
else if(args == NULL || args[0] == '\0')
irc_reply("Usage: part #channel");
else if((y = irc_find_chan(x, args)) == -1)
irc_reply("I don't think I'm on channel %s", args);
irc[x].chan[y][0] = '\0';
sock_write(irc[x].sock, "PART %s :Take care now, bye bye then. :~(?", args);
}
void
ircu_8ball(void)
{
int x = 0;
char *args = irc[global.is].args;
char *answers_8ball[] = {
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes – definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."
};
size_t length = sizeof answers_8ball / sizeof *answers_8ball; // or sizeof answers_8ball[0]
if(args == NULL || args[0] == '\0')
irc_reply("Usage: 8ball question");
else
{
salt_random();
while(*args) /* make user's question meaningful.. sortta */
x += *args++;
srand(x + global.now);
// get random number with `length` of array as max
x = rand() % length;
logger(C_IRC, DEBUG2, "ircu_8ball", "random number generated (%d) maps to '%s'", x, answers_8ball[x]);
irc_reply("🔮 %s", answers_8ball[x]);
}
}
void
ircu_status(void)
{
time_t uptime = global.now - global.start, remainder;
char uptime_str[100], uptime_add[100];
int x = 0, total = 0;
for(;x < IRC_MAX; x++)
if(irc[x].state != IRC_UNUSED)
total++;
irc_reply("Active IRC servers: %d", total);
irc_reply("CURL servicer threads: %d", global.curl_threads);
total = 0;
for(x = 0; x < CURL_MAXQUEUE; x++)
{
pthread_mutex_lock(&cq[x].mutex);
if(cq[x].state != CQS_UNUSED)
total++;
pthread_mutex_unlock(&cq[x].mutex);
}
irc_reply("CURL request queue: %d", total);
irc_reply("logchans:%d", global.conf.logchans);
if(global.dbconn != NULL)
irc_reply("Postgresql connection is active (idle: %d secs)", global.now - global.dbtimer);
uptime_str[0] = '\0';
/* Day(s) */
if(uptime >= 86400)
{
remainder = uptime - ((uptime / 86400) * 86400);
sprintf(uptime_add,"%lu day",uptime / 86400);
if(uptime/86400 != 1)
strcat(uptime_add,"s");
if(remainder)
strcat(uptime_add,", ");
uptime = remainder;
strcat(uptime_str,uptime_add);
}
/* Hours */
if(uptime >= 3600)
{
remainder = uptime - ((uptime / 3600) * 3600);
sprintf(uptime_add,"%lu hour",uptime / 3600);
if(uptime/3600 != 1)
strcat(uptime_add,"s");
if(remainder)
strcat(uptime_add,", ");
uptime = remainder;
strcat(uptime_str,uptime_add);
}
/* Minutes */
if(uptime)
{
sprintf(uptime_add,"%lu minute",uptime / 60);
if(uptime/60 != 1)
strcat(uptime_add,"s");
strcat(uptime_str,uptime_add);
}
irc_reply("I have been alive for %s", uptime_str);
}
void
ircu_mcap(void)
{
char mcap[200], mcap_ath[200], vol[200], vol_ath[200];
d2h(mcap, crypto_global.mcap);
d2h(mcap_ath, crypto_global.mcap_ath);
d2h(vol, crypto_global.vol);
d2h(vol_ath, crypto_global.vol_ath);
irc_reply("mcap: %s (24h: %.2f%% ath: %s btc: %.2f%%) vol: %s (24h: %.2f%% ath: %s) total: %d assets vintage: %d seconds", mcap, crypto_global.mcap_24h, mcap_ath, crypto_global.btc_dom, vol, crypto_global.vol_24h, vol_ath, crypto_global.assets, global.now - global.cpptimer);
}
void
ircu_news(void)
{
cpanic_news(irc[global.is].args);
}
void
print_crypto_single(int num)
{
char mcap[50], vol[50];
d2h(mcap, cryptos[num].mcap);
d2h(vol, cryptos[num].vol);
irc_reply("%s [id: %s sym: %s rank: %d]", cryptos[num].name, cryptos[num].id, cryptos[num].sym, cryptos[num].rank);
irc_reply("last: %.4f ath: %.4f (%2.2f%%) vol: %s (%2.2f%%) mcap: %s", cryptos[num].price, cryptos[num].ath, cryptos[num].ath_diff, vol, cryptos[num].vol_diff, mcap);
irc_reply("15m: %2.2f%% 1h: %2.2f%% 24h: %2.2f%% 7d: %2.2f%% 30d: %2.2f%% 1y: %2.2f%%", cryptos[num].change_15m, cryptos[num].change_1h, cryptos[num].change_24h, cryptos[num].change_7d, cryptos[num].change_30d, cryptos[num].change_1y);
}
void
print_crypto_header(void)
{
irc_reply("Rank Sym Name USD 15-min 1-hour 24-hour 7-Days");
irc_reply("---------------------------------------------------------------------");
}
void
print_crypto(int num)
{
irc_reply("%-5d %-5.5s %-14.14s %-10.4f %-7.2f %-7.2f %-8.2f %.2f", cryptos[num].rank, cryptos[num].sym, cryptos[num].name, cryptos[num].price, cryptos[num].change_15m, cryptos[num].change_1h, cryptos[num].change_24h, cryptos[num].change_7d);
}
void
ircu_export(void)
{
char *temp = irc[global.is].args, *arg;
if((arg = strsep(&temp, " ")))
{
if(!strcasecmp(arg, "products"))
{
if((arg = strsep(&temp, " ")))
{
struct exchange *exch;
if((exch = exch_find(arg)) != NULL)
{
if((arg = strsep(&temp, " ")))
{
if(exch_exportProducts(exch, arg, temp) == SUCCESS)
irc_reply("Successfully exported %s:%s products to %s", exch->name, arg, temp);
else
irc_reply("Export of %s:%s products to %s failed", exch->name, arg, temp);
}
}
}
}
}
}
void
ircu_crypto(void)
{
char *temp = irc[global.is].args, *arg;
int single, header = false;
int found = 0;
if(global.num_cryptos == 0)
{
irc_reply("No data yet");
return;
}
if(temp == NULL || *temp == '\0')
{
int i = 0;
print_crypto_header();
for(; i < 10; i++)
print_crypto(i);
}
else
{
single = (strchr(temp, ' ') == NULL) ? true : false;
while((arg = strsep(&temp, " ")))
{
int i = 0;
for(; i < global.num_cryptos; i++)
{
int num = atoi(arg);
if(!num && (!strcasecmp(arg, cryptos[i].sym) || !strcasecmp(arg, cryptos[i].id)))
num = cryptos[i].rank;
if(num && num <= global.num_cryptos && cryptos[i].rank == num)
{
found++;
if(single == true)
print_crypto_single(num - 1);
else
{
if(header == false)
{
header = true;
print_crypto_header();
}
print_crypto(num - 1);
}
break;
}
}
}
if(!found)
irc_reply("Unknown crypto symbol :~(?");
}
}
void
ircu_showAccounts(const int argc, const char *argv[])
{
struct exchange *exch = NULL;
struct account *a = NULL;
int x = 0, row = 0;
if(argc >= 2)
{
if(!strcasecmp(argv[1], "help"))
{
irc_reply("usage: show acc [exchange] [asset]");
return;
}
if((exch = exch_find(argv[1])) == NULL)
{
irc_reply("unsupported exchange: %s", argv[1]);
return;
}
if(argc >= 3)
{
if((a = account_find(exch, argv[2])) == NULL)
irc_reply("no balance found for account %s:%s", exch->name, argv[2]);
else
irc_reply("bal: %.11f avail: %.11f hold: %.11f", a->balance, a->available, a->hold);
return;
}
}
for(; x < ACCOUNT_MAX; x++)
{
a = &accounts[x];
if( (a->balance != 0 || a->available != 0 || a->hold != 0) &&
( (exch != NULL && exch == a->exch) || exch == NULL ) )
{
if(!row++)
{
irc_reply("exchange asset balance available hold");
irc_reply("--------------------------------------------------------------------");
}
irc_reply("%-9.9s %-6.6s %-17.9f %-17.9f %-17.9f",
a->exch->name, a->currency, a->balance, a->available, a->hold);
}
}
if(!row)
irc_reply("no accounts with balances found");
}
void
ircu_shlong(const struct market *mkt)
{
double sell_price = mkt->cint[0].lc->close;
double sell_fee = (sell_price * mkt->buy_size) * mkt->exch->fee_maker;
irc_reply("buy_price: %f buy_size: %f buy_funds: %f exp: %d\n",
mkt->buy_price, mkt->buy_size, mkt->buy_funds, mkt->exposed);
irc_reply("sell_price: %f sell_size: %f fee_maker: %f sell_fee: %f sell_funds: %f\n",
sell_price, mkt->buy_size, mkt->exch->fee_maker, sell_fee, sell_price - sell_fee);
}
void
ircu_showLongs(const int argc, const char *args[])
{
int x = 0, hdr = 0;
for(; x < MARKET_MAX; x++)
{
struct market *mkt = &markets[x];
char temp[2][50];
if(!(mkt->state & MKS_ACTIVE) || !(mkt->state & MKS_LONG) || !mkt->exposed)
continue;
if(!hdr++)
{
irc_reply("id entry last diff profit high low exp");
irc_reply("--------------------------------------------------------------------------");
}
irc_reply("%02d %-15s %-15s %-6.2f %-6.2f %-6.2f %-6.2f %d", x,
trim0(temp[0], 50, mkt->buy_price),
trim0(temp[1], 50, mkt->cint[0].lc->close),
(mkt->cint[0].lc->close - mkt->buy_price) / mkt->buy_price * 100,
mkt->profit_pct, mkt->profit_pct_high, mkt->profit_pct_low,
mkt->exposed);
}
if(!hdr)
irc_reply("no long markets");
}
void
ircu_showMinprofit(void)
{
int x = 0, hdr = 0;
for(; x < MARKET_MAX; x++)
{
struct market *mkt = &markets[x];
if(!(mkt->state & MKS_ACTIVE) || mkt->state & MKS_LEARNING)
continue;
if(!hdr)
{
hdr = 1;
irc_reply("## market %-6s %-6s %-6s %-6s %-6s %-6s %s",
cint2str(cint_length(0)), cint2str(cint_length(1)), cint2str(cint_length(2)),
cint2str(cint_length(3)), cint2str(cint_length(4)), cint2str(cint_length(5)),
cint2str(cint_length(6)));
irc_reply("--------------------------------------------------------------------------------");
}
irc_reply("%-25s %-6.2f %-6.2f %-6.2f %-6.2f %-6.2f %-6.2f %.2f",
mkt->name,
(mkt->cint[0].tc > HISTSIZE) ? (mkt->minprofit - mkt->cint[0].lc->sma5) / mkt->cint[0].lc->sma5 * 100 : 0,
(mkt->cint[1].tc > HISTSIZE) ? (mkt->minprofit - mkt->cint[1].lc->sma5) / mkt->cint[1].lc->sma5 * 100 : 0,
(mkt->cint[2].tc > HISTSIZE) ? (mkt->minprofit - mkt->cint[2].lc->sma5) / mkt->cint[2].lc->sma5 * 100 : 0,
(mkt->cint[3].tc > HISTSIZE) ? (mkt->minprofit - mkt->cint[3].lc->sma5) / mkt->cint[3].lc->sma5 * 100 : 0,
(mkt->cint[4].tc > HISTSIZE) ? (mkt->minprofit - mkt->cint[4].lc->sma5) / mkt->cint[4].lc->sma5 * 100 : 0,
(mkt->cint[5].tc > HISTSIZE) ? (mkt->minprofit - mkt->cint[5].lc->sma5) / mkt->cint[5].lc->sma5 * 100 : 0,
(mkt->cint[6].tc > HISTSIZE) ? (mkt->minprofit - mkt->cint[6].lc->sma5) / mkt->cint[6].lc->sma5 * 100 : 0);
}
if(!hdr)
irc_reply("no active markets");
}
void
ircu_showMarkets(const struct market *m)
{
int x = 0, hdr = 0;
for(; x < MARKET_MAX; x++)
{
struct market *mkt = &markets[x];
char temp[50], temp2[50];
if(!(markets[x].state & MKS_ACTIVE) || (m != NULL && m != &markets[x]))
continue;
if(!hdr++)
{
irc_reply("## market score last 1-min 5-min 15-min 30-min 1-hour 4-hour 1-day");
irc_reply("----------------------------------------------------------------------------------------------------");
}
snprintf(temp, 49, "%s:%s-%s", mkt->exch->name, mkt->asset, mkt->currency);
if(mkt->state & MKS_LEARNING)
irc_reply("%-02d %-16s learning mode: %d of %d candles",
x, temp, mkt->c_array, mkt->init_candles);
else
{
int y = 0, score = 0;
for(; y < CINTS_MAX; y++)
score += market_score(mkt, y);
irc_reply("%-02d %-16s %-5.2f %-16s %-6.2f %-6.2f %-6.2f %-6.2f %-6.2f %-6.2f %-6.2f",
x, temp, ((float)score / (CINT_SCORE_MAX * CINTS_MAX)) * 100,
trim0(temp2, 49, mkt->cint[0].lc->close),
(mkt->cint[0].tc) ? mkt->cint[0].lc->asize : 0,
(mkt->cint[1].tc) ? mkt->cint[1].lc->asize : 0,
(mkt->cint[2].tc) ? mkt->cint[2].lc->asize : 0,
(mkt->cint[3].tc) ? mkt->cint[3].lc->asize : 0,
(mkt->cint[4].tc) ? mkt->cint[4].lc->asize : 0,
(mkt->cint[4].tc >= 4) ?
(mkt->cint[4].lc->close - mkt->cint[4].c[HISTSIZE - 4].close)
/ mkt->cint[4].c[HISTSIZE - 4].close * 100 : 0,
(mkt->cint[4].tc >= 24) ?
(mkt->cint[4].lc->close - mkt->cint[4].c[HISTSIZE - 24].close)
/ mkt->cint[4].c[HISTSIZE - 24].close * 100 : 0);
}
}
if(!hdr)
irc_reply("no active markets");
}
void
ircu_showMarket(int argc, const char *argv[])
{
struct market *mkt = NULL;
int mid = -1;
if(argc >= 2)
{
if(!strcasecmp(argv[1], "help"))
{
irc_reply("usage: show [mar]ket [id] [item]");
return;
}
mid = atoi(argv[1]);
if((argv[1][0] != '0' && mid == 0) || mid < 0 || mid >= MARKET_MAX)
{
irc_reply("unknown market: %s", argv[1]);
return;
}
mkt = &markets[mid];
if(!(mkt->state & MKS_ACTIVE))
{
irc_reply("unknown market: %s", argv[1]);
return;
}
if(argc > 2)
{
if(!strncasecmp(argv[2], "long", 4))
{
if(mkt->state & MKS_LONG && mkt->exposed)
ircu_shlong(mkt);
else
irc_reply("market #%d is not long", mid);
}
// show market x strategy
else if(!strncasecmp(argv[2], "stra", 4))
{
if(mkt->strat == NULL)
irc_reply("strat: none");
else
irc_reply("strat: %s desc: %s msg: %s",
mkt->strat->name, mkt->strat->desc,
(mkt->a.msg == NULL ) ? "none" : mkt->a.msg);
}
// show market x cint
else if(!strncasecmp(argv[2], "cint", 4))
{
char temp[1000];
int cint = 0;
memset((void *)temp, 0, 1000);
for(; cint < CINTS_MAX; cint++)
{
char temp2[50];
snprintf(temp2, 49, "%s:%d ",
cint2str(cint_length(cint)), mkt->cint[cint].tc);
strncat(temp, temp2, 999);
}
irc_reply(temp);
}
else if(!strncasecmp(argv[2], "can", 3))
{
int cint = 0, idx = 0, x = 0;
char str[1000], *temp;
struct candle *c;
if(argc > 3)
{
cint = atoi(argv[3]);
if(cint < 0 || cint >= CINTS_MAX)
{
irc_reply("candle interval (cint) must be between 0 and %d",
CINTS_MAX - 1);
return;
}
if(argc == 5)
idx = atoi(argv[4]);
}
if(idx < 0 || idx >= HISTSIZE)
{
irc_reply("candle index must be between 0 and %d", HISTSIZE - 1);
return;
}
if(idx > mkt->cint[cint].tc - 1)
{
irc_reply("candle int #%d has only seen %d candles thus far", cint,
mkt->cint[cint].tc);
return;
}
irc_reply_str(candle_str(&mkt->cint[cint].c[idx], cint_length(cint),
false));
}
else if(!strncasecmp(argv[2], "avg", 3))
{
double high = 0, low = 0;
struct candle_int *ci;
int x = 0, cint = 0;
if(argc > 3)
cint = atoi(argv[3]);
if(cint < 0 || cint >= CINTS_MAX)
{
irc_reply("candle interval (cint) must be between 0 and %d",
CINTS_MAX - 1);
return;
}
if(mkt->cint[cint].tc < HISTSIZE)
{
irc_reply("candle interval #%d (%s) is still learning (%d left)",
cint, cint2str(mkt->cint[cint].length),
HISTSIZE - mkt->cint[cint].tc);
return;
}
for(; x < 10; x++)
{
struct candle *c;
ci = &mkt->cint[cint];
c = &ci->c[HISTSIZE - x - 1];
if(low == 0 || c->low < low)
low = c->low;
if(high < c->high)
high = c->high;
}
irc_reply("ci_high: %.11f L10_high: %.11f d: %.2f ci_low: %.11f L10_low: %.11f d: %.2f sma2: %.11f L10_high_sma2: %.2f L10_low_sma2: %.2f",
ci->high, high, (high - ci->high) / ci->high * 100,
ci->low, low, (low - ci->low) / ci->low * 100, ci->lc->sma2,
(high - ci->lc->sma2) / ci->lc->sma2 * 100,
(low - ci->lc->sma2) / ci->lc->sma2 * 100);
}
/*
// show market trend
if(!strncasecmp(argv[2], "trend", 5))
{
struct candle_int *ci;
int cint = 0, x = 0, y;
double x1 = 0, x2 = 0, x3 = 0;
double lowest = 0, highest = 0;
if(argc > 3)
cint = atoi(argv[3]);
ci = &mkt->cint[cint];
y = ci->tc_sma5;
for(;x < TREND_MAX; x++, y = (y) ? y - 1 : TREND_MAX - 1)
{
int z = (y) ? y - 1 : TREND_MAX - 1;
double diff = 0;
if(x > 0 && x < 9)
{
if(ci->sma5[y].dir == UP)
{
x1 += ci->sma5[y].price;
if(highest == 0 || ci->sma5[y].price > highest)
highest = ci->sma5[y].price;
}
else
{
x2 += ci->sma5[y].price;
if(lowest == 0 || ci->sma5[y].price < lowest)
lowest = ci->sma5[y].price;
}
}
if(x < TREND_MAX -1)
diff = (ci->sma5[y].price - ci->sma5[z].price) / ci->sma5[z].price * 100;
irc_reply("[%d:%d] dir: %s price: %.11f count: %d diff: %.2f", x, y,
(ci->sma5[y].dir == UP) ? "UP" : "DN", ci->sma5[y].price,
ci->sma5[y].count, diff);
}
x1 /= 4;
x2 /= 4;
x3 = (x1 - x2) / x2 * 100;
irc_reply("high avg: %.11f high: %.11f (int: %.11f) low avg: %.11f low: %.11f (int: %.11f) diff: %.2f",
x1, highest, ci->high, x2, lowest, ci->low, x3);
}
else if(!strncasecmp(argv[2], "highlow", 7) || !strncasecmp(argv[2], "hl", 2))
{
struct candle_int *ci;
int x = 0, y, cint = 0;
char temp[4][100];
double high_avg = 0, low_avg = 0, diff;
if(argc > 3)
cint = atoi(argv[3]);
ci = &mkt->cint[cint];
y = ci->tc_hl;
for(; x < TREND_MAX; x++, y = (y) ? y - 1 : TREND_MAX - 1)
{
struct trend *t = &ci->hl[y];
diff = (t->high - t->low) / t->low * 100;
if(x) // exclude first frame that is still in progress
{
high_avg += t->high;
low_avg += t->low;
}
if(!x)
irc_reply("%d: %-15s (%-15s) %-15s (%-15s) diff: %.2f frame: %d", x,
trim0(temp[0], 99, t->high),
trim0(temp[1], 99, t->high_sma),
trim0(temp[2], 99, t->low),
trim0(temp[3], 99, t->low_sma),
diff, ci->frame);
else
irc_reply("%d: %-15s (%-15s) %-15s (%-15s) diff: %.2f", x,
trim0(temp[0], 99, t->high),
trim0(temp[1], 99, t->high_sma),
trim0(temp[2], 99, t->low),
trim0(temp[3], 99, t->low_sma),
diff);
}
high_avg /= (TREND_MAX - 1);
low_avg /= (TREND_MAX - 1);
diff = (high_avg - low_avg) / low_avg * 100;
irc_reply("avg high: %s avg low: %s diff: %.2f",
trim0(temp[0], 99, high_avg), trim0(temp[1], 99, low_avg), diff);
} */
// show market settings
else if(!strncasecmp(argv[2], "set", 3))
{
irc_reply("market #%d (%s:%s-%s) settings", mid, mkt->exch->name, mkt->asset, mkt->currency);
irc_reply("stoploss:%.2f stoploss_adjust:%.2f stoploss_new:%.2f", mkt->u.stoploss, mkt->u.stoploss_adjust, mkt->u.stoploss_new);
irc_reply("greed:%.2f minprofit:%.2f p_start_cur:%.2f", mkt->u.greed, mkt->u.minprofit, mkt->stats.p_start_cur);
if(mkt->opt & MKO_TRAILSTOP)
irc_reply("trailstop:enabled");
else
irc_reply("trailstop:disabled");
if(mkt->opt & MKO_AI)
irc_reply("ai:enabled");
else
irc_reply("ai:disabled");
if(mkt->opt & MKO_PAPER)
irc_reply("paper:enabled");
else
irc_reply("paper:disabled");
if(mkt->u.funds > 0)
irc_reply("funds:%.11f (use this amount for LONG)", mkt->u.funds);
else
{
if(mkt->u.pct > 0)
irc_reply("pct:%.11f (use this percentage of available bal for LONG)", mkt->u.pct);
else
irc_reply("using all available balance for LONGs");
}
if(mkt->strat != NULL)
irc_reply("strategy:%s", mkt->strat->name);
else
irc_reply("strategy:none");
return;
}
else
irc_reply("unknown show item: %s", argv[2]);
}
else
ircu_showMarkets(mkt);
}
else
ircu_showMarkets(NULL);
}
void
ircu_showOrder(int argc, const char *argv[])
{
struct exchange *exch = NULL;
int row = 0, x = 0;
if(argc == 2)
{
if((exch = exch_find(argv[1])) == NULL)
{
irc_reply("unsupported exchange: %s", argv[1]);
return;
}
}
for(; x < ORDERS_MAX; x++)
{
if((exch != NULL && orders[x].exch != exch) || !orders[x].type)
continue;
if(!row++)
{
irc_reply("## exchange product state type side price size filled");
irc_reply("---------------------------------------------------------------------------------------");
}
irc_reply("%-02d %-8.8s %-11.11s %-5.5s %-5.5s %-5.5s %-15.9f %-10.5f %.5f", x,
orders[x].exch->name,
orders[x].product,
order_state(orders[x].state),
(orders[x].state & ORS_STOP) ? "stop" : order_type(orders[x].type),
(orders[x].state & ORS_LONG) ? "long" : "short",
orders[x].price,
orders[x].size,
orders[x].filled);
}
if(!row)
irc_reply("no active orders found.");
}
void
ircu_showPerf(int argc, const char *argv[])
{
int x = 0, hdr = 0, paper = (argc > 1) ? true : false;
for(; x < MARKET_MAX; x++)
{
if(markets[x].state & MKS_ACTIVE)
{
struct market *mkt = &markets[x];
char temp[3][50];
if(!hdr++)
{
// 12 12345678901234567890 12345678901234567890 12345678901234567890 123456 123456 1234 1234 123
irc_reply("id market fees profit pct trades wins loss wla");
irc_reply("--------------------------------------------------------------------------------------------------------");
}
sprintf(temp[0], "%s:%s-%s", mkt->exch->name, mkt->asset, mkt->currency);
irc_reply("%-2d %-20s %-20s %-20s %-6.2f %-6d %-4d %-4d %.2f",
x, temp[0],
trim0(temp[1], 50, (paper == true) ? mkt->stats.p_fees : mkt->stats.fees),
trim0(temp[2], 50, (paper == true) ? mkt->stats.p_profit : mkt->stats.profit),
(paper == true) ?
(mkt->stats.p_profit) ? mkt->stats.p_profit / mkt->stats.p_start_cur * 100 : 0 :
(mkt->stats.profit) ? mkt->stats.profit / mkt->stats.start_cur * 100 : 0,
(paper == true) ? mkt->stats.p_trades : mkt->stats.trades,
(paper == true) ? mkt->stats.p_wins : mkt->stats.wins,
(paper == true) ? mkt->stats.p_losses : mkt->stats.losses,
(paper == true) ? (mkt->stats.p_wins) ?
(mkt->stats.p_wins / (double)(mkt->stats.p_wins + mkt->stats.p_losses)) * 100 : 0 :
(mkt->stats.wins) ? (mkt->stats.wins / (double)(mkt->stats.wins + mkt->stats.losses)) * 100 : 0);
} } if(!hdr) irc_reply("no active markets"); }
void
ircu_showProduct(int argc, const char *argv[])
{
struct exchange *exch;
struct product *p;
if(argc >= 2)
{
if(!strcasecmp(argv[1], "help"))
goto ircu_showProductUsage;
else if((exch = exch_find(argv[1])) == NULL)
{
irc_reply("unsupported exchange: %s", argv[1]);
return;