-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·137 lines (113 loc) · 3.85 KB
/
Copy pathindex.js
File metadata and controls
executable file
·137 lines (113 loc) · 3.85 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
'use strict';
//////////////////////////////
// Requires
//////////////////////////////
const express = require('express');
const path = require('path');
const fs = require('fs');
const dotenv = require('dotenv');
const appEnv = require('./lib/env');
const renderer = require('./lib/render');
const dataProvider = require('./lib/dataProvider');
const turtleStrategy = require('./lib/turtleStrategy');
if (fs.existsSync('.env')) {
dotenv.config();
}
const app = express();
const symbolPattern = /^[A-Z0-9.\-^=]{1,20}$/i;
const debugMarketData = process.env.DEBUG_MARKET_DATA === 'true';
const summarizeBars = (bars) => ({
count: bars.length,
firstDate: bars.length ? bars[0].date : null,
lastDate: bars.length ? bars[bars.length - 1].date : null,
});
const debugLog = (message, details) => {
if (!debugMarketData) {
return;
}
console.log(`[market-data] ${message}`, details); // eslint-disable-line no-console
};
const parsePositiveNumber = (value, fallback) => {
const parsed = Number(value);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
};
const isIsoDate = (value) => {
if (!/^\d{4}-\d{2}-\d{2}$/.test(value || '')) {
return false;
}
return !Number.isNaN(Date.parse(`${value}T00:00:00Z`));
};
app.engine('html', renderer);
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json({ limit: '100kb' }));
app.get('/', (req, res) => {
res.render('index');
});
app.get('/api/symbols', async (req, res) => {
try {
const query = String(req.query.q || '').trim();
const symbols = await dataProvider.searchSymbols(query);
res.json({ symbols });
} catch (err) {
res.status(502).json({ error: err.message });
}
});
app.post('/api/simulate', async (req, res) => {
try {
const body = req.body || {};
const {
symbol: rawSymbol,
from,
to,
} = body;
const symbol = String(rawSymbol || '').trim().toUpperCase();
const initialCapital = parsePositiveNumber(body.initialCapital, Number(process.env.STARTING_CAPITAL) || 100000);
const riskPercent = parsePositiveNumber(body.riskPercent, Number(process.env.RISK_PERCENT) || 1);
if (!symbol || !from || !to) {
return res.status(400).json({ error: 'symbol, from, and to are required' });
}
if (!symbolPattern.test(symbol)) {
return res.status(400).json({ error: 'symbol contains unsupported characters' });
}
if (!isIsoDate(from) || !isIsoDate(to) || from > to) {
return res.status(400).json({ error: 'from and to must be valid YYYY-MM-DD dates, with from before to' });
}
if (initialCapital < 1000 || initialCapital > 1000000000) {
return res.status(400).json({ error: 'initialCapital must be between 1,000 and 1,000,000,000' });
}
if (riskPercent <= 0 || riskPercent > 10) {
return res.status(400).json({ error: 'riskPercent must be greater than 0 and no more than 10' });
}
const prices = await dataProvider.getHistoricalPrices({
symbol,
from,
to,
interval: '1d',
});
const filtered = prices.filter((bar) => bar.date >= from && bar.date <= to);
debugLog('history loaded', {
symbol,
requested: { from, to },
allBars: summarizeBars(prices),
filteredBars: summarizeBars(filtered),
});
if (filtered.length < 40) {
return res.status(400).json({ error: 'Not enough historical data for the requested date range' });
}
const result = turtleStrategy.simulateTurtleTrading({
prices: filtered,
initialCapital,
riskPercent,
});
res.json({ symbol, from, to, result });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
//////////////////////////////
// Start the server
//////////////////////////////
app.listen(appEnv.port, () => {
console.log(`Server starting on ${appEnv.url}`); // eslint-disable-line no-console
});