Skip to content

Instantly share code, notes, and snippets.

@BryantAvey
Last active April 24, 2022 20:24
Show Gist options
  • Save BryantAvey/9f878f8d3c014c420a6f4d94c6a17749 to your computer and use it in GitHub Desktop.
Save BryantAvey/9f878f8d3c014c420a6f4d94c6a17749 to your computer and use it in GitHub Desktop.
TD Ameritrade API with Neo4j: Cypher to create NEXT_PERIOD Relationships between Price History Nodes
// Create NEXT_PERIOD relationships
call apoc.periodic.iterate("MATCH (i:Instrument) RETURN i.symbol as Symbol",
"MATCH (price:Price {ticker:Symbol})
WITH price
ORDER BY price.ticker, price.timestamp
WITH collect(price) as prices
FOREACH(i IN range(0, size(prices)-2) |
FOREACH(price1 IN [prices[i]] |
FOREACH(price2 IN [prices[i+1]] |
MERGE (price1)-[:NEXT_PERIOD {priceGap:round(price2.open - price1.close,2)}]->(price2)
)
)
)"
,{batchSize:10000, parallel:false});
// Remove NEXT_PERIOD relationships between instruments that get created
MATCH (p1:Price)-[r:NEXT_PERIOD]->(p2) WHERE p1.ticker <> p2.ticker DELETE r;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment