Files
JSETC/bot split/bot split.py
2026-05-09 16:15:24 +09:00

160 lines
5.2 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
import time
from ETC import ExchangeConnection, Dir, team_name
from manager import Manager
# ~~~~~============== CONFIGURATION ==============~~~~~
# Replace "REPLACEME" with your team name!
# ~~~~~============== 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!
last_print_time = 0
def main():
global last_print_time
args = parse_arguments()
exchange = ExchangeConnection(args=args)
man = Manager(exchange)
hello_message = exchange.read_message()
print("First message from exchange:", hello_message)
#
man.orderMan.sell("BOND", 1010, 99)
man.orderMan.buy("BOND", 990, 99)
last_print_time = time.time()
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"] == "fill":
on_fill(message, man)
elif message["type"] == "book":
on_book(message, man)
def on_fill(message, man: Manager):
if (message["dir"] == Dir.BUY):
man.positionMan.update_position(message["symbol"], message["size"])
elif (message["dir"] == Dir.SELL):
man.positionMan.update_position(message["symbol"], -message["size"])
def on_book(message, man: Manager):
global last_print_time
if (message["buy"]):
man.valueMan.set_bid(message["symbol"], message["buy"][0][0])
if (message["sell"]):
man.valueMan.set_ask(message["symbol"], message["sell"][0][0])
if message["symbol"] == "VALE" or message["symbol"] == "VALBZ":
def best_price(side):
if message[side]:
return message[side][0][0]
# best_price("buy")
# best_price("sell")
now = time.time()
if now > last_print_time:
last_print_time = now
print(message)
print()
valePos = man.positionMan.get_position("VALE")
valeBid = man.valueMan.get_bid("VALE")
valeAsk = man.valueMan.get_ask("VALE")
valbzPos = man.positionMan.get_position("VALBZ")
valbzBid = man.valueMan.get_bid("VALBZ")
valbzAsk = man.valueMan.get_ask("VALBZ")
FEE = 10
if ((valeBid > 0 and valbzBid) and
valePos * valeBid + FEE <
valbzPos * valbzBid):
man.orderMan.convert("VALE", Dir.SELL, valePos)
if ((valeBid > 0 and valbzBid) and
valePos * valeBid >
valbzPos * valbzBid + FEE):
man.orderMan.convert("VALBZ", Dir.SELL, valbzPos)
# ~~~~~============== 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
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()