AmiBroker Built-in HTTP Example

Hi all, happy holidays and new years..

Recently made a few dlls for similar operations , figure I share what can be done w/o all that extra stuff, just a simple standalone snippet showing AmiBroker's built-in HTTP functions:

// =================================================================
  // AmiBroker Built-in HTTP Example
  // Uses native InternetPostRequest() - no DLL required
  // =================================================================

  function SendHTTPSignal(url, json) {
      // Set headers for JSON
      InternetSetHeaders("Content-Type: application/json");

      // Send POST request
      handle = InternetPostRequest(url, json, 0);

      if (handle == 0) {
          // Handle 0 doesn't always mean failure - verify connection
          test = InternetOpenURL(url);
          if (test != 0) {
              InternetClose(test);
              return "Sent (server reachable)";
          }
          return "Error: server unreachable";
      }

      // Check response
      status_code = InternetGetStatusCode(handle);
      response = InternetReadString(handle);
      InternetClose(handle);

      if (status_code == 200) {
          return "Success";
      }
      return "Error: " + NumToStr(status_code, 1);
  }

  // Build JSON payload
  symbol = Name();
  action = "BUY";
  quantity = 100;

  json = "{" +
      "\"symbol\":\"" + symbol + "\"," +
      "\"action\":\"" + action + "\"," +
      "\"quantity\":" + NumToStr(quantity, 1) +
      "}";

  // Send to localhost:5000
  url = "http://127.0.0.1:5000/amibroker_signal";
  result = SendHTTPSignal(url, json);

  Title = "HTTP Result: " + result;

Hope someone can find this useful , good luck.

5 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.