Building a Webgame – How We Made a Stock Market Simulator Out of Nothing and php – part 1

The development of a web-based game such as a stock simulator starts with an idea. Not just 'I want to make a stock market sim' but the idea 'I want to make a different and new stock market sim, one that is better and more unique than anything already out there'

Then comes research – seeing what other market sims are already out there and functional and seeing how they work from an end-user's point of view. There are stock market sims that run using java and server-based engines such as the Hollywood Stock Exchange, sims that run using SSI and ASP modules, and open-source stock sims such as the Futures Exchange. We looked at all of them and determined where the opening lay in regards to the market and where demand should be.

We determined a large void was sitting in regards to prediction markets and futures exchanges for stock market games and simulators in regards to television. There were plenty of websites where you could vote for your show's popularity, and other sites where you could even vote for reality TV shows and try your luck at predicting who was next to go. But no site existed that treated TV as a stock market. so we decided that would be our niche game.

Television is a growth industry – Every year they sell more TV sets than the year before. TV studios work tirelessly to produce new and different shows to put over the airwaves and cable networks to entertain hundreds of millions of people in North America alone. All of those viewers have an opinion about what they like and do not like – that is a market unlike any other.

We sat down and crabbed out a checklist of what a TV stock market would be able to do …

  • Buy and sell stocks in TV shows, TV channels, Studios, and stars
  • Short sell and cover the same stocks ( Short selling and covering is the opposite of a buy or sell, if you short a stock, you hope the price drops so you can gain money on the fall. )
  • Rate or vote for popular TV shows
  • offer easy to use registration that is seamless
  • design the market system for extreme modularity so we can add in new features as we develop them without interrupting the market itself.

That was our original list of features. At the end of the article series – I'll show a list of the current features and show how the market system has grown in complexity but still retains all of the basic modularity we developed into it.

The most important thing to start with was to build a method to buy and sell shares, to track the actual buy and sell process, and to empower the market itself to adjust pricings as product is bought and sold.

We decided to go with mysql for databasing, and also to implement a cron job to do the background calculations and market balancing.

By using the php language, we could more easily implement mysql access and also allow for more flexibility in the goal of keeping the Stocks Online application as modular as possible.

MySql and php have been modified during their lifecycles to work as close to seamlessly together as possible, so it made sense to use the two components to make our stock system as it would enable easier maintenance and future development.

Beginning from a basic math code of

Buy price = Stock price x (Number of shares + commission)

We developed a simple buy and sell process that would enable the player to buy shares or sell them. with the system automatically calculating commission and adding that to the transaction.

We realized that we needed to limit the shares able to be bought by a single player, so set a ceiling of 25,000 shares per stock. This would ensure no one player could hold a monopoly on shares. and also limited the effect of a single bulk buy or sell.

What started as a simple formula and tracking system rapidly ballooned into a complex math function algorithm that now looked something like this:


Number of shares available
= (max shares available - number of shares held by player)
IF
player shares are less than max allowed
THEN
process buy transaction
Buy
current price * number of shares available + commission (1.5%)
Transaction complete - do market calculations
Stock price adjust by (+0.01 x 5,000 shares or fraction thereof)
cron runs, checking transaction and adjusting 0.01 for every 10,000 shares moved
cron also checks if more than 50,000 shares have moved
then stock adjusts slowed to .01 per cron cycle to ensure a run-on stock does not occur.

To ensure this happened properly we had to add in multiple mysql table entries for the cron and the system to track the stock transactions so not only could we gain a history of transactions and stock movements, but also the system could cross-check itself to maintain balance in the event of data corruption or a bad stock transaction.

The sell transaction was pretty much the same process but with negatives instead of positives.

We also implemented a voting system whereby players could vote for their favorite TV shows – adjust adjusting the value of that show stock up or down depending on their vote. If you liked the show, vote yes, if you don't like it, vote no. We set up a random generator to pull 10 show names from the database every time the vote page was loaded so no show would get more weight than any other show. thus ensuring a even spread of votes. The vote system also rewarded the player by adding game cash to their account for each vote clicked. If you voted for all 10 options, you gained $ 10k. Thereby, greed became a factor – get more $ to play by voting.

Building the player portfolio page was just a matter of setting up an if / then loop to pull every stock held one after another and displaying them on the page with their buy price, current price, price difference, and so on.

Nest article will explain how we built the stock ticker and other features, and the future directions the Stock Online application will go in.

Source Article