The HTTP functions can retreive or scrape web content - prices, signals, news, or indicators - and use them in the trading strategy. They can also execute PHP scripts and can be used by a broker plugin for communicating with a REST or FIX API.
// access the CFTC website, read the current Commitment Of Traders Report, // and store it in a file. function main()
{ file_delete("cot.txt"); // delete previous report file_append("cot.txt", http_transfer("http://www.cftc.gov/dea/futures/deacmesf.htm",0)); }
// get current date and time from the Internet function main()
{ string Response = http_transfer("http://worldtimeapi.org/api/ip",0); string Date = strtext(Response,"\"datetime\":","0"); printf("\n%s",Date); }
// Download historical EOD price data from Google
function main()
{
string Code = "SPY";
string URL = strf("https://www.google.com/finance/historical?q=%s&startdate=01-Jan-2000&output=csv",Code);
string Content = http_transfer(URL,0);
if(!Content) continue;
file_write("History\\history.csv",Content,0);
dataNew(1,0,7);
if(dataParse(1,"%d-%b-%y,f3,f1,f2,f4,f6","History\\history.csv"))
dataSave(1,strf("History\\%s.t6",Code));
}
// Download earnings data from AlphaVantage
// and store earnings surprises in a dataset
int loadEarnings()
{
string URL = strf("https://www.alphavantage.co/query?function=EARNINGS&symbol=%s&apikey=%s",
Asset,report(33)); // 33 = AlphaVantage API key
string Content = http_transfer(URL,0); // JSON format
if(!Content) return 0;
string Pos = strstr(Content,"reportedDate");
if(!Pos) return 0;
dataNew(1,0,0);
while(Pos) {
var Date = wdatef("%Y-%m-%d",Pos+12+4);
if(Date == 0) break;
Pos = strstr(Pos,"surprisePercentage");
if(!Pos) break;
int Row = dataRow(1,dataAppendRow(1,2));
dataSet(1,Row,0,Date); // earnings date in field 0
var Surprise = atof(Pos+18+4);
dataSet(1,Row,1,Surprise); // surprise in fields 1
printf("\n%.4f %.4f",dataVar(1,Row,0),dataVar(1,Row,1));
Pos = strstr(Pos,"reportedDate");
}
return 1;
}
// start the script "ip.php" on a remote server, and
// print dots until Zorro's IP address is returned function main() { char ip_str[100]; // just a long empty string int id = http_request("http://myserver.de/php/ip.php",0,0,0); if(!id) return; while(!http_status(id)) { if(!wait(100)) return; // wait for the server to reply printf("."); }
if(http_status(id) > 0) { //transfer successful? http_result(id,ip_str,100); //get the replied IP printf("\n%s",ip_str); } else printf("\nError during transfer!"); http_free(id); //always clean up the id! } ip.php: <?php
echo "Your IP address: " . $_SERVER['REMOTE_ADDR'];
?>
// send an email when a trade is entered
function sendEmailAboutTrade()
{
// compose the message
string Content = strf("content=Zorro has entered a trade!\n%d Lots of %s",
Lots,Asset);
http_transfer("http://www.myserver.com/zorromail.php",Content);
}
zorromail.php:
<?php
$to = "me@myself.com";
$subject = "Message from Zorro!";
$body = $_POST['content'];
mail($to,$subject,$body);
?>
// write text to a file on a server
function sendEmailAboutTrade()
{
// compose the message
string Content = "content=This is my file content");
string echo = http_transfer("http://www.myserver.com/store.php",Content);
printf("\nResponse: $s",echo);
}
store.php:
<?php
$file = 'file.txt';
$data = $_POST['content'];
file_put_contents($file,$data);
echo "Stored in " . $file;
?>