-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
306 lines (253 loc) · 7.88 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
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
const express = require("express");
const AWS = require("aws-sdk");
const app = express();
const math = require("mathjs");
const { PythonShell } = require("python-shell");
const {
addUser,
getUser,
getStockDataById,
getUserFromReplica,
addUserFromReplica,
getHighAndLow,
calculateReturns,
identifySupportResistanceLevels,
generateTradingSignals,
calculateStandardDeviation,
calculateSharpeRatio,
analyzeCorrelation,
calculateAverageReturn,
} = require("./dynamo");
app.use(express.json());
AWS.config.update({
// region: process.env.AWS_DEFAULT_REGION_1,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});
const dynamoClient_1 = new AWS.DynamoDB.DocumentClient({
region: process.env.AWS_DEFAULT_REGION_1,
});
app.get("/", (req, res) => {
res.send("Home");
});
app.get("/health", (req, res) => {
res.status(200).send("OK");
});
app.get("/users-replica/:email", async (req, res) => {
const email = req.params.email;
try {
const user = await getUserFromReplica(email);
res.json(user);
} catch (err) {
console.error(err);
res.status(500).json({ err: "Something went wrong" });
}
});
app.get("/users/:email", async (req, res) => {
const email = req.params.email;
try {
const user = await getUser(email);
res.json(user);
} catch (err) {
console.error(err);
res.status(500).json({ err: "Something went wrong" });
}
});
app.get("/stock-data/:id", async (req, res) => {
const id = req.params.id;
try {
const data = await getStockDataById(id);
res.json(data);
} catch (err) {
console.error(err);
res.status(500).json({ err: "Something went wrong" });
}
});
app.post("/add-user", async (req, res) => {
const user = req.body;
try {
const newUser = await addUser(user);
res.json(newUser);
} catch (err) {
console.error(err);
res.status(500).json({ err: "Something went wrong" });
}
});
app.post("/add-user-replica", async (req, res) => {
const user = req.body;
try {
const newUser = await addUserFromReplica(user);
res.json(newUser);
} catch (err) {
console.error(err);
res.status(500).json({ err: "Something went wrong" });
}
});
app.post("/company-data", async (req, res) => {
const { companyName } = req.body;
try {
const result = await getHighAndLow(companyName);
const items = result.Items;
const closingPrices = items.map((item) => ({
timestamp: item.timestamp,
close: item.close,
}));
const allTimeHigh = Math.max(...closingPrices.map((item) => item.close));
const allTimeLow = Math.min(...closingPrices.map((item) => item.close));
res.json({
allTimeHigh,
allTimeLow,
});
} catch (error) {
console.error("Error querying DynamoDB:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/volatility-analysis", async (req, res) => {
const { companyName } = req.body;
try {
const result = await getHighAndLow(companyName);
const closingPrices = result.Items.map((item) => item.close);
console.log("Closing Prices:", closingPrices);
// Calculate daily returns
const returns = calculateReturns(closingPrices);
// Calculate volatility (standard deviation of returns)
const volatility = math.std(returns);
res.json({
volatility,
});
} catch (error) {
console.error("Error querying DynamoDB:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/support-resistance-levels", async (req, res) => {
const { companyName } = req.body;
try {
const result = await getHighAndLow(companyName);
const items = result.Items;
const supportResistanceLevels = identifySupportResistanceLevels(items);
res.json({
supportResistanceLevels,
});
} catch (error) {
console.error("Error querying DynamoDB:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/trading-signals", async (req, res) => {
const { companyName } = req.body;
try {
const result = await getHighAndLow(companyName);
const items = result.Items;
console.log("Historical Prices:", items);
const tradingSignals = generateTradingSignals(items);
res.json({
tradingSignals,
});
} catch (error) {
console.error("Error querying DynamoDB:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/company-performance-metrics", async (req, res) => {
const { companyName } = req.body;
try {
const result = await getHighAndLow(companyName);
const items = result.Items;
const closingPrices = items.map((item) => parseFloat(item.close)); // Convert to number
// Calculate daily returns
const returns = calculateReturns(closingPrices);
// Calculate average return
const averageReturn = calculateAverageReturn(returns);
// Calculate standard deviation
const standardDeviation = calculateStandardDeviation(returns);
// Assuming risk-free rate is 0 for simplicity
const riskFreeRate = 0;
// Calculate Sharpe ratio
const sharpeRatio = calculateSharpeRatio(
averageReturn,
standardDeviation,
riskFreeRate
);
res.json({
averageReturn,
standardDeviation,
sharpeRatio,
});
} catch (error) {
console.error("Error querying DynamoDB:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/correlation-analysis", async (req, res) => {
const { companyName, comparisonSymbols } = req.body;
try {
// Fetch historical data from DynamoDB for the target stock
const result = await getHighAndLow(companyName);
const targetStockData = result.Items;
// Fetch historical data for comparison symbols
const comparisonDataPromises = comparisonSymbols.map(async (symbol) => {
const comparisonResult = await getHighAndLow(symbol);
return { symbol, data: comparisonResult.Items };
});
const comparisonDataList = await Promise.all(comparisonDataPromises);
// Perform correlation analysis
const correlationResults = analyzeCorrelation(
targetStockData,
comparisonDataList
);
res.json({
correlationResults,
});
} catch (error) {
console.error("Error querying DynamoDB:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/get-prediction", async (req, res) => {
const { companyName } = req.body;
try {
var pythonScriptPath;
if (companyName == "ADANIPORTS") {
pythonScriptPath = "pyfiles/adaniports.py";
} else if (companyName == "ADANIENT") {
pythonScriptPath = "pyfiles/adanien.py";
} else if (companyName == "AARTIIND") {
pythonScriptPath = "pyfiles/aarti.py";
} else if (companyName == "ABFRL") {
pythonScriptPath = "pyfiles/abfrl.py";
} else if (companyName == "ABCAPITAL") {
pythonScriptPath = "pyfiles/abc.py";
}
// Set your input parameters
const lookback = 30;
const future = 1;
// Create a new PythonShell instance with input parameters
let pyshell = new PythonShell(pythonScriptPath, {
args: [lookback.toString(), future.toString()],
});
let prediction = ""; // Variable to store the result
// Register the message event to receive output from the Python script
pyshell.on("message", (message) => {
console.log(message);
prediction = message; // Store the result
});
// End the PythonShell instance
pyshell.end((err, code, signal) => {
if (err) throw err;
console.log("Python script finished with code " + code);
// Send the result back to the client
res.json({
prediction,
});
});
} catch (error) {
console.error("Error getting prediction:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`listening on port port`);
});