-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrabportfolio.php
212 lines (190 loc) · 6.47 KB
/
grabportfolio.php
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
<?php
// for index comparison, portfolio was mostly formed at market open 10/8/13
// note that comparison is slightly biased as index dividend yield is much higher
$DIA_INITIAL_PRICE = 149.31;
$SPY_INITIAL_PRICE = 167.42;
$QQQ_INITIAL_PRICE = 78.75;
$SDOG_INITIAL_PRICE = 31.90;
$CASH = -14374.74;
// ticker, shares, cost basis
$stocks = array
(
array("SDOG", 289, 32.80),
array("AMBA", 314, 19.45),
array("GPRC", 3730, 2.40),
array("SPWR", 160, 31.42),
array("CCCL", 1400, 3.70),
array("PXLW", 1100, 5.02),
array("GALE", 3015, 2.43),
array("KGJI", 2700, 1.89),
array("CAMP", 217, 23.04),
array("GSAT", 2500, 1.25),
array("SGOC", 1100, 2.95),
array("SIAF", 3350, 0.56),
array("CHLN", 400, 3.00)
);
setlocale(LC_MONETARY,"en_US");
function isMarketOpen() {
// simplified--excludes holidays
date_default_timezone_set('US/Eastern');
$day = date('l');
// weekend, closed
if ($day == 'Saturday' || $day == 'Sunday') {
return false;
}
// weekday before or after hours
$hour = date('H');
if ($hour < 9 || $hour >= 16) {
return false;
}
// damn you for opening market at 9:30...
if ($hour == 9 && date('i') < 30) {
return false;
}
return true;
}
function getColoredString($string, $color = null) {
$colored_string = "";
if (isset($color)) {
$colors = array();
$colors['black'] = '0;30';
$colors['light_gray'] = '0;37';
$colors['red'] = '0;31';
$colors['green'] = '0;32';
$colors['blue'] = '0;34';
$colors['purple'] = '0;35';
$colors['cyan'] = '0;36';
$colors['light_cyan'] = '1;36';
// default to black
$txtColor = !empty($colors[$color]) ? $colors[$color] : $colors['black'];
$colored_string .= "\033[" . $txtColor . "m";
}
else {
return $string;
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
function monify_string($value, $paren = false) {
if ($value >= 0) {
$value= getColoredString("+" . $value, 'green');
}
else {
$value = getColoredString($value, 'red');
}
if ($paren) {
$value = '(' . $value . ')';
}
return $value;
}
function displayRow($data, $initialPrice = null) {
$ticker = $data[0];
$shares = $data[1];
$costBasis = $data[2];
$sourceURL = "http://finance.yahoo.com/d/quotes.csv?s=$ticker&f=b3b2c6l1p5rp6";
$sourceData = file_get_contents( $sourceURL );
// separate into lines
$sourceLines = str_getcsv($sourceData, "\n");
foreach( $sourceLines as $line ) {
$contents = str_getcsv( $line );
// Now, is an array of the comma-separated contents of a line
$bid = $contents[0];
$ask = $contents[1];
$change = $contents[2];
$last = $contents[3];
$ps = $contents[4];
$pe = $contents[5];
$pb = $contents[6];
// if bid or ask are not available (or after hours), use last price
if ( empty($bid) || empty($ask) || $bid <= 0 || $ask <= 0 || !isMarketOpen()) {
$price = $last;
}
else {
$price = ($bid+$ask)/2;
// handle anomalies
if ($price > 1.1*$last || $price < 0.9*$last) {
$price = $last;
}
}
$changePercent = monify_string(number_format(($change/$price)*100, 2)) . "%";
$value = $price*$shares;
$dayGain = $change*$shares;
$totalGain = $value - $costBasis*$shares;
if ($value > 0) {
$valueString = "$" . money_format("%!n", $value);
$dayGainString = monify_string(money_format("%!n",$dayGain), true);
$totalGainString = monify_string(money_format("%!n", $totalGain), true);
$totalChangePercent = monify_string(number_format(($totalGain/($costBasis*$shares))*100, 2)) . "%";
}
else {
$valueString = '';
$dayGainString = '';
$totalGainString = '';
$pe = '';
$ps = '';
$pb = '';
if (!empty($initialPrice)) {
// extra tab needed to properly align with table below
$totalChangePercent = "\t" . monify_string(number_format((($price-$initialPrice)/($initialPrice))*100, 2)) . "%";
}
}
// edge case to ensure columns align properly
if ($dayGain > -10 && $dayGain < 10) {
$dayGainString .= "\t";
}
if ($totalGain > -10 && $totalGain < 10) {
$totalGainString .= "\t";
}
echo "$ticker\t$price\t$changePercent\t$dayGainString\t$valueString\t$totalChangePercent\t$totalGainString\t$ps\t$pe\t$pb\n";
return array($dayGain, $value);
}
}
$marketOpen = '';
if (isMarketOpen()) {
$marketOpen = getColoredString('Market Open', 'cyan');
}
else {
$marketOpen = getColoredString('Market Closed', 'light_gray');
}
echo "\nComparison indexes (total from 10/8/13):\t\t\t$marketOpen\n";
$dia = array('DIA', 0, 0);
$spy = array('SPY', 0, 0);
$qqq = array('QQQ', 0, 0);
$sdog = array('SDOG', 0, 0);
displayRow($dia, $DIA_INITIAL_PRICE);
displayRow($spy, $SPY_INITIAL_PRICE);
displayRow($qqq, $QQQ_INITIAL_PRICE);
displayRow($sdog, $SDOG_INITIAL_PRICE);
echo "\n";
echo "Ticker\tPrice\tChange\t(Day Gain)\tValue\t\tTot %\t(Tot gain)\tP/S\tP/E\tP/B\n";
$dayGain = 0;
$total = 0;
$totalGain = 0;
foreach( $stocks as $stock ) {
$vals = displayRow($stock);
$dayGain += $vals[0];
$total += $vals[1];
$totalGain += $vals[1] - $stock[2]*$stock[1];
}
$leverage = number_format($total/($total + $CASH), 2);
$total += $CASH;
$changePercent = monify_string(number_format(($dayGain/($total-$dayGain))*100, 2)) . "%";
$dayGainString = monify_string(money_format("%!n", $dayGain), true);
// edge case to ensure columns align properly
if ($dayGain > -10 && $dayGain < 10) {
$dayGainString .= "\t";
}
$totalString = "$" . money_format("%!n", $total);
$totalChangePercent = monify_string(number_format(($totalGain/($total-$totalGain))*100, 2)) . "%";
$totalGainString = monify_string(money_format("%!n", $totalGain), true);
// cash
if ($CASH < 0) {
$cashString = "-$" . money_format("%!n", abs($CASH));
}
else {
$cashString = "$" . money_format("%!n", $CASH);
}
echo "\nCash:\t\t\t\t\t$cashString ($leverage leverage)\n";
echo "Total:\t\t$changePercent\t$dayGainString\t$totalString\t$totalChangePercent\t$totalGainString\n\n";
?>