wip
This commit is contained in:
@@ -56,6 +56,8 @@ MAIL_ENCRYPTION=null
|
|||||||
MAIL_FROM_ADDRESS="hello@example.com"
|
MAIL_FROM_ADDRESS="hello@example.com"
|
||||||
MAIL_FROM_NAME="${APP_NAME}"
|
MAIL_FROM_NAME="${APP_NAME}"
|
||||||
|
|
||||||
|
MARKET_DATA_PROVIDER=yahoo
|
||||||
|
MARKET_DATA_REFRESH=30
|
||||||
ALPHAVANTAGE_API_KEY=
|
ALPHAVANTAGE_API_KEY=
|
||||||
|
|
||||||
AWS_ACCESS_KEY_ID=
|
AWS_ACCESS_KEY_ID=
|
||||||
|
|||||||
@@ -17,26 +17,40 @@ class AlphaVantageMarketData implements MarketDataInterface
|
|||||||
|
|
||||||
public function quote($symbol): Collection
|
public function quote($symbol): Collection
|
||||||
{
|
{
|
||||||
$fundamental = Alphavantage::fundamentals()->overview($symbol);
|
|
||||||
$quote = Alphavantage::core()->quoteEndpoint($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();
|
if (empty($fundamental)) return collect();
|
||||||
|
|
||||||
return collect([
|
return collect([
|
||||||
'name' => Arr::get($fundamental, 'Name'),
|
'name' => Arr::get($fundamental, 'Name'),
|
||||||
'symbol' => Arr::get($fundamental, 'Symbol'),
|
'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_high' => Arr::get($fundamental, '52WeekHigh'),
|
||||||
'fifty_two_week_low' => Arr::get($fundamental, '52WeekLow'),
|
'fifty_two_week_low' => Arr::get($fundamental, '52WeekLow'),
|
||||||
'forward_pe' => Arr::get($fundamental, 'ForwardPE'),
|
'forward_pe' => Arr::get($fundamental, 'ForwardPE'),
|
||||||
'trailing_pe' => Arr::get($fundamental, 'TrailingPE'),
|
'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
|
public function dividends($symbol, $startDate, $endDate): Collection
|
||||||
{
|
{
|
||||||
|
|
||||||
$dividends = Alphavantage::fundamentals()->dividends($symbol);
|
$dividends = Alphavantage::fundamentals()->dividends($symbol);
|
||||||
|
|
||||||
return collect($dividends)
|
return collect($dividends)
|
||||||
|
|||||||
@@ -23,7 +23,10 @@ class FakeMarketData implements MarketDataInterface
|
|||||||
'fifty_two_week_low' => 341.20,
|
'fifty_two_week_low' => 341.20,
|
||||||
'forward_pe' => 20.1,
|
'forward_pe' => 20.1,
|
||||||
'trailing_pe' => 30.34,
|
'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(),
|
'fifty_two_week_low' => $quote->getFiftyTwoWeekLow(),
|
||||||
'forward_pe' => $quote->getForwardPE(),
|
'forward_pe' => $quote->getForwardPE(),
|
||||||
'trailing_pe' => $quote->getTrailingPE(),
|
'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',
|
'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
|
* Grab new dividend data
|
||||||
*
|
*
|
||||||
@@ -48,7 +60,6 @@ class Dividend extends Model
|
|||||||
if ( $dividends_meta->total_dividends ) {
|
if ( $dividends_meta->total_dividends ) {
|
||||||
|
|
||||||
$start_date = $dividends_meta->last_date->addHours(48);
|
$start_date = $dividends_meta->last_date->addHours(48);
|
||||||
$end_date = now();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get some data
|
// get some data
|
||||||
@@ -67,48 +78,43 @@ class Dividend extends Model
|
|||||||
(new self)->insert($dividend_data->toArray());
|
(new self)->insert($dividend_data->toArray());
|
||||||
|
|
||||||
// sync to holdings
|
// sync to holdings
|
||||||
$dividends = self::where([
|
self::syncHoldings($dividend_data);
|
||||||
'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();
|
|
||||||
|
|
||||||
// iterate through holdings and update
|
// sync last dividend amount to market data table
|
||||||
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
|
|
||||||
$market_data = MarketData::symbol($symbol)->first();
|
$market_data = MarketData::symbol($symbol)->first();
|
||||||
$dividend_data_latest_date = $dividend_data->sortByDesc('date')->first()['date'];
|
$market_data->last_dividend_amount = $dividend_data->sortByDesc('date')->first()['amount'];
|
||||||
|
$market_data->save();
|
||||||
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?
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dividend_data;
|
return $dividend_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function marketData() {
|
public static function syncHoldings($dividend_data): void
|
||||||
return $this->belongsTo(MarketData::class, 'symbol', 'symbol');
|
{
|
||||||
}
|
$symbol = $dividend_data->last()->symbol;
|
||||||
|
|
||||||
public function holdings() {
|
// group by holdings
|
||||||
return $this->hasMany(Holding::class, 'symbol', 'symbol');
|
$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() {
|
// iterate through holdings and update
|
||||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
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',
|
'fifty_two_week_low',
|
||||||
'forward_pe',
|
'forward_pe',
|
||||||
'trailing_pe',
|
'trailing_pe',
|
||||||
'market_cap'
|
'market_cap',
|
||||||
];
|
'book_value',
|
||||||
|
'last_dividend_date',
|
||||||
protected $attributes = [
|
'dividend_yield'
|
||||||
'market_value' => 0,
|
|
||||||
'fifty_two_week_high' => 0,
|
|
||||||
'fifty_two_week_low' => 0,
|
|
||||||
'forward_pe' => 0,
|
|
||||||
'trailing_pe' => 0,
|
|
||||||
'market_cap' => 0
|
|
||||||
];
|
];
|
||||||
|
|
||||||
public function holdings()
|
public function holdings()
|
||||||
|
|||||||
+1
-1
@@ -17,7 +17,7 @@
|
|||||||
"robsontenorio/mary": "^1.35",
|
"robsontenorio/mary": "^1.35",
|
||||||
"scheb/yahoo-finance-api": "^4.10",
|
"scheb/yahoo-finance-api": "^4.10",
|
||||||
"staudenmeir/eloquent-has-many-deep": "^1.20",
|
"staudenmeir/eloquent-has-many-deep": "^1.20",
|
||||||
"tschucki/alphavantage-laravel": "^0.0.1"
|
"tschucki/alphavantage-laravel": "^0.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"fakerphp/faker": "^1.23",
|
"fakerphp/faker": "^1.23",
|
||||||
|
|||||||
Generated
+7
-7
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "47988869f65dbf94a6228627c7806e1f",
|
"content-hash": "40816463478b2d00c6a7c18e71aa7e7c",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "bacon/bacon-qr-code",
|
"name": "bacon/bacon-qr-code",
|
||||||
@@ -7369,16 +7369,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "tschucki/alphavantage-laravel",
|
"name": "tschucki/alphavantage-laravel",
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/Tschucki/alphavantage-laravel.git",
|
"url": "https://github.com/Tschucki/alphavantage-laravel.git",
|
||||||
"reference": "409c78c123ed40a150d8ea6ac095ab36dc077f82"
|
"reference": "1317dab3b0b3b3866c974816ef31e745a888e596"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/Tschucki/alphavantage-laravel/zipball/409c78c123ed40a150d8ea6ac095ab36dc077f82",
|
"url": "https://api.github.com/repos/Tschucki/alphavantage-laravel/zipball/1317dab3b0b3b3866c974816ef31e745a888e596",
|
||||||
"reference": "409c78c123ed40a150d8ea6ac095ab36dc077f82",
|
"reference": "1317dab3b0b3b3866c974816ef31e745a888e596",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -7436,7 +7436,7 @@
|
|||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/Tschucki/alphavantage-laravel/issues",
|
"issues": "https://github.com/Tschucki/alphavantage-laravel/issues",
|
||||||
"source": "https://github.com/Tschucki/alphavantage-laravel/tree/0.0.1"
|
"source": "https://github.com/Tschucki/alphavantage-laravel/tree/0.0.2"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -7444,7 +7444,7 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2024-07-14T17:31:03+00:00"
|
"time": "2024-09-01T11:41:46+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "vlucas/phpdotenv",
|
"name": "vlucas/phpdotenv",
|
||||||
|
|||||||
@@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
'refresh' => 30, // minutes
|
'refresh' => env('MARKET_DATA_REFRESH', 30), // minutes
|
||||||
|
|
||||||
'default' => env('MARKET_DATA_PROVIDER', 'yahoo'),
|
'default' => env('MARKET_DATA_PROVIDER', 'yahoo'),
|
||||||
|
|
||||||
'yahoo' => App\Interfaces\MarketData\YahooMarketData::class,
|
'yahoo' => App\Interfaces\MarketData\YahooMarketData::class,
|
||||||
// 'fake' => App\Interfaces\MarketData\FakeMarketData::class,
|
'alphavantage' => App\Interfaces\MarketData\AlphaVantageMarketData::class,
|
||||||
|
'fake' => App\Interfaces\MarketData\FakeMarketData::class,
|
||||||
];
|
];
|
||||||
@@ -21,10 +21,11 @@ class CreateMarketDataTable extends Migration
|
|||||||
$table->float('fifty_two_week_high', 12, 4)->nullable();
|
$table->float('fifty_two_week_high', 12, 4)->nullable();
|
||||||
$table->timestamp('last_dividend_date')->nullable();
|
$table->timestamp('last_dividend_date')->nullable();
|
||||||
$table->float('last_dividend_amount', 12, 4)->nullable();
|
$table->float('last_dividend_amount', 12, 4)->nullable();
|
||||||
|
$table->float('dividend_yield', 12, 4)->nullable();
|
||||||
$table->unsignedBigInteger('market_cap')->nullable();
|
$table->unsignedBigInteger('market_cap')->nullable();
|
||||||
$table->float('trailing_pe', 12, 4)->nullable();
|
$table->float('trailing_pe', 12, 4)->nullable();
|
||||||
$table->float('forward_pe', 12, 4)->nullable();
|
$table->float('forward_pe', 12, 4)->nullable();
|
||||||
$table->float('pe_growth', 12, 4)->nullable();
|
$table->float('book_value', 12, 4)->nullable();
|
||||||
$table->json('meta_data')->nullable();
|
$table->json('meta_data')->nullable();
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Scheb\YahooFinanceApi\ApiClient;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use App\Http\Controllers\HoldingController;
|
use App\Http\Controllers\HoldingController;
|
||||||
|
use Scheb\YahooFinanceApi\ApiClientFactory;
|
||||||
use App\Http\Controllers\DashboardController;
|
use App\Http\Controllers\DashboardController;
|
||||||
use App\Http\Controllers\PortfolioController;
|
use App\Http\Controllers\PortfolioController;
|
||||||
use App\Http\Controllers\TransactionController;
|
use App\Http\Controllers\TransactionController;
|
||||||
|
use Tschucki\Alphavantage\Facades\Alphavantage;
|
||||||
use Laravel\Jetstream\Http\Controllers\Livewire\PrivacyPolicyController;
|
use Laravel\Jetstream\Http\Controllers\Livewire\PrivacyPolicyController;
|
||||||
use Laravel\Jetstream\Http\Controllers\Livewire\TermsOfServiceController;
|
use Laravel\Jetstream\Http\Controllers\Livewire\TermsOfServiceController;
|
||||||
|
|
||||||
@@ -14,6 +18,33 @@ Route::get('/', function () {
|
|||||||
|
|
||||||
Route::get('/test', function () {
|
Route::get('/test', function () {
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
|
return Alphavantage::fundamentals()->overview('TSLA');
|
||||||
|
|
||||||
|
$quote = Alphavantage::core()->quoteEndpoint('FFRHX');
|
||||||
|
|
||||||
|
$quote = Arr::get($quote, 'Global Quote', []);
|
||||||
|
|
||||||
|
|
||||||
|
return $quote;
|
||||||
|
|
||||||
|
$client = ApiClientFactory::createApiClient();
|
||||||
|
|
||||||
|
return $client->getQuote("IBM");
|
||||||
|
|
||||||
|
return $client->getHistoricalQuoteData(
|
||||||
|
"AAPL",
|
||||||
|
ApiClient::INTERVAL_1_DAY,
|
||||||
|
new \DateTime("-14 days"),
|
||||||
|
new \DateTime("today")
|
||||||
|
);
|
||||||
|
|
||||||
|
return $client->getHistoricalDividendData(
|
||||||
|
"AAPL",
|
||||||
|
new \DateTime("-5 years"),
|
||||||
|
new \DateTime("today")
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::middleware(['auth:sanctum', config('jetstream.auth_session'), 'verified'])->group(function () {
|
Route::middleware(['auth:sanctum', config('jetstream.auth_session'), 'verified'])->group(function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user