Download MetaTrader 5
To add comments, please log in or register
Share your program via Code Base. Gain access to millions of traders!
petermitchell
133
petermitchell 2014.02.09 14:59 

I am updating my EAs to work with the latest release of MT4 Version 4.0 Build 600. Everything has ported across to the new compiler with relative ease, however InternetOpenUrlA is now returning an error. I am using the following code. Can anyone advise why this function is behaving differently with the latest MT4?

   string url = "http://www.website.com/files/";
   
   int result = InternetAttemptConnect(0);
   
   if(result != 0)
   {
      Print("Cannot connect to internet - InternetAttemptConnect()");
      return(FALSE);
   }
   
   int hInternetSession = InternetOpenA("Microsoft Internet Explorer", 0, "", "", 0);
   
   if(hInternetSession <= 0)
   {
       Print("Cannot open internet session - InternetOpenA()");
       return(FALSE);         
   }
   
   int hURL = InternetOpenUrlA(hInternetSession, url, "", 0, 0, 0);
   
   if(hURL <= 0)
   {
      Print("Cannot open URL ", url, " - InternetOpenUrlA()");
      InternetCloseHandle(hInternetSession);
      return(FALSE);
   }
Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.02.09 15:18  
petermitchell:

I am updating my EAs to work with the latest release of MT4 Version 4.0 Build 600. Everything has ported across to the new compiler with relative ease, however InternetOpenUrlA is now returning an error. I am using the following code. Can anyone advise why this function is behaving differently with the latest MT4?

Is there a Unicode version of the DLL function call you are using ? https://www.mql5.com/en/forum/149321
petermitchell
133
petermitchell 2014.02.14 09:49  
petermitchell:

I am updating my EAs to work with the latest release of MT4 Version 4.0 Build 600. Everything has ported across to the new compiler with relative ease, however InternetOpenUrlA is now returning an error. I am using the following code. Can anyone advise why this function is behaving differently with the latest MT4?



I have now solved the problem. The answer is to use Unicode version of these functions, InternetOpenUrlW() and InternetOpenW().

Deryk Banks
6
Deryk Banks 2014.04.21 01:05  

Hello, I'm having the same issue with my EA. I keep getting the InternetURLA error. I have tried to implement the fixes here, but I am missing something. Can you look at the code and tell me why I would get the error? I appreciate it. I had this coded in 2010, and it always worked until now.


//+------------------------------------------------------------------+
//| ZigAndZag EA.mq4 |
//| Copyright © 2010, MethodTraders.com |
//| http://www.MethodTraders.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MethodTraders.com"
#property link "http://www.MethodTraders.com"
#include <WinUser32.mqh>

#import "wininet.dll"

int InternetAttemptConnect (int x);
int InternetOpenA(string sAgent, int lAccessType,
string sProxyName = "", string sProxyBypass = "",
int lFlags = 0);
int InternetOpenUrlA(int hInternetSession, string sUrl,
string sHeaders = "", int lHeadersLength = 0,
int lFlags = 0, int lContext = 0);
int InternetReadFile(int hFile, int& sBuffer[], int lNumBytesToRead,
int& lNumberOfBytesRead[]);
int InternetCloseHandle(int hInet);
#import "kernel32.dll"


extern string
EA_Settings = "-------- General Settings";
extern int
MagicNumber = 12345,
Slippage = 5,
TakeProfit = 0,
StopLoss = 0;
extern double
Lots = 0.10;
extern bool
UseWebSettings = true;
extern string
ZigAndZag_Settings = "------Zig And Zag Settings";
extern int
KeelOver = 25,
Slalom = 7;

extern int
MaxTrades = 1;

extern bool
AutoTrade = true;
extern bool
BoxAlert = true;
extern bool
ConfirmAllTrades = false;
extern bool
Confirm2ndTrade = false;

extern bool
TradeOnBarClose = false;
extern bool
CloseOnReverseSignal = true;

//--------------------
bool TradeLong = true;
bool TradeShort = true;
bool EnableAlerts = true;

//---------------




datetime TT;
datetime AT;
datetime RT;
datetime SigTime;
int MyDigits;
double MyPoint;
string Com = "";
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

if(Digits==5)MyDigits=5;
else if(Digits==3)MyDigits=3;
else MyDigits = Digits;
if (Point == 0.00001) MyPoint = 0.00001; //6 digits
else if (Point == 0.001) MyPoint = 0.001; //3 digits (for Yen based pairs)
else MyPoint = Point; //Normal

TT = iTime(Symbol(),0,0);
AT = iTime(Symbol(),0,0);
RT = 0;

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

if(UseWebSettings==true/* && IsTesting()==false && RT!=iTime(Symbol(),PERIOD_H1,0) && RT!=iTime(Symbol(),PERIOD_H1,1)*/)
{
string Temp = ReadPage();
subSetVars(Temp);
Com = "Using Template: "+Temp;
RT = iTime(Symbol(),PERIOD_H1,0);
}

int ticket = 0;
int shift = 0;
if(TradeOnBarClose)shift = 1;

double ZZarrowUp = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,5,shift);
double ZZarrowDown = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,6,shift);

double ZZarrowUp2 = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,5,shift+1);
double ZZarrowDown2 = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,6,shift+1);

double ZZarrowUp3 = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,5,shift+2);
double ZZarrowDown3 = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,6,shift+2);

double ZZarrowUp4 = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,5,shift+3);
double ZZarrowDown4 = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,6,shift+3);

double ZZarrowUp5 = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,5,shift+4);
double ZZarrowDown5 = iCustom(Symbol(),0,"ZigAndZag",KeelOver,Slalom,6,shift+4);

double CLOSE = iClose(Symbol(),0,shift);


string Signal = "NONE";
if (ZZarrowUp<CLOSE && ZZarrowUp>0)
{
if(SigTime!=iTime(Symbol(),0,shift))
{
Signal = "BUY";
SigTime = iTime(Symbol(),0,shift);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
if (ZZarrowDown>CLOSE && ZZarrowDown>0)
{
if(SigTime!=iTime(Symbol(),0,shift))
{
Signal = "SELL";
SigTime = iTime(Symbol(),0,shift);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
//---
if (ZZarrowUp2<CLOSE && ZZarrowUp2>0)
{
if(SigTime!=iTime(Symbol(),0,shift+1))
{
Signal = "BUY";
SigTime = iTime(Symbol(),0,shift+1);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
if (ZZarrowDown2>CLOSE && ZZarrowDown2>0)
{
if(SigTime!=iTime(Symbol(),0,shift+1))
{
Signal = "SELL";
SigTime = iTime(Symbol(),0,shift+1);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
//---
if (ZZarrowUp3<CLOSE && ZZarrowUp3>0)
{
if(SigTime!=iTime(Symbol(),0,shift+2))
{
Signal = "BUY";
SigTime = iTime(Symbol(),0,shift+2);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
if (ZZarrowDown3>CLOSE && ZZarrowDown3>0)
{
if(SigTime!=iTime(Symbol(),0,shift+2))
{
Signal = "SELL";
SigTime = iTime(Symbol(),0,shift+2);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
//---
if (ZZarrowUp4<CLOSE && ZZarrowUp4>0)
{
if(SigTime!=iTime(Symbol(),0,shift+3))
{
Signal = "BUY";
SigTime = iTime(Symbol(),0,shift+3);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}

if (ZZarrowDown4>CLOSE && ZZarrowDown4>0)
{
if(SigTime!=iTime(Symbol(),0,shift+3))
{
Signal = "SELL";
SigTime = iTime(Symbol(),0,shift+3);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
//---
if (ZZarrowUp5<CLOSE && ZZarrowUp5>0)
{
if(SigTime!=iTime(Symbol(),0,shift+4))
{
Signal = "BUY";
SigTime = iTime(Symbol(),0,shift+4);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
if (ZZarrowDown5>CLOSE && ZZarrowDown5>0)
{
if(SigTime!=iTime(Symbol(),0,shift+4))
{
Signal = "SELL";
SigTime = iTime(Symbol(),0,shift+4);
Print(Signal+" Signal: "+TimeToStr(SigTime,TIME_MINUTES));
}
}
//---

Comment ("Signal: "+Signal+"\n"+Com);


bool Done = false;

if(EnableAlerts==true)
{
if(Signal == "BUY" || Signal == "SELL" && subTotalTrade()==MaxTrades && AT!=iTime(Symbol(),0,0))
{
PlaySound("alert.wav");
Alert("EA will not trade ", Symbol(), " Max trades reached");
AT=iTime(Symbol(),0,0);
}


if(BoxAlert == true && Signal == "BUY" && AT!=iTime(Symbol(),0,0))
{
PlaySound("alert.wav");
Alert("Buy ", Symbol(), ". Close shorts for ", Symbol());
AT=iTime(Symbol(),0,0);
}

if(BoxAlert == true && Signal == "SELL" && AT!=iTime(Symbol(),0,0))
{
PlaySound("alert.wav");
Alert("Sell ", Symbol(), " Close longs for ", Symbol());
AT=iTime(Symbol(),0,0);
}
}



if(Signal == "BUY" && AutoTrade == true && subTotalTrade()<MaxTrades && TT!=iTime(Symbol(),0,0))
{

if(Confirm2ndTrade == true && subTotalTypeOrders(OP_BUY)>0)
{
if(EnableAlerts==true)PlaySound("alert.wav");
int ret=MessageBox(" "+Symbol()+" BUY trade already open.\nOpen another BUY trade?", "NEW BUY SIGNAL", MB_YESNO|MB_ICONQUESTION);
if(ret == IDNO)
{
Signal = "none";
TT=iTime(Symbol(),0,0);
Done = true;
}
if(ret == IDYES)Done=true;
}
}
if(Signal == "SELL" && AutoTrade == true && subTotalTrade()<MaxTrades && TT!=iTime(Symbol(),0,0))
{

if(Confirm2ndTrade == true && subTotalTypeOrders(OP_SELL)>0)
{
if(EnableAlerts==true)PlaySound("alert.wav");
ret=MessageBox(" "+Symbol()+" SELL trade already open.\nOpen another SELL trade?", "NEW SELL SIGNAL", MB_YESNO|MB_ICONQUESTION);
if(ret == IDNO)
{
Signal = "none";
TT=iTime(Symbol(),0,0);
Done = true;
}
if(ret == IDYES)Done=true;
}
}


if(ConfirmAllTrades == true && Done == false && AutoTrade == true && subTotalTrade()<MaxTrades && Signal == "BUY" && TT!=iTime(Symbol(),0,0))
{
if(EnableAlerts==true)PlaySound("alert.wav");
ret=MessageBox(" Buy "+Symbol()+". Close shorts for "+Symbol()+"", "NEW SIGNAL", MB_YESNO|MB_ICONQUESTION);
if(ret == IDNO)
{
Signal = "none";
TT=iTime(Symbol(),0,0);
Done = true;
}
if(ret == IDYES)Done=true;
}


if(ConfirmAllTrades == true && Done == false && AutoTrade == true && subTotalTrade()<MaxTrades && Signal == "SELL" && TT!=iTime(Symbol(),0,0))
{
if(EnableAlerts==true)PlaySound("alert.wav");
ret=MessageBox(" Sell "+Symbol()+". Close longs for "+Symbol()+"", "NEW SIGNAL", MB_YESNO|MB_ICONQUESTION);
if(ret == IDNO)
{
Signal = "none";
TT=iTime(Symbol(),0,0);
Done = true;
}
if(ret == IDYES)Done=true;
}


//CLOSE TRADES
if(CloseOnReverseSignal == true &&subTotalTrade()>0&&TT!=iTime(Symbol(),0,0))
{

if(Signal == "BUY"&&subTotalTypeOrders(OP_SELL)>0)
{
subCloseOrder();
}

if(Signal == "SELL"&&subTotalTypeOrders(OP_BUY)>0)
{
subCloseOrder();
}
}


//OPEN TRADES
if(AutoTrade == true && subTotalTrade()<MaxTrades && TT!=iTime(Symbol(),0,0))
{

//BUY
if(Signal == "BUY" && TradeLong==true)
{
ticket = subOpenOrder(OP_BUY, StopLoss, TakeProfit, Lots);
if(ticket>0)
{
TT=iTime(Symbol(),0,0);
}
}
//SELL
if(Signal == "SELL" && TradeShort==true)
{
ticket = subOpenOrder(OP_SELL, StopLoss, TakeProfit, Lots);
if(ticket>0)
{
TT=iTime(Symbol(),0,0);
}
}

}

//----
return(0);
}
//+------------------------------------------------------------------+
string ReadPage()
{
//Comment(ReadWebPage("http://www.contrariantradefx.info/hidden/ctfx1001.csv"));

string str = ReadWebPage("http://www.contrariantradefx.info/hidden/ctfx1001.csv");
string arr[1000];
int TotalNews = Explode(str, "\r\n", arr);
string plate = "NULL";
//Print("READ PAGE: "+str);
for( int i=0; i<TotalNews; i++)
{
//Print(arr[i]);
int del = StringFind(arr[i],",",0);
string Sym = StringSubstr(arr[i],0,del);
string Com = StringSubstr(arr[i],del+1,0);
//Print("Sym: "+Sym+"| "+Com);
//Print("Sym: "+Sym+"|Com: "+Com);
if(StringFind(Symbol(),Sym,0)!=-1)
{
//Print("String Found: "+Symbol()+","+Sym);
plate = Com;
break;
}
}
//Print("PLATE: "+plate);
return(plate);
}


// COUNTS THE TOTAL ORDERS DEPENDING ON TYPE
int subTotalTypeOrders(int Type)
{
int
cnt,
total = 0;

for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==Type&&OrderSymbol()==Symbol()&&
OrderMagicNumber()==MagicNumber) total++;
}
return(total);
}
//---------------------------
// COUNTS THE TOTAL TRADE
int subTotalTrade()
{
int
cnt,
total = 0;

for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber) total++;
}
return(total);
}

// OPENS ORDERS
int subOpenOrder(int type, int stoploss, int takeprofit,double Lotz)
{
string TicketComment = "ZigAndZag EA";
int
ticket = 0,
err = 0,
c = 0;

double
aStopLoss = 0,
aTakeProfit = 0,
bStopLoss = 0,
bTakeProfit = 0;


int NumberOfTries = 10;
if(type==OP_BUY)
{
for(c=0;c<NumberOfTries;c++)
{
RefreshRates();
if(stoploss!=0)
{
aStopLoss = NormalizeDouble(Ask-stoploss*MyPoint,MyDigits);
bStopLoss = NormalizeDouble(Bid+stoploss*MyPoint,MyDigits);
}

if(takeprofit!=0)
{
aTakeProfit = NormalizeDouble(Ask+takeprofit*MyPoint,MyDigits);
bTakeProfit = NormalizeDouble(Bid-takeprofit*MyPoint,MyDigits);
}
ticket=OrderSend(Symbol(),OP_BUY,Lotz,Ask,Slippage,aStopLoss,aTakeProfit,TicketComment,MagicNumber,0,Green);
err=GetLastError();
if(err==0)
{
if(ticket>0) break;
}
else
{
if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0) break;
}
}
}
}
if(type==OP_SELL)
{
for(c=0;c<NumberOfTries;c++)
{
RefreshRates();
if(stoploss!=0)
{
aStopLoss = NormalizeDouble(Ask-stoploss*MyPoint,MyDigits);
bStopLoss = NormalizeDouble(Bid+stoploss*MyPoint,MyDigits);
}

if(takeprofit!=0)
{
aTakeProfit = NormalizeDouble(Ask+takeprofit*MyPoint,MyDigits);
bTakeProfit = NormalizeDouble(Bid-takeprofit*MyPoint,MyDigits);
}
ticket=OrderSend(Symbol(),OP_SELL,Lotz,Bid,Slippage,bStopLoss,bTakeProfit,TicketComment,MagicNumber,0,Red);
err=GetLastError();
if(err==0)
{
if(ticket>0) break;
}
else
{
if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0) break;
}
}
}
}
return(ticket);
}


//CLOSES ALL OPEN ORDERS
void subCloseOrder()
{
int
cnt,
total = 0,
ticket = 0,
err = 0,
c = 0;

int NumberOfTries = 10;
total = OrdersTotal();
for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber)
{
switch(OrderType())
{
case OP_BUY :
for(c=0;c<NumberOfTries;c++)
{
RefreshRates();
ticket=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(OrderClosePrice(),Digits),Slippage,Violet);
err=GetLastError();
if(err==0)
{
if(ticket>0) break;
}
else
{
if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0) break;
}
}
}
continue;

case OP_SELL :
for(c=0;c<NumberOfTries;c++)
{
RefreshRates();
ticket=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(OrderClosePrice(),Digits),Slippage,Violet);
err=GetLastError();
if(err==0)
{
if(ticket>0) break;
}
else
{
if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0) break;
}
}
}
continue;
}
}
}
}

