Skip to content

Commit

Permalink
Implements Place Order (limit and market)
Browse files Browse the repository at this point in the history
  • Loading branch information
dve committed Jan 24, 2018
1 parent 3d322d1 commit f256378
Show file tree
Hide file tree
Showing 9 changed files with 648 additions and 0 deletions.
126 changes: 126 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
namespace DVE\CEXApiClient;

use DVE\CEXApiClient\ClientTool\ClientToolFactory;
use DVE\CEXApiClient\ConstantHelper\OrderType;
use DVE\CEXApiClient\Definition\Request\BalanceRequest;
use DVE\CEXApiClient\Definition\Request\OrderBookRequest;
use DVE\CEXApiClient\Definition\Request\PlaceLimitOrderRequest;
use DVE\CEXApiClient\Definition\Request\PlaceMarketOrderRequest;
use DVE\CEXApiClient\Definition\Request\RequestInterface;
use DVE\CEXApiClient\Definition\Request\Traits\SignatureTrait;
use DVE\CEXApiClient\Definition\Response\BalanceResponse;
use DVE\CEXApiClient\Definition\Response\OrderBookResponse;
use DVE\CEXApiClient\Definition\Response\PlaceLimitOrderResponse;
use DVE\CEXApiClient\Definition\Response\PlaceMarketOrderResponse;
use DVE\CEXApiClient\Definition\Response\Property\BalanceCurrencyProperty;
use DVE\CEXApiClient\Definition\Response\Property\OrderBookBidAskProperty;
use DVE\CEXApiClient\Exception\CexAPiClientResponseException;
use Shudrum\Component\ArrayFinder\ArrayFinder;

class Client
Expand Down Expand Up @@ -93,6 +99,12 @@ public function balance()
return $response;
}

/**
* @param string $symbol1
* @param string $symbol2
* @param int|null $depth
* @return OrderBookResponse
*/
public function orderBook(string $symbol1, string $symbol2, ?int $depth = null)
{
$orderBook = (new OrderBookRequest())
Expand Down Expand Up @@ -131,9 +143,119 @@ public function orderBook(string $symbol1, string $symbol2, ?int $depth = null)
return $response;
}

/**
* @param string $symbol1
* @param string $symbol2
* @param float $amount
* @param float $price
* @return PlaceLimitOrderResponse
*/
public function placeBuyLimitOrder(string $symbol1, string $symbol2, float $amount, float $price)
{
return $this->placeLimitOrder(OrderType::BUY, $symbol1, $symbol2, $amount, $price);
}

/**
* @param string $symbol1
* @param string $symbol2
* @param float $amount
* @param float $price
* @return PlaceLimitOrderResponse
*/
public function placeSellLimitOrder(string $symbol1, string $symbol2, float $amount, float $price)
{
return $this->placeLimitOrder(OrderType::SELL, $symbol1, $symbol2, $amount, $price);
}

/**
* @param string $type
* @param string $symbol1
* @param string $symbol2
* @param float $amount
* @param float $price
* @return PlaceLimitOrderResponse
*/
public function placeLimitOrder(string $type, string $symbol1, string $symbol2, float $amount, float $price)
{
$placeOrder = (new PlaceLimitOrderRequest())
->setPrice($price)
->setType($type)
->setAmount($amount)
->setSymbol1($symbol1)
->setSymbol2($symbol2)
;

$data = $this->request($placeOrder);

$response = (new PlaceLimitOrderResponse())
->setComplete((bool)$data->get('complete'))
->setAmount((float)$data->get('amount'))
->setPrice((float)$data->get('price'))
->setPending((float)$data->get('pending'))
->setId((int)$data->get('id'))
->setTime((float)($data->get('time')/1000))
->setType((string)$data->get('type'))
;

return $response;
}

/**
* @param string $symbol1
* @param string $symbol2
* @param float $amount
* @return PlaceMarketOrderResponse
*/
public function placeBuyMarketOrder(string $symbol1, string $symbol2, float $amount)
{
return $this->placeMarketOrder(OrderType::BUY, $symbol1, $symbol2, $amount);
}

/**
* @param string $symbol1
* @param string $symbol2
* @param float $amount
* @return PlaceMarketOrderResponse
*/
public function placeSellMarketOrder(string $symbol1, string $symbol2, float $amount)
{
return $this->placeMarketOrder(OrderType::SELL, $symbol1, $symbol2, $amount);
}

/**
* @param string $type
* @param string $symbol1
* @param string $symbol2
* @param float $amount
* @return PlaceMarketOrderResponse
*/
public function placeMarketOrder(string $type, string $symbol1, string $symbol2, float $amount)
{
$placeOrder = (new PlaceMarketOrderRequest())
->setType($type)
->setAmount($amount)
->setSymbol1($symbol1)
->setSymbol2($symbol2)
;

$data = $this->request($placeOrder);

$response = (new PlaceMarketOrderResponse())
->setSymbol1amount((float)$data->get('symbol1Amount'))
->setSymbol2amount((float)$data->get('symbol2Amount'))
->setMessage((string)$data->get('message'))
->setId((int)$data->get('id'))
->setTime((float)($data->get('time')/1000))
->setType((string)$data->get('type'))
;

return $response;
}

/**
* @param RequestInterface $request
* @return ArrayFinder
* @throws CexAPiClientResponseException
*/
private function request(RequestInterface $request): ArrayFinder
{
Expand Down Expand Up @@ -177,6 +299,10 @@ private function request(RequestInterface $request): ArrayFinder

$data = json_decode((string)$guzzleResponse->getBody(), true);

if(array_key_exists('error', $data)) {
throw new CexAPiClientResponseException($guzzleResponse);
}

return new ArrayFinder($data);
}
}
11 changes: 11 additions & 0 deletions src/ConstantHelper/OrderType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace DVE\CEXApiClient\ConstantHelper;

class OrderType
{
const BUY = 'buy';
const SELL = 'sell';
const MARKET = 'market';
const LIMIT = 'limit';
}
59 changes: 59 additions & 0 deletions src/Definition/Request/PlaceLimitOrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace DVE\CEXApiClient\Definition\Request;

class PlaceLimitOrderRequest extends PlaceOrderRequest
{
/**
* @var float
*/
private $price;

/**
* @return string
*/
public function getUri(): string
{
return '/place_order/'.$this->getSymbol1().'/'.$this->getSymbol2();
}

/**
* @return string
*/
public function getMethod(): string
{
return 'POST';
}

/**
* @return array
*/
public function getBodyParams(): array
{
return parent::getBodyParams() + [
'type' => $this->getType(),
'amount' => $this->getAmount(),
'price' => $this->price
];
}

/**
* @return float
*/
public function getPrice(): float
{
return $this->price;
}

/**
* @param float $price
* @return PlaceLimitOrderRequest
*/
public function setPrice(float $price): PlaceLimitOrderRequest
{
$this->price = $price;
return $this;
}


}
35 changes: 35 additions & 0 deletions src/Definition/Request/PlaceMarketOrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace DVE\CEXApiClient\Definition\Request;

class PlaceMarketOrderRequest extends PlaceOrderRequest
{
/**
* @return string
*/
public function getUri(): string
{
return '/place_order/'.$this->getSymbol1().'/'.$this->getSymbol2();
}

/**
* @return string
*/
public function getMethod(): string
{
return 'POST';
}

/**
* @return array
*/
public function getBodyParams(): array
{
return parent::getBodyParams() + [
'type' => $this->getType(),
'amount' => $this->getAmount(),
'order_type' => 'market'
];
}

}
Loading

0 comments on commit f256378

Please sign in to comment.