forked from pavankat/js-trading-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocks.js
315 lines (275 loc) · 6.95 KB
/
stocks.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
/// <reference types="jsstp" />
"use strict";
/** @type {typeof import("jsstp").jsstp} */
var jsstp;
$(async function () {
let isAvailableSSTP = await jsstp.available();
const sendTrade = (amount, name) => {
if (isAvailableSSTP) {
jsstp.NOTIFY({
'Option':'nobreak',
'Event':'OnGetMoney',
'Reference0':'StockTrading/0.1',
'Reference1':String(Math.round(amount)),
'Reference2':name,
});
}
};
//stock data
const companies = [
{
name: 'Apple'
, symbol: 'AAPL'
, price: '577'
, shares: '0'
},
{
name: 'Google'
, symbol: 'GOOG'
, price: '650'
, shares: '0'
},
{
name: 'JP Morgan Chase'
, symbol: 'JPM'
, price: '33'
, shares: '0'
},
{
name: 'Microsoft'
, symbol: 'MSFT'
, price: '35'
, shares: '0'
},
{
name: 'Facebook'
, symbol: 'FB'
, price: '27'
, shares: '0'
}
];
//your cash flow
var cashflow = 1000;
/*
- your portfolio value
- calculates based on
current price of stocks
and shares owned
*/
var portfolioValue = function () {
var total = 0;
for (var key in companies) {
var obj = companies[key];
var sharesOwned = 0;
var priceOfThisStock = 0;
for (var prop in obj) {
switch (prop) {
case 'price':
//typecast string to float
priceOfThisStock = parseFloat(obj[prop]);
break;
case 'shares':
//typecast string to float
var sharesOwned = parseFloat(obj[prop]);
total += priceOfThisStock * sharesOwned;
break;
}
}
}
return total;
}
var changePrice = function (price) {
//generate random number -1 or 1
const chance = Math.round(Math.random()) * 2 - 1;
//adds chance/10 to price
price += chance / 10;
return price;
}
//build the table
for (var key in companies) {
var obj = companies[key];
var symbol = '';
var html = "<tr>";
for (var prop in obj) {
/*
- grab the symbol value
- to use to make unique ids for tds
*/
if (prop === 'symbol')
symbol = obj[prop];
//this will build the table dimensions
if (prop !== 'price') {
html += "<td id='" + prop + symbol + "'>";
html += obj[prop];
html += "</td>";
} else {
html += "<td id='" + prop + symbol + "'>";
html += "<span class='price'>" + obj[prop] + "</span>";
html += "</td>";
}
/*
- if the td is price then add
the tds for Shares You own,
Buy and Sell buttons
to the table row
- manipulate the ids to be unique
*/
if (prop === 'shares')
html += "<td> <a href='#' class='buy' id='" + symbol + "Buy" + "'>buy 1 share</a> </td> <td> <a href='#' class='sell' id='" + symbol + "Sell" + "'>sell 1 share</a> </td>";
}
html += "</tr>";
$("#myPortfolio tbody").append(html);
}
//every second do this
setInterval(function () {
/*
- iterate through the stock data
- change the price to a float
- run the price through changePrice()
- replace the new price into the stock data
- replace the new price of the stock into the table
*/
for (var key in companies) {
var obj = companies[key];
var symbol = '';
for (var prop in obj) {
switch (prop) {
case 'symbol':
symbol = obj[prop];
break;
case 'price':
var priceOfThisStock = parseFloat(obj[prop]);
obj[prop] = changePrice(priceOfThisStock);
var roundedPrice = (obj[prop]).toFixed(2);
$('#' + prop + symbol).html("<span class='price'>" + roundedPrice + "</span>");
break;
}
}
}
}, 1000 /* every second */);
//every second do this
setInterval(function () {
/*
- run cash flow, portfolio value, total worth functions
and then put them in the dom
- decide whether buy/sell buttons should appear
and then put them or remove them from the dom
*/
for (var key in companies) {
var obj = companies[key];
//initialize
var symbol = '';
var price = 0;
var p = 0;
var t = 0;
for (var prop in obj) {
if (prop === 'symbol')
symbol = obj[prop];
p = portfolioValue();
t = cashflow + p;
$('#cashflow').html(cashflow);
$('#portfolio').html(p);
$('#netWorth').html(t);
if (cashflow === 0) $('a').each(function () {
var id = String($(this).attr('id'));
if (id.indexOf('Buy') > 0)
$(this).hide();
});
switch (prop) {
case 'price':
price = parseFloat(obj[prop]);
$('#' + symbol + 'Buy').toggle(cashflow >= price);
break;
case 'shares':
var sharesOwned = parseFloat(obj[prop]);
$('#' + symbol + 'Sell').toggle(sharesOwned > 0);
break;
}
}
}
}, 1000 /* every 1000 mili seconds */);
//happens live
$(document).on('click', "a", function () {
//grab the id of this link and typecast it to a string
var id = String($(this).attr('id'));
//if this is a buy button
if (id.indexOf('Buy') > 0) {
//extract the symbol of the stock this is for
symbol = id.substr(0, id.indexOf('Buy'));
//only do if cash flow is greater than share price
//add a share
//subtract share amount from cashflow
//initialize
var thisObj = 0; //specific object in companies
var subtractPrice = false;
for (var key in companies) {
var obj = companies[key];
for (var prop in obj) {
switch (prop) {
case 'symbol':
if (obj[prop] === symbol)
thisObj = key;
break;
case 'price':
if (key === thisObj) {
var PriceForThisStock = parseFloat(obj[prop]);
if (cashflow > PriceForThisStock) {
subtractPrice = true;
cashflow = cashflow - PriceForThisStock;
sendTrade(-1*PriceForThisStock, obj.name);
}
}
break;
case 'shares':
if (key === thisObj && subtractPrice) {
var sharesForThisStock = parseFloat(obj[prop]);
sharesForThisStock += 1;
obj[prop] = sharesForThisStock;
$('#' + 'shares' + symbol).html(sharesForThisStock);
}
break;
}
}
}
}
//if this is a sell button
if (id.indexOf('Sell') > 0) {
//extract the symbol of the stock this is for
symbol = id.substr(0, id.indexOf('Sell'));
//only do if share of this stock is owned
//subtract a share
//add share amount to cash flow
//initialize
var thisObj = 0; //specific object in companies
for (var key in companies) {
var obj = companies[key];
for (var prop in obj) {
switch (prop) {
case 'symbol':
if (obj[prop] === symbol)
thisObj = key;
break;
case 'price':
if (key === thisObj)
PriceForThisStock = parseFloat(obj[prop]);
break;
case 'shares':
if (key === thisObj) {
sharesForThisStock = parseFloat(obj[prop]);
if (sharesForThisStock) {
cashflow += PriceForThisStock;
sharesForThisStock -= 1;
obj[prop] = sharesForThisStock;
$('#' + 'shares' + symbol).html(sharesForThisStock);
sendTrade(PriceForThisStock, obj.name);
}
}
break;
}
}
}
}
//act like a button not a link
return false;
});
});