File: l2sr.c

package info (click to toggle)
linuxtrade 3.65-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,100 kB
  • ctags: 1,978
  • sloc: ansic: 30,168; sh: 4,922; perl: 566; makefile: 126
file content (779 lines) | stat: -rw-r--r-- 14,250 bytes parent folder | download | duplicates (2)
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
/*b
 * Copyright (C) 2001,2002  Rick Richardson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Rick Richardson <rickr@mn.rr.com>
b*/

/*
 * Display the L2 book using a generic streamer datafeed (e.g. schwab)
 *
 * N.B. Since only one streamer has been implemented so far, this model
 * might need to be changed to accomodate other streamers.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ncurses.h>
#include <panel.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include "curse.h"
#include "error.h"
#include "rc.h"
#include "streamer.h"
#include "linuxtrade.h"
#include "l2sr.h"
#include "p2open.h"
#include "util.h"

static WINDOW	*Win;
static WINDOW	*Hdrwin;
static WINDOW	*Subwin;
static PANEL	*Panel;
static char	Symbol[SYMLEN+1];
static STREAMER	Sr;

/*
 * Book
 */
typedef struct
{
	char	recid[RECIDLEN+1];	// Record ID, key for adding/deleting
	char	mmid[MMIDLEN+1];	// Market Maker name
	double	price;
	int	size;
	int	time;			// Time in seconds since midnight
	int	key2;			// Secondary sorting key
	char	code;			// Display selection code
	char	tick;			// =, +, -
} BOOKE;

#define		NUMBOOK	2000
#define		NUMSET	5

// Used for displaying bar
typedef struct
{
	int	num;
	int	size;
} SET;

// Entire book
typedef struct
{
	int	num;		// num orders
	int	total;		// total size
	SET	set[NUMSET];
	BOOKE	ent[NUMBOOK];
} BOOK;

static BOOK	Ask, Bid;
static	char	Code = 0;
static int	DisplayOn = TRUE;

/*
 * Calculate and display the bid/ask relative volume bar
 */
static attr_t SetColors[NUMSET] =
{
	COLOR_PAIR(BLACKonGREEN),
	COLOR_PAIR(BLACKonYELLOW),
	COLOR_PAIR(BLACKonCYAN),
	A_REVERSE|COLOR_PAIR(REDonWHITE),
	A_REVERSE|COLOR_PAIR(BLUEonWHITE)
};

static void
calc_bar(BOOK *b)
{
	int	i;
	int	n = -1;
	double	lastprice = -1;

	b->total = 0;
	for (i = 0; i < b->num; ++i)
	{
		BOOKE	*e = &b->ent[i];

		b->total += e->size * 100;
		if (n < (NUMSET-1) && e->price != lastprice)
		{
			++n;
			lastprice = e->price;
			b->set[n].size = e->size*100;
			b->set[n].num = 1;
		}
		else
		{
			b->set[n].size += e->size*100;
			++b->set[n].num;
		}
	}
	while (++n < NUMSET)
	{
		b->set[n].size = 0;
		b->set[n].num = 0;
	}
}

static void
disp_bar(void)
{
	int	i, x;
	int	total;
	int	barcols = (getmaxx(Hdrwin) - 1) & ~1;
	int	bidcols, askcols;

	calc_bar(&Ask);
	calc_bar(&Bid);

	total = Ask.total + Bid.total;

	wblankrect(Hdrwin, 1, 0, 1, getmaxx(Hdrwin)-1, FALSE);
	if (total == 0)
	{
		return;
	}
	else if (Ask.total == 0)
	{
		askcols = 0;
		bidcols = barcols;
	}
	else if (Bid.total == 0)
	{
		bidcols = 0;
		askcols = barcols;
	}
	else
	{
		bidcols = lrint((double)barcols * Bid.total / total);
		askcols = lrint((double)barcols * Ask.total / total);
	}

	wattrset(Hdrwin, A_REVERSE);
	mvwaddch(Hdrwin, 1, bidcols, ACS_VLINE);

	if (bidcols)
	{
		x = bidcols;
		for (i = 0; i < NUMSET; ++i)
		{
			int	cols;

			cols = lrint((double)bidcols * Bid.set[i].size
							/ Bid.total);
			if (cols == 0 && Bid.set[i].size)
				cols = 1;

			wattrset(Hdrwin, SetColors[i]);
			while (cols--)
				mvwaddch(Hdrwin, 1, --x, ' ');
		}
	}

	if (askcols)
	{
		x = bidcols;
		for (i = 0; i < NUMSET; ++i)
		{
			int	cols;

			cols = lrint((double)askcols * Ask.set[i].size
							/ Ask.total);
			if (cols == 0 && Ask.set[i].size)
				cols = 1;

			wattrset(Hdrwin, SetColors[i]);
			while (cols--)
				mvwaddch(Hdrwin, 1, ++x, ' ');
		}
	}
	wattrset(Hdrwin, A_NORMAL);
}

static void
disp_book(BOOK *b)
{
	int	y, x;
	int	maxy = getmaxy(Subwin) - 1;
	int	n = -1;
	double	lastprice = -1;
	int	incnt = 0;
	int	insize = 0;
	int	remain = 0;
	int	wid =  getmaxx(Subwin)/3;
	int	i;

	if (!DisplayOn)
		return;

	x = (b == &Bid) ? 0 : wid;

	for (y = i = 0; i < b->num; ++i)
        {
		BOOKE	*e = &b->ent[i];

		if (Code && Code != e->code)
			continue;

		if (e->price != lastprice)
		{
			if (++n == NUMSET) n = NUMSET - 1;
			lastprice = e->price;
		}
		if (n == 0)
		{
			++incnt;
			insize += e->size;
		}
		wattrset(Subwin, SetColors[n]);
		if (e->price >= 1000)
			mvwprintw(Subwin, y, x,
				"%-5.5s%6.1f %4d",
				e->mmid, e->price, e->size);
		else if (e->price < 1.0)
			mvwprintw(Subwin, y, x,
				"%-5.5s%6.3f %4d",
				e->mmid, e->price, e->size);
		else
			mvwprintw(Subwin, y, x,
				"%-5.5s%6.2f %4d",
				e->mmid, e->price, e->size);
		wprintw(Subwin, " %02d:%02d:%02d",
				e->time/3600,
				(e->time/60) % 60,
				e->time % 60);
		wattrset(Subwin, A_NORMAL);
		if (y++ == maxy)
			break;
        }
	if (y == 0)
		mvwprintw(Subwin, y++, x, "%*s", -wid, "...no entries...");
	while (y <= maxy)
	{
		// \240 charater is a hack to force gnome-terminal to
		// clear these lines
		mvwprintw(Subwin, y++, x, "%*s", wid, "\240");
	}
	remain = b->num - i;
	if (remain > 0)
	{
		mvwprintw(Subwin, maxy, x, "%*s", wid, "");
		mvwprintw(Subwin, maxy, x, "%d more", remain);
	}

	disp_bar();
}

static void
disp_codes(void)
{
	mvwprintw(Win, getmaxy(Win)-1, getmaxx(Win)/2 - 24/2,
		"Showing Code (%c) of (*,K,O)", Code ? Code : '*');
}

static int
askcmp(const void *ve1, const void *ve2)
{
	BOOKE	*e1 = (BOOKE *) ve1;
	BOOKE	*e2 = (BOOKE *) ve2;

	if (e1->price < e2->price)
		return -1;
	if (e1->price > e2->price)
		return 1;

	if (e1->key2 > e2->key2)
		return 1;
	if (e1->key2 < e2->key2)
		return -1;

	if (e1->time > e2->time)
		return 1;
	if (e1->time < e2->time)
		return -1;

	if (e1->size < e2->size)
		return 1;
	if (e1->size > e2->size)
		return -1;

	return (strcmp(e1->mmid, e2->mmid));

}

