added examples

This commit is contained in:
Taha Afzal committed 2023-03-21 12:39:58 -04:00
1 parent 7da128da6b
commit 317f17d7da
15 files changed
+660

No files matched your search

@@ -0,0 +1,24 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'delete_order', 'params': {'order_id': 541002}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
ws.close()
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,24 @@
import json
import websocket
json_payload = {'price': 1646.8, 'quantity': 1, 'order_type': 'cover', 'side': 'buy', 'account': 'main', 'user': 'bts', 'exchange_securty_name': 'ETH-31MAR23', 'venue': 'deribit', 'custom': {'hedge_ticks': 2, 'cancel_on_external_hedge_cancel': True, 'cancel_hedges_on_cancel': True, 'detach_hedges': False}}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
ws.close()
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,24 @@
import json
import websocket
json_payload = {'price': 1646.8, 'quantity': 1, 'order_type': 'iceberg', 'side': 'buy', 'account': 'main', 'user': 'bts', 'exchange_securty_name': 'ETH-31MAR23', 'venue': 'deribit', 'custom': {'showquanity': 5}}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
ws.close()
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,24 @@
import json
import websocket
json_payload = {'price': 1646.8, 'quantity': 1, 'order_type': 'limit', 'side': 'buy', 'account': 'main', 'user': 'bts', 'exchange_securty_name': 'ETH-31MAR23', 'venue': 'deribit', 'custom': {}}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
ws.close()
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,303 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### installs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install websocket"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": 16,
"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 custom,type:{\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": 17,
"metadata": {},
"outputs": [],
"source": [
"REST_code=lambda payload :f'''import json\n",
"import websocket\n",
"json_string = {json.dumps(payload,indent=4)}\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": 18,
"metadata": {},
"outputs": [],
"source": [
"sub_code=lambda payload :f'''import json\n",
"import websocket\n",
"json_string = {json.dumps(payload,indent=4)}\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": 19,
"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": 20,
"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_tickets\"]}\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": 21,
"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": 22,
"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": 24,
"metadata": {},
"outputs": [],
"source": [
"file_writer(\"delete_order.py\",REST_code(outer(\"delete_order\",{\"order_id\":541002})))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"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
}
@@ -0,0 +1,27 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'subscribe', 'params': {'subscription_id': 1, 'type': 'books', 'filter': {'symbol': ['ETH'], 'expiration': ['2023/04/28'], 'venue': ['deribit']}}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
message_count = [0]
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
if(message_count[0]>0):
ws.close()
message_count[0]+=1
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,27 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'subscribe', 'params': {'subscription_id': 1, 'type': 'fills', 'filter': {'symbol': ['ETH'], 'expiration': ['2023/04/28'], 'venue': ['deribit']}}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
message_count = [0]
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
if(message_count[0]>0):
ws.close()
message_count[0]+=1
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,27 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'subscribe', 'params': {'subscription_id': 1, 'type': 'orders', 'filter': {'symbol': ['ETH'], 'expiration': ['2023/04/28'], 'venue': ['deribit']}}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
message_count = [0]
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
if(message_count[0]>0):
ws.close()
message_count[0]+=1
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,27 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'subscribe', 'params': {'subscription_id': 1, 'type': 'price_stats', 'filter': {'symbol': ['ETH'], 'expiration': ['2023/04/28'], 'venue': ['deribit']}}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
message_count = [0]
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
if(message_count[0]>0):
ws.close()
message_count[0]+=1
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,27 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'subscribe', 'params': {'subscription_id': 1, 'type': 'price_tickets', 'filter': {'symbol': ['ETH'], 'expiration': ['2023/04/28'], 'venue': ['deribit']}}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
message_count = [0]
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
if(message_count[0]>0):
ws.close()
message_count[0]+=1
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,27 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'subscribe', 'params': {'subscription_id': 1, 'type': 'securities', 'filter': {'symbol': ['ETH'], 'expiration': ['2023/04/28'], 'venue': ['deribit']}}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
message_count = [0]
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
if(message_count[0]>0):
ws.close()
message_count[0]+=1
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,27 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'subscribe', 'params': {'subscription_id': 1, 'type': 'trades', 'filter': {'symbol': ['ETH'], 'expiration': ['2023/04/28'], 'venue': ['deribit']}}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
message_count = [0]
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
if(message_count[0]>0):
ws.close()
message_count[0]+=1
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,24 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'update_order', 'params': {'order_id': 538001, 'price': 1641.8, 'quantity': 2, 'order_type': {'hedge_ticks': 2, 'cancel_on_external_hedge_cancel': True, 'cancel_hedges_on_cancel': True, 'detach_hedges': False}, 'custom': 'cover'}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
ws.close()
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,24 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'update_order', 'params': {'order_id': 538001, 'price': 1641.8, 'quantity': 2, 'order_type': {'showquanity': 5}, 'custom': 'iceberg'}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
ws.close()
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")
@@ -0,0 +1,24 @@
import json
import websocket
json_payload = {'jsonrpc': 2, 'method': 'update_order', 'params': {'order_id': 538001, 'price': 1641.8, 'quantity': 2, 'order_type': {}, 'custom': 'limit'}, 'id': 1}
json_string = json.dumps(json_payload)
ws_url = "ws://localhost:8080"
# Define a callback function to handle received messages
def on_message(ws, message):
print("Received message: ", message)
ws.close()
# Define a callback function to handle the WebSocket connection opening
def on_open(ws):
print("WebSocket connection opened")
ws.send(json_string)
# Define a callback function to handle the WebSocket connection closing
def on_close(ws):
print("WebSocket connection closed")
# Create a WebSocket connection and send the JSON payload
ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_open=on_open, on_close=on_close)
# Start the WebSocket connection and listen for incoming messages
ws.run_forever()
input("press any key to continue")