#!/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 # ~~~~~============== 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) # 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": on_fill(message) elif message["type"] == "book": on_book(message) def on_fill(message): ; def on_book(message): 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, } ) elif message["symbol"] == "VALBZ": # ~~~~~============== 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 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()