Automated Trading with PHP

Discussion in 'Automated Trading' started by chuzek, Apr 7, 2010.

  1. chuzek



    I wanted to know if anyone has any code samples for using PHP to execute trades via API.

    I am able to get TD Ameritrade's API
    ( to work seamlessly with PHP even though they do not provide any guidance on the integration. However, their commissions are relatively high so they aren't the best brokerage to use.

    I'm most interested in using Interactive Brokers (IB). However,
    their documentation only covers Java, C++, and ActiveX
    ( Unfortunately, I am not the best programmer in those languages and their API is not very straight forward like TD Ameritrade's.

    But I'd settle for any PHP code samples for any API whose brokerage charges less than $9.99 per trade.

    Please Help

  2. I think you should really sit down and analze your needs to see if php is the answer.
  3. I use php for many many things related to trading; order execution is NOT one of them.
  4. I should add one thing about php. As I said above, I use it for many tasks. However, a note to all programmers, it's slow.

    I have a data anaylsis script I use to search for patterns. A run would take over an hour with php. Converted the script to C and run under TCC and now only takes 10-12 minutes.
  5. chuzek


    These answers are not helpful. As I said, I'm not familiar with Java, C++, nor ActiveX. Additionally, I'm not interested in learning them at the moment nor hiring developers.
  6. thejam


    I can think of 1001+ reasons PHP will blow-up an automated order system.

    If you don't believe me, do a search on google for php and automated order execution or some mix of terms. See how many results you get. Thats right: NULL.

    If not C/C++ try VB. C/C++ is not very different from PHP.
  7. byteme


    Are you going to be building a web-based application or desktop-based application?

    To be honest, PHP is probably the wrong tool for the job.

    Your best bet might be to create a new PHP extension wrapping the C++ implementation:

    But I think you've stated you might not have the skills for that.

    Alternatively, if you can at least read the IB API Java code, you might be able to replicate the protocol that is used to communicate with IB TWS.

    From PHP, you will have to open a socket the same way the Java code does and then send and receive the appropriate messages.

    The main classes that do the communications are and

    There are Python versions here:

    If you can auto-generate the PHP code from the Java code-base this might be an option but I suspect you won't pursue this option.

    MB Trading has a Quote API that you should be able to use from PHP without much trouble:

    However, for order management you'll need to implement FIX so you're back to making a PHP extension for an existing FIX engine or trying to implement the FIX protocol from scratch in PHP.

    In short, if all you have is a hammer, everthing looks like a nail. PHP is your hammer. Another tool might be better here.

    If you're genuinely competent with PHP, learning something like Python should be a snap. You can be productive in a couple of days.
  8. chuzek


    To your point, all I have is a hammer.

    Yes, it's a web based application, not desktop one.

    I've been reviewing sockets in PHP. So I'll take a look at the links you provided.

    I had also looked into Lightspeed Gateway's API that also utilizes sockets. One of the problems I'm facing with them is it requires VPN access to connect. While that may be practical for a desktop application, creating a VPN connection on a webserver is difficult and once done, prevents it from dishing out web pages to the Internet.

    Do you know anything about Vision Financial Market's API? (
    Their website says that it uses REST. If so, that may be easier to integrate with PHP. Unfortunately, their commissions are not listed.

  9. byteme


    I forgot about Vision - they use the Rithmic API if I am not mistaken.

    REST is obviously going to be the easiest option from PHP so could be perfect for you. We were talking about web service APIs in another thread a while back that has recently resurfaced.

    Have no idea about commissions though but there are a couple of people on these forums who use Vision.
  10. corbel


    Hey there, here is an example for PHP with TDAPI.

    I actually came here trying to get the same answer you were, and had to read through all these pages of people saying not to use PHP before deciding I wasn't going to find any help here, and did it all on my own.

    To some people, PHP isn't what they need, but for you and I, it's perfect, since those half-seconds don't matter to us


    // create curl resource
    $ch = curl_init();

    echo "connecting ....";

    //set up cURL parameters
    curl_setopt($ch, CURLOPT_URL, "");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    url_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'userid=corbel&password=blahblah123&source=AAAA&version=1.0');

    //log in, save response
    $output = curl_exec($ch);

    //bah, f the xml parser, make my own

    $first = explode("/session-id", $output); //elitetrader's system gets ride of the html, so you need to put carrots around /session-id
    $second = explode("session-id", $first[0]);
    $_SESSION['session-id']=$second[1]; //it's not truely necessary to make this a SESSION variable...yet

    curl_setopt($ch, CURLOPT_URL, ";jsessionid=".$_SESSION['session-id']);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'source=AAAA&symbol=AAPL');
    $output = curl_exec($ch);

    echo "AAPL quote: ".$output;


    #10     Apr 23, 2010