## [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$200\$.

## 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 3: 17:33 07/08/2018**

    Name								Balance

    Experienced Greedy Idiot			$14802860126910608746226775271608441476740220190868405578697473058787503167301288688412912141064764060957801420415934984247914753474481204843420999117641289792179203440895025689047561483400211597324662824868794009792985857917296068788434607950379253177065699908166901854516163240207641611196996217004494096517064741782361827125867827455285639964058498121173062045074772914323311612234964464095317202678432969866099864014974786854889944224928268964434751475446606732939913688961295787813863551384458839364617299883106342420461998689419913505735314365685264187374513996061826694192786379011458348988554845036604940421113739997490412464158065355335378462589602228039730
    Equalizer							$763185511031294813246284506179317396432985772155750823910419030867990447973211564091988995290789610193513321528772412563772470011147066425321453744308521967943712734185479563642323459564466177543928912648398244481744861744565800383179966018254551412512770699653538211331184147038781605464336206279313836606330
    Percentage Trader					$448397954167281544772103458977846133762031629256561243713673243996259286459758487106045850187688160858986472490834559645508673466589151486119551222357206708156491069820990603783876340193236064700332082781080188011584263709364962735827741094223755467455209136453381715027369221484319039100339776026752813930
    OYAIB								$8935960891618546760585096898089377896156886097652629690033599419878768424984255852521421137695754769495085398921618469764914237729576710889307470954692315601571866328742408488796145771039574397444873926883379666840494456194839899502761180282430561362538663182006432392949099112239702124912922930
    Chimps on a Typewriter				$861072628924758912951993552410621873138049903905294437843283726446631022625933123453110
    Greedy B*****d						$17689013777381240
    Lone Accountant						$90954970320
    Illiterate Dividend Investor		$1847030
    Buy/Reinvest						$127330
    Lucky Number 6						$106330
    Dollar Cost Averager				$106130
    Fibonacci							$69930
    Novice Broker						$28130
    Buy Low								$6130
    Naive Statistician					$6130
    Fallacious Gambler					$6130
    Passive Trader						$4980
    Half More or Nothing				$4920
    Monkeys on a Typewriter				$56

[**View graphs of each contestant**](https://i.imgur.com/FiNJnEF.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>