static int
bidcmp(const void *ve1, const void *ve2)
{
	BOOKE	*e1 = (BOOKE *) ve1;
	BOOKE	*e2 = (BOOKE *) ve2;

	if (e1->price < e2->price)
		return 1;
	if (e1->price > e2->price)
		return -1;

	if (e1->key2 > e2->key2)
		return 1;
	if (e1->key2 < e2->key2)
		return -1;

	if (e1->time > e2->time)
		return 1;
	if (e1->time < e2->time)
		return -1;

	if (e1->size < e2->size)
		return 1;
	if (e1->size > e2->size)
		return -1;

	return (strcmp(e1->mmid, e2->mmid));
}

static void
add_entry(BOOK *b, BOOKE *e)
{
	int	i;

	for (i = 0; i < b->num; ++i)
		if (strcmp(b->ent[i].recid, e->recid) == 0)
			break;

	if (b->num == NUMBOOK)
	{
		error(1, "Too many book entries\n");
		return;
	}

	b->ent[i++] = *e;
	if (i > b->num)
		b->num = i;

	qsort(b->ent, b->num, sizeof(b->ent[0]),
		b == &Bid ? bidcmp : askcmp);
}

static int
del_entry(BOOK *b, char *recid)
{
	int	i;

	for (i = 0; i < b->num; ++i)
		if (strcmp(b->ent[i].recid, recid) == 0)
			break;
	if (i == b->num)
		return 0;

	for (i = i+1; i < b->num; ++i)
		b->ent[i-1] = b->ent[i];
	--b->num;

	return 1;
}

int
l2sr_getlast(STREAMER sr, char *sym, char *mmid, L2BA *bap)
{
	int	i;

	if (!Win)
		return -1;
	if (sr != Sr)
		return -1;
	if (strcmp(sym, Symbol))
		return -1;

	memset(bap, 0, sizeof(*bap));

	for (i = 0; i < Bid.num; ++i)
	{
		if (strcmp(Bid.ent[i].mmid, mmid))
			continue;
		// strcpy(bap->mmid, Bid.ent[i].mmid);
		bap->bid = Bid.ent[i].price;
		bap->bidsz = Bid.ent[i].size;
		bap->time = Bid.ent[i].time;
		bap->code = Bid.ent[i].code;
	}
	for (i = 0; i < Ask.num; ++i)
	{
		if (strcmp(Ask.ent[i].mmid, mmid))
			continue;
		// strcpy(bap->mmid, Ask.ent[i].mmid);
		bap->ask = Ask.ent[i].price;
		bap->asksz = Ask.ent[i].size;
		bap->time = Ask.ent[i].time;
		bap->code = Ask.ent[i].code;
	}
	return (bap->ask || bap->bid);
}

void
l2sr_ba(STREAMER sr, char *sym, L2BA *bap, int numba)
{
	if (!Win)
		return;
	if (sr != Sr)
		return;
	if (strcmp(sym, Symbol))
		return;

	while (numba--)
	{
		BOOKE	be;

		strcpy(be.mmid, bap->mmid);
		strcpy(be.recid, bap->mmid);
		be.time = bap->time;
		be.code = bap->code;
		be.tick = bap->bidtick;
		be.key2 = 0;

		be.price = bap->bid;
		be.size = bap->bidsz;
		if (be.price)
			add_entry(&Bid, &be);
		else
			del_entry(&Bid, be.recid);

		be.price = bap->ask;
		be.size = bap->asksz;
		be.tick = bap->asktick;
		if (be.price)
			add_entry(&Ask, &be);
		else
			del_entry(&Ask, be.recid);

		++bap;
	}

	disp_book(&Bid);
	disp_book(&Ask);
	disp_codes();
	touchwin(Win);
	update_panels(); refresh(); // doupdate();
}

