-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
132 lines (109 loc) · 4.13 KB
/
app.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
const { IEXCloudClient } = require("node-iex-cloud")
const fetch = require("node-fetch")
const express = require('express')
const app = express()
var balance = 0
var starting_balance = 0
var start = null
var end = null
var start_string = null
var end_string = null
var portfolio_array = []
var data_array = []
var end_data_array = []
var result_array = []
var count = 0;
const iex = new IEXCloudClient(fetch, {
sandbox: false,
publishable: "pk_afeb706cc2704012b3bd7f693320751b",
version: "stable"
});
app.set('view engine', 'ejs')
app.use(express.urlencoded({extended:false}))
app.get('/', function (req, res) {
res.render('index', { data : null });
});
app.post('/start', function (req, res){
res.render('input', {})
});
app.post('/portfolio', function(req, res){
if(Number.isNaN(req.body.balance[0]) || !(req.body.balance[0] > 0)){
throw("balace invalid")
}
portfolio_array = []
data_array = []
result_array = []
end_data_array = []
start_string = req.body.start_date
end_string = req.body.end_date
start = req.body.start_date.replace(/-/g, '')
end = req.body.end_date.replace(/-/g, '')
balance = req.body.balance
starting_balance = req.body.balance
if(start >= end){
throw("End date is either before or the same date as start date")
}
res.render('portfolio', { quote: null, balance : balance, start_date : req.body.start_date, end_date : req.body.end_date, portfolio: portfolio_array })
});
app.post('/add', function(req, res){
let val = 0
try{
iex.symbol(req.body.stock_name).chart("date", { date: start , chartByDay: true }).then(function(obj){
val = obj[0].close * req.body.shares
if(balance-val < 0){
throw("cant afford stock")
}
else{
balance -= val;
portfolio_array.push(["Stock Symbol: " + req.body.stock_name + " ", " Close on date: " + obj[0].close + " ", " Shares: " + req.body.shares + " ", " Total value: " + val + " "])
data_array.push([req.body.stock_name, obj[0].close, req.body.shares])
try{
iex.symbol(req.body.stock_name).chart("date", { date: end , chartByDay: true }).then(function(tmp){
end_data_array.push([req.body.stock_name, tmp[0].close, req.body.shares])
res.render('portfolio', { quote: null, balance : balance , start_date : start_string, end_date : end_string, portfolio : portfolio_array});
});
}
catch(error){
}
}
});
}
catch(error){
console.log('caught')
}
});
app.post('/quote', function(req, res){
try{
iex.symbol(req.body.stock_quote).chart("date", { date: start , chartByDay: true }).then(function(obj){
let tmp = obj[0].close
res.render('portfolio', { quote: tmp, balance : balance , start_date : start_string, end_date : end_string, portfolio : portfolio_array});
});
}
catch(error){
console.log('caught')
}
});
app.post('/result', function(req, res){
var retval = 0
var final_value = 0
try{
for(var i = 0; i < data_array.length; i++){
var sym = data_array[i][0]
var close = data_array[i][1]
var shares = data_array[i][2]
retval = ((end_data_array[i][1]-close)/close)*100
var equity = shares*end_data_array[i][1]
final_value+=equity
result_array.push(["Symbol: " + sym + " ", " Bought at: " + close + " "," Close at: " + end_data_array[i][1] + " ", " Amount of shares bought: " + shares + " ", " Gain/Loss(%): " + retval + " " + "Equity($): " + equity + " " ])
}
balance+=final_value
var final_percentage = ((balance-starting_balance)/(starting_balance)) * 100
res.render('result', {portfolio: result_array, final_value: balance, start_value: starting_balance, percent : final_percentage})
}
catch(error){
console.log(error)
}
});
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});