Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c691ee922a | |||
| 3eb9bad840 | |||
| 370f7bb54b | |||
| 62bf6797e6 | |||
| c4e3645145 | |||
| 69e4d0fb3a | |||
| 20c2cb37cc | |||
| d2bb065822 | |||
| 0c00f28d97 | |||
| 5eab00ee33 | |||
| 56064ad84e | |||
| c96ff0e45f | |||
| 33e0df5ae2 | |||
| a5a333f784 | |||
| 89b5505e1d | |||
| 60923b3c93 |
+39
-22
@@ -119,58 +119,74 @@ 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;
|
||||
}
|
||||
|
||||
dump('diff curr');
|
||||
|
||||
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
|
||||
|
||||
$currencies = Currency::all()->pluck('currency')->toArray();
|
||||
|
||||
dump('got currencies');
|
||||
|
||||
// call api in chunks
|
||||
$rates = [];
|
||||
foreach (collect($period)->chunk(500) as $chunk) {
|
||||
|
||||
dump('calling frankf time series');
|
||||
|
||||
$chunkRates = Frankfurter::setSymbols($currencies)->timeSeries($chunk->min(), $chunk->max());
|
||||
|
||||
$rates = array_merge($rates, Arr::get($chunkRates, 'rates', []));
|
||||
}
|
||||
$rates = Arr::get($chunkRates, 'rates', []);
|
||||
|
||||
// loop through each date
|
||||
$updates = [];
|
||||
foreach ($period as $date) {
|
||||
// loop through each date
|
||||
$updates = [];
|
||||
|
||||
$lookupDate = self::getNearestPastDate($date, $rates);
|
||||
foreach ($chunk as $date) {
|
||||
|
||||
if (is_null($lookupDate)) {
|
||||
continue;
|
||||
}
|
||||
$lookupDate = self::getNearestPastDate($date, $rates);
|
||||
|
||||
// loop through each rate
|
||||
foreach ($rates[$lookupDate->toDateString()] as $curr => $rate) {
|
||||
if (is_null($lookupDate)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// loop through each rate
|
||||
foreach ($rates[$lookupDate->toDateString()] as $curr => $rate) {
|
||||
|
||||
// add to updates
|
||||
$updates[] = [
|
||||
'currency' => $curr,
|
||||
'date' => $date->toDateString(),
|
||||
'rate' => $rate,
|
||||
'updated_at' => now()->toDateTimeString(),
|
||||
'created_at' => now()->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
|
||||
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');
|
||||
|
||||
return collect($updates)
|
||||
->whereBetween('date', [$start, $end ?? now()])
|
||||
@@ -183,6 +199,7 @@ class CurrencyRate extends Model
|
||||
|
||||
private static function getNearestPastDate(CarbonInterface $date, array $rates): ?CarbonInterface
|
||||
{
|
||||
|
||||
$datesWithRates = array_keys($rates);
|
||||
sort($datesWithRates);
|
||||
|
||||
|
||||
+25
-5
@@ -95,19 +95,36 @@ 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);
|
||||
|
||||
// get historic conversion rates
|
||||
$rate_to_base = CurrencyRate::timeSeriesRates($market_data->currency, $start_date, $end_date);
|
||||
dump('4. got market data for '.$symbol);
|
||||
|
||||
// todo: use this for start_date - $dividend_data->first()->get('date')
|
||||
|
||||
// get historic conversion rates
|
||||
$rate_to_base = CurrencyRate::timeSeriesRates($market_data->currency, $dividend_data->first()->get('date'), $end_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);
|
||||
@@ -120,6 +137,7 @@ class Dividend extends Model
|
||||
// insert records
|
||||
(new self)->insertOrIgnore($dividend_data->toArray());
|
||||
|
||||
dump('6. inserted for '.$symbol);
|
||||
// sync to holdings
|
||||
self::syncHoldings($symbol);
|
||||
|
||||
@@ -129,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
|
||||
|
||||
Reference in New Issue
Block a user