Login Page - Create Account

Support Board


Date/Time: Wed, 15 Jan 2025 15:35:53 +0000



Post From: How to repeat HTTP Requests within a study?

[2017-08-11 08:59:23]
Blue Brazilian - Posts: 42
I am trying to access data from various pages on a finance web site using the study shown below.

The study accepts the URL for a specific page on the web site and then applies some rudimentary parsing of the response to extract price data. This is successful on a single request, and the remote side then gracefully closes the socket.
However, if I then enter a new URL to the study, in order to access a new page, it doesn’t repeat the access request. I have tried re-setting the RequestState to REQUEST_NOT_SENT after the first response is received, but it still does not make a new request. Is there a way to execute repeated sc.MakeHTTPRequest calls from within the same study? And if so, could I then simply use a Sleep function to execute the HTTP requests at, say, one hour intervals?



#include <iostream>
#include <string>
#include <stdlib.h>

#include <algorithm>

#include "sierrachart.h"

using namespace std;

SCDLLName("htmlext5a")


SCSFExport scsf_htmlext5a(SCStudyGraphRef sc)
{

  
  SCInputRef URLtext = sc.Input[0];

  if (sc.SetDefaults)
  {
    URLtext.Name = "URL of website";
    sc.Input[0].SetString("URL");
    sc.FreeDLL = 1;
    return;
  }
  

  int& RequestState = sc.GetPersistentInt(1);
  SCString URLinput;
  URLinput = URLtext.GetString();

  enum { REQUEST_NOT_SENT = 0, REQUEST_SENT, REQUEST_RECEIVED };

  if (sc.Index == 0)

  {
    if (RequestState == REQUEST_NOT_SENT)
    {
    // Make a request for a text file on the server. When the request is complete and all of the data
    //has been downloaded, this study function will be called with the file placed into the sc.HTTPResponse character string array.

      if (!sc.MakeHTTPRequest(URLinput))
      {
      sc.AddMessageToLog("Error making HTTP request.", 1);

      // Indicate that the request was not sent.
      // Therefore, it can be made again when sc.Index equals 0.
      RequestState = REQUEST_NOT_SENT;
      }
    else
    RequestState = REQUEST_SENT;
    }
  }

  if (RequestState == REQUEST_SENT && sc.HTTPResponse != "")
  {
    RequestState = REQUEST_RECEIVED;

    std::string SResp;
    SResp = sc.HTTPResponse;

    int str_len = SResp.size();
    std::string TextString;
    std::string Subtext;
    Subtext = "alertValue";
    if (SResp.find(Subtext) != std::string::npos)
    {

      int posn = SResp.find(Subtext);
      if (str_len == 0) sc.AddMessageToLog("size of str_len is zero", 1);
      else
      {
        SCString Buffer;
        Buffer = " Length of str is ";
        SCString Num2Str;
        Num2Str.Format("%d ", str_len);
        Buffer += Num2Str;
        Buffer += " value of posn ";
        SCString NumStr;
        NumStr.Format("%d ", posn);

        Buffer += NumStr;
        Buffer += "\n";

        std::string TextStr = SResp.substr(posn + 11, 30);


        std::string price;
        std::string tester = "\"";
        int findPos = TextStr.find(tester); //finds first quote mark
        int findPos2 = TextStr.find(tester, findPos + 1); //finds second quote mark
        price = TextStr.substr(findPos + 1, (findPos2 - findPos - 1));

        SCString Buffer1;
        Buffer1 = "...Price is ";
        Buffer1 += price.c_str();
        price.erase(std::remove(price.begin(), price.end(), ','), price.end());
        float pricevalue;
        pricevalue = strtof(price.c_str(), 0);

        SCString Buffer2;
        Buffer2 = " price ";
        SCString Num4Str;
        Num4Str.Format("%0.06f ", pricevalue);

        Buffer2 += Num4Str;
        Buffer2 += "\n";
        sc.AddMessageToLog(Buffer2, 1);
    


      }
    }


  }
  else if (RequestState == REQUEST_SENT && sc.HTTPResponse == "")
  {
    sc.AddMessageToLog("Request did not complete...", 1);
    //The request has not completed, therefore there is nothing to do so we will return
    return;
  }

}