-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle missing weekend stock prices in sync process (#1242)
* Don't append missing prices if already known * Add failing test * Handle weekend stock prices * Fix tests and gapfill logic
- Loading branch information
Showing
5 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class Gapfiller | ||
attr_reader :series | ||
|
||
def initialize(series, start_date:, end_date:, cache:) | ||
@series = series | ||
@date_range = start_date..end_date | ||
@cache = cache | ||
end | ||
|
||
def run | ||
gapfilled_records = [] | ||
|
||
date_range.each do |date| | ||
record = series.find { |r| r.date == date } | ||
|
||
if should_gapfill?(date, record) | ||
prev_record = gapfilled_records.find { |r| r.date == date - 1.day } | ||
|
||
if prev_record | ||
new_record = create_gapfilled_record(prev_record, date) | ||
gapfilled_records << new_record | ||
end | ||
else | ||
gapfilled_records << record if record | ||
end | ||
end | ||
|
||
gapfilled_records | ||
end | ||
|
||
private | ||
attr_reader :date_range, :cache | ||
|
||
def should_gapfill?(date, record) | ||
date.on_weekend? && record.nil? | ||
end | ||
|
||
def create_gapfilled_record(prev_record, date) | ||
new_record = prev_record.class.new(prev_record.attributes.except("id", "created_at", "updated_at")) | ||
new_record.date = date | ||
new_record.save! if cache | ||
new_record | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters