Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
Added the alpaca positions
Browse files Browse the repository at this point in the history
  • Loading branch information
timothymarois committed Jan 13, 2020
1 parent 0352893 commit 11e3225
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Change Log
==========

### 01/13/2020 - 1.3
* Added `positions()` class with access to Alpaca positions endpoints.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,54 @@ $alpaca->orders()->replace('ORDER_ID',[
]);
```

**[Get Position](https://docs.alpaca.markets/api-documentation/api-v2/orders/#get-open-positions)**: Get an open position

```php
$position = $alpaca->positions()->get('SYMBOL');
```

**[Get All Positions](https://docs.alpaca.markets/api-documentation/api-v2/orders/#get-an-open-position)**: Get all open positions

```php
$positions = $alpaca->positions()->getAll();
```

**Position Entity Response:**

```json
{
"asset_id": "904837e3-3b76-47ec-b432-046db621571b",
"symbol": "AAPL",
"exchange": "NASDAQ",
"asset_class": "us_equity",
"avg_entry_price": "100.0",
"qty": "5",
"side": "long",
"market_value": "600.0",
"cost_basis": "500.0",
"unrealized_pl": "100.0",
"unrealized_plpc": "0.20",
"unrealized_intraday_pl": "10.0",
"unrealized_intraday_plpc": "0.0084",
"current_price": "120.0",
"lastday_price": "119.0",
"change_today": "0.0084"
}
```

**[Close a Position](https://docs.alpaca.markets/api-documentation/api-v2/orders/#close-a-position)**: Close a position

```php
$alpaca->positions()->close('SYMBOL');
```

**[Close All Positions](https://docs.alpaca.markets/api-documentation/api-v2/orders/#close-a-position)**: Close all open positions

```php
$alpaca->positions()->closeAll();
```


**[Get Activity](https://docs.alpaca.markets/api-documentation/api-v2/account-activities/)**: Get the account activity, like order fills, dividends etc.

```php
Expand Down
61 changes: 61 additions & 0 deletions src/Account/Positions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php namespace Alpaca\Account;

class Positions
{

/**
* Response array
*
* @var Alpaca\Alpaca
*/
private $alpaca;

/**
* __construct
*
*/
public function __construct(\Alpaca\Alpaca $alpaca)
{
$this->alpaca = $alpaca;
}

/**
* get()
*
* @return array
*/
public function get($stock)
{
return $this->alpaca->request('position',['stock'=>$stock])->results();
}

/**
* getAll()
*
* @return array
*/
public function getAll()
{
return $this->alpaca->request('positions',[])->results();
}

/**
* close()
*
* @return array
*/
public function close($stock)
{
return $this->alpaca->request('position',['stock'=>$stock],'DELETE')->results();
}

/**
* closeAll()
*
* @return array
*/
public function closeAll()
{
return $this->alpaca->request('positions',[],'DELETE')->results();
}
}
21 changes: 21 additions & 0 deletions src/Alpaca.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class Alpaca
*/
private $orders;

/**
* positions
*
* @var Alpaca\Account\Positions
*/
private $positions;

/**
* activity
*
Expand Down Expand Up @@ -165,6 +172,20 @@ public function orders()
return ($this->orders = (new Orders($this)));
}

/**
* positions()
*
* @return Alpaca\Account\Positions
*/
public function positions()
{
if ($this->positions) {
return $this->positions;
}

return ($this->positions = (new Positions($this)));
}

/**
* activity()
*
Expand Down

0 comments on commit 11e3225

Please sign in to comment.