Authorize use of a custom indicator

Back to topics list To post a new topic, please log in or register
avatar
4
bradbbt 2016.06.28 19:46
 

I've written a custom indicator and am trying to hook it up to my authorization server. I've looked at https://docs.mql4.com/common/webrequest but of course you can't use it in an indicator, have also looked into using WinINet which just crashes, and I also tried https://www.mql5.com/en/code/10121 with no success.

Does anyone have any experience or insight into making GET calls to a REST server work inside of a custom indicator? Currently I'm at a loss, thinking it can't be done. 

avatar
12983
WHRoeder 2016.06.29 03:48 #
 
Indicators can not sleep, therefor any synchronous web calls can not work.
avatar
4
bradbbt 2016.06.29 04:24 #
 
WHRoeder:
Indicators can not sleep, therefor any synchronous web calls can not work.
I don't see anything mentioning async calls available for MT4, guessing they don't exist? 
avatar
145
graziani 2016.06.29 11:33 #
 

Only hope you have, if you can use WebRequest/dll/WinINet from OnInit().
From OnCalculate() it doesn't work for sure.

Slim chance, but you can try it.

avatar
1197
FMIC 2016.06.29 11:46 #
 

I don't know the solution, but I do know of a user that develops Indicators with an "Authorise" mechanism via a Web access, so if he does not say anything here, try to contact him (do a search for his profile name "Ovo" on the mql5.com site).

PS! I suspect however, that he uses DLL calls to the Win32 API (probably "WinINet" or "WinHTTP"), but uses asynchronous non-blocking functions so as not to pause the indicator functionality while waiting for a response.

avatar
4
bradbbt 2016.06.29 16:23 #
 

Thansk FMIC, I sent him an email but I think I actually got it figured out! Here's the code, maybe it can help someone in the future!

 

//this goes at the top of the indicator
#import  "Wininet.dll"
   int InternetOpenW(string, int, string, string, int);
   int InternetConnectW(int, string, int, string, string, int, int, int); 
   int InternetOpenUrlW(int, string, string, int, int, int);
   int InternetReadFile(int, uchar &sBuffer[], int, int& OneInt);
   int InternetCloseHandle(int); 
   int HttpOpenRequestW(int, string, string, string, string, string, int, int);
   bool HttpSendRequestW(int, string, int, string, int);
#import
#import "kernel32.dll"
int GetLastError(void);
#import

//this is in my OnCalculate()
//path on the server, r= TimeToStr is used for cache busting, ensuring we always get the latest value from the server (safety precaution), i.e. /Authorization?Username=mql4&uid=1234&r=...
string path = "/<endpoint>?<Query String Parameters>&r="+TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);

string headers = "Content-Type: application/x-www-form-urlencoded";
string data = "";
int HttpOpen = InternetOpenW(" ", 0, " ", "", 0);  
int HttpConnect = InternetConnectW(HttpOpen, "<URL without the HTTP/HTTPS, i.e. www.google.com", 80, "", "", 3, 0, 0); //80 is the port number
int HttpRequest = HttpOpenRequestW(HttpConnect, "GET", path, "", "", data, 0, 0);   
bool result = HttpSendRequestW(HttpRequest, headers, StringLen(headers), data, StringLen(data));
int read[1];

uchar ch[500];
string toStr="";
int dwBytes,h;
string strWebPage = "";
while(InternetReadFile(HttpRequest,ch,500,dwBytes))
{
   if(dwBytes<=0) break;
   toStr=toStr+CharArrayToString(ch,0,dwBytes);
}

strWebPage=toStr; 

//this should be your custom logic
if(strWebPage == "whatever you send back if authorized") //i.e. strWebPage == "LetThemIn"
   authorized = true;
else
   authorized = false;

InternetCloseHandle(HttpOpen);
InternetCloseHandle(HttpRequest);

 

avatar
1197
FMIC 2016.06.29 17:14 #
 
bradbbt:

Thansk FMIC, I sent him an email but I think I actually got it figured out! Here's the code, maybe it can help someone in the future!

I am not exactly sure, as I have never needed such a solution, but your code does not seem to be asynchronous and non-blocking, which will cause the indicator (and MetaTrader itself) to become unresponsive under non-ideal conditions, such as loss of connectivity to the web-service.
avatar
4
bradbbt 2016.06.29 17:28 #
 
I'm fine with it not being async. It only gets ran once a day, the very first time they run it, and it does time out if the server is unavailable. The only other platform I know off the top of my head that's built-in web request is async is Sierra Charts, all the other ones work this way (again afaik, ninjatrader and tradestation I know for sure) and it's a non-issue.
avatar
1197
FMIC 2016.06.29 17:38 #
 
bradbbt:
I'm fine with it not being async. It only gets ran once a day, the very first time they run it, and it does time out if the server is unavailable. The only other platform I know off the top of my head that's built-in web request is async is Sierra Charts, all the other ones work this way (again afaik, ninjatrader and tradestation I know for sure) and it's a non-issue.
I certainly don't agree with your assessment, but since I am definitely not going to be one of your customers, who am I to say otherwise!
Back to topics list  

To add comments, please log in or register