File: holdings.c

package info (click to toggle)
linuxtrade 3.65-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,880 kB
  • ctags: 1,969
  • sloc: ansic: 30,091; sh: 2,278; perl: 566; makefile: 126
file content (445 lines) | stat: -rw-r--r-- 8,765 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
/*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*/

#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ncurses.h>
#include <panel.h>
#include <errno.h>
#include <ctype.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"

static WINDOW	*Win;
static WINDOW	*Subwin;
static PANEL	*Panel;

static int	StockNum, FieldNum;
static int	SaveStockCursor;
static char	FieldBuf[256];

static int	Obese;
static int	PerShare;	// Data entry by pershare price

// Shares ...
#define	F1X	15
#define	F1W	7
#define	OF1X	82
#define	OF1W	7

// Total cost ...
#define	F2X	23
#define	F2W	8
#define	OF2X	90
#define	OF2W	10

// Gain ...
#define	F3X	31
#define	F3W	9
#define	OF3X	100
#define	OF3W	11

// %Gain ...
#define	OF4X	112
#define	OF4W	7

static void
display_cursor(int sn, int fn, int on)
{
	int	y, x;
	int	fx;
	int	fwid;

	if (fn == 0)
	{
		fx = Obese ? OF1X : F1X;
		fwid = Obese ? OF1W : F1W;
	}
	else
	{
		fx = Obese ? OF2X : F2X;
		fwid = Obese ? OF2W : F2W;
	}

	y = Stock[sn].y;

	if (on)
	{
		mvaddch(y, fx-1, '[');
		for (x = 0; x < fwid; ++x)
		{
			chtype	ch = mvinch(y, fx + x);
			ch |= RevOrBold;
			mvaddch(y, fx + x, ch);
		}
		addch(']');
	}
	else
	{
		mvaddch(y, fx-1, ' ');
		for (x = 0; x < fwid; ++x)
		{
			chtype	ch = mvinch(y, fx + x);
			ch &= ~RevOrBold;
			mvaddch(y, fx + x, ch);
		}
		addch(' ');
	}
}

static char *Help0[] =
{
"Enter the number of shares in your portfolio and the total cost of",
"those shares in the SHARES and TOTCOST fields.  Use the arrow keys,",
"tab, space, or enter to move between the SHARES and the TOTCOST",
"fields.  Enter short positions using a negative TOTCOST. To enter",
"by cost per share, type '*<cost-per-share>' in the TOTCOST field.",
"",
"Press 'q' to quit or 's' to save and quit.",
NULL
};

static char *Help1[] =
{
"Enter the number of shares in your portfolio and the cost per share",
"of those shares in the SHARES and SHRCOST fields.  Use the arrow keys,",
"tab, space, or enter to move between the SHARES and the SHRCOST",
"fields.  Enter short positions using a negative SHRCOST. To enter",
"by total cost, type '=<total-cost>' in the SHRCOST field.",
"",
"Press 'q' to quit or 's' to save and quit.",
NULL
};

int
holdings_mode(int stocknum, int obese, int pershare)
{
	int	cols;
	int	i;
	char	**help;

	Win = newwin(9, cols80(), 4+NumStock, 0);
	if (!Win)
	{
		// error(1, "Can't create holdings window\n");
		return 1;
	}

	cols = getmaxx(Win);

	wbkgd(Win, Reverse ? A_REVERSE : A_NORMAL);

	box(Win, 0, 0);
	mvwprintw(Win, 0, (cols - 19)/2, "Holdings Data Entry");

	Subwin = derwin(Win, getmaxy(Win) - 2, getmaxx(Win) - 2, 1, 1);
	if (!Subwin)
	{
		// error(1, "Can't create info subwindow\n");
		delwin(Win);
		Win = NULL;
		return 2;
	}

	Obese = obese;
	StockNum = stocknum;
	PerShare = pershare;
	FieldNum = 0;
	display_cursor(StockNum, FieldNum, TRUE);

	SaveStockCursor = StockCursor;
	StockCursor = -1;

	FieldBuf[0] = 0;

	help = PerShare ? Help1 : Help0;
	for (i = 0; help[i]; ++i)
		mvwprintw(Subwin, i, 5, help[i]);

	Panel = new_panel(Win);
	return (0);
}

void
display_fieldbuf(void)
{
	int	y = Stock[StockNum].y;
	int	x, w;

	if (FieldNum == 0)
	{
		x = Obese ? OF1X : F1X;
		w = Obese ? OF1W : F1W;
	}
	else
	{
		x = Obese ? OF2X : F2X;
		w = Obese ? OF2W : F2W;
	}

	if (strlen(FieldBuf) > w)
	{
		FieldBuf[w] = 0;
		beep();
	}

	mvwprintw(stdscr, y, x, "%-*.*s", w, w, FieldBuf);
}

