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
def get_transaction_data(txn_hash): | |
"gets the data for the specified transaction hash" | |
# the url with the given hash | |
url = f"https://web3api.io/api/v2/transactions/{txn_hash}" | |
# the headers | |
headers = { | |
"x-amberdata-blockchain-id": "bitcoin-mainnet", | |
"x-api-key": api_key["AMBERDATA_API_KEY"] | |
} | |
# get the response |
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
def parse_addresses(i: int = 0, n: int = 0) -> str: | |
"""Returns the address from record i in position n""" | |
# parse the list of addresses | |
address_list = data_sorted.iloc[i, 1].split("['")[-1].split("']")[0] | |
return address_list.split("', '")[n] | |
# the top transaction number we consider | |
i = 0 | |
# the sender's address | |
sender = parse_addresses(i) |
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
i = 0 # initializing i | |
win_size = 7 # the window size after a large txn | |
windows = [] # an array for saving our windows | |
# iterate through the ohlcv data | |
while i <= len(ohlcv): | |
# if the transaction is a large transaction | |
if ohlcv.iloc[i, -1] == True: | |
# save the txn and the minutes after | |
windows.append(ohlcv.iloc[i:i+win_size]) |
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
async def listen(api_key): | |
"Opens the websocket connection and listens for pending transactions" | |
# the amberdata websocket uri | |
uri = 'wss://ws.web3api.io' | |
# our headers for the connection | |
headers = { | |
"x-api-key": api_key["AMBERDATA_API_KEY"], | |
"x-amberdata-blockchain-id": "408fa195a34b533de9ad9889f076045e" | |
} | |
# outer loop |
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
dfs = [] | |
n_days = 5 | |
for i in range(n_days): | |
print(f"Iteration: {i}") | |
# get the start and end dates in timestamps | |
startDate = results.timestamp.min() + timedelta(i-1) | |
endDate = startDate + timedelta(1) | |
# convert to UNIX format |
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
# helper functions | |
def to_df(response): | |
"Turns the repsonse into a dataframe" | |
# load the data into a DataFrame and rename the columns | |
data = pd.DataFrame(response["data"]).rename({0: "date", 1: "volatility", 2: "sharpe"}, axis=1) | |
# turn the date column into datetime type | |
data["date"] = pd.to_datetime(data["date"]) | |
# set the date as the index | |
data.set_index("date", inplace=True) | |
return data |
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
# helper functions | |
def CAGR(DF): | |
"Calculates the Compound Annual Growth Rate" | |
df = DF.copy() | |
df['daily_ret'] = df['Adj Close'].pct_change() | |
df['cum_return'] = (1 + df['daily_ret']).cumprod() | |
return (df['cum_return'])**(252/len(df)) - 1 | |
def volatility(DF): | |
"Calculates the daily Volatility over a year, given we trade 252x a year" |
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
# The S&P 500 Index | |
spy = pdr.get_data_yahoo("SPY", start, end) | |
# Gold futures | |
gold = pdr.get_data_yahoo("GC=F", start, end) | |
# Apple Stock | |
aapl = pdr.get_data_yahoo("AAPL", start, end) | |
# Bond index | |
bond = pdr.get_data_yahoo("BND", start, 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
# load the data into a DataFrame and rename the columns | |
data = pd.DataFrame(response["data"]).rename({0: "date", 1: "volatility", 2: "sharpe"}, axis=1) | |
# turn the date column into datetime type | |
data["date"] = pd.to_datetime(data["date"]) | |
# set the date as the index | |
data.set_index("date", inplace=True) |
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
# our first token to try out | |
token = "btc" | |
# the url for our token | |
url = f"https://web3api.io/api/v2/market/metrics/{token}/historical/sharpe" | |
# our API key | |
headers = {'x-api-key': api_key["AMBERDATA_API_KEY"]} | |
# the start and end of the period |