Compare commits

..

16 Commits

Author SHA1 Message Date
hackerESQ c691ee922a wip test 2025-04-12 21:25:00 -05:00
hackerESQ 3eb9bad840 www 2025-04-12 20:39:12 -05:00
hackerESQ 370f7bb54b wip 2025-04-12 20:31:49 -05:00
hackerESQ 62bf6797e6 wip 2025-04-12 20:29:28 -05:00
hackerESQ c4e3645145 wip 2025-04-12 20:27:06 -05:00
hackerESQ 69e4d0fb3a wip 2025-04-12 20:22:43 -05:00
hackerESQ 20c2cb37cc wip 2025-04-12 20:16:34 -05:00
hackerESQ d2bb065822 wip 2025-04-12 20:11:05 -05:00
hackerESQ 0c00f28d97 wip 2025-04-12 20:06:21 -05:00
hackerESQ 5eab00ee33 wip 2025-04-12 20:03:40 -05:00
hackerESQ 56064ad84e try again 2025-04-12 18:02:23 -05:00
hackerESQ c96ff0e45f wip 2025-04-12 17:51:13 -05:00
hackerESQ 33e0df5ae2 wip 2025-04-12 17:37:26 -05:00
hackerESQ a5a333f784 wip 2025-04-12 17:31:28 -05:00
hackerESQ 89b5505e1d wip 2025-04-12 17:27:38 -05:00
hackerESQ 60923b3c93 wip 2025-04-12 17:16:36 -05:00
47 changed files with 2294 additions and 1981 deletions
-3
View File
@@ -24,9 +24,6 @@ OPENAI_ORGANIZATION=
MARKET_DATA_PROVIDER=yahoo
ALPHAVANTAGE_API_KEY=
FINNHUB_API_KEY=
ALPACA_API_KEY=
ALPACA_API_SECRET=
TWELVEDATA_API_SECRET=
# Cadence to refresh market data (in minutes)
MARKET_DATA_REFRESH=30
+6 -14
View File
@@ -43,16 +43,7 @@ jobs:
- name: Extract version from tag
id: extract-version
run: |
VERSION="${GITHUB_REF_NAME#v}"
TAGS="investbrainapp/investbrain:${VERSION},ghcr.io/investbrainapp/investbrain:${VERSION}"
# Conditionally add 'latest' tags unless 'pre-release' is in the version
if [[ "${GITHUB_REF_NAME}" != *alpha* && "${GITHUB_REF_NAME}" != *beta* && "${GITHUB_REF_NAME}" != *rc* ]]; then
TAGS="$TAGS,investbrainapp/investbrain:latest,ghcr.io/investbrainapp/investbrain:latest"
fi
echo "tags=$TAGS" >> $GITHUB_OUTPUT
echo "version=${GITHUB_REF_NAME#v}" >> $GITHUB_ENV
- name: Build and push
uses: docker/build-push-action@v6
@@ -60,7 +51,8 @@ jobs:
platforms: linux/amd64,linux/arm64
file: ./docker/Dockerfile
push: true
tags: ${{ steps.extract-version.outputs.tags }}
build-args: |
VERSION=${{ github.ref_name }}
tags: |
investbrainapp/investbrain:latest
investbrainapp/investbrain:${{ env.version }}
ghcr.io/investbrainapp/investbrain:latest
ghcr.io/investbrainapp/investbrain:${{ env.version }}
+3 -6
View File
@@ -28,7 +28,7 @@ Investbrain is a smart open-source investment tracker that helps you manage, tra
## Under the hood
Investbrain is a Laravel PHP web application that leverages Livewire and Tailwind for its frontend. Most databases should work, including MySQL and SQLite. Out of the box, we feature many market data providers. But we also offer an extensible market data provider interface for intrepid developers to create their own! We also offer integrations with OpenAI and Ollama for our ["chat with your holdings"](#chat-with-your-holdings) capability. Finally, of course we have robust support for i18n, a11y, and dark mode.
Investbrain is a Laravel PHP web application that leverages Livewire and Tailwind for its frontend. Most databases should work, including MySQL and SQLite. Out of the box, we feature three market data providers: [Yahoo Finance](https://finance.yahoo.com/), [Finnhub](https://finnhub.io/pricing-stock-api-market-data), and [Alpha Vantage](https://www.alphavantage.co/support/). But we also offer an extensible market data provider interface for intrepid developers to create their own! We also offer integrations with OpenAI and Ollama for our ["chat with your holdings"](#chat-with-your-holdings) capability. Finally, of course we have robust support for i18n, a11y, and dark mode.
## Self hosting
@@ -74,7 +74,7 @@ Always keep in mind the limitations of LLMs. When in doubt, consult a licensed i
## Market data providers
Investbrain includes an extensible market data provider interface that allows you to retrieve stock market data from multiple providers, such as [Yahoo Finance](https://finance.yahoo.com/), [Twelve Data](https://twelvedata.com), [Finnhub](https://finnhub.io/pricing-stock-api-market-data), [Alpaca](https://alpaca.markets/), and [Alpha Vantage](https://www.alphavantage.co/support/). The interface includes a built-in fallback mechanism to ensure reliable data access, even if a provider fails.
Investbrain includes an extensible market data provider interface that allows you to retrieve stock market data from multiple providers, such as Yahoo Finance, Alpha Vantage, or Finnhub. The interface includes a built-in fallback mechanism to ensure reliable data access, even if a provider fails.
### Configuration
@@ -138,12 +138,9 @@ There are several optional configurations available when installing using the re
| APP_URL | The URL where your Investbrain installation will be accessible | http://localhost |
| APP_PORT | The HTTP port exposed by the NGINX container | 8000 |
| APP_KEY | Must be set during install - encryption key for various security-related functions | `null` |
| MARKET_DATA_PROVIDER | The market data provider to use (either `yahoo`, `twelvedata`, `alphavantage`, `alpaca`, or `finnhub`) | yahoo |
| MARKET_DATA_PROVIDER | The market data provider to use (either `yahoo`, `alphavantage`, or `finnhub`) | yahoo |
| ALPHAVANTAGE_API_KEY | If using the Alpha Vantage provider | `null` |
| FINNHUB_API_KEY | If using the Finnhub provider | `null` |
| ALPACA_API_KEY | If using the Alpaca provider | `null` |
| ALPACA_API_SECRET | If using the Alpaca provider | `null` |
| TWELVEDATA_API_SECRET | If using the Twelve Data provider | `null` |
| MARKET_DATA_REFRESH | Cadence to refresh market data in minutes | 30 |
| APP_TIMEZONE | Timezone for the application, including daily change captures | UTC |
| AI_CHAT_ENABLED | Whether to enable AI chat features | `false` |
+2 -8
View File
@@ -14,18 +14,12 @@ class EnsureCostBasisAddedToSale
// cost basis is required for sales to calculate realized gains
if ($model->transaction_type == 'SELL') {
$cost_basis = Transaction::where([
$average_cost_basis = Transaction::where([
'portfolio_id' => $model->portfolio_id,
'symbol' => $model->symbol,
'transaction_type' => 'BUY',
])->whereDate('date', '<=', $model->date)
->selectRaw('SUM(transactions.cost_basis * transactions.quantity) as total_cost_basis')
->selectRaw('SUM(transactions.quantity) as total_quantity')
->first();
$average_cost_basis = empty($cost_basis->total_quantity)
? 0
: $cost_basis->total_cost_basis / $cost_basis->total_quantity;
->average('cost_basis');
$model->cost_basis = $average_cost_basis ?? 0;
}
+8 -1
View File
@@ -49,9 +49,16 @@ class CaptureDailyChange extends Command
->portfolio($portfolio->id)
->getPortfolioMetrics(config('investbrain.base_currency'));
$total_cost_basis = $metrics->get('total_cost_basis');
$total_market_value = $metrics->get('total_market_value');
$portfolio->daily_change()->create([
'date' => now(),
'total_market_value' => $metrics->get('total_market_value'),
'total_market_value' => $total_market_value,
'total_cost_basis' => $total_cost_basis,
'total_gain' => $total_market_value - $total_cost_basis,
'total_dividends_earned' => $metrics->get('total_dividends_earned'),
'realized_gains' => $metrics->get('realized_gain_dollars'),
]);
});
}
+2 -1
View File
@@ -7,6 +7,7 @@ namespace App\Console\Commands;
use App\Models\Holding;
use App\Models\MarketData;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class RefreshMarketData extends Command
{
@@ -60,7 +61,7 @@ class RefreshMarketData extends Command
try {
MarketData::getMarketData($holding->symbol, $force);
} catch (\Throwable $e) {
$this->line('Could not refresh '.$holding->symbol.' ('.$e->getMessage().')');
Log::error('Could not refresh '.$holding->symbol.' ('.$e->getMessage().')');
}
}
}
+3 -4
View File
@@ -47,13 +47,12 @@ class ConfigSheet implements FromCollection, WithHeadings, WithTitle
]);
// reinvested holdings
$reinvested_holdings = Holding::myHoldings()->where('reinvest_dividends', true)->get(['portfolio_id', 'symbol']);
if ($reinvested_holdings->isNotEmpty()) {
Holding::myHoldings()->where('reinvest_dividends', true)->get()->each(function ($holding) use (&$configs) {
$configs->push([
'key' => 'reinvested_dividends',
'value' => $reinvested_holdings->toJson(),
'value' => $holding->id,
]);
}
});
return $configs;
}
+2 -1
View File
@@ -22,8 +22,9 @@ class DailyChangesSheet implements FromCollection, WithHeadings, WithTitle
'Portfolio ID',
'Total Market Value',
'Total Cost Basis',
'Realized Gains',
'Total Gain',
'Total Dividends Earned',
'Realized Gains',
'Annotation',
];
}
@@ -5,10 +5,11 @@ declare(strict_types=1);
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Number;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Events\LocaleUpdated;
class LocalizationMiddleware
{
@@ -23,12 +24,14 @@ class LocalizationMiddleware
$locale = auth()->user()->getLocale();
app()->setLocale(Str::before($locale, '_'));
config(['app.locale' => $locale]);
app('translator')->setLocale(Str::before($locale, '_'));
app('events')->dispatch(new LocaleUpdated($locale));
Number::useLocale($locale);
Number::useCurrency(auth()->user()->getCurrency());
}
return $next($request);
}
}
+5 -11
View File
@@ -54,17 +54,11 @@ class ConfigSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadi
$this->backupImport->user->save();
break;
case 'reinvested_dividends':
if (json_validate($config['value'])) {
foreach (json_decode($config['value'], true) as $reinvest) {
Holding::myHoldings($this->backupImport->user->id)
->where('portfolio_id', $reinvest['portfolio_id'])
->where('symbol', $reinvest['symbol'])
->update([
'reinvest_dividends' => true,
]);
}
}
case 'reinvest_dividends':
Holding::myHoldings()->where('id', $config['value'])->update([
'reinvest_dividends' => true,
]);
break;
default:
+1 -1
View File
@@ -48,7 +48,7 @@ class TransactionsSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
// if has any transactions not in base currency, need to sync timeseries conversion rates
if ($transactions->where('currency', '!=', config('investbrain.base_currency'))->isNotEmpty()) {
CurrencyRate::timeSeriesRates('', $transactions->min('date'), $transactions->max('date'));
CurrencyRate::timeSeriesRates('', $transactions->min('date'));
}
$totalBatches = count($transactions) / $this->batchSize();
@@ -1,148 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Interfaces\MarketData;
use App\Interfaces\MarketData\Types\Dividend;
use App\Interfaces\MarketData\Types\Ohlc;
use App\Interfaces\MarketData\Types\Quote;
use App\Interfaces\MarketData\Types\Split;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
class AlpacaMarketData implements MarketDataInterface
{
public PendingRequest $client;
public string $dataBaseUrl = 'https://data.alpaca.markets/';
public string $apiBaseUrl = 'https://api.alpaca.markets/';
public function __construct()
{
$this->client = Http::withOptions([
'headers' => [
'content-type' => 'application/json',
'accept' => 'application/json',
'Apca-Api-Key-Id' => config('alpaca.key'),
'Apca-Api-Secret-Key' => config('alpaca.secret'),
],
]);
}
public function exists(string $symbol): bool
{
return (bool) $this->quote($symbol);
}
public function quote(string $symbol): Quote
{
$response = $this->client->baseUrl($this->dataBaseUrl)->get("v2/stocks/{$symbol}/trades/latest");
$quote = $response->json('trade');
if (is_null(Arr::get($quote, 'p'))) {
throw new \Exception('Could not find ticker on Alpaca');
}
$fundamental = cache()->remember(
'ap-symbol-'.$symbol,
1440,
function () use ($symbol) {
$basic = $this->client->baseUrl($this->apiBaseUrl)->get("v2/assets/{$symbol}")->json();
$fifty_two_week = $this->client->baseUrl($this->dataBaseUrl)->withQueryParameters([
'timeframe' => '12M',
'start' => now()->subWeeks(53)->format('Y-m-d'),
'end' => now()->subWeeks(1)->format('Y-m-d'), // todo: can't query recent SIP data
])->get("v2/stocks/{$symbol}/bars")->json();
return array_merge($fifty_two_week, $basic);
}
);
return new Quote([
'name' => Arr::get($fundamental, 'name'),
'symbol' => $symbol,
'currency' => 'USD', // Alpaca only has US equitities
'market_value' => Arr::get($quote, 'p'),
'fifty_two_week_high' => Arr::get($fundamental, 'bars.0.h'),
'fifty_two_week_low' => Arr::get($fundamental, 'bars.0.l'),
]);
}
public function dividends(string $symbol, $startDate, $endDate): Collection
{
$response = $this->client->baseUrl($this->dataBaseUrl)->withQueryParameters([
'symbols' => $symbol,
'limit' => 1000,
'sort' => 'asc',
'types' => 'cash_dividend',
'start' => $startDate->format('Y-m-d'),
'end' => $endDate->format('Y-m-d'),
])->get('v1/corporate-actions');
$dividends = $response->json('corporate_actions.cash_dividends');
return collect($dividends)
->map(function ($dividend) use ($symbol) {
return new Dividend([
'symbol' => $symbol,
'date' => Carbon::parse(Arr::get($dividend, 'ex_date')),
'dividend_amount' => Arr::get($dividend, 'rate'),
]);
});
}
public function splits(string $symbol, $startDate, $endDate): Collection
{
$response = $this->client->baseUrl($this->dataBaseUrl)->withQueryParameters([
'symbols' => $symbol,
'limit' => 1000,
'sort' => 'asc',
'types' => 'forward_split',
'start' => $startDate->format('Y-m-d'),
'end' => $endDate->format('Y-m-d'),
])->get('v1/corporate-actions');
$splits = $response->json('corporate_actions.forward_splits');
return collect($splits)
->map(function ($split) use ($symbol) {
return new Split([
'symbol' => $symbol,
'date' => Carbon::parse(Arr::get($split, 'ex_date')),
'split_amount' => Arr::get($split, 'new_rate') / Arr::get($split, 'old_rate'),
]);
});
}
public function history(string $symbol, $startDate, $endDate): Collection
{
$response = $this->client->baseUrl($this->dataBaseUrl)->withQueryParameters([
'timeframe' => '1D',
'start' => Carbon::parse($startDate)->format('Y-m-d'),
'end' => Carbon::parse($endDate)->subHours(36)->format('Y-m-d'), // todo: can't query recent SIP data
])->get("v2/stocks/{$symbol}/bars");
$history = $response->json('bars');
return collect($history)
->map(function ($history) use ($symbol) {
$date = Carbon::parse($history['t'])->format('Y-m-d');
return [$date => new Ohlc([
'symbol' => $symbol,
'date' => $date,
'close' => Arr::get($history, 'c'),
])];
});
}
}
@@ -69,7 +69,7 @@ class AlphaVantageMarketData implements MarketDataInterface
? Arr::get($fundamental, 'DividendDate')
: null,
'dividend_yield' => Arr::get($fundamental, 'DividendYield') != 'None'
? ((float) Arr::get($fundamental, 'DividendYield')) * 100
? Arr::get($fundamental, 'DividendYield') * 100
: null,
'meta_data' => [
'industry' => Arr::get($fundamental, 'Industry'),
@@ -1,169 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Interfaces\MarketData;
use App\Interfaces\MarketData\Types\Dividend;
use App\Interfaces\MarketData\Types\Ohlc;
use App\Interfaces\MarketData\Types\Quote;
use App\Interfaces\MarketData\Types\Split;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
class TwelveDataMarketData implements MarketDataInterface
{
public PendingRequest $client;
public string $apiBaseUrl = 'https://api.twelvedata.com/';
public function __construct()
{
$this->createNewClient();
}
private function createNewClient()
{
$this->client = Http::withOptions([
'headers' => [
'content-type' => 'application/json',
'accept' => 'application/json',
],
])->withQueryParameters([
'apikey' => config('twelvedata.secret'),
]);
}
public function exists(string $symbol): bool
{
return (bool) $this->quote($symbol);
}
public function quote(string $symbol): Quote
{
$response = $this->client
->baseUrl($this->apiBaseUrl)
->withQueryParameters(['symbol' => $symbol])
->get('price');
$quote = $response->json();
if (! isset($quote['price'])) {
throw new \Exception('Could not find ticker on Twelve Data');
}
$current_market_value = Arr::get($quote, 'price');
$fundamental = cache()->remember(
'twelve-data-symbol-'.$symbol,
1440,
function () use ($symbol) {
$this->createNewClient();
$response = $this->client
->baseUrl($this->apiBaseUrl)
->withQueryParameters(['symbol' => $symbol])
->get('quote');
return $response->json();
}
);
return new Quote([
'name' => Arr::get($fundamental, 'name'),
'symbol' => $symbol,
'currency' => Arr::get($fundamental, 'currency'),
'market_value' => (float) $current_market_value,
'fifty_two_week_high' => (float) Arr::get($fundamental, 'fifty_two_week.high'),
'fifty_two_week_low' => (float) Arr::get($fundamental, 'fifty_two_week.low'),
'meta_data' => [
'exchange' => Arr::get($fundamental, 'exchange'),
'source' => 'twelvedata',
],
]);
}
public function dividends(string $symbol, $startDate, $endDate): Collection
{
$response = $this->client
->baseUrl($this->apiBaseUrl)
->withQueryParameters([
'symbol' => $symbol,
'start_date' => Carbon::parse($startDate)->toDateString(),
'end_date' => Carbon::parse($endDate)->toDateString(),
])
->get('dividends');
$dividends = $response->json('dividends');
return collect($dividends)
->map(function ($dividend) use ($symbol) {
return new Dividend([
'symbol' => $symbol,
'date' => Arr::get($dividend, 'ex_date'),
'dividend_amount' => Arr::get($dividend, 'amount'),
]);
});
}
public function splits(string $symbol, $startDate, $endDate): Collection
{
$response = $this->client
->baseUrl($this->apiBaseUrl)
->withQueryParameters([
'symbol' => $symbol,
'start_date' => Carbon::parse($startDate)->toDateString(),
'end_date' => Carbon::parse($endDate)->toDateString(),
])
->get('splits');
$splits = $response->json('splits');
return collect($splits)
->map(function ($split) use ($symbol) {
return new Split([
'symbol' => $symbol,
'date' => Arr::get($split, 'date'),
'split_amount' => Arr::get($split, 'from_factor') / Arr::get($split, 'to_factor'),
]);
});
}
public function history(string $symbol, $startDate, $endDate): Collection
{
$response = $this->client
->baseUrl($this->apiBaseUrl)
->withQueryParameters([
'symbol' => $symbol,
'interval' => '1day',
'start_date' => Carbon::parse($startDate)->toDateString(),
'end_date' => Carbon::parse($endDate)->toDateString(),
])
->get('time_series');
$values = $response->json('values');
return collect($values)
->mapWithKeys(function ($history) use ($symbol) {
$date = Carbon::parse(Arr::get($history, 'datetime'))->toDateString();
return [$date => new Ohlc([
'symbol' => $symbol,
'date' => $date,
'close' => (float) Arr::get($history, 'close'),
])];
});
}
}
@@ -21,10 +21,7 @@ class YahooMarketData implements MarketDataInterface
{
// create yahoo finance client factory
$this->client = YahooFinance::createApiClient(
clientOptions: ['headers' => ['User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36']],
cache: app('cache.psr6')
);
$this->client = YahooFinance::createApiClient();
}
public function exists(string $symbol): bool
+65 -73
View File
@@ -111,7 +111,7 @@ class CurrencyRate extends Model
*
* @return array<string, float>
*/
public static function timeSeriesRates(string|array|null $currency = null, mixed $start = null, mixed $end = null): array
public static function timeSeriesRates(string $currency, mixed $start = null, mixed $end = null): array
{
if (empty($start)) {
return [];
@@ -119,122 +119,112 @@ class CurrencyRate extends Model
$end = $end ?? now();
dump('Creating period');
$period = CarbonPeriod::create($start, $end);
// No need to send network request - just generate 1s
if ($currency === config('investbrain.base_currency')) {
dump('same curr');
$dateRange = [];
foreach ($period as $date) {
$dateRange[$date->toDateString()] = 1;
}
return $dateRange;
}
if (is_array($currency)) {
dump('diff curr');
$i = 1;
foreach ($currency as $curr) {
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
dispatch(fn () => self::timeSeriesRates($curr, $start, $end))->delay(now()->addSeconds(30 * $i));
$i++;
}
$currencies = Currency::all()->pluck('currency')->toArray();
return [];
}
dump('got currencies');
// handle currency alias
if (! empty($currency)) {
// call api in chunks
foreach (collect($period)->chunk(500) as $chunk) {
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
dump('calling frankf time series');
} else {
$chunkRates = Frankfurter::setSymbols($currencies)->timeSeries($chunk->min(), $chunk->max());
$currency = Currency::all()->pluck('currency')->toArray();
}
$rates = Arr::get($chunkRates, 'rates', []);
// get rates
$rates = Frankfurter::setSymbols($currency)->timeSeries($period->first(), $period->last());
// loop through each date
$updates = [];
$rates = collect(Arr::get($rates, 'rates', []))->sortKeys()->toArray();
foreach ($chunk as $date) {
$datesOnly = array_keys($rates);
$lookupDate = self::getNearestPastDate($date, $rates);
// loop through each date
$updates = [];
foreach ($period as $date) {
if (is_null($lookupDate)) {
continue;
}
$lookupDate = self::getNearestPastDate($date, $datesOnly, $rates);
// loop through each rate
foreach ($rates[$lookupDate->toDateString()] as $curr => $rate) {
if (is_null($lookupDate)) {
continue;
}
// add to updates
$updates[] = [
'currency' => $curr,
'date' => $date->toDateString(),
'rate' => $rate,
'updated_at' => now()->toDateTimeString(),
'created_at' => now()->toDateTimeString(),
];
}
// loop through each rate
foreach ($rates[$lookupDate->toDateString()] as $curr => $rate) {
dump('inserting');
// persist
self::chunkInsert($updates);
// add to updates
$updates[] = [
'currency' => $curr,
'date' => $date->toDateString(),
'rate' => $rate,
'updated_at' => now()->toDateTimeString(),
'created_at' => now()->toDateTimeString(),
];
}
}
// persist
self::chunkInsert($updates);
dump('done');
if (is_string($currency)) {
return collect($updates)
->whereBetween('date', [$start, $end ?? now()])
->where('currency', $currency)
->mapWithKeys(fn ($rate) => [
$rate['date'] => $rate['rate'] * ($adjustment ?? 1),
])
->toArray();
}
return [];
return collect($updates)
->whereBetween('date', [$start, $end ?? now()])
->where('currency', $currency)
->mapWithKeys(fn ($rate) => [
$rate['date'] => $rate['rate'] * $adjustment,
])
->toArray();
}
private static function getNearestPastDate(CarbonInterface $date, array $datesOnly, array $rates): ?CarbonInterface
private static function getNearestPastDate(CarbonInterface $date, array $rates): ?CarbonInterface
{
// if no dates, nothing to do...
if (empty($datesOnly)) {
return null;
}
$mutableDate = $date->copy();
$weekAgo = $date->copy()->subWeek();
$firstDate = Carbon::parse($datesOnly[0]);
$datesWithRates = array_keys($rates);
sort($datesWithRates);
// get rates or find closest valid rate (handles missing weekend rates)
while (! isset($rates[$mutableDate->toDateString()])) {
// prevent runaway infinite loops
if ($mutableDate->lessThan($weekAgo)) {
return null;
}
while (! isset($rates[$date->toDateString()])) {
// is this the start of a range that falls on a weekend?
if ($mutableDate->lessThan($firstDate)) {
if ($date->lessThan($first_date = Carbon::parse($datesWithRates[0]))) {
return $firstDate;
$date = $first_date;
break;
}
// try the day before then
$mutableDate = $mutableDate->subDay();
$date = Carbon::parse($date)->subDay();
// prevent runaway infinite loops
if ($date->lessThan($date->copy()->subWeek())) {
$date = null;
break;
}
}
return $mutableDate;
return $date;
}
public static function refreshCurrencyData($force = false): void
@@ -275,13 +265,15 @@ class CurrencyRate extends Model
public static function chunkInsert(array $updates): void
{
foreach (array_chunk($updates, 500) as $chunk) {
$chunks = array_chunk($updates, 500);
foreach ($chunks as $chunk) {
QueuedCurrencyRateInsertJob::dispatch($chunk);
}
}
protected static function getCurrencyAliasAdjustments(string $currency)
protected static function getCurrencyAliasAdjustments($currency)
{
$adjustment = 1;
+97 -65
View File
@@ -32,7 +32,7 @@ class DailyChange extends Model
'date' => 'datetime',
'total_market_value' => 'float',
'total_cost_basis' => 'float',
'total_market_gain' => 'float',
'total_gain' => 'float',
'realized_gain_dollars' => 'float',
'total_dividends_earned' => 'float',
];
@@ -42,9 +42,9 @@ class DailyChange extends Model
return $query->where('daily_change.portfolio_id', $portfolio);
}
public function scopeMyDailyChanges($query)
public function scopeMyDailyChanges()
{
return $query->whereHas('portfolio', function ($query) {
return $this->whereHas('portfolio', function ($query) {
$query->whereHas('users', function ($query) {
return $query->where('id', auth()->id());
});
@@ -86,81 +86,113 @@ class DailyChange extends Model
AS total_dividends_earned")
->groupBy(['holdings.portfolio_id', 'dividends.date', 'tx.transaction_type', 'tx.quantity']);
$transactionTotals = DB::table('transactions')
->select(['transactions.portfolio_id', 'transactions.date'])
->selectRaw("
SUM(
(CASE WHEN transactions.transaction_type = 'BUY' THEN 1 ELSE -1 END)
* transactions.quantity
* transactions.cost_basis_base
* COALESCE(cr.rate, 1)
) AS daily_cost_basis
")
->selectRaw("
SUM(
(CASE
WHEN transactions.transaction_type = 'SELL'
THEN ( transactions.sale_price_base - transactions.cost_basis_base )
* transactions.quantity
* COALESCE(cr.rate, 1)
END)
) AS daily_realized_gains
")
$totalCostBasisSub = DB::table('transactions as tx1')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join
->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(transactions.date)'))
->where('cr.currency', $currency);
$join->on('cr.date', '=', 'tx1.date')
->where('cr.currency', '=', $currency);
})
->groupBy('transactions.portfolio_id', 'transactions.date');
$cumulativeCostBasis = DB::table(DB::raw("({$transactionTotals->toSql()}) AS transaction_totals"))
->mergeBindings($transactionTotals)
->select(['portfolio_id', 'date'])
->selectRaw('SUM(daily_cost_basis) AS cumulative_cost_basis')
->selectRaw('SUM(daily_realized_gains) AS cumulative_realized_gains')
->groupBy('portfolio_id', 'date');
->select([
'tx1.portfolio_id',
'tx1.date',
'tx1.symbol',
'tx1.transaction_type',
'tx1.quantity',
])
->selectRaw("(CASE
WHEN tx1.transaction_type = 'BUY'
THEN COALESCE(cr.rate, 1)
ELSE (
SELECT
SUM(COALESCE(cr2.rate, 1) * buy.cost_basis_base)
/ SUM(buy.cost_basis_base)
FROM transactions as buy
LEFT JOIN currency_rates as cr2
ON cr2.date = buy.date
AND cr2.currency = '{$currency}'
WHERE buy.symbol = tx1.symbol
AND buy.portfolio_id = tx1.portfolio_id
AND buy.transaction_type = 'BUY'
AND buy.date <= tx1.date
) END)
AS rate")
->selectRaw(
"(CASE
WHEN tx1.transaction_type = 'BUY'
THEN AVG(tx1.cost_basis_base)
ELSE (
SELECT
AVG(-buy.cost_basis_base)
FROM transactions as buy
WHERE buy.symbol = tx1.symbol
AND buy.portfolio_id = tx1.portfolio_id
AND buy.transaction_type = 'BUY'
AND buy.date <= tx1.date
) END)
AS cost_basis_base")
->selectRaw(
"(CASE
WHEN tx1.transaction_type = 'SELL'
THEN tx1.sale_price_base - tx1.cost_basis_base
ELSE 0 END)
* tx1.quantity
* COALESCE(cr.rate, 1)
AS realized_gain_dollars")
->groupBy([
'tx1.portfolio_id',
'tx1.date',
'tx1.symbol',
'tx1.transaction_type',
'tx1.cost_basis_base',
'tx1.quantity',
'cr.rate',
'tx1.sale_price_base',
]);
return $query
->select(['daily_change.portfolio_id', 'daily_change.date'])
->selectRaw('daily_change.total_market_value * COALESCE(cr.rate, 1) AS total_market_value')
->selectRaw('SUM(COALESCE(ccb.cumulative_cost_basis, 0)) AS total_cost_basis')
->selectRaw('daily_change.total_market_value * COALESCE(cr.rate, 1)
- SUM(COALESCE(ccb.cumulative_cost_basis, 0))
AS total_market_gain')
->selectRaw('SUM(COALESCE(ccb.cumulative_realized_gains, 0)) AS realized_gain_dollars')
->select(['daily_change.date', 'daily_change.portfolio_id'])
->leftJoinSub($totalCostBasisSub, 'cost_basis_display', function ($join) {
$join->on('daily_change.date', '>=', 'cost_basis_display.date')
->whereColumn('daily_change.portfolio_id', '=', 'cost_basis_display.portfolio_id');
})
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join->on('cr.date', '=', 'daily_change.date')
->where('cr.currency', '=', $currency);
})
->selectRaw('
SUM(
cost_basis_display.cost_basis_base
* cost_basis_display.quantity
* cost_basis_display.rate
) as total_cost_basis')
->selectRaw('(
daily_change.total_market_value * COALESCE(cr.rate, 1)
) - SUM(
cost_basis_display.cost_basis_base
* cost_basis_display.quantity
* cost_basis_display.rate
) as total_gain')
->selectRaw('(
daily_change.total_market_value * COALESCE(cr.rate, 1)
) as total_market_value')
->selectRaw('
SUM(
cost_basis_display.realized_gain_dollars
) as realized_gain_dollars')
->selectSub(function ($query) use ($dividendSub) {
$query->fromSub($dividendSub, 'd')
->selectRaw('SUM(d.total_dividends_earned)')
->whereColumn('d.date', '<=', 'daily_change.date')
->whereColumn('d.portfolio_id', '=', 'daily_change.portfolio_id');
}, 'total_dividends_earned')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join
->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(daily_change.date)'))
->where('cr.currency', $currency);
})
->leftJoinSub($cumulativeCostBasis, 'ccb', function ($join) {
$join
->on('ccb.portfolio_id', '=', 'daily_change.portfolio_id')
->whereRaw('ccb.date <= daily_change.date');
})
->groupBy(['daily_change.date', 'daily_change.portfolio_id', 'cr.rate'])
->groupBy([
'daily_change.date',
'cr.rate',
'daily_change.total_market_value',
'daily_change.portfolio_id',
])
->orderBy('daily_change.date');
}
public function scopeWithMultipleDailyPerformance($query)
{
return DB::table(DB::raw("({$query->toSql()}) AS daily_query"))
->addBinding($query->getQuery()->getBindings(), 'join')
->select('date')
->selectRaw('SUM(total_market_value) AS total_market_value')
->selectRaw('SUM(total_cost_basis) AS total_cost_basis')
->selectRaw('SUM(total_market_gain) AS total_market_gain')
->selectRaw('SUM(realized_gain_dollars) AS realized_gain_dollars')
->selectRaw('SUM(total_dividends_earned) AS total_dividends_earned')
->groupBy('date');
}
public function portfolio()
{
return $this->belongsTo(Portfolio::class);
+32 -18
View File
@@ -95,36 +95,49 @@ class Dividend extends Model
return;
}
// get some data
if ($dividend_data = collect() && $start_date && $end_date) {
$dividend_data = app(MarketDataInterface::class)->dividends($symbol, $start_date, $end_date);
dump('1. getting div data for '.$symbol);
try {
// get some data
if ($dividend_data = collect() && $start_date && $end_date) {
$dividend_data = app(MarketDataInterface::class)->dividends($symbol, $start_date, $end_date);
}
} catch (\Throwable $e) {
dump('exception: '.$e->getMessage());
}
dump('2. got div data for '.$symbol);
// ah, we found some dividends...
if ($dividend_data->isNotEmpty()) {
$dividend_data = $dividend_data->sortBy('date');
dump('3. getting mkt data for '.$symbol);
$market_data = MarketData::getMarketData($symbol);
$dividend_data
->chunk(10)
->each(function ($chunk) use ($market_data) {
dump('4. got market data for '.$symbol);
// get historic conversion rates
$rate_to_base = CurrencyRate::timeSeriesRates($market_data->currency, $chunk->min('date'), $chunk->max('date'));
// todo: use this for start_date - $dividend_data->first()->get('date')
// create mass insert
foreach ($chunk as $index => $dividend) {
$rate_to_base_date = 1 / Arr::get($rate_to_base, Carbon::parse(Arr::get($dividend, 'date'))->toDateString(), 1);
// get historic conversion rates
$rate_to_base = CurrencyRate::timeSeriesRates($market_data->currency, $dividend_data->first()->get('date'), $end_date);
$dividend['dividend_amount_base'] = $dividend['dividend_amount'] * $rate_to_base_date;
dump('5. got time series for '.$symbol);
// create mass insert
foreach ($dividend_data as $index => $dividend) {
$rate_to_base_date = 1 / Arr::get($rate_to_base, Carbon::parse(Arr::get($dividend, 'date'))->toDateString(), 1);
$chunk[$index] = [...$dividend, ...['id' => Str::uuid()->toString(), 'updated_at' => now(), 'created_at' => now()]];
}
$dividend['dividend_amount_base'] = $dividend['dividend_amount'] * $rate_to_base_date;
// insert records
(new self)->insertOrIgnore($chunk->toArray());
});
$dividend_data[$index] = [...$dividend, ...['id' => Str::uuid()->toString(), 'updated_at' => now(), 'created_at' => now()]];
}
// insert records
(new self)->insertOrIgnore($dividend_data->toArray());
dump('6. inserted for '.$symbol);
// sync to holdings
self::syncHoldings($symbol);
@@ -134,7 +147,9 @@ class Dividend extends Model
// sync last dividend amount to market data table
$market_data->last_dividend_amount = $dividend_data->sortByDesc('date')->first()['dividend_amount'];
$market_data->save();
}
}
public static function syncHoldings(string $symbol): void
@@ -191,7 +206,6 @@ class Dividend extends Model
'date' => $dividend['date'],
'portfolio_id' => $holding->portfolio_id,
'symbol' => $holding->symbol,
'currency' => $holding->market_data->currency,
'transaction_type' => 'BUY',
'reinvested_dividend' => true,
'cost_basis' => 0,
+107 -143
View File
@@ -39,7 +39,7 @@ class Holding extends Model
'total_cost_basis' => 'float',
'realized_gain_dollars' => 'float',
'dividends_earned' => 'float',
'total_market_gain_dollars' => 'float',
'total_gain_dollars' => 'float',
'market_gain_dollars' => 'float',
'total_market_value' => 'float',
'total_dividends_earned' => 'float',
@@ -228,7 +228,7 @@ class Holding extends Model
return collect([
'total_cost_basis' => $result->sum('total_cost_basis'),
'total_market_value' => $result->sum('total_market_value'),
'total_market_gain_dollars' => $result->sum('total_market_gain_dollars'),
'total_gain_dollars' => $result->sum('total_gain_dollars'),
'realized_gain_dollars' => $result->sum('realized_gain_dollars'),
'total_dividends_earned' => $result->sum('total_dividends_earned'),
]);
@@ -243,113 +243,11 @@ class Holding extends Model
{
$currency = $currency ?? auth()->user()->getCurrency();
$cost_basis_sub = DB::table('transactions')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join
->on('cr.date', '=', 'transactions.date')
->where('cr.currency', '=', $currency);
})
->select([
'transactions.id',
'transactions.symbol',
'transactions.portfolio_id',
])
->leftJoinSub(
DB::table('transactions')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join
->on('cr.date', '=', 'transactions.date')
->where('cr.currency', '=', $currency);
})
->select([
'transactions.symbol',
'transactions.portfolio_id',
'transactions.quantity',
'transactions.cost_basis_base',
'transactions.date',
])
->selectRaw("
(CASE
WHEN
transactions.transaction_type = 'BUY'
OR SUM(transactions.cost_basis_base) = 0
THEN
COALESCE(cr.rate, 1)
ELSE (
SELECT
SUM(COALESCE(cr2.rate, 1) * buy.cost_basis_base)
/ SUM(buy.cost_basis_base)
FROM transactions as buy
LEFT JOIN currency_rates as cr2
ON cr2.date = buy.date
AND cr2.currency = '{$currency}'
WHERE buy.symbol = transactions.symbol
AND buy.portfolio_id = transactions.portfolio_id
AND buy.transaction_type = 'BUY'
AND buy.date <= transactions.date
) END)
AS rate")
->groupBy([
'transactions.id',
'transactions.symbol',
'transactions.date',
'transactions.portfolio_id',
'transactions.transaction_type',
'transactions.cost_basis_base',
'transactions.quantity',
'cr.rate',
]),
'cost_basis_display',
function ($join) {
$join
->on('transactions.symbol', '=', 'cost_basis_display.symbol')
->on(
'transactions.portfolio_id',
'=',
'cost_basis_display.portfolio_id'
)
->on('transactions.date', '=', 'cost_basis_display.date');
}
)
->selectRaw(
"CASE WHEN transactions.transaction_type = 'SELL' THEN (transactions.sale_price_base - transactions.cost_basis_base) * transactions.quantity * COALESCE(cr.rate, 1) END AS realized_gain_dollars"
)
->selectRaw(
"CASE WHEN transactions.transaction_type = 'BUY' THEN transactions.cost_basis_base * transactions.quantity * cost_basis_display.rate END AS total_cost_basis"
)
->selectRaw(
"CASE WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity END AS total_purchases"
)
->groupBy([
'transactions.id',
'transactions.symbol',
'transactions.portfolio_id',
'transactions.cost_basis_base',
'transactions.quantity',
'cost_basis_display.rate',
'cr.rate',
]);
$dividends_sub = DB::table('dividends')
->join('transactions as tx', function ($join) {
$join
->on('tx.symbol', '=', 'dividends.symbol')
->on('tx.date', '<=', 'dividends.date');
})
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join
->on('cr.date', '=', 'dividends.date')
->where('cr.currency', '=', $currency);
})
->select(['dividends.symbol', 'tx.portfolio_id'])
->selectRaw(
"SUM(((CASE WHEN transaction_type = 'BUY' THEN tx.quantity ELSE 0 END) - (CASE WHEN transaction_type = 'SELL' THEN tx.quantity ELSE 0 END)) * dividends.dividend_amount_base * COALESCE(cr.rate, 1)) AS total_dividends_earned"
)
->groupBy(['dividends.symbol', 'tx.portfolio_id']);
return $query->select([
'holdings.symbol',
'holdings.portfolio_id',
'transactions_display.total_cost_basis',
'transactions_display.realized_gain_dollars',
'dividends_display.total_dividends_earned',
])
->groupBy([
@@ -357,6 +255,8 @@ class Holding extends Model
'holdings.quantity',
'holdings.portfolio_id',
'cr.rate',
'transactions_display.total_cost_basis',
'transactions_display.realized_gain_dollars',
'dividends_display.total_dividends_earned',
'market_data.market_value_base',
])
@@ -364,49 +264,121 @@ class Holding extends Model
$join->where('cr.currency', '=', $currency);
if (config('database.default') === 'sqlite') {
$join->whereRaw("strftime('%Y-%m-%d', cr.date) = ?", [
now()->toDateString(),
]);
$join->whereRaw("strftime('%Y-%m-%d', cr.date) = ?", [now()->toDateString()]);
} else {
$join->on('cr.date', '=', DB::raw("'".now()->toDateString()."'"));
}
})
->leftJoin('market_data', function ($join) {
$join->on('market_data.symbol', '=', 'holdings.symbol');
})
->selectRaw('
holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1)
AS total_market_value
')
->selectRaw('
SUM(transactions_display.realized_gain_dollars)
AS realized_gain_dollars
')
->selectRaw('
(SUM(transactions_display.total_cost_basis) / SUM(transactions_display.total_purchases))
* holdings.quantity
AS total_cost_basis
')
->selectRaw('
(holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1))
- (SUM(transactions_display.total_cost_basis) / SUM(transactions_display.total_purchases))
* holdings.quantity
AS total_market_gain_dollars
')
->leftJoinSub($cost_basis_sub, 'transactions_display',
->selectRaw(
'holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1) AS total_market_value'
)
->selectRaw('(
holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1)
) - transactions_display.total_cost_basis as total_gain_dollars')
->leftJoinSub(
DB::table('transactions')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join->on('cr.date', '=', 'transactions.date')
->where('cr.currency', '=', $currency);
})
->select(['transactions.symbol', 'transactions.portfolio_id'])
->leftJoinSub(
DB::table('transactions')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join
->on('cr.date', '=', 'transactions.date')
->where('cr.currency', '=', $currency);
})
->select([
'transactions.symbol',
'transactions.portfolio_id',
'transactions.quantity',
'transactions.date',
])
->selectRaw(
"(CASE
WHEN transactions.transaction_type = 'BUY'
THEN COALESCE(cr.rate, 1)
ELSE (
SELECT
SUM(COALESCE(cr2.rate, 1) * buy.cost_basis_base)
/ SUM(buy.cost_basis_base)
FROM transactions as buy
LEFT JOIN currency_rates as cr2
ON cr2.date = buy.date
AND cr2.currency = '{$currency}'
WHERE buy.symbol = transactions.symbol
AND buy.portfolio_id = transactions.portfolio_id
AND buy.transaction_type = 'BUY'
AND buy.date <= transactions.date
) END)
AS rate"
)
->selectRaw(
"(CASE
WHEN transactions.transaction_type = 'BUY'
THEN AVG(transactions.cost_basis_base)
ELSE (
SELECT
AVG(-buy.cost_basis_base)
FROM transactions as buy
WHERE buy.symbol = transactions.symbol
AND buy.portfolio_id = transactions.portfolio_id
AND buy.transaction_type = 'BUY'
AND buy.date <= transactions.date
) END)
AS cost_basis_base"
)
->groupBy([
'transactions.symbol',
'transactions.date',
'transactions.portfolio_id',
'transactions.transaction_type',
'transactions.quantity',
'cr.rate',
]), 'cost_basis_display', function ($join) {
$join->on('transactions.symbol', '=', 'cost_basis_display.symbol')
->on('transactions.portfolio_id', '=', 'cost_basis_display.portfolio_id')
->on('transactions.date', '=', 'cost_basis_display.date');
})
->selectRaw(
"SUM(CASE WHEN transactions.transaction_type = 'SELL' THEN (transactions.sale_price_base - transactions.cost_basis_base) * transactions.quantity * COALESCE(cr.rate, 1) ELSE 0 END) AS realized_gain_dollars"
)
->selectRaw(
'SUM(cost_basis_display.cost_basis_base * cost_basis_display.quantity * cost_basis_display.rate) AS total_cost_basis'
)
->groupBy(['transactions.symbol', 'transactions.portfolio_id']),
'transactions_display',
function ($join) {
$join
->on('holdings.symbol', '=', 'transactions_display.symbol')
$join->on('holdings.symbol', '=', 'transactions_display.symbol')
->on('holdings.portfolio_id', '=', 'transactions_display.portfolio_id');
}
)
->leftJoinSub($dividends_sub, 'dividends_display',
->leftJoinSub(
DB::table('dividends')
->join('transactions as tx', function ($join) {
$join->on('tx.symbol', '=', 'dividends.symbol')
->on('tx.date', '<=', 'dividends.date');
})
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join->on('cr.date', '=', 'dividends.date')
->where('cr.currency', '=', $currency);
})
->select(['dividends.symbol'])
->selectRaw(
"SUM(((CASE WHEN transaction_type = 'BUY' THEN tx.quantity ELSE 0 END) - (CASE WHEN transaction_type = 'SELL' THEN tx.quantity ELSE 0 END)) * dividends.dividend_amount_base * COALESCE(cr.rate, 1)) AS total_dividends_earned"
)
->groupBy(['dividends.symbol']),
'dividends_display',
function ($join) {
$join->on('holdings.symbol', '=', 'dividends_display.symbol') // todo: this isnt limiting to port ids
->on('holdings.portfolio_id', '=', 'dividends_display.portfolio_id');
$join->on('holdings.symbol', '=', 'dividends_display.symbol');
}
);
}
public function syncTransactionsAndDividends()
@@ -421,14 +393,6 @@ class Holding extends Model
->selectRaw("SUM(CASE WHEN transaction_type = 'BUY' THEN (quantity * cost_basis) ELSE 0 END) AS total_cost_basis")
->first();
// delete holding if no transactions
if (empty($query->qty_purchases + $query->qty_sales)) {
$this->delete();
return;
}
$total_quantity = round($query->qty_purchases - $query->qty_sales, 4);
$average_cost_basis = (
+3 -11
View File
@@ -152,13 +152,7 @@ class Portfolio extends Model
$total_performance = [];
// get unique currencies for holdings
$currency_rates = [];
foreach ($holdings->groupBy('market_data.currency')->keys() as $currency) {
$currency_rates[$currency] = CurrencyRate::timeSeriesRates($currency, $holdings->min('first_transaction_date'), now());
}
$holdings->each(function ($holding) use (&$total_performance, $currency_rates) {
$holdings->each(function ($holding) use (&$total_performance) {
$period = CarbonPeriod::create(
$holding->first_transaction_date,
@@ -169,6 +163,7 @@ class Portfolio extends Model
$daily_performance = $holding->dailyPerformance($holding->first_transaction_date, now());
$all_history = app(MarketDataInterface::class)->history($holding->symbol, $holding->first_transaction_date, now());
$currency_rates = CurrencyRate::timeSeriesRates($holding->market_data->currency, $holding->first_transaction_date, now());
$holding_performance = [];
@@ -184,7 +179,7 @@ class Portfolio extends Model
$holding_performance[$date] = [
'date' => $date,
'portfolio_id' => $this->id,
'total_market_value' => $total_market_value * (1 / Arr::get($currency_rates[$holding->market_data->currency], $date, 1)),
'total_market_value' => $total_market_value * (1 / Arr::get($currency_rates, $date, 1)),
];
}
}
@@ -218,9 +213,6 @@ class Portfolio extends Model
);
});
}
cache()->forget('graph-YTD-'.$this->id);
cache()->forget('graph-YTD-'.request()->user()?->id);
}
protected function getMostRecentCloseData($history, $date, $i = 0, $max_attempts = 5)
+1 -1
View File
@@ -99,7 +99,7 @@ class User extends Authenticatable implements MustVerifyEmail
return Arr::get($this->options, 'locale') ?? request()->getPreferredLanguage($available_locales) ?? config('app.locale');
}
public function setOption(mixed $key, ?string $value = null): self
public function setOption(mixed $key, string $value): self
{
$options = is_array($key) ? $key : [$key => $value];
+1 -2
View File
@@ -24,9 +24,8 @@
"openai-php/client": "^0.10.3",
"predis/predis": "^2.2",
"robsontenorio/mary": "^1.35",
"scheb/yahoo-finance-api": "^5.0",
"scheb/yahoo-finance-api": "^4.11",
"staudenmeir/eloquent-has-many-deep": "^1.20",
"symfony/cache": "^7.3",
"tschucki/alphavantage-laravel": "^0.0"
},
"require-dev": {
Generated
+543 -945
View File
File diff suppressed because it is too large Load Diff
-8
View File
@@ -1,8 +0,0 @@
<?php
declare(strict_types=1);
return [
'key' => env('ALPACA_API_KEY'),
'secret' => env('ALPACA_API_SECRET'),
];
-2
View File
@@ -11,9 +11,7 @@ return [
'interfaces' => [
'yahoo' => App\Interfaces\MarketData\YahooMarketData::class,
'alphavantage' => App\Interfaces\MarketData\AlphaVantageMarketData::class,
'alpaca' => App\Interfaces\MarketData\AlpacaMarketData::class,
'finnhub' => App\Interfaces\MarketData\FinnhubMarketData::class,
'twelvedata' => App\Interfaces\MarketData\TwelveDataMarketData::class,
'fake' => App\Interfaces\MarketData\FakeMarketData::class,
],
-7
View File
@@ -1,7 +0,0 @@
<?php
declare(strict_types=1);
return [
'secret' => env('TWELVEDATA_API_SECRET'),
];
-14
View File
@@ -59,20 +59,6 @@ class TransactionFactory extends Factory
]);
}
public function sixMonthsAgo(): static
{
return $this->state(fn (array $attributes) => [
'date' => now()->subMonths(6)->toDateString(),
]);
}
public function today(): static
{
return $this->state(fn (array $attributes) => [
'date' => now()->toDateString(),
]);
}
public function recent(): static
{
return $this->state(fn (array $attributes) => [
@@ -3,12 +3,10 @@
declare(strict_types=1);
use App\Models\CurrencyRate;
use App\Models\Holding;
use App\Models\Transaction;
use Database\Seeders\CurrencySeeder;
use Database\Seeders\MarketDataSeeder;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
@@ -25,15 +23,10 @@ return new class extends Migration
* Add options column to users table
*/
Schema::table('users', function (Blueprint $table) {
$locale = config('app.locale', 'en');
$currency = config('investbrain.base_currency', 'USD');
$default = config('database.default') === 'mysql'
? new Expression("(JSON_OBJECT('locale', '{$locale}', 'display_currency', '{$currency}'))")
: json_encode(['locale' => $locale, 'display_currency' => $currency]);
$table->json('options')->default($default)->after('profile_photo_path');
$table->json('options')->default(json_encode([
'locale' => config('app.locale', 'en'),
'display_currency' => config('investbrain.base_currency', 'USD'),
]))->after('profile_photo_path');
});
/**
@@ -103,17 +96,17 @@ return new class extends Migration
'--force' => true,
]);
Artisan::call('db:seed', [
'--class' => MarketDataSeeder::class,
'--force' => true,
]);
CurrencyRate::timeSeriesRates(
Holding::all()->groupBy('market_data.currency')->keys()->toArray(),
'', // use fake currency to force
Transaction::min('date')
);
CurrencyRate::refreshCurrencyData();
Artisan::call('db:seed', [
'--class' => MarketDataSeeder::class,
'--force' => true,
]);
}
/**
+10 -10
View File
@@ -12,6 +12,8 @@ class MarketDataSeeder extends Seeder
{
use WithoutModelEvents;
public array $rows = [];
/**
* Run the database seeds.
*/
@@ -42,7 +44,7 @@ class MarketDataSeeder extends Seeder
$meta_data = json_decode(base64_decode($data['meta_data']), true);
$meta_data['source'] = 'market_data_seeder';
$rows[] = [
$this->rows[] = [
'symbol' => $data['symbol'],
'name' => $data['name'],
'currency' => $data['currency'],
@@ -52,17 +54,16 @@ class MarketDataSeeder extends Seeder
$rowCount++;
if ($rowCount % $chunkSize == 0) {
$this->bulkInsert($rows);
$rows = [];
DB::table('market_data')->upsert($this->rows, ['symbol'], ['name', 'currency', 'meta_data']);
$this->rows = [];
}
}
}
// final clean up
if (! empty($rows)) {
if (! empty($this->rows)) {
$this->bulkInsert($rows);
$rows = [];
$this->bulkInsert($this->rows);
}
// Close the CSV file
@@ -76,17 +77,16 @@ class MarketDataSeeder extends Seeder
}
}
private function bulkInsert($rows): void
public function bulkInsert(array $rows)
{
try {
DB::table('market_data')->upsert($rows, ['symbol'], ['name', 'currency', 'meta_data']);
DB::table('market_data')->insertOrIgnore($rows);
$this->rows = [];
} catch (\Throwable $e) {
throw new \Exception('Error: '.$e->getMessage());
}
gc_collect_cycles();
}
}
File diff suppressed because it is too large Load Diff
+15 -15
View File
@@ -11,9 +11,9 @@ services:
- 8000:80
environment: # You can either use these properties OR an .env file. Do not use both!
APP_URL: "http://localhost:8000"
DB_CONNECTION: pgsql
DB_HOST: investbrain-pgsql
DB_PORT: 5432
DB_CONNECTION: mysql
DB_HOST: investbrain-mysql
DB_PORT: 3306
DB_DATABASE: investbrain
DB_USERNAME: investbrain
DB_PASSWORD: investbrain
@@ -25,7 +25,7 @@ services:
- investbrain-storage:/var/app/storage # You can use a volume...
# - /path/to/storage:/var/app/storage:delegated # ...or you can use a path on host
depends_on:
- pgsql
- mysql
- redis
networks:
- investbrain-network
@@ -40,22 +40,22 @@ services:
- investbrain-redis:/data
networks:
- investbrain-network
pgsql:
image: postgres:15-alpine
container_name: investbrain-pgsql
mysql:
image: mysql:8.0
container_name: investbrain-mysql
restart: unless-stopped
ports:
- "5432:5432"
environment:
POSTGRES_DB: ${DB_DATABASE:-investbrain}
POSTGRES_USER: ${DB_USERNAME:-investbrain}
POSTGRES_PASSWORD: ${DB_PASSWORD:-investbrain}
command: postgres -c log_min_messages=error
MYSQL_DATABASE: ${DB_DATABASE:-investbrain}
MYSQL_USER: ${DB_USERNAME:-investbrain}
MYSQL_PASSWORD: ${DB_PASSWORD:-investbrain}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD:-investbrain}
command:
- --cte-max-recursion-depth=25000
volumes:
- investbrain-pgsql:/var/lib/postgresql/data
- investbrain-mysql:/var/lib/mysql
networks:
- investbrain-network
volumes:
investbrain-storage:
investbrain-redis:
investbrain-pgsql:
investbrain-mysql:
+1 -4
View File
@@ -44,9 +44,6 @@ FROM php:8.3-fpm-alpine
# Set the working directory
WORKDIR /var/app
ARG VERSION=dev
ENV VERSION=$VERSION
# Copy necessary files from the builder stage
COPY --from=builder /var/app /var/app
COPY --from=builder /usr/local/etc/php/conf.d /usr/local/etc/php/conf.d
@@ -65,7 +62,7 @@ RUN apk add --no-cache \
bash \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
gd pgsql zip pdo_mysql pdo_pgsql mysqli intl
gd pgsql zip pdo_mysql mysqli intl
# Remove default nginx config
RUN rm -rf /var/www/html \
+3 -14
View File
@@ -3,8 +3,7 @@
cd /var/app
# Starting Investbrain
echo "CuKWhOKWliAgICAgICAg4paXIOKWjCAgICAg4paYICAK4paQIOKWm+KWjOKWjOKWjOKWiOKWjOKWm+KWmOKWnOKWmOKWm+KWjOKWm+KWmOKWgOKWjOKWjOKWm+KWjArilp/ilpbilozilozilprilpjilpnilpbiloTilozilpDilpbilpnilozilowg4paI4paM4paM4paM4paMCg==" | base64 -d
printf "%15s$VERSION\n"
echo "CiAgKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICAqICBJSUkgICBOICAgTiAgViAgIFYgIEVFRUVFICBTU1NTICBUVFRUVCAgQkJCQkIgICBSUlJSICAgIEFBQUFBICBJSUkgICBOICAgTiAgKgogICogICBJICAgIE5OICBOICBWICAgViAgRSAgICAgIFMgICAgICAgVCAgICBCICAgIEIgIFIgICBSICAgQSAgIEEgICBJICAgIE5OICBOICAqCiAgKiAgIEkgICAgTiBOIE4gIFYgICBWICBFRUVFICAgU1NTUyAgICBUICAgIEJCQkJCICAgUlJSUiAgICBBQUFBQSAgIEkgICAgTiBOIE4gICoKICAqICAgSSAgICBOICBOTiAgViAgIFYgIEUgICAgICAgICAgUyAgIFQgICAgQiAgICBCICBSICBSICAgIEEgICBBICAgSSAgICBOICBOTiAgKgogICogIElJSSAgIE4gICBOICAgVlZWICAgRUVFRUUgIFNTU1MgICAgVCAgICBCQkJCQiAgIFIgICBSICAgQSAgIEEgIElJSSAgIE4gICBOICAqCiAgKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICA=" | base64 -d
echo -e "\n====================== Validating environment... ====================== "
@@ -55,6 +54,7 @@ RETRIES=12
DELAY=5
run_migrations() {
sleep $DELAY
# php artisan migrate --force
output=$(php artisan migrate --force 2>/dev/null)
if [[ $? -eq 0 ]]; then
echo "$output"
@@ -72,18 +72,7 @@ until run_migrations; do
echo -e "\n > Waiting for database to be ready... retrying in $DELAY seconds. \n"
done
echo -e "\n====================== Cleaning up... ====================== \n"
# Clear caches
echo $(php artisan cache:clear)
echo $(php artisan view:clear)
echo $(php artisan route:clear)
echo $(php artisan event:clear)
# Re-create caches
echo $(php artisan route:cache)
echo $(php artisan event:cache)
echo -e "\n====================== Spinning up Supervisor daemon... ====================== \n"
exec supervisord -c /etc/supervisor/conf.d/supervisord.conf
File diff suppressed because one or more lines are too long
@@ -26,7 +26,7 @@ new class extends Component
<x-icon name="o-bars-3" class="cursor-pointer" />
</label>
<div class="hidden md:block" style="height:2.5em">
<div class="hidden md:block" style="height:3.1em">
<x-application-logo />
</div>
+1 -1
View File
@@ -9,7 +9,7 @@
<div class="grid sm:grid-cols-5 gap-5">
<x-card class="col-span-5 sm:col-span-1 bg-slate-100 dark:bg-base-200 rounded-lg">
<div class="text-sm text-gray-400 whitespace-nowrap truncate">{{ __('Market Gain/Loss') }}</div>
<div class="font-black text-xl"> {{ Number::currency($metrics->get('total_market_gain_dollars', 0)) }} </div>
<div class="font-black text-xl"> {{ Number::currency($metrics->get('total_gain_dollars', 0)) }} </div>
</x-card>
<x-card class="col-span-5 sm:col-span-1 bg-slate-100 dark:bg-base-200 rounded-lg">
@@ -7,7 +7,7 @@ use Livewire\Volt\Component;
new class extends Component
{
// props
public ?Portfolio $portfolio = null;
public ?Portfolio $portfolio;
public string $name = 'portfolio';
@@ -53,49 +53,45 @@ new class extends Component
$dailyChangeQuery->whereDate('daily_change.date', '>=', now()->{$filterMethod['method']}(...$filterMethod['args']));
}
$dailyChange = cache()->remember(
'graph-'.$this->scope.'-'.(isset($this->portfolio) ? $this->portfolio->id : request()->user()->id),
10,
function () use ($dailyChangeQuery) {
return $dailyChangeQuery->withMultipleDailyPerformance()->get();
}
);
$dailyChange = $dailyChangeQuery->get();
$marketValueData = [];
$costBasisData = [];
$marketGainData = [];
foreach ($dailyChange as $data) {
$date = $data->date;
$marketValueData[] = [$date, round($data->total_market_value, 2)];
$costBasisData[] = [$date, round($data->total_cost_basis, 2)];
$marketGainData[] = [$date, round($data->total_market_gain, 2)];
// $dividendSeries[] = [$date, round($data->total_dividends_earned, 2)];
// $realizedGainSeries[] = [$date, round($data->realized_gains, 2)];
}
$dailyChange = $dailyChange
->sortBy('date')
->groupBy('date')
->map(function ($group) {
return (object) [
'date' => $group->first()->date->toDateString(),
'total_market_value' => $group->sum('total_market_value'),
'total_cost_basis' => $group->sum('total_cost_basis'),
'total_gain' => $group->sum('total_gain'),
'realized_gain_dollars' => $group->sum('realized_gain_dollars'),
'total_dividends_earned' => $group->sum('total_dividends_earned'),
];
})
->values();
return [
'series' => [
[
'name' => __('Market Value'),
'data' => $marketValueData,
'data' => $dailyChange->map(fn ($data) => [$data->date, $data->total_market_value])->toArray(),
],
[
'name' => __('Cost Basis'),
'data' => $costBasisData,
'data' => $dailyChange->map(fn ($data) => [$data->date, $data->total_cost_basis])->toArray(),
],
[
'name' => __('Market Gain'),
'data' => $marketGainData,
'data' => $dailyChange->map(fn ($data) => [$data->date, $data->total_gain])->toArray(),
],
// [
// 'name' => __('Dividends Earned'),
// 'data' => $dividendSeries
// 'data' => $dailyChange->map(fn($data) => [$data->date, $data->total_dividends_earned])->toArray()
// ],
// [
// 'name' => __('Realized Gains'),
// 'data' => $realizedGainSeries
// 'data' => $dailyChange->map(fn($data) => [$data->date, $data->realized_gains])->toArray()
// ],
],
];
@@ -105,8 +101,6 @@ new class extends Component
{
$this->scope = $scope;
cache()->forget('graph-'.$this->scope.'-'.(isset($this->portfolio) ? $this->portfolio->id : request()->user()->id));
$this->chartSeries = $this->generatePerformanceData();
}
+1 -1
View File
@@ -67,7 +67,7 @@
<x-card class="col-span-5 sm:col-span-1 bg-slate-100 dark:bg-base-200 rounded-lg">
<div class="text-sm text-gray-400 whitespace-nowrap truncate">{{ __('Market Gain/Loss') }}</div>
<div class="font-black text-xl"> {{ Number::currency($metrics->get('total_market_gain_dollars', 0)) }} </div>
<div class="font-black text-xl"> {{ Number::currency($metrics->get('total_gain_dollars', 0)) }} </div>
</x-card>
<x-card class="col-span-5 sm:col-span-1 bg-slate-100 dark:bg-base-200 rounded-lg">
@@ -97,7 +97,7 @@ new class extends Component
$transaction->transaction_type == 'BUY'
? $transaction->cost_basis
: $transaction->sale_price,
$transaction->market_data?->currency
$transaction->market_data->currency
) }})
<x-loading x-show="loading" x-cloak class="text-gray-400 ml-2" />
Binary file not shown.
+2 -5
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests;
use App\Models\User;
use Illuminate\Support\Facades\Log;
use Illuminate\Foundation\Testing\RefreshDatabase;
class AuthenticationTest extends TestCase
@@ -13,17 +14,15 @@ class AuthenticationTest extends TestCase
public function test_first_user_is_admin(): void
{
$response = $this->post('/register', [
$this->post('/register', [
'name' => 'should_be_admin',
'email' => 'should_be_admin@example.net',
'password' => 'password',
'password_confirmation' => 'password',
'terms' => 1,
]);
$should_be_admin = User::where(['email' => 'should_be_admin@example.net'])->first();
$this->assertModelExists($should_be_admin);
$this->assertTrue($should_be_admin->admin);
}
@@ -36,12 +35,10 @@ class AuthenticationTest extends TestCase
'email' => 'not_admin@example.net',
'password' => 'password',
'password_confirmation' => 'password',
'terms' => 1,
]);
$not_admin = User::where(['email' => 'not_admin@example.net'])->first();
$this->assertModelExists($not_admin);
$this->assertNotTrue($not_admin->admin);
}
+4 -5
View File
@@ -143,7 +143,6 @@ class DailyChangeTest extends TestCase
$daily_change = DailyChange::withDailyPerformance()
->portfolio($this->portfolio->id)
->whereDate('daily_change.date', '=', $sale_transaction->date->copy()->nextWeekday())
// ->withMultipleDailyPerformance()
->first();
$realized_gain = ($sale_transaction->sale_price - $sale_transaction->cost_basis) * $sale_transaction->quantity;
@@ -207,13 +206,13 @@ class DailyChangeTest extends TestCase
$portfolio = Portfolio::factory()->create();
// 1. test daily change will fill to the date of first transaction
$transactions = Transaction::factory(5)->buy()->lastMonth()->portfolio($portfolio->id)->symbol('AAPL')->create();
$first_transaction = Transaction::factory(5)->buy()->lastMonth()->portfolio($portfolio->id)->symbol('AAPL')->create();
$portfolio->syncDailyChanges();
$first_date = DailyChange::min('date');
$this->assertEquals($transactions->min('date')->next(Carbon::MONDAY)->startOfDay()->toDateString(), $first_date);
$this->assertEquals($first_transaction->min('date')->toDateString(), $first_date);
// 2. test daily change will fill when new transaction pre-dates earliest daily change
config()->set('app.env', 'local');
@@ -222,7 +221,7 @@ class DailyChangeTest extends TestCase
$second_transaction = Transaction::create([
'symbol' => 'AAPL',
'portfolio_id' => $portfolio->id,
'date' => now()->subDays(1080), // 3 years
'date' => now()->subYears(3),
'quantity' => 1,
'cost_basis' => 39.89,
'transaction_type' => 'BUY',
@@ -230,7 +229,7 @@ class DailyChangeTest extends TestCase
$second_date = DailyChange::min('date');
$this->assertEquals($second_transaction->date->next(Carbon::MONDAY)->toDateString(), $second_date);
$this->assertEquals($second_transaction->date->toDateString(), $second_date);
// 3. test daily change will fill when new transaction is between earliest daily change and earliest transaction
$third_transaction = Transaction::create([
-19
View File
@@ -70,23 +70,4 @@ class DividendsTest extends TestCase
$this->assertEquals(3, $dividend_count);
}
public function test_dividend_earnings_are_not_shared_between_portfolios(): void
{
$this->actingAs($user = User::factory()->create());
$portfolioOne = Portfolio::factory()->create();
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolioOne->id)->symbol('ACME')->create();
$portfolioTwo = Portfolio::factory()->create();
Transaction::factory(2)->buy()->sixMonthsAgo()->portfolio($portfolioTwo->id)->symbol('ACME')->create();
Dividend::refreshDividendData('ACME');
$holdingOne = Holding::query()->portfolio($portfolioOne->id)->symbol('ACME')->first();
$holdingTwo = Holding::query()->portfolio($portfolioTwo->id)->symbol('ACME')->first();
$this->assertEquals(4.95, $holdingOne->dividends_earned);
$this->assertEquals(8, $holdingTwo->dividends_earned);
}
}
-77
View File
@@ -1,77 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests;
use App\Models\Holding;
use App\Models\Portfolio;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
class HoldingsTest extends TestCase
{
use RefreshDatabase;
public function test_calculates_cost_basis(): void
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
Transaction::factory()->buy()->lastYear()->costBasis(200)->portfolio($portfolio->id)->symbol('AAPL')->create();
Transaction::factory()->buy()->lastMonth()->costBasis(300)->portfolio($portfolio->id)->symbol('AAPL')->create();
$holding = Holding::query()->getPortfolioMetrics();
$this->assertEquals(500, $holding->get('total_cost_basis'));
}
public function test_calculates_cost_basis_after_multiple_sales(): void
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
Transaction::factory()->buy()->lastYear()->costBasis(200)->portfolio($portfolio->id)->symbol('AAPL')->create();
Transaction::factory()->buy()->lastMonth()->costBasis(300)->portfolio($portfolio->id)->symbol('AAPL')->create();
Transaction::factory()->sell()->recent()->costBasis(250)->portfolio($portfolio->id)->symbol('AAPL')->create();
$holding = Holding::query()->getPortfolioMetrics();
$this->assertEquals(250, $holding->get('total_cost_basis'));
Transaction::factory()->sell()->recent()->costBasis(250)->portfolio($portfolio->id)->symbol('AAPL')->create();
$holding = Holding::query()->getPortfolioMetrics();
$this->assertEquals(0, $holding->get('total_cost_basis'));
}
public function test_calculates_cost_bases_on_same_day_buy_sell_transaction(): void
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
Transaction::factory(2)->buy()->lastYear()->costBasis(100)->portfolio($portfolio->id)->symbol('AAPL')->create();
Transaction::factory(2)->buy()->lastYear()->costBasis(300)->portfolio($portfolio->id)->symbol('AAPL')->create();
Transaction::factory()->sell()->lastYear()->portfolio($portfolio->id)->symbol('AAPL')->create();
Transaction::factory()->sell()->recent()->portfolio($portfolio->id)->symbol('AAPL')->create();
$holding = Holding::query()->getPortfolioMetrics();
$this->assertEquals(400, $holding->get('total_cost_basis'));
}
public function test_delete_holding_on_sync_if_no_transactions(): void
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
$transaction = Transaction::factory()->buy()->lastYear()->costBasis(100)->portfolio($portfolio->id)->symbol('AAPL')->create();
$this->assertDatabaseCount('holdings', 1);
$transaction->delete();
$this->assertDatabaseEmpty('holdings');
}
}
+3 -4
View File
@@ -78,20 +78,19 @@ class ImportExportTest extends TestCase
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::create([
Portfolio::create([
'id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
'title' => 'Test Portfolio',
]);
$holding = Holding::create([
'portfolio_id' => $portfolio->id,
'id' => '9cf8a662-7347-49fb-b9de-0cc1430a8d1f',
'portfolio_id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
'symbol' => 'ACME',
'quantity' => 0,
'reinvest_dividends' => false,
]);
Transaction::factory()->buy()->lastYear()->costBasis(100)->portfolio($portfolio->id)->symbol('AAPL')->create();
$this->assertEquals(false, $holding->reinvest_dividends);
BackupImportModel::create([
+1 -1
View File
@@ -21,7 +21,7 @@ class MarketDataTest extends TestCase
'--force' => true,
]);
$this->assertEquals(13187, MarketData::count('symbol'));
$this->assertEquals(14464, MarketData::count('symbol'));
}
public function test_can_get_quote_from_provider()
+42 -108
View File
@@ -133,8 +133,11 @@ class MultiCurrencyTest extends TestCase
$portfolio = Portfolio::factory()->create();
$transaction = Transaction::factory()->buy()->lastYear()->portfolio($portfolio->id)->symbol('ACME')->create();
$expected_num_calls = count(collect(CarbonPeriod::create($transaction->date, now()))->chunk(500));
Frankfurter::expects('setSymbols')
->andReturnSelf();
->andReturnSelf()
->times($expected_num_calls);
Frankfurter::expects('timeSeries')
->andReturn(['rates' => [
now()->subDays(3)->toDateString() => [
@@ -149,7 +152,8 @@ class MultiCurrencyTest extends TestCase
now()->toDateString() => [
'ZZZ' => .01,
],
]]);
]])
->times($expected_num_calls);
CurrencyRate::timeSeriesRates(
'', // use fake currency to force
@@ -225,71 +229,8 @@ class MultiCurrencyTest extends TestCase
->andReturn(['rates' => $results]);
$result = CurrencyRate::timeSeriesRates('ZZZ', $start, $end);
$this->assertEquals(count($period) - 1, count($result));
$result = CurrencyRate::all();
$this->assertEquals(count($period), count($result));
}
public function test_can_get_time_series_rates_with_null_currency()
{
$start = now()->subWeeks(2);
$end = now();
$period = CarbonPeriod::create($start, $end);
// mock response from Frankfurter
$results = [];
collect($period->copy()->filter('isWeekday'))->each(function ($date) use (&$results) {
$date = $date->toDateString();
$results[$date] = [
'FOO' => random_int(10, 150) / 1000,
];
});
Frankfurter::expects('setSymbols')
->andReturnSelf();
Frankfurter::expects('timeSeries')
->andReturn(['rates' => $results]);
$result = CurrencyRate::timeSeriesRates(null, $start, $end);
$this->assertEquals(0, count($result));
$result = CurrencyRate::all();
$this->assertEquals(count($period), count($result));
}
public function test_can_get_time_series_rates_with_currencies()
{
$start = now()->subWeeks(2);
$end = now();
$period = CarbonPeriod::create($start, $end);
// mock response from Frankfurter
$results = [];
collect($period->copy()->filter('isWeekday'))->each(function ($date) use (&$results) {
$date = $date->toDateString();
$results[$date] = [
'FOO' => random_int(10, 150) / 1000,
'BAR' => random_int(10, 150) / 1000,
];
});
Frankfurter::expects('setSymbols')
->andReturnSelf();
Frankfurter::expects('timeSeries')
->andReturn(['rates' => $results]);
$result = CurrencyRate::timeSeriesRates(null, $start, $end);
$this->assertEquals(0, count($result));
$result = CurrencyRate::all();
$this->assertEquals(count($period) * 2, count($result));
}
public function test_time_series_rate_calls_are_chunked()
@@ -311,9 +252,11 @@ class MultiCurrencyTest extends TestCase
});
Frankfurter::expects('setSymbols')
->andReturnSelf();
->andReturnSelf()
->times(4);
Frankfurter::expects('timeSeries')
->andReturn(['rates' => $results]);
->andReturn(['rates' => $results])
->times(4);
CurrencyRate::timeSeriesRates('ZZZ', $start, $end);
}
@@ -544,20 +487,28 @@ class MultiCurrencyTest extends TestCase
$this->actingAs($user = User::factory()->create());
$monthAgo = now()->subMonth()->toDateString();
$fiveWeeksAgo = now()->subWeeks(5)->toDateString();
$fiveDaysAgo = now()->subDays(5)->toDateString();
$portfolio = Portfolio::factory()->create();
Transaction::factory(5)->buy()->costBasis(100)->date($monthAgo)->portfolio($portfolio->id)->symbol('AAPL')->create();
Transaction::factory(5)->buy()->costBasis(190)->date($fiveWeeksAgo)->portfolio($portfolio->id)->symbol('ACME')->create();
Transaction::factory()->sell()->date($fiveDaysAgo)->portfolio($portfolio->id)->symbol('ACME')->create();
Transaction::factory(5)->buy()->lastMonth()->portfolio($portfolio->id)->symbol('AAPL')->create();
Transaction::factory(5)->buy()->lastMonth()->portfolio($portfolio->id)->symbol('ACME')->create();
Transaction::factory()->sell()->recent()->portfolio($portfolio->id)->symbol('ACME')->create();
$portfolio->syncDailyChanges();
$dailyChange = DailyChange::withDailyPerformance()
->portfolio($portfolio->id)
->get();
->get()
->sortBy('date')
->groupBy('date')
->map(function ($group) {
return (object) [
'date' => $group->first()->date->toDateString(),
'total_market_value' => $group->sum('total_market_value'),
'total_cost_basis' => $group->sum('total_cost_basis'),
'total_gain' => $group->sum('total_gain'),
'realized_gain_dollars' => $group->sum('realized_gain_dollars'),
'total_dividends_earned' => $group->sum('total_dividends_earned'),
];
});
$metrics = Holding::query()
->portfolio($portfolio->id)
@@ -565,37 +516,8 @@ class MultiCurrencyTest extends TestCase
$this->assertEqualsWithDelta($metrics->get('total_market_value'), $dailyChange->last()->total_market_value, 0.01);
$this->assertEqualsWithDelta($metrics->get('total_cost_basis'), $dailyChange->last()->total_cost_basis, 0.01);
$this->assertEqualsWithDelta(Holding::get()->sum('total_cost_basis'), $dailyChange->last()->total_cost_basis, 0.01);
$this->assertEqualsWithDelta($metrics->get('realized_gain_dollars'), $dailyChange->last()->realized_gain_dollars, 0.01);
$this->assertEqualsWithDelta($metrics->get('total_market_value') - $metrics->get('total_cost_basis'), $dailyChange->last()->total_market_gain, 0.01);
// add currency rates
$rates = collect([[
'currency' => 'GBP',
'rate' => .88,
'date' => $fiveWeeksAgo,
], [
'currency' => 'GBP',
'rate' => .88,
'date' => $fiveDaysAgo,
], [
'currency' => 'GBP',
'rate' => .88,
'date' => $monthAgo,
], [
'currency' => 'GBP',
'rate' => .88,
'date' => now()->subDay()->toDateString(),
], [
'currency' => 'GBP',
'rate' => .88,
'date' => now()->toDateString(),
], [
'currency' => 'GBP',
'rate' => .88,
'date' => now()->addDay()->toDateString(),
]]);
$rates->each(fn ($rate) => CurrencyRate::create($rate));
$this->assertEqualsWithDelta($metrics->get('total_market_value') - $metrics->get('total_cost_basis'), $dailyChange->last()->total_gain, 0.01);
// switch user display currency
$user->options = array_merge($user->options ?? [], [
@@ -605,7 +527,19 @@ class MultiCurrencyTest extends TestCase
$dailyChange = DailyChange::withDailyPerformance()
->portfolio($portfolio->id)
->get();
->get()
->sortBy('date')
->groupBy('date')
->map(function ($group) {
return (object) [
'date' => $group->first()->date->toDateString(),
'total_market_value' => $group->sum('total_market_value'),
'total_cost_basis' => $group->sum('total_cost_basis'),
'total_gain' => $group->sum('total_gain'),
'realized_gain_dollars' => $group->sum('realized_gain_dollars'),
'total_dividends_earned' => $group->sum('total_dividends_earned'),
];
});
$metrics = Holding::query()
->portfolio($portfolio->id)
@@ -614,7 +548,7 @@ class MultiCurrencyTest extends TestCase
$this->assertEqualsWithDelta($metrics->get('total_market_value'), $dailyChange->last()->total_market_value, 0.01);
$this->assertEqualsWithDelta($metrics->get('total_cost_basis'), $dailyChange->last()->total_cost_basis, 0.01);
$this->assertEqualsWithDelta($metrics->get('realized_gain_dollars'), $dailyChange->last()->realized_gain_dollars, 0.01);
$this->assertEqualsWithDelta($metrics->get('total_market_value') - $metrics->get('total_cost_basis'), $dailyChange->last()->total_market_gain, 0.01);
$this->assertEqualsWithDelta($metrics->get('total_market_value') - $metrics->get('total_cost_basis'), $dailyChange->last()->total_gain, 0.01);
}
public function test_multi_currency_import_calculates_correct_holding_data(): void