158 lines
5.9 KiB
Python
158 lines
5.9 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
|
|
from ETC import Dir
|
|
from ETC import team_name
|
|
|
|
# ~~~~~============== 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!
|
|
|
|
|
|
def main():
|
|
args = parse_arguments()
|
|
|
|
exchange = ExchangeConnection(args=args)
|
|
|
|
# Store and print the "hello" message received from the exchange. This
|
|
# contains useful information about your positions. Normally you start with
|
|
# all positions at zero, but if you reconnect during a round, you might
|
|
# have already bought/sold symbols and have non-zero positions.
|
|
hello_message = exchange.read_message()
|
|
print("First message from exchange:", hello_message)
|
|
|
|
# Send an order for BOND at a good price, but it is low enough that it is
|
|
# unlikely it will be traded against. Maybe there is a better price to
|
|
# pick? Also, you will need to send more orders over time.
|
|
exchange.send_add_message(order_id=1, symbol="BOND", dir=Dir.BUY, price=990, size=1)
|
|
|
|
# Set up some variables to track the bid and ask price of a symbol. Right
|
|
# now this doesn't track much information, but it's enough to get a sense
|
|
# of the VALE market.
|
|
vale_bid_price, vale_ask_price = None, None
|
|
vale_last_print_time = time.time()
|
|
|
|
# Here is the main loop of the program. It will continue to read and
|
|
# process messages in a loop until a "close" message is received. You
|
|
# should write to code handle more types of messages (and not just print
|
|
# the message). Feel free to modify any of the starter code below.
|
|
#
|
|
# Note: a common mistake people make is to call write_message() at least
|
|
# once for every read_message() response.
|
|
#
|
|
# Every message sent to the exchange generates at least one response
|
|
# message. Sending a message in response to every exchange message will
|
|
# cause a feedback loop where your bot's messages will quickly be
|
|
# rate-limited and ignored. Please, don't do that!
|
|
while True:
|
|
message = exchange.read_message()
|
|
|
|
# Some of the message types below happen infrequently and contain
|
|
# important information to help you understand what your bot is doing,
|
|
# so they are printed in full. We recommend not always printing every
|
|
# message because it can be a lot of information to read. Instead, let
|
|
# your code handle the messages and just print the information
|
|
# important for you!
|
|
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":
|
|
print(message)
|
|
elif message["type"] == "book":
|
|
if message["symbol"] == "VALE":
|
|
|
|
def best_price(side):
|
|
if message[side]:
|
|
return message[side][0][0]
|
|
|
|
vale_bid_price = best_price("buy")
|
|
vale_ask_price = best_price("sell")
|
|
|
|
now = time.time()
|
|
|
|
if now > vale_last_print_time + 1:
|
|
vale_last_print_time = now
|
|
print(
|
|
{
|
|
"vale_bid_price": vale_bid_price,
|
|
"vale_ask_price": vale_ask_price,
|
|
}
|
|
)
|
|
|
|
|
|
# ~~~~~============== 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()
|
|
|