Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
uv-orbs committed Jul 21, 2022
0 parents commit 751874c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
INFLUX_URL=http://34.121.125.182:8086
INFLUX_TOKEN=YpEMZ5Eg_HFo4noahZQ1C1D6vfVDI1iCP9IsuJqLPk04p3we0t8Iy-dg6ArC24RlqrZEGe9IQkCA-O2FVplT8g==
INFLUX_ORG=Orbs
STRIKE=23000
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# deribit-data
3 changes: 3 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#gcloud compute ssh --zone "us-central1-a" "influx-2" --project "stunning-choir-314214"
gcloud compute scp main.py influx-2:~/derinit-data --zone="us-central1-a" --project "stunning-choir-314214"
gcloud compute scp --recursive .venv/ influx-2:~/derinit-data/.venv/* --zone="us-central1-a" --project "stunning-choir-314214"
102 changes: 102 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import asyncio
import websockets
import json
import influxdb_client
import os
from influxdb_client.client.write_api import SYNCHRONOUS
from collections.abc import MutableMapping
from dotenv import load_dotenv
from datetime import datetime, timedelta
load_dotenv()

#################################################################
# influx
influx_bucket = 'deribit-1'
client = influxdb_client.InfluxDBClient(
url=os.getenv('INFLUX_URL'),
token=os.getenv('INFLUX_TOKEN'),
org=os.getenv('INFLUX_ORG')
)
write_api = client.write_api(write_options=SYNCHRONOUS)

# get tomorrow
dt = datetime.now() + timedelta(days=1)
tom = dt.strftime("%d%b%y").upper()

# 21JUL22
# get strike
strike = int(os.getenv('STRIKE'))
#################################################################
chans = [
f'ticker.BTC-{tom}-{strike}-C.100ms',
f'ticker.BTC-{tom}-{strike}-P.100ms',
]
msgSub = \
{
"jsonrpc": "2.0",
"id": 3600,
"method": "public/subscribe",
"params": {
"channels": chans
}
}


def flatten_dict(d: MutableMapping, parent_key: str = '', sep: str = '.') -> MutableMapping:
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, MutableMapping):
items.extend(flatten_dict(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)


def create_data_point(data):
p = influxdb_client.Point("options").tag("version", "dev1.1")
flat = flatten_dict(data)
for f in flat:
if isinstance(flat[f], str):
p = p.tag(f, flat[f])
elif type(flat[f]) == int or type(flat[f]) == float:
p = p.field(f, flat[f])

return p


def write_data(j):
params = j['params']
if params is None:
return
channel = params['channel']
if channel is None or channel not in chans:
return
data = params['data']
if data is None:
return

p = create_data_point(data)
res = write_api.write(
bucket='deribit-1', org='Orbs', record=p)

if(res):
print(res)

print(data['instrument_name'], data['underlying_price'])


async def call_api(msg):
async with websockets.connect('wss://www.deribit.com/ws/api/v2') as websocket:
await websocket.send(msg)
while websocket.open:
response = await websocket.recv()
# do something with the response...
# print(response)
j = json.loads(response)
try:
write_data(j)
except Exception as e:
print('Exception:', e)

asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msgSub)))

0 comments on commit 751874c

Please sign in to comment.