int Explode(string str, string delimiter, string& arr[])
{
int i = 0;
int pos = StringFind(str, delimiter);
while(pos != -1)
{
if(pos == 0) arr[i] = ""; else arr[i] = StringSubstr(str, 0, pos);
i++;
str = StringSubstr(str, pos+StringLen(delimiter));
pos = StringFind(str, delimiter);
if(pos == -1 || str == "") break;
}
arr[i] = str;

return(i+1);
}
string ReadWebPage(string url)
{
if(!IsDllsAllowed())
{
Alert("DLL Disabled. Please go to EA Properties Common Tab and Check Allow DLL");
return("");
}
int rv = InternetAttemptConnect(0);
if(rv != 0)
{
Alert("InternetAttemptConnect()");
return("");
}
int hInternetSession = InternetOpenA("Microsoft Internet Explorer",
0, "", "", 0);
if(hInternetSession <= 0)
{
Alert("InternetOpenA()");
return("");
}
int hURL = InternetOpenUrlA(hInternetSession,
url, "", 0, 0, 0);
if(hURL <= 0)
{
Alert("InternetOpenUrlA()");
InternetCloseHandle(hInternetSession);
return(0);
}
int cBuffer[256];
int dwBytesRead[1];
string TXT = "";
while(!IsStopped())
{
for(int i = 0; i<256; i++) cBuffer[i] = 0;
bool bResult = InternetReadFile(hURL, cBuffer, 1024, dwBytesRead);
if(dwBytesRead[0] == 0) break;
string text = "";
for(i = 0; i < 256; i++)
{
text = text + CharToStr(cBuffer[i] & 0x000000FF);
if(StringLen(text) == dwBytesRead[0]) break;
text = text + CharToStr(cBuffer[i] >> 8 & 0x000000FF);
if(StringLen(text) == dwBytesRead[0]) break;
text = text + CharToStr(cBuffer[i] >> 16 & 0x000000FF);
if(StringLen(text) == dwBytesRead[0]) break;
text = text + CharToStr(cBuffer[i] >> 24 & 0x000000FF);
}
TXT = TXT + text;
Sleep(1);
}
if(TXT == "") Alert("Íåò ñ÷èòàííûõ äàííûõ");
InternetCloseHandle(hInternetSession);

return(TXT);
}
void subSetVars(string Temp)
{
if(Temp == "CTBuy")
{
TradeLong = true;
TradeShort = false;
EnableAlerts = true;
MagicNumber = 12345;
Slippage = 5;
TakeProfit = 0;
StopLoss = 0;
KeelOver = 25;
Slalom = 7;
MaxTrades = 1;
AutoTrade = true;
BoxAlert = true;
ConfirmAllTrades = false;
Confirm2ndTrade = false;
TradeOnBarClose = false;
CloseOnReverseSignal = true;
}
if(Temp == "CTSell")
{
TradeLong = false;
TradeShort = true;
EnableAlerts = true;
MagicNumber = 12345;
Slippage = 5;
TakeProfit = 0;
StopLoss = 0;
KeelOver = 25;
Slalom = 7;
MaxTrades = 1;
AutoTrade = true;
BoxAlert = true;
ConfirmAllTrades = false;
Confirm2ndTrade = false;
TradeOnBarClose = false;
CloseOnReverseSignal = true;
}
if(Temp == "CTBuySell")
{
TradeLong = true;
TradeShort = true;
EnableAlerts = true;
MagicNumber = 12345;
Slippage = 5;
TakeProfit = 100;
StopLoss = 0;
KeelOver = 25;
Slalom = 7;
MaxTrades = 1;
AutoTrade = true;
BoxAlert = true;
ConfirmAllTrades = false;
Confirm2ndTrade = false;
TradeOnBarClose = false;
CloseOnReverseSignal = true;
}
if(Temp == "CTNoTrade")
{
TradeLong = false;
TradeShort = false;
EnableAlerts = false;
MagicNumber = 12345;
Slippage = 5;
TakeProfit = 10;
StopLoss = 0;
KeelOver = 25;
Slalom = 7;
MaxTrades = 1;
AutoTrade = true;
BoxAlert = true;
ConfirmAllTrades = false;
Confirm2ndTrade = false;
TradeOnBarClose = false;
CloseOnReverseSignal = false;
}
}

Deryk Banks
6
Deryk Banks 2014.04.21 03:42  
Never mind. I fixed it. Changed to InternetOpenURLW and saved as unicode. No errors and everything is working again. Thanks for your previous posts!
Jitendra Parmar
1093
Jitendra Parmar 2015.05.28 20:50  
petermitchell:



I have now solved the problem. The answer is to use Unicode version of these functions, InternetOpenUrlW() and InternetOpenW().


thanks solutions worked for me..!!! 
/
To add comments, please log in or register