254 lines
8.1 KiB
Python
254 lines
8.1 KiB
Python
#!/usr/bin/env python3
|
|
# ~~~~~============== HOW TO RUN ==============~~~~~
|
|
# 1) Configure things in CONFIGURATION section
|
|
# 2) Change permissions: chmod +x bot.py
|
|
# 3) Run in loop: while true; do ./bot.py --test prod-like; sleep 1; done
|
|
|
|
import argparse
|
|
from collections import deque
|
|
from enum import Enum
|
|
import time
|
|
import socket
|
|
import json
|
|
|
|
from state import StateManager
|
|
from order import OrderManager
|
|
|
|
# ~~~~~============== CONFIGURATION ==============~~~~~
|
|
# Replace "REPLACEME" with your team name!
|
|
team_name = "HanyangFloorFunction"
|
|
|
|
# ~~~~~============== MAIN LOOP ==============~~~~~
|
|
|
|
# You should put your code here! We provide some starter code as an example,
|
|
# but feel free to change/remove/edit/update any of it as you'd like. If you
|
|
# have any questions about the starter code, or what to do next, please ask us!
|
|
#
|
|
# To help you get started, the sample code below tries to buy BOND for a low
|
|
# price, and it prints the current prices for VALE every second. The sample
|
|
# code is intended to be a working example, but it needs some improvement
|
|
# before it will start making good trades!
|
|
|
|
|
|
def main():
|
|
args = parse_arguments()
|
|
state = StateManager()
|
|
exchange = ExchangeConnection(args=args)
|
|
orderman = OrderManager(exchange)
|
|
|
|
hello_message = exchange.read_message()
|
|
print("First message from exchange:", hello_message)
|
|
|
|
orderman.sell("BOND", 1001, -99)
|
|
orderman.buy("BOND", 999, 99)
|
|
|
|
while True:
|
|
message = exchange.read_message()
|
|
|
|
if message["type"] == "close":
|
|
print("The round has ended")
|
|
break
|
|
elif message["type"] == "error":
|
|
print(message)
|
|
elif message["type"] == "reject":
|
|
print(message)
|
|
elif message["type"] == "book":
|
|
on_book(message, state)
|
|
elif message["type"] == "trade":
|
|
on_trade(message, orderman, state)
|
|
elif message["type"] == "fill":
|
|
on_fill(message, orderman, state)
|
|
|
|
|
|
def on_book(message: dict, state: StateManager):
|
|
symbol = message["symbol"]
|
|
|
|
def best_price(side) -> int | None:
|
|
if message[side]:
|
|
return message[side][0][0]
|
|
else:
|
|
return None
|
|
|
|
state.update_bid_ask_price(symbol, best_price("buy"), best_price("sell"))
|
|
|
|
|
|
def on_fill(message: dict, orderman: OrderManager, state: StateManager):
|
|
symbol = message["symbol"]
|
|
dir = message["dir"]
|
|
size = message["size"]
|
|
|
|
quantity = size if dir == Dir.BUY else -size
|
|
orderman.update_position(symbol, message["order_id"], quantity)
|
|
|
|
|
|
def on_trade(message: dict, orderman: OrderManager, state: StateManager):
|
|
symbol = message["symbol"]
|
|
price = message["price"]
|
|
|
|
state.set_last_price(symbol, price)
|
|
|
|
# 거래도
|
|
if symbol == "VALBZ" or symbol == "VALE":
|
|
print(
|
|
f"Current prices - VALBZ: {state.get_last_price('VALBZ')}, VALE: {state.get_last_price('VALE')}"
|
|
)
|
|
p_valbz = state.get_last_price("VALBZ")
|
|
p_vale = state.get_last_price("VALE")
|
|
|
|
if p_vale is None or p_valbz is None:
|
|
print("NONE")
|
|
return
|
|
|
|
FEE = 10
|
|
SZ = 10
|
|
if p_vale + 1 < p_valbz - 1:
|
|
print("Good")
|
|
if (p_vale + 1) * SZ + FEE < (p_valbz - 1) * SZ:
|
|
orderman.buy("VALE", p_vale + 1, -SZ)
|
|
orderman.sell("VALBZ", p_valbz - 1, SZ)
|
|
|
|
|
|
# ~~~~~============== PROVIDED CODE ==============~~~~~
|
|
|
|
# You probably don't need to edit anything below this line, but feel free to
|
|
# ask if you have any questions about what it is doing or how it works. If you
|
|
# do need to change anything below this line, please feel free to
|
|
|
|
|
|
class Dir(str, Enum):
|
|
BUY = "BUY"
|
|
SELL = "SELL"
|
|
|
|
|
|
class ExchangeConnection:
|
|
def __init__(self, args):
|
|
self.message_timestamps = deque(maxlen=500)
|
|
self.exchange_hostname = args.exchange_hostname
|
|
self.port = args.port
|
|
exchange_socket = self._connect(add_socket_timeout=args.add_socket_timeout)
|
|
self.reader = exchange_socket.makefile("r", 1)
|
|
self.writer = exchange_socket
|
|
|
|
self._write_message({"type": "hello", "team": team_name.upper()})
|
|
|
|
def read_message(self):
|
|
"""Read a single message from the exchange"""
|
|
message = json.loads(self.reader.readline())
|
|
if "dir" in message:
|
|
message["dir"] = Dir(message["dir"])
|
|
return message
|
|
|
|
def send_add_message(
|
|
self, order_id: int, symbol: str, dir: Dir, price: int, size: int
|
|
):
|
|
"""Add a new order"""
|
|
self._write_message(
|
|
{
|
|
"type": "add",
|
|
"order_id": order_id,
|
|
"symbol": symbol,
|
|
"dir": dir,
|
|
"price": price,
|
|
"size": size,
|
|
"tif": "DAY",
|
|
}
|
|
)
|
|
|
|
def send_convert_message(self, order_id: int, symbol: str, dir: Dir, size: int):
|
|
"""Convert between related symbols"""
|
|
self._write_message(
|
|
{
|
|
"type": "convert",
|
|
"order_id": order_id,
|
|
"symbol": symbol,
|
|
"dir": dir,
|
|
"size": size,
|
|
}
|
|
)
|
|
|
|
def send_cancel_message(self, order_id: int):
|
|
"""Cancel an existing order"""
|
|
self._write_message({"type": "cancel", "order_id": order_id})
|
|
|
|
def _connect(self, add_socket_timeout):
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
if add_socket_timeout:
|
|
# Automatically raise an exception if no data has been recieved for
|
|
# multiple seconds. This should not be enabled on an "empty" test
|
|
# exchange.
|
|
s.settimeout(5)
|
|
s.connect((self.exchange_hostname, self.port))
|
|
return s
|
|
|
|
def _write_message(self, message):
|
|
what_to_write = json.dumps(message)
|
|
if not what_to_write.endswith("\n"):
|
|
what_to_write = what_to_write + "\n"
|
|
|
|
length_to_send = len(what_to_write)
|
|
total_sent = 0
|
|
while total_sent < length_to_send:
|
|
sent_this_time = self.writer.send(
|
|
what_to_write[total_sent:].encode("utf-8")
|
|
)
|
|
if sent_this_time == 0:
|
|
raise Exception("Unable to send data to exchange")
|
|
total_sent += sent_this_time
|
|
|
|
now = time.time()
|
|
self.message_timestamps.append(now)
|
|
if len(
|
|
self.message_timestamps
|
|
) == self.message_timestamps.maxlen and self.message_timestamps[0] > (now - 1):
|
|
print(
|
|
"WARNING: You are sending messages too frequently. The exchange will start ignoring your messages. Make sure you are not sending a message in response to every exchange message."
|
|
)
|
|
|
|
|
|
def parse_arguments():
|
|
test_exchange_port_offsets = {"prod-like": 0, "slower": 1, "empty": 2}
|
|
|
|
parser = argparse.ArgumentParser(description="Trade on an ETC exchange!")
|
|
exchange_address_group = parser.add_mutually_exclusive_group(required=True)
|
|
exchange_address_group.add_argument(
|
|
"--production", action="store_true", help="Connect to the production exchange."
|
|
)
|
|
exchange_address_group.add_argument(
|
|
"--test",
|
|
type=str,
|
|
choices=test_exchange_port_offsets.keys(),
|
|
help="Connect to a test exchange.",
|
|
)
|
|
|
|
# Connect to a specific host. This is only intended to be used for debugging.
|
|
exchange_address_group.add_argument(
|
|
"--specific-address", type=str, metavar="HOST:PORT", help=argparse.SUPPRESS
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
args.add_socket_timeout = True
|
|
|
|
if args.production:
|
|
args.exchange_hostname = "production"
|
|
args.port = 25000
|
|
elif args.test:
|
|
args.exchange_hostname = "test-exch-" + team_name
|
|
args.port = 22000 + test_exchange_port_offsets[args.test]
|
|
if args.test == "empty":
|
|
args.add_socket_timeout = False
|
|
elif args.specific_address:
|
|
args.exchange_hostname, port = args.specific_address.split(":")
|
|
args.port = int(port)
|
|
|
|
return args
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Check that [team_name] has been updated.
|
|
assert team_name != "REPLAC" + "EME", (
|
|
"Please put your team name in the variable [team_name]."
|
|
)
|
|
|
|
main()
|