150 lines
5.0 KiB
Python
150 lines
5.0 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)
|
|
|
|
#
|
|
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
|
|
|
|
man.valueMan.set_value(message["symbol"], message["buy"][0][0], 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
|
|
|
|
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 (valePos * man.positionMan.get_position["VALE"] + FEE <
|
|
valbzPos * man.positionMan.get_position["VALBZ"]):
|
|
man.orderMan.convert("VALE", Dir.SELL, valePos)
|
|
if (valePos * man.positionMan.get_position["VALE"] >
|
|
valbzPos * man.positionMan.get_position["VALBZ"] + 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()
|
|
|