-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
1108 lines (924 loc) · 39 KB
/
Copy pathindex.js
File metadata and controls
1108 lines (924 loc) · 39 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(async () => {
"use strict";
// Dependencies
const client = await require("./modules/mongodb.js")
const cookieParser = require("cookie-parser")
const compression = require("compression")
const sAES256 = require("simple-aes-256")
const { ObjectId } = require("mongodb")
const { parse } = require("smol-toml")
const { filterXSS } = require("xss")
const express = require("express")
const hashJS = require("hash.js")
const helmet = require("helmet")
const cryptr = require("cryptr")
const axios = require("axios")
const path = require("path")
const fs = require("fs")
// Variables
const config = parse(fs.readFileSync("./config.toml", "utf8"))
const cT = new cryptr(config.security.cookieMasterKey, { encoding: config.security.cookieEncoding, pbkdf2Iterations: config.security.cookiePBKDF2Iterations, saltLength: config.security.cookieSaltLength })
const web = express()
const port = config.web.port
const database = client.db(config.database.databaseName)
const users = database.collection(config.database.usersCollection)
const trades = database.collection(config.database.tradesCollection)
// Functions
const SHA512 = (string) => { return hashJS.sha512().update(string).digest("hex") }
const setCookie = (res, data) => {
res.cookie("d", data, {
maxAge: 12 * 60 * 60 * 1000, // 12 hours
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "strict"
})
}
const dS = async (session) => {
try {
const sessionData = JSON.parse(cT.decrypt(session.d))
return sessionData
} catch { return false }
}
const sAES256E = (password, string) => {
return sAES256.encrypt(password, string).toString("hex")
}
const sAES256D = (password, string) => {
return sAES256.decrypt(password, Buffer.from(string, "hex")).toString("utf8")
}
const promptAI = async (type, rules, strategies, data, recentTrades) => {
// Variables
var systemPrompt = ""
// IDK lmfao
if (type === "trade") {
systemPrompt = "You are an expert Trading Analyst. Provide a summary of this trade from the data given, and offer advice for improvement. Use three headings: Introduction, Mistakes, and Improvements. Address the user as 'you'.";
} else if (type === "trades") {
systemPrompt = "You are a Performance Analyst. Summarize the user's trading performance based on the dashboard data provided. Mention specific statistics and progress. Keep it in a single paragraph. Address the user as 'you'.";
} else if (type === "chat") {
systemPrompt = "You are the TradeGodly AI Trading Mentor. Guide the user based on their stated rules and strategies. Be concise, professional, and authentic. Address the user as 'you'.";
} else {
systemPrompt = type
}
// Core
const rulesText = (rules && rules.length) ? rules.join("\n") : "None provided yet."
const strategiesText = (strategies && strategies.length) ? strategies.map(s => `${s.name}: ${s.description || "No description"}${s.howItWorks ? `\nHow it works: ${s.howItWorks}` : ""}`).join("\n---\n") : "None provided yet."
const recentTradesText = (recentTrades && recentTrades.length) ? recentTrades.map(t => `${t.date}: ${t.asset} (${t.type}) -> ${t.status}`).join("\n") : "None provided yet."
const promptPayload = `System Context: ${systemPrompt}
User Trading Rules:
${rulesText}
User Strategies:
${strategiesText}
Recent Trades (History):
${recentTradesText}
Input Data/Message: ${data}`
try {
const response = await axios.post("https://public-api.firstdecree.org/api/v1.0/ai/chatgptlite",
{ data: promptPayload },
{
headers: { "x-key": "All Hail Decree!" },
timeout: 25000
}
)
if (response.data) return response.data
return "I'm sorry, I couldn't generate a response at this time."
} catch {
return "I'm sorry, I'm having trouble connecting to my AI core. Please try again in a moment."
}
}
// Configurations
//* Express
web.use(compression({ level: 1 }))
web.use(helmet({ contentSecurityPolicy: false }))
web.use(cookieParser())
web.use(express.json({ limit: "50mb" }))
web.set("views", path.join(__dirname, "views"))
web.set("view engine", "ejs")
// Main
//* API
web.post("/api/login", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! Username (POST BODY)
* ! - If empty, invalidated.
* ! - If contains special character, invalidated.
*
* ! Password (POST BODY)
* ! - If empty, invalidated.
*
* ! If account does not exist, invalidated.
*/
// Variables
const { username, password } = req.body
// Validations
if (!username || !password) return res.send("0")
if(/[^\w\s]/.test(username)) return res.send("0")
const accountData = await users.findOne({
hashedUsername: SHA512(username),
password: SHA512(password)
})
if (!accountData) return res.send("0")
// Core
setCookie(res, cT.encrypt(JSON.stringify({
username: sAES256D(password, accountData.username),
password: password,
plan: accountData.plan,
planExpiration: accountData.planExpiration,
account: "all" // Default is all.
})))
res.send("1")
})
web.post("/api/new-rule", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! Rule (POST BODY)
* ! - If empty, invalidated.
* ! - If more than 125 characters, invalidated.
* ! - If account has more than 40 rules, reject.
* ! - If contains special characters except ",", -, _, ' and . then invalidated.
*/
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
var { rule } = req.body
if (!rule || rule.length > 125) return res.send("0")
if(/[^\w\s\-,.'"']/.test(rule)) return res.send("0")
rule = filterXSS(rule)
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
if (!accountData) return res.send("0")
if (accountData.rules && accountData.rules.length >= 40) return res.send("max")
await users.updateOne({ hashedUsername: SHA512(userData.username) }, {
$push: { rules: rule }
})
res.send("1")
})
web.post("/api/remove-rule", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! Rule (POST BODY)
* ! - If empty, invalidated.
* ! - If more than 125 characters, invalidated.
* ! - If contains special characters except ",", -, _, ' and . then invalidated.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
const { rule } = req.body
// Validations
if (!rule) return res.send("0")
if (!rule || rule.length > 125) return res.send("0")
if(/[^\w\s\-,.'"']/.test(rule)) return res.send("0")
// Core
await users.updateOne({ hashedUsername: SHA512(userData.username) }, {
$pull: { rules: rule }
})
res.send("1")
})
web.post("/api/new-strategy", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! ALL (POST BODY)
* ! - If empty, invalidated.
* ! - All are sanitized to avoid XSS exploitation.
*
* ! Name (POST BODY)
* ! - If more than 25 characters, invalidated.
*
* ! Description (POST BODY)
* ! - If more than 300 characters, invalidated.
*
* ! howItWorks (POST BODY)
* ! - If more than 2500 characters, invalidated.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
var { name, description, howItWorks } = req.body
// Validations
if (!name || name.length > 25) return res.send("0")
if (!description || description.length > 300) return res.send("0")
if (!howItWorks || howItWorks.length > 2500) return res.send("0")
name = filterXSS(name)
description = filterXSS(description)
howItWorks = filterXSS(howItWorks)
// Core
await users.updateOne({ hashedUsername: SHA512(userData.username) }, {
$push: {
strategies: {
id: new ObjectId().toString(),
name,
description,
howItWorks,
createdAt: new Date().toISOString()
}
}
})
res.send("1")
})
web.post("/api/remove-strategy", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! ID (POST BODY)
* ! - If empty, invalidated.
* ! - If more than 100 characters, invalidated.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
const { id } = req.body
// Validations
if (!id) return res.send("0")
if(id.length > 100) return res.send("0")
// Core
await users.updateOne({ hashedUsername: SHA512(userData.username) }, {
$pull: {
strategies: { id: id }
}
})
res.send("1")
})
web.post("/api/new-account", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! Name (POST BODY)
* ! - If empty, invalidated.
* ! - If more than 30 characters, invalidated.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
var { name, deposit } = req.body
// Validations
if (!name) return res.send("0")
if(name.length > 30) return res.send("0")
name = filterXSS(name)
// Core
await users.updateOne({ hashedUsername: SHA512(userData.username) }, {
$push: {
accounts: {
name: name,
deposit: parseFloat(deposit) || 0,
createdAt: new Date().toISOString()
}
}
})
res.send("1")
})
web.post("/api/remove-account", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! Name (POST BODY)
* ! - If empty, invalidated.
* ! - If more than 30 characters, invalidated.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
const { name } = req.body
// Validations
if (!name) return res.send("0")
if(name.length > 30) return res.send("0")
// Core
await users.updateOne({ hashedUsername: SHA512(userData.username) }, {
$pull: {
accounts: name
}
})
await users.updateOne({ hashedUsername: SHA512(userData.username) }, {
$pull: {
accounts: { name: name }
}
})
await trades.deleteMany({
owner: SHA512(userData.username),
account: name
})
res.send("1")
})
web.post("/api/new-trade", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! ALL (POST BODY)
* ! - If empty, invalidated.
* ! - All strings are sanitized to avoid XSS exploitation.
* ! - Numerical values are parsed to ensure validity.
*
* ! Asset (POST BODY)
* ! - If more than 20 characters, invalidated.
* ! - Allowed characters: Alphanumeric, spaces, -, /, .
*
* ! Type/Status/Session/Timeframe (POST BODY)
* ! - Validated against allowed enum values.
*
* ! Limits
* ! - Maximum 5000 trades per user to prevent database bloat/abuse.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
if (userData.account === "all") return res.send("0")
// Variables
var { date, exitDate, type, asset, session, status, qty, entry, exit, tp, sl, ret, entryTime, exitTime, timeframe, timezone } = req.body
// Validations
if (!date || !type || !asset || !status || qty === undefined || entry === undefined || exit === undefined) return res.send("0")
if (!["LONG", "SHORT"].includes(type)) return res.send("0")
if (!["WIN", "LOSE", "BREAKEVEN"].includes(status)) return res.send("0")
if (!["Sydney", "Tokyo", "London", "New York"].includes(session)) session = "London"
if (!["1m", "3m", "5m", "15m", "30m", "1h", "4h", "1d"].includes(timeframe)) timeframe = "1h"
if (asset.length > 20 || /[^\w\s\-\/\.]/.test(asset)) return res.send("0")
if (timezone && timezone.length > 30) return res.send("0")
if (entryTime && entryTime.length > 20) return res.send("0")
if (exitTime && exitTime.length > 20) return res.send("0")
// Sanitization
asset = filterXSS(asset)
timezone = filterXSS(timezone || "UTC")
entryTime = filterXSS(entryTime || "00:00:00")
exitTime = filterXSS(exitTime || "00:00:00")
// Numbers
qty = parseFloat(qty) || 0
entry = parseFloat(entry) || 0
exit = parseFloat(exit) || 0
tp = parseFloat(tp) || 0
sl = parseFloat(sl) || 0
ret = parseFloat(ret) || 0
// Abuse Prevention
const tradeCount = await trades.countDocuments({ owner: SHA512(userData.username) })
if (tradeCount >= 5000) return res.send("max")
// Core
await trades.insertOne({
owner: SHA512(userData.username),
account: userData.account,
date,
exitDate,
type,
asset,
session,
status,
qty,
entry,
exit,
tp,
sl,
ret,
entryTime,
exitTime,
timeframe,
timezone,
note: {
content: "",
screenshots: []
}
})
res.send("1")
})
web.post("/api/remove-trade", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! ID (POST BODY)
* ! - If empty, invalidated.
* ! - If more than 100 characters, invalidated.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
const { id } = req.body
// Validations
if (!id) return res.send("0")
if(id.length > 100) return res.send("0")
// Core
try {
await trades.deleteOne({
_id: new ObjectId(id),
owner: SHA512(userData.username)
})
res.send("1")
} catch{
res.send("0")
}
})
web.post("/api/set-account", async (req, res) => {
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
const { account } = req.body
// VAlidation
if (!account) return res.send("0")
// Core
if (account !== "all") {
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
const allAccountNames = (accountData && accountData.accounts) ? accountData.accounts.map(a => typeof a === "object" ? a.name : a) : []
if (!allAccountNames.includes(account)) return res.send("0")
}
setCookie(res, cT.encrypt(JSON.stringify({
username: userData.username,
password: userData.password,
plan: userData.plan,
planExpiration: userData.planExpiration,
account: account === "all" ? "all" : account.charAt(0).toUpperCase() + account.slice(1)
})))
res.send("1")
})
web.post("/api/update-trade", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! ALL (POST BODY)
* ! - If empty, invalidated.
* ! - All strings are sanitized to avoid XSS exploitation.
* ! - Numerical values are parsed to ensure validity.
*
* ! Asset (POST BODY)
* ! - If more than 20 characters, invalidated.
* ! - Allowed characters: Alphanumeric, spaces, -, /, .
*
* ! Type/Status/Session/Timeframe (POST BODY)
* ! - Validated against allowed enum values.
*
* ! Limits
* ! - Maximum 5000 trades per user to prevent database bloat/abuse.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
var { id, date, exitDate, type, asset, session, status, qty, entry, exit, tp, sl, ret, entryTime, exitTime, timeframe, timezone } = req.body
// Validations
if (!id || !date || !type || !asset || !status || qty === undefined || entry === undefined || exit === undefined) return res.send("0")
if (!["LONG", "SHORT"].includes(type)) return res.send("0")
if (!["WIN", "LOSE", "BREAKEVEN"].includes(status)) return res.send("0")
if (!["Sydney", "Tokyo", "London", "New York"].includes(session)) session = "London"
if (!["1m", "3m", "5m", "15m", "30m", "1h", "4h", "1d"].includes(timeframe)) timeframe = "1h"
if (asset.length > 20 || /[^\w\s\-\/\.]/.test(asset)) return res.send("0")
if (timezone && timezone.length > 30) return res.send("0")
if (entryTime && entryTime.length > 20) return res.send("0")
if (exitTime && exitTime.length > 20) return res.send("0")
// Sanitization
asset = filterXSS(asset)
timezone = filterXSS(timezone || "UTC")
entryTime = filterXSS(entryTime || "00:00:00")
exitTime = filterXSS(exitTime || "00:00:00")
// Numbers
qty = parseFloat(qty) || 0
entry = parseFloat(entry) || 0
exit = parseFloat(exit) || 0
tp = parseFloat(tp) || 0
sl = parseFloat(sl) || 0
ret = parseFloat(ret) || 0
// Core
try {
await trades.updateOne(
{ _id: new ObjectId(id), owner: SHA512(userData.username) },
{
$set: {
date, exitDate, type, asset, session, status, qty, entry, exit, tp, sl, ret,
entryTime, exitTime, timeframe, timezone
}
}
)
res.send("1")
} catch {
res.send("0")
}
})
web.post("/api/update-trade-note", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! ID (POST BODY)
* ! - If empty, invalidated.
*
* ! Content (POST BODY)
* ! - If empty, invalidated.
* ! - Sanitized to ensure anti-XSS exploitation.
*/
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
var { id, content } = req.body
// Validations
if (!id || !content) return res.send("0")
content = filterXSS(content)
// Core
try {
await trades.updateOne(
{ _id: new ObjectId(id), owner: SHA512(userData.username) },
{ $set: { "note.content": content } }
)
res.send("1")
} catch{
res.send("0")
}
})
web.post("/api/add-screenshot", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! ALL (POST BODY)
* ! - If empty, invalidated.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
const { id, img } = req.body
// Validations
if (!id || !img) return res.send("0")
// Core
try {
await trades.updateOne(
{ _id: new ObjectId(id), owner: SHA512(userData.username) },
{ $push: { "note.screenshots": img } }
)
res.send("1")
} catch {
res.send("0")
}
})
web.post("/api/delete-screenshot", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! ALL (POST BODY)
* ! - If empty, invalidated.
*/
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
// Variables
const { id, index } = req.body
// Validations
if (!id || index === undefined) return res.send("0")
// Core
try {
const tradeData = await trades.findOne({ _id: new ObjectId(id), owner: SHA512(userData.username) })
if (!tradeData || !tradeData.note || !tradeData.note.screenshots) return res.send("0")
const screenshots = tradeData.note.screenshots
screenshots.splice(index, 1)
await trades.updateOne(
{ _id: new ObjectId(id), owner: SHA512(userData.username) },
{ $set: { "note.screenshots": screenshots } }
)
res.send("1")
} catch {
res.send("0")
}
})
//* Handler
web.get("/logout", (req, res) => { res.clearCookie("d").redirect("/login") })
web.get("/delete-account", async (req, res) => {
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.redirect("/login")
// Core
await trades.deleteMany({ owner: SHA512(userData.username) })
await users.deleteOne({ hashedUsername: SHA512(userData.username) })
res.clearCookie("d").redirect("/")
})
web.get("/login", async (req, res, next) => {
const userData = await dS(req.cookies)
if (userData) return res.redirect("/dashboard")
next()
})
web.use(express.static(path.join(__dirname, "public"), { extensions: ["html"] }))
//* EJS
web.get("/dashboard", async (req, res) => {
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.redirect("/login")
// Variables
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
userData.accountsList = accountData && accountData.accounts ? accountData.accounts.map(a => typeof a === "object" ? a.name : a) : []
const query = { owner: SHA512(userData.username) }
userData.account = (userData.account || "all").trim()
if (userData.account !== "all") query.account = userData.account
const allTrades = await trades.find(query).toArray()
allTrades.sort((a, b) => new Date(a.date) - new Date(b.date))
var totalWins = 0, totalLosses = 0, totalLongs = 0, totalShorts = 0
var sumWins = 0, sumLosses = 0
const dailyPnL = {}
// Core
allTrades.forEach(trade => {
var ret = parseFloat(trade.ret) || 0
if (trade.status === "WIN") { totalWins++; sumWins += Math.abs(ret); ret = Math.abs(ret) }
if (trade.status === "LOSE") { totalLosses++; sumLosses += Math.abs(ret); ret = -Math.abs(ret) }
if (trade.type === "LONG") totalLongs++
if (trade.type === "SHORT") totalShorts++
const dStr = new Date(trade.date).toLocaleDateString('en-US', { month: 'short', day: 'numeric', timeZone: 'UTC' })
if (!dailyPnL[dStr]) dailyPnL[dStr] = 0
dailyPnL[dStr] += ret
})
const pnlLabels = []
const pnlData = []
var cumulativePnL = 0
for (const date in dailyPnL) {
cumulativePnL += dailyPnL[date]
pnlLabels.push(date)
pnlData.push(cumulativePnL)
}
if (pnlLabels.length === 0) {
pnlLabels.push(new Date().toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" }))
pnlData.push(0)
}
userData.stats = {
wins: totalWins,
losses: totalLosses,
longs: totalLongs,
shorts: totalShorts,
avgWin: totalWins > 0 ? (sumWins / totalWins) : 0,
avgLoss: totalLosses > 0 ? (sumLosses / totalLosses) : 0,
pnl: cumulativePnL
}
userData.chart = {
labels: JSON.stringify(pnlLabels),
data: JSON.stringify(pnlData)
}
userData.recentTrades = allTrades.reverse().slice(0, 50)
const recentTData = userData.recentTrades.map(t => ({
date: t.date,
asset: t.asset,
status: t.status,
type: t.type,
ret: t.ret
}))
userData.ai = !userData.recentTrades.length ? "No trades data yet to analyze." : await promptAI("trades", accountData.rules || [], accountData.strategies || [], JSON.stringify({
stats: userData.stats,
recentTrades: recentTData
}), recentTData)
res.render("dashboard", userData)
})
web.get("/trades", async (req, res) => {
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.redirect("/login")
// Variables
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
userData.accountsList = accountData && accountData.accounts ? accountData.accounts.map(a => typeof a === "object" ? a.name : a) : []
const page = parseInt(req.query.page) || 1
const limit = 10
const skip = (page - 1) * limit
// Core
const query = { owner: SHA512(userData.username) }
userData.account = (userData.account || "all").trim()
if (userData.account !== "all") query.account = userData.account
userData.tradesList = await trades.find(query).sort({ _id: -1 }).skip(skip).limit(limit).toArray()
const totalTrades = await trades.countDocuments(query)
userData.currentPage = page
userData.totalPages = Math.ceil(totalTrades / limit) || 1
userData.totalTrades = totalTrades
res.render("trades", userData)
})
web.get("/calendar", async (req, res) => {
// High Validation
const userData = await dS(req.cookies)
if (!userData) return res.redirect("/login")
// Variables
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
userData.accountsList = accountData && accountData.accounts ? accountData.accounts.map(a => typeof a === "object" ? a.name : a) : []
const query = { owner: SHA512(userData.username) }
userData.account = (userData.account || "all").trim()
if (userData.account !== "all") query.account = userData.account
// Core
userData.allTrades = await trades.find(query).toArray()
userData.hideAccountSelect = true
res.render("calendar", userData)
})
web.get("/seasonality-charts", async (req, res) => {
// Variables
const userData = await dS(req.cookies)
if (!userData) return res.redirect("/login")
// Core
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
userData.accountsList = accountData && accountData.accounts ? accountData.accounts.map(a => typeof a === "object" ? a.name : a) : []
const query = { owner: SHA512(userData.username) }
userData.account = (userData.account || "all").trim()
if (userData.account !== "all") query.account = userData.account
userData.allTrades = await trades.find(query).toArray()
userData.hideAccountSelect = true
res.render("seasonality-charts", userData)
})
web.get("/api/chart-data/:symbol", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! Symbol (PARAM)
* ! - If empty, invalidated.
* ! - If more than 30 characters, invalidated.
*
* ! - Internal (QUERY)
* ! - If more than 30 characters, invalidated.
*/
// Variables
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
var { symbol } = req.params
var { interval } = req.query
// Validations
if(!symbol) return res.send("0")
if (!interval) interval = "1h"
if(symbol.length > 30) return res.send("0")
if(interval.length > 30) return res.send("0")
// Core
if (symbol === "XAUUSD") symbol = "GC=F"
else if (symbol.length === 6) symbol = `${symbol}=X`
try {
const response = await fetch(`https://query1.finance.yahoo.com/v8/finance/chart/${symbol}?interval=${interval}&range=7d`)
const data = await response.json()
if (data && data.chart && data.chart.result) {
const result = data.chart.result[0]
const timestamps = result.timestamp
const quotes = result.indicators.quote[0]
if (timestamps) {
const formatted = timestamps.map((t, i) => ({
time: t,
open: quotes.open[i],
high: quotes.high[i],
low: quotes.low[i],
close: quotes.close[i]
})).filter(p => p.open != null)
return res.json(formatted)
}
}
const cryptoSymbol = req.params.symbol.endsWith("USD") ? req.params.symbol + "T" : req.params.symbol
const binanceInterval = interval === "1d" ? "1d" : (interval.endsWith("h") ? interval : (interval.endsWith("m") ? interval : "1h"))
const binanceRes = await fetch(`https://api.binance.com/api/v3/klines?symbol=${cryptoSymbol}&interval=${binanceInterval}&limit=500`)
const bData = await binanceRes.json()
if (Array.isArray(bData)) {
const formatted = bData.map(d => ({
time: Math.floor(d[0] / 1000),
open: parseFloat(d[1]),
high: parseFloat(d[2]),
low: parseFloat(d[3]),
close: parseFloat(d[4])
}))
return res.json(formatted)
}
res.send("0")
} catch{
res.send("0")
}
})
web.get("/settings", async (req, res) => {
// Variables
const userData = await dS(req.cookies)
if (!userData) return res.redirect("/login")
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
userData.accountsList = accountData && accountData.accounts ? accountData.accounts.map(a => typeof a === "object" ? a.name : a) : []
userData.rules = accountData && accountData.rules ? accountData.rules : []
// Core
userData.accountsData = []
if (accountData && accountData.accounts) {
const allTrades = await trades.find({ owner: SHA512(userData.username) }).toArray()
userData.accountsData = accountData.accounts.map(acc => {
// Variables
const isObj = typeof acc === "object"
const name = isObj ? acc.name : acc
const deposit = isObj ? acc.deposit : 0
var crDate = isObj ? new Date(acc.createdAt) : new Date()
if (isNaN(crDate)) crDate = new Date()
const accTrades = allTrades.filter(t => t.account === name)
var wins = 0, sumWin = 0, losses = 0, sumLoss = 0, pnl = 0
// Core
accTrades.forEach((t) => {
var ret = parseFloat(t.ret) || 0
if (t.status === "WIN") { wins++; sumWin += Math.abs(ret); pnl += Math.abs(ret) }
else if (t.status === "LOSE") { losses++; sumLoss += Math.abs(ret); pnl -= Math.abs(ret) }
})
const total = wins + losses
const winrate = total > 0 ? ((wins / total) * 100).toFixed(0) : 0
const avgWin = wins > 0 ? (sumWin / wins).toFixed(2) : 0
const avgLoss = losses > 0 ? (sumLoss / losses).toFixed(2) : 0
const blown = deposit > 0 && (deposit + pnl <= 0)
return { name, deposit, createdAt: typeof acc === "object" ? crDate.toLocaleDateString() : 'N/A', winrate, avgWin, avgLoss, pnl, blown }
})
}
userData.hideAccountSelect = true
res.render("settings", userData)
})
web.get("/strategies", async (req, res) => {
// Variables
const userData = await dS(req.cookies)
if (!userData) return res.redirect("/login")
// Core
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
userData.accountsList = accountData && accountData.accounts ? accountData.accounts.map(a => typeof a === "object" ? a.name : a) : []
userData.strategies = accountData && accountData.strategies ? accountData.strategies : []
userData.hideAccountSelect = true
res.render("strategies", userData)
})
web.get("/strategy-builder", async (req, res) => {
// Variables
const userData = await dS(req.cookies)
if (!userData) return res.redirect("/login")
// Core
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
userData.accountsList = accountData && accountData.accounts ? accountData.accounts.map(a => typeof a === "object" ? a.name : a) : []
userData.hideAccountSelect = true
res.render("strategy-builder", userData)
})
web.post("/api/ai-strategy-builder", async (req, res) => {
/**
* ! AUDITED (2026-03-30)
* ! Prompt (POST BODY)
* ! - If prompt is empty, invalidated.
* ! - If prompt is more than 1500 characters, invalidated.
*/
// Variables
const userData = await dS(req.cookies)
if (!userData) return res.send("0")
var { prompt } = req.body
// Variables
if (!prompt) return res.send("0")
if(prompt.length > 1500) return res.send("0")
prompt = filterXSS(prompt)
// Core
const accountData = await users.findOne({ hashedUsername: SHA512(userData.username) })
const systemPrompt = `Prompt rule:
You are an expert quantitative trading strategist. The user will give you a description or an idea for a trading strategy.
You must output a highly detailed, professional strategy broken into EXACTLY three parts, separated by the delimiter "|||".