## [Join the chat!](https://chat.stackexchange.com/rooms/81067/the-break-room)

## Introduction

Good evening, traders! You are all traders for the golfing company PPCG. Your task is to make as much money as possible.

## Challenge

Write a program that buys and sells shares on the Stack Exchange Stock Exchange with the aim of making as much money as possible.

## Gameplay

All players will start with 5 shares and $100 in their bank. The game always starts with a share price of $10.

Each game will have 1000 rounds where the first round is round `1`. In each round, your program will be supplied four arguments as input: the current share price, the number of shares you hold, the amount of money you own and the round number (1-indexed). 

For example, if my program is `test1.py`, the share price is `100`, the number of shares I hold is `3`, the amount of money I have is `1200`, and the round number is `576`, my program will be run like so:

    python test1.py 100 3 1200 576

In a round, the share price given to each player will be the same. This doesn't change until the end of the round.

In response, the player must print their command. There are two options:

 - Buy shares: This command is given as `bn` where `n` is the number of shares you wish to buy. For example, if you want to buy 100 shares, you would output:

<pre>
<code>b100</code>
</pre>

When buying shares, you are allowed an overdraft of up to $1000. If you try to buy enough shares that exceed this overdraft (your bank balance goes below $-1000), you will be declared bankrupt. This means that you will lose all of your shares and your balance will be set to $50.

The share price will be unaffected by your command if you go bankrupt.

(If your balance is $-1000, you are not bankrupt. However if your balance is $-1001, you are bankrupt)

 - Sell shares: This command is given as `sn` where `n` is the number of shares you wish to sell. For example, if you want to sell 100 shares, you would output:

<pre>
<code>s100</code>
</pre>

You may not sell more shares than you own. If you try to do this, your request will be denied, and you will skip the round.

If you want to skip the round and do nothing, output either `b0` or `s0`.

Your request will be denied if you try to buy or sell a negative number of shares and/or a non-integer number of shares.

After 5 rounds, at the end of each round, all players will be payed a dividend, the value of which is 5% of the mean average share price of the last 5 rounds.

## How does it work?

Initially the share price will be $10. At the end of each round, it will be recalculated using the formula:

$$\text{New Share Price} = \text{Old Share Price} + (\text{Number of shares bought}-\text{Number of shares sold})$$

The share price will be limited so that it never falls below $1.

To prevent overly rapid change, the change in share price is limited to a maximum of \$\pm$1000\$.

## Rules

 - Your program must have a name

<br/>

 - Your program is allowed a single text file for data storage. It must be stored in the same folder as your program

<br/>

 - Include in your answer details of how to run your program

<br/>

 - This KotH is open to all programming languages that are free-to-use and can be run on Windows 10

<br/>

 - Your score is based solely on the contents of your balance. Any money locked up in shares will not be counted

<br/>

 - You may edit your program at any time. Before each game, the latest code will be saved and compiled

<br/>


 - You should not write code which specifically targets another bot.

## Controller

The controller is written in Python and can be found here: https://gist.github.com/beta-decay/a6abe40fc9f4ff6cac443395377ec31f

At the end it will print a leaderboard and display a graph of how the share price changed throughout the game. 

For example, when two random bots were playing

<img src="https://i.sstatic.net/irSp0.png" width="500" />

## Winning

The player with the highest amount of money in their balance at the end of the last game wins.

## Leaderboard

**Game 1: 12:36 03/08/2018**

    Name		                Balance

    Chimps on a Typewriter		$1.6473029229478853e+96
    OYAIB		                $4.995097067283967e+74
    Lone Accountant		        $35267349684676.305
    Greedy Idiot		        $855456824.9199829
    Novice Broker		        $71024028.54999995
    Dollar Cost Averager		$4675198.92
    Fallacious Gambler	       	$792059.9199999977
    Buy Low		                $608419.9199999976
    Passive Trader		        $606954.9199999977
    Naive Statistician		    $100092.60000000008
    Monkeys on a Typewriter		$1920.0500000000002

**Graph:**

![](https://i.sstatic.net/vSPf6.png)

---

<sub>**[Related](https://codegolf.stackexchange.com/questions/91566/stock-exchange-koth) but the gameplay and winning criterion are very different to this challenge.**</sub>