wip
This commit is contained in:
@@ -17,26 +17,40 @@ class AlphaVantageMarketData implements MarketDataInterface
|
||||
|
||||
public function quote($symbol): Collection
|
||||
{
|
||||
$fundamental = Alphavantage::fundamentals()->overview($symbol);
|
||||
$quote = Alphavantage::core()->quoteEndpoint($symbol);
|
||||
$quote = Arr::get($quote, 'Global Quote', []);
|
||||
|
||||
$fundamental = cache()->tags(['quote', 'alpha-vantage', $symbol])->remember(
|
||||
'symbol-'.$symbol,
|
||||
1440,
|
||||
function () use ($symbol) {
|
||||
return Alphavantage::fundamentals()->overview($symbol);
|
||||
}
|
||||
);
|
||||
|
||||
if (empty($fundamental)) return collect();
|
||||
|
||||
return collect([
|
||||
'name' => Arr::get($fundamental, 'Name'),
|
||||
'symbol' => Arr::get($fundamental, 'Symbol'),
|
||||
'market_value' => Arr::get($quote, 'Global Quote')['05. price'],
|
||||
'market_value' => Arr::get($quote, '05. price'),
|
||||
'fifty_two_week_high' => Arr::get($fundamental, '52WeekHigh'),
|
||||
'fifty_two_week_low' => Arr::get($fundamental, '52WeekLow'),
|
||||
'forward_pe' => Arr::get($fundamental, 'ForwardPE'),
|
||||
'trailing_pe' => Arr::get($fundamental, 'TrailingPE'),
|
||||
'market_cap' => Arr::get($fundamental, 'MarketCapitalization')
|
||||
'market_cap' => Arr::get($fundamental, 'MarketCapitalization'),
|
||||
'book_value' => Arr::get($fundamental, 'BookValue'),
|
||||
'last_dividend_date' => Arr::get($fundamental, 'DividendDate') != 'None'
|
||||
? Arr::get($fundamental, 'DividendDate')
|
||||
: null,
|
||||
'dividend_yield' => Arr::get($fundamental, 'DividendYield') != 'None'
|
||||
? Arr::get($fundamental, 'DividendYield')
|
||||
: null
|
||||
]);
|
||||
}
|
||||
|
||||
public function dividends($symbol, $startDate, $endDate): Collection
|
||||
{
|
||||
|
||||
$dividends = Alphavantage::fundamentals()->dividends($symbol);
|
||||
|
||||
return collect($dividends)
|
||||
|
||||
@@ -23,7 +23,10 @@ class FakeMarketData implements MarketDataInterface
|
||||
'fifty_two_week_low' => 341.20,
|
||||
'forward_pe' => 20.1,
|
||||
'trailing_pe' => 30.34,
|
||||
'market_cap' => 9800700600
|
||||
'market_cap' => 9800700600,
|
||||
'book_value' => 4.7,
|
||||
'last_dividend_date' => now()->subDays(45),
|
||||
'dividend_yield' => .033
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,10 @@ class YahooMarketData implements MarketDataInterface
|
||||
'fifty_two_week_low' => $quote->getFiftyTwoWeekLow(),
|
||||
'forward_pe' => $quote->getForwardPE(),
|
||||
'trailing_pe' => $quote->getTrailingPE(),
|
||||
'market_cap' => $quote->getMarketCap()
|
||||
'market_cap' => $quote->getMarketCap(),
|
||||
'book_value' => $quote->getBookValue(),
|
||||
'last_dividend_date' => $quote->getDividendDate(),
|
||||
'dividend_yield' => $quote->getTrailingAnnualDividendYield()
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
+41
-35
@@ -26,6 +26,18 @@ class Dividend extends Model
|
||||
'last_date' => 'datetime',
|
||||
];
|
||||
|
||||
public function marketData() {
|
||||
return $this->belongsTo(MarketData::class, 'symbol', 'symbol');
|
||||
}
|
||||
|
||||
public function holdings() {
|
||||
return $this->hasMany(Holding::class, 'symbol', 'symbol');
|
||||
}
|
||||
|
||||
public function transactions() {
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab new dividend data
|
||||
*
|
||||
@@ -48,7 +60,6 @@ class Dividend extends Model
|
||||
if ( $dividends_meta->total_dividends ) {
|
||||
|
||||
$start_date = $dividends_meta->last_date->addHours(48);
|
||||
$end_date = now();
|
||||
}
|
||||
|
||||
// get some data
|
||||
@@ -67,48 +78,43 @@ class Dividend extends Model
|
||||
(new self)->insert($dividend_data->toArray());
|
||||
|
||||
// sync to holdings
|
||||
$dividends = self::where([
|
||||
'dividends.symbol' => $dividend_data->last()->symbol,
|
||||
])->select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
||||
->selectRaw('@purchased:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "BUY" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `purchased`')
|
||||
->selectRaw('@sold:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "SELL" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `sold`')
|
||||
->selectRaw('@owned:=(@purchased - @sold) AS `owned`')
|
||||
->selectRaw('@dividends_received:=(@owned * dividends.dividend_amount) AS `dividends_received`')
|
||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
|
||||
->groupBy(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
||||
->get();
|
||||
self::syncHoldings($dividend_data);
|
||||
|
||||
// iterate through holdings and update
|
||||
Holding::where(['symbol' => $symbol])
|
||||
->get()
|
||||
->each(function ($holding) use ($dividends) {
|
||||
$holding->update([
|
||||
'dividends_earned' => $dividends->where('portfolio_id', $holding->portfolio_id)->sum('dividends_received')
|
||||
]);
|
||||
});
|
||||
|
||||
// sync most last dividend date in market data
|
||||
// sync last dividend amount to market data table
|
||||
$market_data = MarketData::symbol($symbol)->first();
|
||||
$dividend_data_latest_date = $dividend_data->sortByDesc('date')->first()['date'];
|
||||
|
||||
if ($market_data->dividend_date < $dividend_data_latest_date) {
|
||||
$market_data->update(['dividend_date' => $dividend_data_latest_date]); // why is this set to latest date?
|
||||
}
|
||||
$market_data->last_dividend_amount = $dividend_data->sortByDesc('date')->first()['amount'];
|
||||
$market_data->save();
|
||||
}
|
||||
|
||||
return $dividend_data;
|
||||
}
|
||||
|
||||
public function marketData() {
|
||||
return $this->belongsTo(MarketData::class, 'symbol', 'symbol');
|
||||
}
|
||||
public static function syncHoldings($dividend_data): void
|
||||
{
|
||||
$symbol = $dividend_data->last()->symbol;
|
||||
|
||||
public function holdings() {
|
||||
return $this->hasMany(Holding::class, 'symbol', 'symbol');
|
||||
}
|
||||
// group by holdings
|
||||
$dividends = self::where([
|
||||
'dividends.symbol' => $dividend_data->last()->symbol,
|
||||
])
|
||||
->select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
||||
->selectRaw('@purchased:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "BUY" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `purchased`')
|
||||
->selectRaw('@sold:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "SELL" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `sold`')
|
||||
->selectRaw('@owned:=(@purchased - @sold) AS `owned`')
|
||||
->selectRaw('@dividends_received:=(@owned * dividends.dividend_amount) AS `dividends_received`')
|
||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
|
||||
->groupBy(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
||||
->get();
|
||||
|
||||
public function transactions() {
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
||||
// iterate through holdings and update
|
||||
Holding::where(['symbol' => $symbol])
|
||||
->get()
|
||||
->each(function ($holding) use ($dividends) {
|
||||
$holding->update([
|
||||
'dividends_earned' => $dividends->where('portfolio_id', $holding->portfolio_id)
|
||||
->sum('dividends_received')
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,16 +22,10 @@ class MarketData extends Model
|
||||
'fifty_two_week_low',
|
||||
'forward_pe',
|
||||
'trailing_pe',
|
||||
'market_cap'
|
||||
];
|
||||
|
||||
protected $attributes = [
|
||||
'market_value' => 0,
|
||||
'fifty_two_week_high' => 0,
|
||||
'fifty_two_week_low' => 0,
|
||||
'forward_pe' => 0,
|
||||
'trailing_pe' => 0,
|
||||
'market_cap' => 0
|
||||
'market_cap',
|
||||
'book_value',
|
||||
'last_dividend_date',
|
||||
'dividend_yield'
|
||||
];
|
||||
|
||||
public function holdings()
|
||||
|
||||
Reference in New Issue
Block a user