Creating a trading stock portfolio in Neo4j

Hi everyone,
I am a newbie in Neo4j and trying to learn Neo4j by converting a relational database into a neo4j model.


I am confused when a table is a node or a relation.

So far I have nodes for

  1. Trader
  2. Country
  3. Currency
  4. Item (Stock, Currency, or fund)
  5. Price(? should this be relation)
  6. Offer(Should trade or offer to be a relation)
  7. Rate (Currency conversion rate)

Relations

  1. CURRENCY_USED
  2. ITEM_PRICE
  3. ITEM_CURRENCY
  4. TRADER_COUNTRY
  5. TRADER_CURRENCY
  6. TRADE

Report and CurrentInventory, no clue whether they belong to either node or relation.

    CREATE (USA:Country)
    CREATE (India:Country)
    
    CREATE (USD:Currency {code: "USD", name: "Dollar", is_active: true, is_base_currency: true})
    CREATE (INR:Currency {code: "INR", name: "Rupee", is_active: true, is_base_currency: true})
    
    CREATE (USD) -[:CURRENCY_USED {date_from: "", date_to: ""}]-> (USA)
    CREATE (INR) -[:CURRENCY_USED {date_from: "", date_to: ""}]-> (India)
    
    CREATE (INR:CurrencyRate {code: "INR", name: "Rupee", is_active: true, is_base_currency: true})
    CREATE (INR:CurrencyRate {code: "INR", name: "Rupee", is_active: true, is_base_currency: true})
    
    CREATE (Tesla:Item {code: "TSLA", name: "Tesla", is_active: true })
    CREATE (TeslaPrice:Price {buy: 400, sell: 399, timestamp: datetime()})
    
    CREATE (Tesla) -[:ITEM_PRICE]-> (TeslaPrice)
    
    CREATE (TeslaPrice) -[:ITEM_CURRENCY]-> (USD)
    
    CREATE (Adam:Trader {first_name: "Adam", last_name: "West", user_name: "adam",  password: "12345"})
    
    CREATE (Adam) -[:TRADER_COUNTRY]-> (USA)
    CREATE (Adam) -[:TRADER_CURRENCY]-> (USD)
    
    CREATE (AdamPortfolio:Portfolio {quantity: 10})
    CREATE (AdamPortfolio:Portfolio) -[:PORTFOLIO_ITEM]-> (Tesla)
    CREATE (AdamPortfolio:Portfolio) -[:TRADER_PORTFOLIO]-> (Adam)
    CREATE (AdamBuyTesla:Trade {qunatity: 10, unit_price: 400})
    CREATE (AdamBuyTeslaOffer:Offer {qunatity: 10, unit_price: 400, buy: true, sell: false, timestamp: datetime()})
    
    CREATE (AdamBuyTesla) -[:TRADE_OFFER]-> (AdamBuyTeslaOffer)

Hi @siddiqui.salman

I think the design of the graph depends on what kind of queries you do.
What type of queries do you use most often?
Also, what is the volume and speed of your records?

It is a good start. The best part is you can start with a schema and then refactor as you figure out the questions you want to ask.

1 Like

Thank you very much for the reply. Most would be read queries on the Items with few trades which will be create and updates.
Would like to maintain a portfolio of all the trades which is done in a separate table (CurrentInventory) in relational database.
Also other option is to just create Node for every table and create relations for the foreign key.
Atleast would be simple for my understanding as I have bit of experience with Relational database