-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-client.js
More file actions
203 lines (184 loc) · 5.68 KB
/
api-client.js
File metadata and controls
203 lines (184 loc) · 5.68 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
/**
* SmartTradeAI API Client
* Communicates with the Python backend API
*/
class SmartTradeAIClient {
constructor(baseUrl = 'http://127.0.0.1:5000') {
this.baseUrl = baseUrl;
this.isConnected = false;
}
/**
* Check if backend API is available
*/
async checkConnection() {
try {
const response = await this.makeRequest('GET', '/health');
this.isConnected = response.models_loaded;
return this.isConnected;
} catch (error) {
console.error('API connection failed:', error);
this.isConnected = false;
return false;
}
}
/**
* Analyze current market with all strategies
*/
async analyzeMarket(symbol = 'BTCUSDT', interval = '1h', limit = 100) {
try {
return await this.makeRequest('POST', '/api/analyze', {
symbol,
interval,
limit
});
} catch (error) {
throw new Error(`Market analysis failed: ${error.message}`);
}
}
/**
* Predict next 5 minutes
*/
async predict5Min(symbol = 'BTCUSDT') {
try {
return await this.makeRequest('POST', '/api/predict/5min', { symbol });
} catch (error) {
throw new Error(`5-minute prediction failed: ${error.message}`);
}
}
/**
* Predict next 15 minutes
*/
async predict15Min(symbol = 'BTCUSDT') {
try {
return await this.makeRequest('POST', '/api/predict/15min', { symbol });
} catch (error) {
throw new Error(`15-minute prediction failed: ${error.message}`);
}
}
/**
* Predict next 30 minutes
*/
async predict30Min(symbol = 'BTCUSDT') {
try {
return await this.makeRequest('POST', '/api/predict/30min', { symbol });
} catch (error) {
throw new Error(`30-minute prediction failed: ${error.message}`);
}
}
/**
* Predict next 1 hour
*/
async predict1H(symbol = 'BTCUSDT') {
try {
return await this.makeRequest('POST', '/api/predict/1h', { symbol });
} catch (error) {
throw new Error(`1-hour prediction failed: ${error.message}`);
}
}
/**
* Predict price movement for specific timeframe
*/
async predictTimeframe(symbol = 'BTCUSDT', timeframe = '1h') {
try {
return await this.makeRequest('POST', `/api/predict/${timeframe}`, { symbol });
} catch (error) {
throw new Error(`${timeframe} prediction failed: ${error.message}`);
}
}
/**
* Analyze specific strategy in detail
*/
async analyzeStrategy(strategyName, symbol = 'BTCUSDT', interval = '1h', limit = 100) {
try {
return await this.makeRequest('POST', `/api/strategy/${strategyName.toLowerCase()}`, {
symbol,
interval,
limit
});
} catch (error) {
throw new Error(`Strategy analysis failed for ${strategyName}: ${error.message}`);
}
}
/**
* Get available timeframes
*/
async getTimeframes() {
try {
const response = await this.makeRequest('GET', '/api/timeframes');
return response.timeframes;
} catch (error) {
console.error('Failed to fetch timeframes:', error);
return ['5m', '15m', '30m', '1h'];
}
}
/**
* Get list of all strategies
*/
async getStrategies() {
try {
const response = await this.makeRequest('GET', '/api/strategies');
return response.strategies;
} catch (error) {
console.error('Failed to fetch strategies:', error);
return [];
}
}
/**
* Get available trading pairs
*/
async getPairs() {
try {
const response = await this.makeRequest('GET', '/api/pairs');
return response.pairs;
} catch (error) {
console.error('Failed to fetch pairs:', error);
return [];
}
}
/**
* Trigger model retraining
*/
async triggerTraining(symbols = ['BTCUSDT'], days = 365, timeframes = ['5m', '15m', '30m', '1h']) {
try {
return await this.makeRequest('POST', '/api/train', {
symbols,
days,
timeframes
});
} catch (error) {
throw new Error(`Training trigger failed: ${error.message}`);
}
}
/**
* Generic HTTP request method
*/
async makeRequest(method, endpoint, data = null) {
const url = this.baseUrl + endpoint;
const options = {
method,
headers: {
'Content-Type': 'application/json'
},
timeout: 30000
};
if (data && (method === 'POST' || method === 'PUT')) {
options.body = JSON.stringify(data);
}
try {
const response = await fetch(url, options);
if (!response.ok) {
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
throw new Error(error.error || `HTTP ${response.status}`);
}
return await response.json();
} catch (error) {
if (error instanceof TypeError && error.message.includes('Failed to fetch')) {
throw new Error('Cannot connect to backend API. Make sure the backend server is running on port 5000.');
}
throw error;
}
}
}
// Create global instance
const apiClient = new SmartTradeAIClient();
window.apiClient = apiClient;