Skip to content

Instantly share code, notes, and snippets.

@MattFaus
Created March 2, 2014 02:24
Show Gist options
  • Save MattFaus/9300937 to your computer and use it in GitHub Desktop.
Save MattFaus/9300937 to your computer and use it in GitHub Desktop.
public function checkBuyOrders($currentBuyPrice) {
$sqlWhere = Array('status' => 'ACTIVE', 'action' => 'BUY', "$currentBuyPrice <= `at_price`");
foreach ($this->context->orders->findAll()->where($sqlWhere) as $order) { //double check the price for each before buying
try {
$userAssociatedWithOrder = $this->context->authenticator->getUser($order->user_id);
if(!empty($order->at_price) && !empty($order->amount) && $userAssociatedWithOrder->coinbase_access_token){ //his Coinbase API tokens are set
$totalBuyPrice = $this->context->coinbase->user($order->user_id)->order($order)->getBuyPrice($order->amount);
if ($totalBuyPrice !== NULL && $totalBuyPrice->subtotal->amount <= $order->at_price * $order->amount) {
$result = $this->context->coinbase->user($order->user_id)->order($order)->buy($order->amount); //Buy the coins
$this->context->orders->findAll()->get($order->order_id)->update(Array('status' => 'EXECUTED')); //Update order status
new SendEmail($userAssociatedWithOrder->email, 'You just bought Bitcoin using limit order on Coinbase!', 'Hi there!<br/><br/>The system just executed your order to buy Bitcoin. You can check the details at <a href="http://coinbaseorders.com/">http://coinbaseorders.com/</a>.<br/><br/>Coinbase Orders is a free service. Please consider a small donation, others have donated too. The donation address is 13ejFczTyMsdZQHkrfVEfiGY8RLD2rDs9i, alternatively <a href="http://coinbaseorders.com/homepage/donate">click here to get donation QR code</a>.<br/><br/>I appriciate your help!<br/><br/>Tom');
}
}
} catch (Exception $e) {
// Do not let one order execution interfere with other order executions
\Nette\Diagnostics\Debugger::log($e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment