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!
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.
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;
}
}
}
?>