Yes
string SendChatGPTRequest(string request_body) { string headers = "Content-Type: application/json\r\n" + "Authorization: Bearer " + api_key + "\r\n" + "OpenAI-Organization: " + org_id + "\r\n"; // Optional char result[]; // Buffer for response string result_headers; // Convert request body to a char array and ensure it's properly null-terminated char post_data[]; ArrayResize(post_data, StringLen(request_body) + 1); // Adjust size StringToCharArray(request_body, post_data); post_data[StringLen(request_body)] = '\0'; // Ensure null-termination int attempts = 0; while (attempts < RetryAttempts) { Print("Sending request to OpenAI (Attempt: ", attempts + 1, ")"); int res = WebRequest("POST", api_url, headers, RequestTimeout, post_data, result, result_headers); if (res == 200) // Success { Print("Request successful."); return CharArrayToString(result); // Return response as a string } else { Print("Attempt ", attempts + 1, ": Error in request. Status code: ", res, ", HTTP code: ", GetLastError(), ", Response: ", result_headers); attempts++; Sleep(1000 * attempts); // Exponential backoff: increase delay on each attempt } } Print("Failed to get a valid response after ", RetryAttempts, " attempts."); return ""; // Return empty if all attempts fail }
Good luck.
After closer look at the error code returned, please modify your code as following:
Good luck.
Getting different errors now...
I know 401 is unauthorised in terms of a api request.
I can send you my API key in a private message so you can test if you dont mind ?
FF 0 15:10:22.010 AI Beast (EURUSD,H1) Sending request to OpenAI (Attempt: 1)
PQ 0 15:10:22.158 AI Beast (EURUSD,H1) Attempt 1: Error in request. Status code: 401, HTTP code: 0, Response: Date: Thu, 05 Sep 2024 14:10:22 GMT
EN 0 15:10:22.158 AI Beast (EURUSD,H1) Content-Type: application/json; charset=utf-8
MH 0 15:10:22.158 AI Beast (EURUSD,H1) Content-Length: 218
IP 0 15:10:22.158 AI Beast (EURUSD,H1) Connection: keep-alive
JE 0 15:10:22.158 AI Beast (EURUSD,H1) vary: Origin
OR 0 15:10:22.158 AI Beast (EURUSD,H1) x-request-id: req_79abc03967a6c03b0d764920bc472244
MN 0 15:10:22.158 AI Beast (EURUSD,H1) strict-transport-security: max-age=15552000; includeSubDomains; preload
JE 0 15:10:22.158 AI Beast (EURUSD,H1) CF-Cache-Status: DYNAMIC
CM 0 15:10:22.158 AI Beast (EURUSD,H1) X-Content-Type-Options: nosniff
LD 0 15:10:22.158 AI Beast (EURUSD,H1) Server: cloudflare
IS 0 15:10:22.158 AI Beast (EURUSD,H1) CF-RAY: 8be6cd66af9071f8-LHR
OJ 0 15:10:22.158 AI Beast (EURUSD,H1) alt-svc: h3=":443"; ma=86400
DH 0 15:10:22.158 AI Beast (EURUSD,H1)
HK 0 15:10:23.023 AI Beast (EURUSD,H1) Sending request to OpenAI (Attempt: 2)
PO 0 15:10:23.023 AI Beast (EURUSD,H1) Attempt 2: Error in request. Status code: 1003, HTTP code: 5203, Response: Date: Thu, 05 Sep 2024 14:10:22 GMT
MH 0 15:10:23.023 AI Beast (EURUSD,H1) Content-Type: application/json; charset=utf-8
MS 0 15:10:23.023 AI Beast (EURUSD,H1) Content-Length: 218
IJ 0 15:10:23.023 AI Beast (EURUSD,H1) Connection: keep-alive
RO 0 15:10:23.023 AI Beast (EURUSD,H1) vary: Origin
OE 0 15:10:23.023 AI Beast (EURUSD,H1) x-request-id: req_79abc03967a6c03b0d764920bc472244
EP 0 15:10:23.023 AI Beast (EURUSD,H1) strict-transport-security: max-age=15552000; includeSubDomains; preload
RO 0 15:10:23.023 AI Beast (EURUSD,H1) CF-Cache-Status: DYNAMIC
CG 0 15:10:23.023 AI Beast (EURUSD,H1) X-Content-Type-Options: nosniff
LN 0 15:10:23.023 AI Beast (EURUSD,H1) Server: cloudflare
QI 0 15:10:23.023 AI Beast (EURUSD,H1) CF-RAY: 8be6cd66af9071f8-LHR
OP 0 15:10:23.023 AI Beast (EURUSD,H1) alt-svc: h3=":443"; ma=86400
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I am trying to get my EA working that calls on Open Ai API for chat gpt to make trades automatically based on current price and history. Maybe news too.
But i keep getting these errors -
Yes i know my API key should not be hardcoded but for testing purposes i have put it in. Also open ai api url is allowed in tools - options - expert advisors.
I hope someone can help. Been trying to get it working for the past couple of weeks or so...