PHP example code for order placement through API


Is it possible to update the documentation to show example PHP code to interact with the API in order to place orders?

Has anyone maybe coded this already ?

Would really help me out if you can share / update this.

This is the page:

Thank you very much.

i have… what do you need

Yes, I need this as well. I would like a PHP page with the full code example to do a limit order. So I could hit that PHP page and it would place the limit order. I see the javacript code example, and that is great I can customize that, but I need a full code example. If anyone has this, please post it so I can get started!


maybe you should use my example server there’s a api plugin that i built that still works for v2

I would like to use your server but I want to code my own as well and need a full code example to create a limit order using PHP on a web server. Actually just need any working full code PHP example and I can customize it and get started… Please help.


I used this API to create a PHP application to interface with Alpaca and do some test trades:

The API seems to work fairly well.

Below is a portion of code that I was playing with to test proof of concept to have PHP automate trades using the API above. I removed some of the formulas from it but you should get the basic idea…

    echo "\n";

    $url = "".$symbol;

    require './vendor/autoload.php';

    use Alpaca\Alpaca;
    $alpaca = new Alpaca("Your Key from Alpaca", "Your Secret Key from Alpaca");

    //change for real trading

    $space= str_repeat(' ', 3);

    $price = 0.00;
    $refPrice = 0.00;
    //read from positions

    $avgPriceAcc = 0;
    $avgPriceTick = 0;

    echo "\nSymbol: ".$symbol."\n";

    while (true){
        //Read account cash and share balanaces
        $account = $alpaca->getAccount()->getResponse();
        $accountCash = $account->cash;
        $accountEquity = $account->equity;

        $positions = $alpaca->getPositions()->getResponse();
        if (!empty($positions)){
            $positionSymbol = $positions[0]->symbol;
            $positionShares = $positions[0]->qty;
            $positionCostBasis = $positions[0]->cost_basis / $positionShares;
        else {

        $orders = $alpaca->getOrders()->getResponse();
        if (!empty($orders)){
//Put code here to determine order status

        $clock = $alpaca->getClock()->getResponse();
        if (!empty($clock)){
        //    if (!empty($clock->is_open)){

        //  Not reliable for detecting open or closed market            
        //        $isOpen = $clock->is_open;
        //    }else{
        //        $isOpen = false;
        //    }
            if (!empty($clock->next_open)){
                $nextOpenDT=new DateTime($clock -> next_open);                
                $nextCloseDT=new DateTime($clock -> next_close);
            $nowDT = new DateTime();
            $isOpen = false;
            if ($nextOpenDT>$nextCloseDT) $isOpen = true;            
            $timeToOpen = $nowDT -> diff($nextOpenDT) -> format("%H:%I:%S");

        if ($isOpen){            

             * Insert your code to run formulas and whatnot...
            if ($price <= $lowRange && $accountCash>$price && !$activeOrder){
                $shares = floor($accountCash / $price);

                $alpaca->createOrder($symbol, $shares, "buy", "limit", "day", $price, null, null);

                echo $space."\nBuy ".$shares." shares". $space;
                $avgPriceAcc = 0;
                $avgPriceTick = 0;
                $refPrice = $price;

            if ($price >= $highRange && $positionShares>0 && !$activeOrder){
                $test=$alpaca->createOrder($symbol, $positionShares, "sell", "limit", "day", $price, null, null);
//              print_r($test);
                echo $space."\nSell " . $positionShares. " shares". $space;

                $avgPriceAcc = 0;
                $avgPriceTick = 0;                
                $refPrice = $price;

1 Like