void
set_fieldbuf(void)
{
	STOCK	*sp = &Stock[StockNum];
	int	y = sp->y;
	double	gain, shrcost;

	qfmt_init();
	if (FieldNum == 0)
	{
		if (FieldBuf[0])
			sp->nshares = atoi(FieldBuf);
		mvwprintw(stdscr, y,
			Obese ? OF1X : F1X, "%*d",
			Obese ? OF1W : F1W, sp->nshares);
	}
	else
	{
		if (FieldBuf[0])
		{
			if (FieldBuf[0] == '*')
				sp->cost = atof(FieldBuf+1) * sp->nshares;
			else if (FieldBuf[0] == '=')
				sp->cost = atof(FieldBuf+1);
			else if (PerShare)
				sp->cost = atof(FieldBuf) * sp->nshares;
			else
				sp->cost = atof(FieldBuf);
		}
		shrcost = sp->nshares ? (sp->cost/sp->nshares) : 0;
		mvwprintw(stdscr, y,
			Obese ? OF2X : F2X, "%*.2f",
			Obese ? OF2W : F2W,
			PerShare ? shrcost : sp->cost);
	}

	if (sp->cost >= 0)
		gain = sp->nshares * sp->cur.last - sp->cost;
	else
		gain = -sp->cost - sp->nshares * sp->cur.last;

	mvwprintw(stdscr, y, Obese ? OF3X : F3X,
			"%s", Obese ? dolfmt11(gain) : dolfmt9(gain));
	set_color(sp->y, Obese ? OF3X : F3X, Obese ? OF3W : F3W,
			gain, 0.0,
			COLOR_PAIR(1), A_NORMAL, COLOR_PAIR(2));
	if (Obese)
	{
		mvwprintw(stdscr, y, OF4X,
			"%*s", OF4W,
			pctfmt7(gain, sp->cost >= 0 ? sp->cost : - sp->cost));
		set_color(sp->y, OF4X, OF4W,
			gain, 0.0,
			COLOR_PAIR(1), A_NORMAL, COLOR_PAIR(2));
	}

	FieldBuf[0] = 0;
}

void
holdings_cursor(void)
{
	int	x;

	if (FieldNum == 0)
		x = Obese ? OF1X : F1X;
	else
		x = Obese ? OF2X : F2X;
	x += strlen(FieldBuf);

	move(Stock[StockNum].y, x);
}

int
holdings_command(int c, STREAMER sr)
{
	char	*p;
	MEVENT	m;

	switch (c)
	{
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	case '+': case '-': case '.':
	value:
		p = strchr(FieldBuf, 0);
		if (p == FieldBuf)
		{
		}
		*p++ = c;
		*p = 0;
		display_fieldbuf();
		display_cursor(StockNum, FieldNum, TRUE);
		break;
	case '*':
	case '=':
		if (FieldNum == 0)
		{
			display_cursor(StockNum, FieldNum, FALSE);
			set_fieldbuf();
			FieldNum = 1;
			display_cursor(StockNum, FieldNum, TRUE);
			goto value;
		}
		else if (FieldNum == 1 && FieldBuf[0] == 0)
			goto value;
		else
			beep();
		break;
	case '\b':
		p = strchr(FieldBuf, 0);
		if (p == FieldBuf)
		{
			beep();
			break;
		}
		--p;
		*p = 0;
		display_fieldbuf();
		display_cursor(StockNum, FieldNum, TRUE);
		break;
	case KEY_DL:
	case KEY_DC:
	case 127:
		FieldBuf[0] = 0;
		display_fieldbuf();
		display_cursor(StockNum, FieldNum, TRUE);
		break;
	case KEY_HOME:
		display_cursor(StockNum, FieldNum, FALSE);
		set_fieldbuf();
		StockNum = FieldNum = 0;
		display_cursor(StockNum, FieldNum, TRUE);
		break;
	case KEY_END:
		display_cursor(StockNum, FieldNum, FALSE);
		set_fieldbuf();
		StockNum = NumStock - 1;
		FieldNum = 0;
		display_cursor(StockNum, FieldNum, TRUE);
		break;
	case '\r':
	case 'l':
	case KEY_RIGHT:
	case CTRL('I'):
	case ' ':
		display_cursor(StockNum, FieldNum, FALSE);
		set_fieldbuf();
		if (++FieldNum == 2)
		{
			FieldNum = 0;
			if (++StockNum == NumStock)
				StockNum = 0;
		}
		display_cursor(StockNum, FieldNum, TRUE);
		break;
	case 'h':
	case KEY_LEFT:
		display_cursor(StockNum, FieldNum, FALSE);
		set_fieldbuf();
		if (--FieldNum < 0)
		{
			FieldNum = 1;
			if (--StockNum < 0)
				StockNum = NumStock - 1;
		}
		display_cursor(StockNum, FieldNum, TRUE);
		break;
	case 'j':
	case KEY_DOWN:
		display_cursor(StockNum, FieldNum, FALSE);
		set_fieldbuf();
		if (++StockNum == NumStock)
			StockNum = 0;
		display_cursor(StockNum, FieldNum, TRUE);
		break;
	case 'k':
	case KEY_UP:
		display_cursor(StockNum, FieldNum, FALSE);
		set_fieldbuf();
		if (--StockNum < 0)
			StockNum = NumStock - 1;
		display_cursor(StockNum, FieldNum, TRUE);
		break;

	case KEY_MOUSE:
		if (getmouse(&m) != OK)
			break;
		// Ignore all clicks
		break;

	case KEY_F(11):
		print_rect_troff(2, 0,
				getbegy(Win)+getmaxy(Win) - 2, getmaxx(Win),
				NULL, "screen.tr");
		break;

	case 's':
		set_fieldbuf();
		display_cursor(StockNum, FieldNum, FALSE);
		StockCursor = SaveStockCursor;
		del_panel(Panel);
		delwin(Subwin);
		delwin(Win);
		Win = NULL;
		Subwin = NULL;
		// Let main code issue save confirmation
		return (1);

	case 033:
	case 'q':
	case 'x':
	case '#':
		set_fieldbuf();
		display_cursor(StockNum, FieldNum, FALSE);
		StockCursor = SaveStockCursor;
		del_panel(Panel);
		delwin(Subwin);
		delwin(Win);
		Win = NULL;
		Subwin = NULL;
		// Quit no save
		return (2);
	default:
		beep();
		break;
	}
	return (0);
}