PHP example code for order placement through API

Hello

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: https://alpaca.markets/docs/api-documentation/how-to/orders/

Thank you very much.
Andreas

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!

-Chris

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.

-Chris

I used this API to create a PHP application to interface with Alpaca and do some test trades:
https://github.com/JeffreyHyer/alpaca-trade-api-php

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…

<?php
    echo "\n";
    $symbol="aapl";
    $percent=1.50;

    $url = "https://elite.finviz.com/quote.ashx?t=".$symbol;

    require './vendor/autoload.php';

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

    //change for real trading
    $alpaca->setPaper(true);

    $space= str_repeat(' ', 3);

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

    $avgPriceAcc = 0;
    $avgPriceTick = 0;

    $tickCount=0;
    $isOpen=false;
    
    $symbol=strtoupper($symbol);
    
    
    echo "\nSymbol: ".$symbol."\n";

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

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

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

        //$isOpen=false;
        $clock = $alpaca->getClock()->getResponse();
//print_r($clock);
        
        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;
                $activeOrder=true;
            }
  
 

            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;
                $activeOrder=true;
            }
        
        } 
    }

?>
1 Like