Bitcoin Trading
code-challenge?
This is mainly an idea for something I could potentially host on my KOTH server.
Everybody knows that bitcoins are the next big thing. It's just a question of when they are going to take off. Right now, they are worth $250 each, but who knows, maybe someday they will be worth over $1000! The growth trend is phenomenal.
You are a tech-savvy investor who wants to get in on this action.
The Challenge
Your goal is to write a bot that can predict the market and tell you how you should invest your money, given hourly updates of the Bitcoin price.
Keeping Balance
To overcome the fastest-gun-in-the-west effect, wherein early answers have more time to make more money, this challenge will not keep track of any absolute balances. Instead, the assets of each entrant will be scaled up/down between each round.
Each entrant will be given a single float in the range 0
to 1
representing the percent of total assets are currently invested in Bitcoin. This is calculated by (BTC_cur_rate*BTC_owned)/(USD_owned + BTC_cur_rate*BTC_owned)
.
A value of 0
means that you currently have nothing invested in Bitcoin, while a value of 1
means that you have everything invested in Bitcoin. An input of 0.3
means that 30% of your total value is in Bitcoin, while the other 70% of your value is in dollars.
Examples
input -> assets as portion of your total value
BTC % USD %
0.0 -> 0.0 0% 1.0 100%
0.3 -> 0.3 30% 0.7 70%
0.6 -> 0.6 60% 0.4 40%
Price Data
Players will also have access to a file history.txt
which will contain the BTC price history, measured in cents, over the duration of the competition. Each time a player is called, they are presented with a fresh copy of history.txt
, with one line appended each turn. Do not attempt to modify this file.
Example File
This could be the history.txt file after 3 hours of competition. The most recent price is $247.49.
24694
24724
24749
There will be a trailing newline at the end of the file.
Making a Trade (ouput)
The output of your program should be another float in the range of 0 to 1, representing the new portion of your assets that you want invested in Bitcoin. The difference between your input number and output number represent the amount of value being exchanged.
Examples
input -> BTC USD | output -> BTC USD | trade being made
0.3 -> 0.3 0.7 | 0.2 -> 0.2 0.8 | 0.1 in BTC -> 0.1 in USD
0.3 -> 0.3 0.7 | 0.6 -> 0.6 0.4 | 0.3 in USD -> 0.3 in BTC
Calculating Score for a Round
Your score for a round is based on your change of value for that round. You start every round with a total value of 1
, but your ending value is influenced by two things:
- A 0.2% commission on your trade taken by the controller
- The change in Bitcoin value over the next hour
Taking Commission
Commission is taken whenever you buy or sell bitcoins. Whenever you convert a certain amount of value from one currency to the other, you will receive 0.2% less of the new currency than what you actually ordered.
Examples
input | output | trade | commission | result after commission
0.3 | 0.6 | 0.3 -> BTC | 0.0006 BTC | 0.5994 BTC & 0.4 USD
0.75 | 0.05 | 0.7 -> USD | 0.0014 USD | 0.05 BTC & 0.9486 USD
Adjusting BTC Value
After taking commission, your value of BTC is multiplied by the price percent change in BTC over the next hour. The amount of value you have in USD will stay constant.
Examples
BTC after commission | prices in cents | % change | new BTC value
0.5 | 30000 -> 29850 | -0.5% | 0.4975
0.236 | 20000 -> 30447 | +2.0% | 0.24072
Overall Process of a Round
Below is an example showing all of the steps in a single round.
BTC USD
.3 .7 = 1.0 input to entrant is 0.3
.6 .4 = 1.0 output of entrant is 0.6
.0006 .0 0.2% commission of the trade
.5994 .4 = 0.9994 result after commission
+0.3% percent change in bitcoin price over 1 hour
.6012 .4 = 1.0012 result after the flow of time = score for that round
.60048 .39952 = 1 input for the next round is 0.60048 after scaling
Determining the Winner
For a given round, your score is your new total value. This is after taking the 0.2% commission and calculating the change in Bitcoin value. For the above example, the score was about 1.0012
.
At any given time, the aggregate score for an entrant shall be the product of the scores for its most recent (up to) 50 rounds. At any given time, the current winner is the player with the highest aggregate score.
For example, a bot could get these scores for its first 5 rounds: 1.001 1.002 0.998 0.999 1.003
. The total score of the bot is about 1.00299
.
The Controller
I haven't written the controller yet, but I think it's going to be written in Perl with support for entrants in a variety of other languages (Java/Python/Ruby/C++).
I plan to use this API for bitcoin price data.
The controller will probably run all of the entrants in parallel, each with their own thread. This simply allows it to put a stop to any infinite looping that may occur. I hope it will work if all of the programs are reading the same history file at once.
Additional Rules
Since this is a PvE competition and not a PvP competition, and takes place on a server, there are some slight differences in rules.
- There's no set restriction on submitting multiple bots, since you can't make a team.
- Similar to always, you can't call other programs, like the controller or other bots, during your turn.
- The time limit is loose. A long as a single round with all of the bots doesn't take up most of an hour to perform, it'll be fine. It really shouldn't take more than a couple minutes for each bot to make a move.
- You may create a single file, with the filename
[botname]-data.txt
, in the current directory. This file will persist, even across updates of your bot or the controller.