I’ll share a easy methodology to obtain financial information knowledge from a web based JSON file and reserve it regionally utilizing the WebRequest() operate.
The aim is to fetch knowledge from https://nfs.faireconomy.media/ff_calendar_thisweek.json, which gives the financial calendar for the present week.
Supply code:
void DownloadAndParse() { string json_url = "https://nfs.faireconomy.media/ff_calendar_thisweek.json"; string fileName = "calendar_thisweek.json"; uchar outcome[]; char knowledge[]; string headers; string result_headers; int timeout = 5000; int res; Print("Requesting JSON from: ", json_url); ResetLastError(); res = WebRequest("GET", json_url, headers, timeout, knowledge, outcome, result_headers); if (res == 200) { Print("JSON downloaded!"); string jsonRaw = ""; int dimension = ArraySize(outcome); jsonRaw = CharArrayToString(outcome, 0, dimension); int fileHandle = FileOpen(fileName, FILE_WRITE | FILE_TXT | FILE_ANSI); if (fileHandle != INVALID_HANDLE) { FileWriteString(fileHandle, jsonRaw); FileClose(fileHandle); Print("JSON saved to file."); } else { Print("Didn't open file for writing. Error: ", GetLastError()); } } else Error: ", GetLastError()); } void OnStart() { DownloadAndParse(); }
The way it works:
WebRequest() is used to ship a GET request to the desired URL.
If profitable ( HTTP code 200 ), the JSON content material is retrieved.
The downloaded knowledge is transformed from a uchar[] array right into a readable string .
The string is then saved to an area .json file for later use.
Essential notes:
It’s essential to enable URL entry in your MetaTrader terminal:
At all times deal with errors rigorously to keep away from surprising failures in your EA or scrip
You’ll be able to simply combine this obtain methodology into your EA or indicator to fetch the most recent financial occasions robotically.
