Skip to content

Instantly share code, notes, and snippets.

View BryantAvey's full-sized avatar

Bryant Avey BryantAvey

View GitHub Profile
@BryantAvey
BryantAvey / TDA_Create_PRICE_HISTORY_Relationships
Last active April 24, 2022 20:24
TD Ameritrade API with Neo4j: Cypher to create PRICE_HISTORY Relationships between Instrument and Price
// Create PRICE_HISTORY relationship between instrument and Price
MATCH (i:Instrument), (p:Price) WHERE i.symbol = p.ticker
MERGE (i)-[:PRICE_HISTORY {tradingDate:p.timestamp}]->(p)
@BryantAvey
BryantAvey / TDA_Cypher_Create_NEXT_PERIOD_Relationships
Last active April 24, 2022 20:24
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)
@BryantAvey
BryantAvey / TDA_Cypher_Load_Daily_Price_History_for_all_Instruments
Last active April 24, 2022 20:24
TD Ameritrade API with Neo4j: Cypher to load daily price history for each Instrument node
//Load Daily Price History for every Instrument
CALL apoc.periodic.iterate("MATCH (i:Instrument) RETURN i.symbol as Symbol"
, "CALL apoc.load.jsonParams('https://api.tdameritrade.com/v1/marketdata/'+ Symbol + '/pricehistory?apikey=' + 'YOUR_CONSUMER_KEY_GOES_HERE' + '&frequencyType=daily&periodType=year&frequency=1&endDate=' + timestamp() + '&startDate=' + dateTime('2022-04-01').epochMillis , {Authorization:" + '\''+$token[0].access_token+'\'' + "} ,null)
YIELD value
UNWIND value.candles AS cndl
FOREACH (candle in cndl | MERGE (s:Price {ticker: value.symbol,timestamp: datetime.fromepochmillis(candle.datetime) })
ON CREATE SET
s.open = candle.open,
s.close = candle.close,
s.volume = candle.volume,
@BryantAvey
BryantAvey / TDA_Cypher_Load_Instrument_Fundamental_Data
Last active April 24, 2022 20:25
TD Ameritrade API with Neo4j: Cypher to Load Instrument nodes with fundamental data
// Load instrument and fundamental
CALL apoc.periodic.iterate("UNWIND ['MSFT','SPY','TSLA','AAPL','QQQ','NFLX','NVDA'] AS Symbol RETURN Symbol"
,"CALL apoc.load.jsonParams('https://api.tdameritrade.com/v1/instruments?apikey='+ 'YOUR_CONSUMER_KEY_GOES_HERE' +'&symbol=' + Symbol + '&projection=fundamental',{Authorization:'$token[0].access_token' },null)
YIELD value
UNWIND value[Symbol] AS instrument
UNWIND instrument.fundamental as fundamental
MERGE (i:Instrument {symbol:instrument.symbol})
SET
i.cusip=instrument.cusip,
i.description=instrument.description,
@BryantAvey
BryantAvey / TDA_Cypher_Create_Param_Access_Token
Last active April 24, 2022 20:25
TD Ameritrade API with Neo4j: Cypher to create a parameter for a reusable Access Token
// Parameterize the access bearer token
:param token => {CALL apoc.load.jsonParams("https://api.tdameritrade.com/v1/oauth2/token",null,"client_id=YOUR_CONSUMER_KEY_GOES_HERE%40AMER.OAUTHAP&grant_type=refresh_token&refresh_token=" + apoc.text.urlencode("YOUR_90_DAY_REFRESH_TOKEN_GOES_HERE_FROM_PREVIOUS_STEP"))
YIELD value
RETURN "Bearer " + value.access_token as access_token}
@BryantAvey
BryantAvey / TDA_Generate_Refresh_Token_with_Cypher
Last active April 24, 2022 20:25
TD Ameritrade API with Neo4j: Generate a new refresh token using token code
// Generate New Refresh Token
CALL apoc.load.jsonParams("https://api.tdameritrade.com/v1/oauth2/token",null,"grant_type=authorization_code&refresh_token=&access_type=offline&code=" + "Paste_Your_CODE_Here" + "&client_id=YOUR_CONSUMER_KEY_GOES_HERE %40AMER.OAUTHAP&redirect_uri=YOUR_CALLBACK_URL_GOES_HERE")
YIELD value
UNWIND value.refresh_token as refresh_token_90days
//UNWIND value.access_token as access_token_30m
RETURN refresh_token_90days //, access_token_30m
@BryantAvey
BryantAvey / TDA_Token_Code_Generation_Link
Last active April 24, 2022 20:25
TD Ameritrade Token Code Generation Link
@BryantAvey
BryantAvey / Neo4jJoltPropertiesTemplate.pq
Last active March 29, 2023 09:22
Power Query to import Neo4j data using named properties in a Cypher query. Uses the Jolt HTTP API header setting.
//Use Neo4j's jolt formating with the HTTP API to get properties
//This Power Query uses Accept Header "application/vnd.neo4j.jolt" in the API Call, which makes for a cleaner and shorter PQ query
let
//Enter the base URL for the Neo4j Server
__Neo4jURL = "http://localhost:7474",
// Enter the name of the database in Neo4j
__Neo4jDatabase = "blogs",
// Enter the username for database access in the Neo4j database
__Username = "neo4j",
@BryantAvey
BryantAvey / Neo4jPropertiesTemplate.pq
Last active May 17, 2021 21:03
Import Neo4j to Power BI using a named property Cypher query. This uses the default HTTP API with Neo4j.
// This Power Query connects to Neo4j returning data from a Cypher query that returns named properties id(n), n.name, etc.
// Get Properties and Column Names
let
//Enter the base URL for the Neo4j Server
__Neo4jURL = "http://localhost:7474",
// Enter the name of the database in Neo4j
__Neo4jDatabase = "blogs",
// Enter the username for database access in the Neo4j database
__Username = "neo4j",
// Enter the user password for databases in the Neo4j database
@BryantAvey
BryantAvey / Neo4jNodesTemplate.pq
Last active May 17, 2021 21:03
Import Neo4j Nodes Data to Power BI. This power query uses the default HTTP API to get nodes only data from Neo4j.
// This Power Query script connects to Neo4j and runs a Cypher query that returns node objects.
// Get Nodes
let
//Enter the base URL for the Neo4j Server
__Neo4jURL = "http://localhost:7474",
// Enter the name of the database in Neo4j
__Neo4jDatabase = "blogs",
// Enter the username for database access in the Neo4j database
__Username = "neo4j",
// Enter the user password for databases in the Neo4j database