void
l2sr_sets(STREAMER sr, char *sym, L2SETS *sp, int numsets)
{
	int	bids = 0;
	int	asks = 0;

	if (!Win)
		return;
	if (sr != Sr)
		return;
	if (strcmp(sym, Symbol))
		return;

	while (numsets--)
	{
		BOOKE	be;

		strcpy(be.mmid, sp->mmid);
		strcpy(be.recid, sp->recid);
		be.time = sp->time;
		be.code = sp->code;
		be.tick = sp->tick;
		be.key2 = sp->key2;

		be.price = sp->price;
		be.size = sp->size;
		if (sp->isbid)
		{
		    if (be.price)
			    add_entry(&Bid, &be);
		    else
			    del_entry(&Bid, be.recid);
		    ++bids;
		}
		else
		{
		    if (be.price)
			    add_entry(&Ask, &be);
		    else
			    del_entry(&Ask, be.recid);
		    ++asks;
		}
		++sp;
	}

	if (bids)
	    disp_book(&Bid);
	if (asks)
	    disp_book(&Ask);
	if (!bids && !asks)
	    return;
	disp_codes();
	touchwin(Win);
	update_panels(); refresh(); // doupdate();
}

void
l2sr_trade(STREAMER sr, double price, int size, int time, int row, int tstype)
{
	attr_t	attr = A_NORMAL;

	if (!Win)
		return;
	if (sr != Sr)
		return;

	if (row == -1)
	{
		wvscrollrect(Subwin,
			0, getmaxx(Subwin)*2/3,
			getmaxy(Subwin), getmaxx(Subwin),
			-1);
		row = 0;
	}

	switch(tstype)
	{
	case TS_ATHIGH: attr = COLOR_PAIR(1) | RevOrBold; break;
	case TS_ABOVE:
	case TS_ATASK:	attr = COLOR_PAIR(1) | A_NORMAL; break;
	case TS_ATBID:
	case TS_BELOW:	attr = COLOR_PAIR(2) | A_NORMAL; break;
	case TS_ATLOW:	attr = COLOR_PAIR(2) | RevOrBold; break;
	}

	qfmt_init();
	wattrset(Subwin, attr);
	if (price < 1.0)
		mvwprintw(Subwin, row, getmaxx(Subwin)*2/3,
			"%7.3f %-6.6s %02d:%02d:%02d",
			price, volfmt6b(size),
			time/3600, (time/60)%60, time%60);
	else
		mvwprintw(Subwin, row, getmaxx(Subwin)*2/3,
			"%7.2f %-6.6s %02d:%02d:%02d",
			price, volfmt6b(size),
			time/3600, (time/60)%60, time%60);
	wattrset(Subwin, A_NORMAL);
	touchwin(Win);
	update_panels(); refresh(); // doupdate();
}

void
l2sr_display(STREAMER sr, int on)
{
	if (!Win)
		return;
	if (sr != Sr)
		return;
	if (on == DisplayOn)
		return;

	DisplayOn = on;
	if (DisplayOn)
	{
		disp_book(&Bid);
		disp_book(&Ask);
		disp_codes();
		touchwin(Win);
		update_panels(); refresh(); // doupdate();
	}
}

