Showing posts with label automation. Show all posts
Showing posts with label automation. Show all posts

Wednesday, October 10, 2012

High Frequency Trading, Providing Liquidity, or HFT

A student was asking me about high frequency trading as he is getting offers to work for such companies in Montreal. I wanted to give him a short briefing on the financial aspect of it and the IT aspect (as I am mixing both worlds).

I found a very interesting story on /. (slashdot.org) that simplifies the concept in very concise manner.

It is and this is the basis of high frequency trading... though on Wallstreet they call it "providing liquidity". It works like this:
Alice wants to sell 1000 shares of Acme Corp. She places an sell order for 1000 shares at $25.00 on the exchange, but she also places a minimum bid of $23.90 on the sell order. This minimum bid what Alice is willing to accept should someone counter-offer but is suppose to be secret, only the sell price will be published.
Bob is looking for 1000 shares of Acme Corp. He wants to place it in his portfolio for long-term growth, but he thinks it is currently worth less. Bob places a general buy order at $24.40 on the exchange. For the sake of simplicity we will say that is his only price, though he too could have a maximum bid he is will to pay.
So there is a sell order at $25.00 and a buy order at $24.40 pending on the exchange, nothing trades. Now Bob could make a buy offer to Alice at $24.40 and the trade would go thru, or Alice could make a sell offer to Bob at a lower price and follow thru. In a perfect world the exchange would figure it out and match the orders... but that doesn't happen without further action on the part of Alice or Bob.
Eve is a high frequency trader... Actually, Eve is a high frequency trading program at MegaTraders LLC. and has spotted that there are buy and sell orders for Acme Corp on the exchange. Eve places a bid at $24.99 for Alice's share, the exchange accepts, and then Eve immediately cancels the bid order. Eve has just learned that Alice is will to sell for less than the sell order posted. Eve then continues placing bids on Alice's stock, $24.98, $24.97, $24.96, etc., each time immediately canceling the buy when the exchange accepts the bid. Eve gets down to $23.89, at which point the exchange does not accept the bid for Alice's stock. Eve has just learned that Alice is willing to sell for as little as $23.90 and all of this has happened within 10s of milliseconds.
Remember all those articles on Slashdot about high frequency firm X laying their own fiber directly to the exchange to cut milliseconds off transit time? Having custom L2 firmware on their switches and no firewalls on their trading links to cut milliseconds off transit time? This is why they do it, so they can submit hundreds/thousands of buy/sell/cancel orders on a single stock within a fraction of a second to learn pricing differences between orders that otherwise should be secret.
So Eve now knows that Alice is will to sell for $23.90 and would perform the same procedure against Bob to discover his highest buy price. Once found Eve can now see a price difference advantages to herself. Eve buys the 1000 shares from Alice at $23.90 and then immediately sells the shares to Bob at $24.40, pocketing the $500 difference. On Wallstreet they call this "providing liquidity", anywhere else this would be considered insider trading and illegal. Multiple all this by several hundred firms with special inside access to the market place, each running their own competing Eve programs, and you quickly realize how the market can go into turmoil within seconds....

