-
Notifications
You must be signed in to change notification settings - Fork 3
/
univ3wsop.py
139 lines (129 loc) · 5.99 KB
/
univ3wsop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
import asyncio
from unicodedata import decimal
from numpy import place
import streamlit as st
from web3 import Web3
import plotly.express as px
import requests
from websockets import connect
from eth_abi import decode_single, decode_abi
import math
from datetime import datetime
import pandas as pd
st.set_page_config(page_title="usdcwethop", page_icon="🔐", layout="wide")
def bollinger_strat(df, window, no_of_std):
rolling_mean = df['USDC'].rolling(window).mean()
rolling_std = df['USDC'].rolling(window).std()
df['rolling_mean_value'] = rolling_mean
df['Bollinger High'] = rolling_mean + (rolling_std * no_of_std)
df['Bollinger Low'] = rolling_mean - (rolling_std * no_of_std)
return df['Bollinger High'] , df['Bollinger Low'], df['rolling_mean_value']
def bollinger_strat2(df, window, no_of_std):
rolling_mean = df['cumsum'].rolling(window).mean()
rolling_std = df['cumsum'].rolling(window).std()
df['rolling_mean_cumsum'] = rolling_mean
df['Bollinger High_cumsum'] = rolling_mean + (rolling_std * no_of_std)
df['Bollinger Low_cumsum'] = rolling_mean - (rolling_std * no_of_std)
return df['Bollinger High_cumsum'] , df['Bollinger Low_cumsum'], df['rolling_mean_cumsum']
session = requests.Session()
w3 = Web3(Web3.WebsocketProvider("wss://opt-mainnet.g.alchemy.com/v2/r7rVLgcc79AyvNX8gQAdV5rRbju98_2j"))
df = pd.DataFrame(columns=['price', 'timestamp','WETH','USDC', 'side'])
false = False
placeholder0 = st.empty()
placeholder1 = st.empty()
placeholder2 = st.empty()
placeholder3 = st.empty()
placeholder4 = st.empty()
placeholder5 = st.empty()
placeholder6 = st.empty()
placeholder7 = st.empty()
placeholder8 = st.empty()
placeholder9 = st.empty()
placeholder10 = st.empty()
placeholder11 = st.empty()
placeholder12 = st.empty()
async def get_event():
global df
async with connect("wss://opt-mainnet.g.alchemy.com/v2/r7rVLgcc79AyvNX8gQAdV5rRbju98_2j") as ws:
global df
await ws.send(json.dumps(
{"id": 1, "method": "eth_subscribe", "params":
["logs",
{
"address": "0x85149247691df622eaF1a8Bd0CaFd40BC45154a9",
"topics": [
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"
]
}
]
}
)
)
subscription_response = await ws.recv()
with placeholder0:
st.json(subscription_response)
while True:
message = await asyncio.wait_for(ws.recv(), timeout=60000)
lord_jesus = json.loads(message)
lord_jesus = json.dumps(lord_jesus)
lord_jesus = json.loads(lord_jesus)
# with placeholder1:
# st.write(lord_jesus, use_container_width=True)
now = datetime.now()
lord_jesus = lord_jesus["params"]["result"]
fromm = lord_jesus["topics"][1]
fromm = decode_single('address', bytes.fromhex(fromm[2:]))
fromm = str(fromm)
too = lord_jesus["topics"][2]
too = decode_single('address', bytes.fromhex(too[2:]))
too = str(too)
number = lord_jesus["data"][2:]
number = decode_single('(int256,int256,uint160,uint128,int24)',bytearray.fromhex(number))
# with placeholder2:
# st.write(number, use_container_width=True)
liq = number[3]
# liq = str(liq)
tick = number[4]
# tick = str(tick)
prinnt = (number[1]*math.pow(10,12)* -1)/number[0]
if number[0] < 0:
side = "BUY"
else:
side = "SELL"
usdc = abs(number[1]/math.pow(10,6))
usdc_net = (number[1]/math.pow(10,6))
weth = abs(number[0]/math.pow(10,18))
d = {'too': too, 'fromm':fromm, 'price': prinnt, 'timestamp': now, 'WETH': weth, 'USDC': usdc, 'side': side, 'USDC_net': usdc_net, 'Tick': tick, 'Liq': liq}
fixed_df = pd.DataFrame(d, index=[0])
df = pd.concat([df, fixed_df], ignore_index=True)
df['cumsum'] = df['USDC_net'].cumsum()
df['price_impact'] = df['price'].diff(periods=1)
df['price_impact_w_size'] = df['price_impact']/df['USDC']
df['taken_liquidity'] = df['Liq'].diff(periods=1)
df['current_price'] = 1/(1/(((1.0001) ** (df['Tick'])))/math.pow(10,12))
df['price_deviation'] = abs(df['current_price'] - df['price'])
df['price_deviation_w_size'] = df['price_deviation']/df['USDC']
df['tick_change'] = df['Tick'].diff(periods=1)
bollinger_strat(df=df,window=5,no_of_std=1)
bollinger_strat2(df=df,window=5,no_of_std=1)
with placeholder3:
st.write(df, use_container_width=True)
# with placeholder3:
# st.plotly_chart(px.line(df, x="timestamp", y="price", color="side"), use_container_width=True)
with placeholder4:
st.plotly_chart(px.scatter(df, x="timestamp", y="price", size="USDC", color='side'), use_container_width=True)
with placeholder5:
st.plotly_chart(px.scatter(df, x="timestamp", y="current_price", size = 'price_deviation') , use_container_width=True)
# with placeholder6:
# st.plotly_chart(px.scatter(df, x="WETH", y="price", size="USDC", color='WETH') , use_container_width=True)
# with placeholder7:
# st.plotly_chart(px.scatter(df, x='timestamp', y='cumsum', size='USDC',marginal_y="violin", marginal_x="rug"),use_container_width=True)
# with placeholder8:
# st.plotly_chart(px.scatter(df, x='timestamp', y=['Bollinger High_cumsum','Bollinger Low_cumsum','rolling_mean_cumsum','cumsum'], size = 'USDC',marginal_y="violin", marginal_x="rug"),use_container_width=True)
# with placeholder9:
# st.plotly_chart(px.bar(df, x="timestamp", y="price"), use_container_width=True)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
while True:
loop.run_until_complete(get_event())