void
l2sr_popup(STOCK *sp, STREAMER sr)
{
	char	title[256];
	int	rc;

	Win = minmaxwin(22, 36, FALSE, FALSE);
	if (!Win)
		error(1, "Can't create l2sr window\n");

	wbkgd(Win, Reverse ? A_REVERSE : A_NORMAL);

	box(Win, 0, 0);

	wattrset(Win, A_BOLD);
	mvwprintw(Win, 0, 3, "%s", sp->sym);
	sprintf(title, "%s Level II", sr->id);
	mvwprintw(Win, 0, getmaxx(Win)-strlen(title)-3, title);
	wattrset(Win, A_NORMAL);

	Hdrwin = derwin(Win, 4, getmaxx(Win) - 2, 1, 1);
	if (!Hdrwin)
		error(1, "Can't create l2sr hdrwindow\n");

	Subwin = derwin(Win,
			getmaxy(Win) - 2 - getmaxy(Hdrwin), getmaxx(Win) - 2,
			1 + getmaxy(Hdrwin), 1);
	if (!Subwin)
		error(1, "Can't create l2sr subwindow\n");

	DisplayOn = TRUE;

	strcpy(Symbol, sp->sym);

	wattrset(Hdrwin, A_BOLD);
	mvwprintw(Hdrwin, getmaxy(Hdrwin) - 1, 0,
			"MMID    Bid Size Time");
	mvwprintw(Hdrwin, getmaxy(Hdrwin) - 1, getmaxx(Hdrwin)/3,
			"MMID    Ask Size Time");
	mvwprintw(Hdrwin, getmaxy(Hdrwin) - 1, 2*getmaxx(Hdrwin)/3,
			"  Price   Size Time....");
	wattrset(Hdrwin, A_NORMAL);

	Panel = new_panel(Win);

	Bid.num = Ask.num = 0;
	Sr = sr;

	rc = (*sr->send_l2)(sr, sp->sym, TRUE);
	if (rc == SR_AUTH)
	{
		wattrset(Subwin, A_BOLD);
		mvwcenter(Subwin, getmaxy(Subwin)/2,
			"Sorry, you are not authorized for L2 quotes");
		wattrset(Subwin, A_NORMAL);
	}

	blankrect(LINES-1, 0, LINES-1, COLS-1, 0);
	touchwin(Win);
}

static void
popdown(void)
{
	if (Sr && Sr->send_l2)
		(*Sr->send_l2)(Sr, Symbol, FALSE);

	hide_panel(Panel);
	update_panels();
	del_panel(Panel);
	delwin(Hdrwin);
	delwin(Subwin);
	delwin(Win);
	Win = NULL;
}

int
l2sr_command(int c, STREAMER sr)
{
	MEVENT	m;

	switch (c)
	{
	case '*':
	case 'K':
	case 'O':
		if (c == '*') Code = 0; else Code = c;
		disp_book(&Bid);
		disp_book(&Ask);
		disp_codes();
		touchwin(Win);
		update_panels(); refresh(); // doupdate();
		break;
	case KEY_MOUSE:
		if (getmouse(&m) != OK)
			break;

		// Ignore clicks in our window
		if (m.y >= getbegy(Win)
			&& m.y < getbegy(Win) + getmaxy(Win))
			break;

		// popdown and reprocess clicks in main window
		if (ungetmouse(&m) == OK)
			Ungetch = 1;
		popdown();
		return 2;

	case KEY_F(11):
		if (1) print_rect_troff(getbegy(Win), getbegx(Win),
				getmaxy(Win), getmaxx(Win),
				NULL, "screen.tr");
		break;

	case KEY_UP:
	case KEY_DOWN:
		// Allow cursor motion to select next stock
		popdown();
		return 1;

	default:
		popdown();
		return 2;
	}
	return 0;
}

char *
mmid_lookup(char *mmid)
{
	FILE		*fp;
	char		*home = getenv("HOME");
	static char	buf[BUFSIZ];
	char		*p;
	char		*name = "Unknown";
	int		fileno = 1;

	fp = fopen("mmids.txt", "r");
	if (!fp)
	{
	userfile:
		++fileno;
		sprintf(buf, "%s/." PROGNAMESTR "/players.txt", home);
		fp = fopen(buf, "r");
		if (!fp)
		{
		systemfile:
			++fileno;
			fp = fopen("/usr/share/" PROGNAMESTR "/players.txt",
					"r");
			if (!fp)
				return "Unknown";
		}
	}

	// Find mmid tag in file
	while (fgets(buf, sizeof(buf), fp))
	{
		p = strchr(buf, '\n'); if (p) *p = 0;
		p = strchr(buf, '\r'); if (p) *p = 0;
		p = strchr(buf, '#'); if (p) *p = 0;
		if (!buf[0]) continue;

		name = strchr(buf, '\t');
		if (name) *name++ = 0; else name = "";

		if (strcasecmp(buf, mmid) == 0)
			break;
	}
	if (feof(fp))
	{
		fclose(fp);
		if (fileno < 2)
			goto userfile;
		if (fileno < 3)
			goto systemfile;
		return "Unknown";
	}
	return name;
}