You will find more interesting stories about HFT online.It seems robots are already playing with our money. Nora Young (http://www.cbc.ca/spark/) was speaking last week about robots helping humans. It seems, there is a need now to explore the options of looking at how robots can beat humans. Yes computers are dummy machines that execute what they are told to do. However, computers can accumulate knowledge and use it much faster than humans. This is the horror part of it.

Saturday, November 26, 2011

My story with http://www.proveyourworth.net/


There was a little nice test on that site back in 2006. I did not need the job back then but I wanted to prove my net worth for the sake of coding. 


I spent few hours on this test but the result was really good. The guy emailed me back for an interview and mentioned in the email that this was one of the neatest code that they have got out of few thousands. 


I did not continue to the interview as I did not need the job back then and I was moving from programming into Finance.


It is another nice memory and I would like to share my code with those old PHP nerds who would like to read some // 2cents.









//***********************************************************
//***********************************************************
// Script name: prove_your_worth_code.php                   *
// Version: 1.0                                             *
// Coder: Bilal Abdul Kader                                 *
// Date: October 29th, 2006                                 *
//                                                          *
// This script automate the submission of a mixed form      *
// (strings + files) to the ProveYourWorth website.         *
//                                                          *
// It emulates the browser using CURL library for PHP, the  *
// same task can be done using fsockopen and Snoopy.        *
//                                                          *
// The first task is to open the form in order to get the   *
// PHPSESSID and any other cookies. Then the code emulates  *
// the form submission and send it to the server again.     *
//                                                          *
// I kept the code to a minimum as it is a PROOF OF CONCEPT *
// only.                                                    *
//***********************************************************
//***********************************************************
//
// Get the cookie of the site first
// The script should fetch the index page
// in order to get the cookie(s)
// Apparently the site sets the PHPSESSID cookie only
//
//
// Define the curl handler
//
$my_curl_handler = curl_init();
//
// The web site to fetch
//
$url = "http://www.proveyourworth.net/index.php?p=begin&mistake=little";
//
// Add the URL operator
//
curl_setopt($my_curl_handler, CURLOPT_URL, $url);
//
// Handle the cookies of the site in order to send it later on
// the tmp folder is used on Nix machines or temp on Windows
// Add an agent header line
// Add a referer header line
//
curl_setopt($my_curl_handler, CURLOPT_COOKIEFILE, "c:\\temp\\cookie.txt");
curl_setopt($my_curl_handler, CURLOPT_COOKIEJAR, "c:\\temp\\cookie.txt");
curl_setopt($my_curl_handler, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-AU; rv:1.8.1) Gecko/20061010 Firefox/2.0");


//
// Follow any location HTTP command if any
// It seems that redirection is not used on this page
// However, this is kept for portability
//
curl_setopt($my_curl_handler, CURLOPT_FOLLOWLOCATION, 1);
//
// Return the trasnfer as part of the return value in curl_exec()
//
curl_setopt($my_curl_handler, CURLOPT_RETURNTRANSFER, 1);
//
// Execute the command and get the html page that has the forum
//
$htmlPage=curl_exec ($my_curl_handler);
//
// Parse the page in order to get the new hash key
//
preg_match("/\bhash\b\" value=\"(.*)\"/i",$htmlPage, $matches);
$myHashKey = $matches[1];
// echo "Hash Key is: $myHashKey
";

//
// Other action can be added here to check if the curl has
// got the correct page. This can be done using the
// $headers variable (For example $headers['url'])
//
//*******************************************************
//
// Now start the actual work to automate the form
// submission and to send the fields and the files
// fsock or Snoopy is not that hard to implement
// However, I prefered to use curl this time
// The other two implementations are as hard.
//
//*******************************************************
//
// Change the URL that the form should be submitted to
//
$url="http://www.proveyourworth.net/?p=auto_submit";
curl_setopt($my_curl_handler, CURLOPT_URL, $url);
//
// Now change the CURL operating model to POST (not GET)
// Regular HTTP POST (application/x-www-form-urlencoded)
// Add the new URL to send the form to
//
curl_setopt($my_curl_handler, CURLOPT_POST, 1);
//
// In case the form action need a valid referer to work
//
curl_setopt($my_curl_handler, CURLOPT_REFERER, "http://www.proveyourworth.net/index.php?p=begin&mistake=little");
//
// Build the data for the form
// I am using hard coded data in here for simplicity
// However, dynamic data can be easily incorporated
//
// Select the files to attach
//
$cvFile = "cv";
$sourceFile="prove_your_worth_code.php";
$myFormData = array(
"form_id"=>"job",
"name"=>"Bilal Abdul Kader",
"email"=>"bilal@brightvision.net",
"about"=>"Simple, I have a love affair with PHP and Programming in general. The puzzle was nice and tricky. I have emulated other more complex forms in real time.",
"incoming_file"=>"@$cvFile.doc",
"incoming_source"=>"@$sourceFile.txt",
"hash"=>"$myHashKey",
"Submit"=>"Submit"
);
//
// Build the form data now
//
curl_setopt ($my_curl_handler, CURLOPT_POSTFIELDS, $myFormData);
//
// Send the POST request and store the resuts in myResults
//
$myResult=curl_exec($my_curl_handler);
//
// In case of error during the submission
// Show the error.
//
echo curl_error($my_curl_handler);
//
// Otherwise, show the return of the form
//
echo $myResult;
//
//
//
?>