Created
October 2, 2020 04:10
-
-
Save benjiqq/7e0bd4cefaf85d288a1ffa4dbf00364f to your computer and use it in GitHub Desktop.
uniswap candle
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
import requests | |
import json | |
import datetime | |
import pandas as pd | |
import numpy as np | |
BASE_URL = "https://api.thegraph.com/subgraphs/name/ianlapham/uniswapv2" | |
txnum = 100 | |
#addr = "0x6a3d23fa07c455f88d70c29d230467c407a3964b" | |
addr = "0x32ce7e48debdccbfe0cd037cc89526e4382cb81b" | |
query_swaps = ''' | |
{ | |
swaps(first: ''' + str(txnum) + ''', orderBy: timestamp, orderDirection: desc, where: | |
{ pair: "''' + addr + '''" }) { | |
pair { | |
token0 { | |
symbol | |
} | |
token1 { | |
symbol | |
} | |
} | |
amount0In | |
amount0Out | |
amount1In | |
amount1Out | |
amountUSD | |
to | |
timestamp | |
transaction { | |
id | |
} | |
} | |
} | |
''' | |
query_chart = ''' | |
{ | |
pairDayDatas(first: 1000, orderBy: date, orderDirection: asc, where: { pairAddress: "0x6a3d23fa07c455f88d70c29d230467c407a3964b" }) { | |
id | |
date | |
dailyVolumeToken0 | |
dailyVolumeToken1 | |
dailyVolumeUSD | |
reserveUSD | |
} | |
} | |
''' | |
def get_query(): | |
with open('q_tx.gql','r') as f: | |
return f.read() | |
def make_query(query): | |
response=requests.post(BASE_URL, json={'query': query}) | |
#print (response.content) | |
z = response.content.decode("utf-8") | |
x = json.loads(z)['data'] | |
return x | |
def get_chart(): | |
data = make_query(query_chart)['pairDayDatas'] | |
return data | |
def get_swaps(): | |
swaps = make_query(query_swaps)['swaps'] | |
print (len(swaps)) | |
txlist = list() | |
reverse = False | |
for swap in swaps[:]: | |
#t = swap['timestamp'] | |
t = int(swap['timestamp']) | |
#date = datetime.datetime.fromtimestamp(t) | |
#sdate = date.strftime("%m/%d/%Y, %H:%M:%S") | |
sell_eth_qty = float(swap['amount0Out']) | |
#sell_token_qty = round(float(swap['amount1In']),1) | |
sell_token_qty = float(swap['amount1In']) | |
buy_eth_qty = float(swap['amount0In']) | |
#buy_token_qty = round(float(swap['amount1Out']),1) | |
buy_token_qty = float(swap['amount1Out']) | |
#ausd = float(swap['amountUSD']) | |
txid = swap['transaction']['id'] | |
if float(swap['amount0In']) == 0.0: | |
if reverse: | |
buysell = "SELL" | |
else: | |
buysell = "BUY" | |
price_sell_eth = sell_token_qty/sell_eth_qty | |
arr = [t,buysell,sell_token_qty,sell_eth_qty,price_sell_eth,txid] | |
txlist.append(arr) | |
else: | |
if reverse: | |
buysell = "BUY" | |
else: | |
buysell = "SELL" | |
#print (buy_token_qty) | |
price_buy_eth = buy_token_qty/buy_eth_qty | |
arr = [t,buysell,buy_token_qty,buy_eth_qty,price_buy_eth,txid] | |
txlist.append(arr) | |
return txlist | |
data = get_swaps() | |
for row in data[:10]: | |
print (row) | |
#print (data[-1]) | |
numpy_data = np.array(data) | |
#df = pd.DataFrame(data=numpy_data, index=["row1", "row2"], columns=["column1", "column2"]) | |
df = pd.DataFrame(data=numpy_data, columns=["timestamp", "buysell","buy_token_qty","buy_eth_qty","price_buy_eth","txid"]) | |
df['date'] = pd.to_datetime(df['timestamp'],unit='s') | |
df.index = pd.to_datetime(df.index, unit='s') | |
#https://stackoverflow.com/questions/30857680/pandas-resampling-error-only-valid-with-datetimeindex-or-periodindex | |
print(df) | |
bars = df.resample('1H').ohlc() #.last() | |
#ticks = data.ix[:, ['Price', 'Volume']] | |
#bars = ticks.Price.resample('30min').ohlc() | |
print (x) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment