adds finnhub market data provider
This commit is contained in:
@@ -15,7 +15,7 @@ class AlphaVantageMarketData implements MarketDataInterface
|
||||
return $this->quote($symbol)->isNotEmpty();
|
||||
}
|
||||
|
||||
public function quote($symbol): Collection
|
||||
public function quote(String $symbol): Collection
|
||||
{
|
||||
$quote = Alphavantage::core()->quoteEndpoint($symbol);
|
||||
$quote = Arr::get($quote, 'Global Quote', []);
|
||||
@@ -49,7 +49,7 @@ class AlphaVantageMarketData implements MarketDataInterface
|
||||
]);
|
||||
}
|
||||
|
||||
public function dividends($symbol, $startDate, $endDate): Collection
|
||||
public function dividends(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
$dividends = Alphavantage::fundamentals()->dividends($symbol);
|
||||
|
||||
@@ -67,7 +67,7 @@ class AlphaVantageMarketData implements MarketDataInterface
|
||||
});
|
||||
}
|
||||
|
||||
public function splits($symbol, $startDate, $endDate): Collection
|
||||
public function splits(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
$splits = Alphavantage::fundamentals()->splits($symbol);
|
||||
@@ -86,7 +86,7 @@ class AlphaVantageMarketData implements MarketDataInterface
|
||||
});
|
||||
}
|
||||
|
||||
public function history($symbol, $startDate, $endDate): Collection
|
||||
public function history(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
$history = Alphavantage::timeSeries()->daily($symbol, 'full');
|
||||
|
||||
@@ -13,7 +13,7 @@ class FakeMarketData implements MarketDataInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
public function quote($symbol): Collection
|
||||
public function quote(String $symbol): Collection
|
||||
{
|
||||
|
||||
return collect([
|
||||
@@ -31,7 +31,7 @@ class FakeMarketData implements MarketDataInterface
|
||||
]);
|
||||
}
|
||||
|
||||
public function dividends($symbol, $startDate, $endDate): Collection
|
||||
public function dividends(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
return collect([
|
||||
@@ -53,7 +53,7 @@ class FakeMarketData implements MarketDataInterface
|
||||
]);
|
||||
}
|
||||
|
||||
public function splits($symbol, $startDate, $endDate): Collection
|
||||
public function splits(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
return collect([
|
||||
@@ -65,7 +65,7 @@ class FakeMarketData implements MarketDataInterface
|
||||
]);
|
||||
}
|
||||
|
||||
public function history($symbol, $startDate, $endDate): Collection
|
||||
public function history(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
$numDays = Carbon::parse($startDate)->diffInDays($endDate, true);
|
||||
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Interfaces\MarketData;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class FinnhubMarketData implements MarketDataInterface
|
||||
{
|
||||
public \Finnhub\Api\DefaultApi $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
$this->client = new \Finnhub\Api\DefaultApi(
|
||||
new \GuzzleHttp\Client(),
|
||||
\Finnhub\Configuration::getDefaultConfiguration()->setApiKey('token', config('finnhub.key'))
|
||||
);
|
||||
}
|
||||
public function exists(String $symbol): Bool
|
||||
{
|
||||
|
||||
return $this->quote($symbol)->isNotEmpty();
|
||||
}
|
||||
|
||||
public function quote($symbol): Collection
|
||||
{
|
||||
|
||||
|
||||
$quote = $this->client->quote($symbol);
|
||||
|
||||
$fundamental = cache()->tags(['quote', 'finnhub', $symbol])->remember(
|
||||
'symbol-'.$symbol,
|
||||
1440,
|
||||
function () use ($symbol) {
|
||||
return $this->client->companyBasicFinancials($symbol, "all");
|
||||
}
|
||||
);
|
||||
|
||||
if (empty($fundamental)) return collect();
|
||||
|
||||
return collect([
|
||||
'name' => Arr::get($fundamental, 'metric.name'),
|
||||
'symbol' => $symbol,
|
||||
'market_value' => Arr::get($quote, 'c'),
|
||||
'fifty_two_week_high' => Arr::get($fundamental, 'metric.52WeekHigh'),
|
||||
'fifty_two_week_low' => Arr::get($fundamental, 'metric.52WeekLow'),
|
||||
'forward_pe' => Arr::get($fundamental, 'metric.forwardPE'), // confirm
|
||||
'trailing_pe' => Arr::get($fundamental, 'metric.trailingPE'), // confirm
|
||||
'market_cap' => Arr::get($fundamental, 'metric.marketCapitalization'), // confirm
|
||||
'book_value' => Arr::get($fundamental, 'metric.bookValuePerShare'), // confirm
|
||||
'last_dividend_date' => Arr::get($fundamental, 'metric.lastDivDate'), // confirm
|
||||
'dividend_yield' => Arr::get($fundamental, 'metric.dividendYield'), // confirm
|
||||
]);
|
||||
}
|
||||
|
||||
public function dividends($symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
$dividends = $this->client->stockDividends($symbol, $startDate->format('Y-m-d'), $endDate->format('Y-m-d'));
|
||||
|
||||
return collect($dividends)->map(function($dividend) use ($symbol) {
|
||||
|
||||
return [
|
||||
'symbol' => $symbol,
|
||||
'date' => Carbon::parse(Arr::get($dividend, 'date'))
|
||||
->format('Y-m-d H:i:s'),
|
||||
'dividend_amount' => Arr::get($dividend, 'amount'),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function splits($symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
$splits = $this->client->stockSplits($symbol, $startDate->format('Y-m-d'), $endDate->format('Y-m-d'));
|
||||
|
||||
return collect($splits)->map(function($split) use ($symbol) {
|
||||
|
||||
return [
|
||||
'symbol' => $symbol,
|
||||
'date' => Carbon::parse(Arr::get($split, 'date'))
|
||||
->format('Y-m-d H:i:s'),
|
||||
'split_amount' => Arr::get($split, 'toFactor') / Arr::get($split, 'fromFactor'),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function history($symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
$history = $this->client->stockCandles($symbol, "D", $startDate->timestamp, $endDate->timestamp);
|
||||
|
||||
$timestamps = Arr::get($history, 't', []);
|
||||
$closes = Arr::get($history, 'c', []);
|
||||
|
||||
return collect($timestamps)->mapWithKeys(function ($timestamp, $index) use ($symbol, $closes) {
|
||||
$date = Carbon::createFromTimestamp($timestamp)->format('Y-m-d');
|
||||
return [ $date => [
|
||||
'symbol' => $symbol,
|
||||
'date' => $date,
|
||||
'close' => (float) $closes[$index],
|
||||
]];
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ class YahooMarketData implements MarketDataInterface
|
||||
return $this->quote($symbol)->isNotEmpty();
|
||||
}
|
||||
|
||||
public function quote($symbol): Collection
|
||||
public function quote(String $symbol): Collection
|
||||
{
|
||||
|
||||
$quote = $this->client->getQuote($symbol);
|
||||
@@ -44,7 +44,7 @@ class YahooMarketData implements MarketDataInterface
|
||||
]);
|
||||
}
|
||||
|
||||
public function dividends($symbol, $startDate, $endDate): Collection
|
||||
public function dividends(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
return collect($this->client->getHistoricalDividendData($symbol, $startDate, $endDate))
|
||||
@@ -58,7 +58,7 @@ class YahooMarketData implements MarketDataInterface
|
||||
});
|
||||
}
|
||||
|
||||
public function splits($symbol, $startDate, $endDate): Collection
|
||||
public function splits(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
return collect($this->client->getHistoricalSplitData($symbol, $startDate, $endDate))
|
||||
@@ -73,7 +73,7 @@ class YahooMarketData implements MarketDataInterface
|
||||
});
|
||||
}
|
||||
|
||||
public function history($symbol, $startDate, $endDate): Collection
|
||||
public function history(String $symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
return collect($this->client->getHistoricalQuoteData($symbol, ApiClient::INTERVAL_1_DAY, $startDate, $endDate))
|
||||
|
||||
Reference in New Issue
Block a user