Add exchange and currency fields to trade imports #1822
Reference in New Issue
Block a user
Delete Branch "feature/add-trade-import"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Related Issues
Fixes #1718
/claim #1718
https://github.com/user-attachments/assets/e306e7b6-0c87-45dc-a10c-b456ed39b2cc
Hey @onyedikachi-david thanks for the submission! Just an update—we're currently working on some related data issues regarding the
Securitymodel over in #1826, so we'll need to put this one on hold for the moment as I believe the fields required for this will need to change / be updated based on where we land with that.I'll post here when we're ready to resume work and add some comments to the PR!
Hey @onyedikachi-david, I think we're able to resume work on this now that the securities updates were pushed to production.
I would check out #1826 for some context to what changed, but in summary:
Securitytickers unique by[ :ticker, :exchange_operating_mic ]exchange_operating_micis set toniland the Security'shas_prices?method returns false.In order to properly import these trades, we will now need the following information from the user's CSV:
currency- what currency is the price in? (we already should have this field)ticker- the security ticker (we already have this)AAPLtrades onXNAS(operating mic), which is the broad name for "Nasdaq"For this PR, I think the updates we need to make are:
exchange_operating_micis NOT provided by the user, we should set it tonilwhen wefind_or_create_bythe Securityexchange_operating_micis present, we need to make a call to Synth to check if thisticker/exchange_operating_miccombination is present with a valid price.nil) regardless of whether it could be validhttps://github.com/user-attachments/assets/f642bbb6-bd40-491c-81ee-9a9ebbacdd14
@zachgoll I just pushed some changes addressing the updates.
@@ -29,6 +29,7 @@ class Import::ConfigurationsController < ApplicationController:account_col_label,I think this needs updating
I'm second guessing this. I know I said in the requirements that we need a per-cell validation, but I think our best bet is to remove this and check for tickers directly during the import operation.
This will make a Synth call for every single row in the import, which could quickly exhaust a user's Synth credits. I think I'd rather just remove this for now.
As I mentioned above in another comment, I think we need to remove the row-level validation, which means by the time we get to this
import!operation, we're not 100% certain whether the tickers are correct.I think we need to rework the flow of this method a little bit to something like this:
We'll need to update the
search_securitiesmethod on the Synth provider:exchange_operating_micparam (also optional)We'll want to remove this field and index. When we search our provider for a specific security, it will return a
currencyvalue that we then load directly into theSecurity::Pricemodel.This way, we have the
priceandcurrencysitting on the same table where it is needed to constructMoneyobjects.This is outdated I believe. I think we can remove this column and all related code now?
I think we can skip this step of migrating old imports and just let the user fix it manually. Most users will likely need to reconfigure their import data anyways.
Overall looking good! Once changes are addressed we'll do a final sweep + testing and get it merged.
Let's remove this so that users can import a CSV of trades where some have exchange mic codes provided (will have historical prices) while others don't (custom, or unknown tickers).
Our backend logic during our "sync" process will handle this okay.
@@ -41,12 +51,63 @@ class TradeImport < ImportWe'll want to remove the
exchange_operating_micfrom this so that ourSecuritymodel properly identifies it as "missing prices":79e1a2c0ff/app/models/security.rb (L38-L40)Would you mind consolidating all of these migrations into one? Should be able to run
bin/rails db:rollback STEP=8, delete all but one, consolidate, then runbin/rails db:migrateagain.If you want a clean slate,
bin/rails db:migrate:resetalso works.I think the
currencyandnumber_formatmight be stale from a prior PR correct? I'm guessing once you clean up migrations this should be resolved.Did some local testing of this and I think we may need to bring back a portion of your original solution where we check to see if the security exists in the Maybe DB first prior to hitting Synth. Here's the final flow I'm thinking:
We should also add
test/models/trade_import_test.rbwith a basic test to validate all of this. Here's a starter test that will need to be adapted to this new code:There are quite a few changes here that are not needed. Let's keep this PR's migration solely to deal with:
exchangefromimport_rowsexchange_col_labelfromimportsexchange_operating_mictoimport_rowsexchange_operating_mic_col_labeltoimportsWe'll need to add this one back in as it was a migration from a prior commit
I don't think this migration is necessary. Given we aren't updating
number_formatin this PR there shouldn't be any issues with the data.addressed all, with the test.
I pushed #1876 to address some general issues with imports causing this trade import to fail. You'll need to accept a "combination merge" to resolve conflicts when pulling in the
mainbranch.Is this being used anywhere? I think we already have a variation of this in our helpers module.
This file should not be deleted as it is a prior migration on the
mainbranch. You can add back with:So what I meant here is that we need to get rid of this migration from this pull request entirely.
We should not be adjusting prior migrations that are already committed to the
mainbranch:https://github.com/maybe-finance/maybe/blob/main/db/migrate/20250207194638_adjust_securities_indexes.rb
You can revert this and get back to a clean state with this:
@@ -0,0 +3,4 @@add_column :import_rows, :exchange_operating_mic, :stringadd_column :imports, :exchange_operating_mic_col_label, :stringendendWith rails we should always prefer a
changemigration as it's much easier to read and reason about. Here's what this migration should be:okay
@@ -41,12 +51,63 @@ class TradeImport < ImportWe need to explicitly set
exchange_operating_mic: nilhere as I had in my snippet below to prevent invalid exchange mics from being persisted to the DB.The issue with adding in this logic is in the case where the user has provided a valid ticker + exchange mic in their CSV that doesn't exist in our current DB. For example, let's say they provide
AAPL+XNAS(valid in Synth) and it is not currently in our DB, but there is aSecurityin our DB of[AAPL,nil]. This logic will pick that existing security up even though what we want to do is go fetch from Synth to find the new security.I think given that scenario, we should remove this and go with something like the example code I provided: https://github.com/maybe-finance/maybe/pull/1822/files#r1963740617
Migrations are looking good now, but it appears that this is still from a stale migration. Running
bin/rails db:migrate:resetshould fix and remove this. If it doesn't let me know.As you can see in production:
8539ac7dec/db/schema.rb (L417)This default doesn't exist. And we didn't alter this at all in this PR, so it shouldn't be showing up in the output schema.
Looks good now, nice work!
The import was failing in the UI due to issues with security provider responses.
The test suite continues to pass.
@onyedikachi-david nice work! If you have any issues with the bounty payment be sure to let me know.