342 lines
8.6 KiB
Plaintext
342 lines
8.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### installs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Defaulting to user installation because normal site-packages is not writeable\n",
|
|
"Requirement already satisfied: websocket-client in /home/arham/.local/lib/python3.10/site-packages (1.5.1)\n",
|
|
"Note: you may need to restart the kernel to use updated packages.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%pip install websocket-client"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Imports"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 21,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"import websocket"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### data"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### packets"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 40,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"outer = lambda method,params:{\n",
|
|
" \"jsonrpc\": 2,\n",
|
|
" \"method\": method,\n",
|
|
" \"params\": params,\n",
|
|
" \"id\": 1\n",
|
|
"}\n",
|
|
"enter_order_params=lambda type,custom:{\n",
|
|
" \"price\":1646.8,\n",
|
|
" \"quantity\":1,\n",
|
|
" \"order_type\":type,\n",
|
|
" \"side\":\"buy\",\n",
|
|
" \"account\":\"main\",\n",
|
|
" \"user\":\"bts\",\n",
|
|
" \"exchange_securty_name\": \"ETH-31MAR23\",\n",
|
|
" \"venue\":\"deribit\",\n",
|
|
" \"custom\":custom\n",
|
|
"}\n",
|
|
"upd_order_params=lambda type,custom:{\n",
|
|
" \"order_id\":538001,\n",
|
|
" \"price\":1641.8,\n",
|
|
" \"quantity\":2,\n",
|
|
" \"order_type\":type,\n",
|
|
" \"custom\":custom\n",
|
|
"}\n",
|
|
"simple_filter={\n",
|
|
" \"symbol\":[\"ETH\"],\n",
|
|
" \"expiration\":[\"2023/04/28\"],\n",
|
|
" \"venue\":[\"deribit\"]\n",
|
|
"}\n",
|
|
"order_customs = {\n",
|
|
" \"limit\":{},\n",
|
|
" \"iceberg\":{\"showquanity\":5},\n",
|
|
" \"cover\":{\"hedge_ticks\":2,\"cancel_on_external_hedge_cancel\":True,\"cancel_hedges_on_cancel\":True,\"detach_hedges\":False}\n",
|
|
" }\n",
|
|
"order_customs_upd = {\n",
|
|
" \"limit\":{},\n",
|
|
" \"iceberg\":{\"showquanity\":7},\n",
|
|
" \"cover\":{\"hedge_ticks\":6,\"cancel_on_external_hedge_cancel\":False,\"cancel_hedges_on_cancel\":True,\"detach_hedges\":False}}\n"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### code"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 41,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"REST_code=lambda payload :f'''import json\n",
|
|
"import websocket\n",
|
|
"json_payload = {json.dumps(payload,indent=4)}\n",
|
|
"json_string = json.dumps(json_payload)\n",
|
|
"ws_url = \"ws://localhost:8080\"\n",
|
|
"# Define a callback function to handle received messages\n",
|
|
"def on_message(ws, message):\n",
|
|
" print(\"Received message: \", message)\n",
|
|
" ws.close()\n",
|
|
"\n",
|
|
"# Define a callback function to handle the WebSocket connection opening\n",
|
|
"def on_open(ws):\n",
|
|
" print(\"WebSocket connection opened\")\n",
|
|
" ws.send(json_string)\n",
|
|
"\n",
|
|
"# Define a callback function to handle the WebSocket connection closing\n",
|
|
"def on_close(ws):\n",
|
|
" print(\"WebSocket connection closed\")\n",
|
|
"# Create a WebSocket connection and send the JSON payload\n",
|
|
"ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)\n",
|
|
"\n",
|
|
"# Start the WebSocket connection and listen for incoming messages\n",
|
|
"ws.run_forever()\n",
|
|
"input(\"press any key to continue\")'''"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 42,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"sub_code=lambda payload :f'''import json\n",
|
|
"import websocket\n",
|
|
"json_payload = {json.dumps(payload,indent=4)}\n",
|
|
"json_string = json.dumps(json_payload)\n",
|
|
"ws_url = \"ws://localhost:8080\"\n",
|
|
"message_count = [0]\n",
|
|
"# Define a callback function to handle received messages\n",
|
|
"def on_message(ws, message):\n",
|
|
" print(\"Received message: \", message)\n",
|
|
" if(message_count[0]>0):\n",
|
|
" ws.close()\n",
|
|
" message_count[0]+=1\n",
|
|
"\n",
|
|
"# Define a callback function to handle the WebSocket connection opening\n",
|
|
"def on_open(ws):\n",
|
|
" print(\"WebSocket connection opened\")\n",
|
|
" ws.send(json_string)\n",
|
|
"\n",
|
|
"# Define a callback function to handle the WebSocket connection closing\n",
|
|
"def on_close(ws):\n",
|
|
" print(\"WebSocket connection closed\")\n",
|
|
"# Create a WebSocket connection and send the JSON payload\n",
|
|
"ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)\n",
|
|
"\n",
|
|
"# Start the WebSocket connection and listen for incoming messages\n",
|
|
"ws.run_forever()\n",
|
|
"input(\"press any key to continue\")\n",
|
|
"'''"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### file writers"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 43,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def file_writer(name,val):\n",
|
|
" with open(name,\"w\") as f:\n",
|
|
" f.write(val)"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### subscription"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 44,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"get_sub = lambda typ,filter: outer(\"subscribe\",{\"subscription_id\":1,\"type\":typ,\"filter\":filter})\n",
|
|
"subscriptions={key:get_sub(key,simple_filter) for key in [\"books\",\"orders\",\"fills\",\"trades\",\"securities\",\"price_stats\",\"price_tickers\"]}\n",
|
|
"for key, value in subscriptions.items():\n",
|
|
" file_writer(f\"subscribe_{key}.py\",sub_code(value))"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### order entry"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 45,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"get_enter_order = lambda ord_type,params:outer(\"enter_order\",enter_order_params(ord_type,params))\n",
|
|
"for key, value in order_customs.items():\n",
|
|
" file_writer(f\"enter_{key}_order.py\",REST_code(get_enter_order(key,value)))"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### update order"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 46,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"get_upd_order = lambda ord_type,params:outer(\"update_order\",upd_order_params(ord_type,params))\n",
|
|
"for key, value in order_customs.items():\n",
|
|
" file_writer(f\"upd_{key}_order.py\",REST_code(get_upd_order(key,value)))"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### delete order"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 47,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"file_writer(\"delete_order.py\",REST_code(outer(\"delete_order\",{\"order_id\":541002})))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": []
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Run Examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 51,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"WebSocket connection opened\n",
|
|
"Received message: {\"jsonrpc\":2.0,\"id\":\"1\",\"status\":\"1\",\"result\":{\"subscription_id\":\"1\"}}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%run subscribe_price_stats.py"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.7"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|