1. Computing & Technology

Discuss in my forum

How Do I write a Bot for the Stock Trading Challenge?

By , About.com Guide

I thought I'd encourage people to enter by describing how to write a Bot and provide one as well. Because it is a dll, it might be seen as difficult. The challenge is described in the main article below. There is nothing magical about a Windows dll. It is just a file like an exe except you can't run it directly. It has code for functions and variables. When you create a dll, you describe exactly what is made visible to the outside world. Which functions can be called from a program. These are known as export or interface functions and only these can be called by an external caller.

As Windows has evolved over a twenty year period in nearly a dozen versions running on many different 16, 32 and 64 bit processors, there has to be a degree of consistency in dll functions that you write and how those functions are called. It doesn't matter if you write your dll in C, C++ or even C#, it must stick to the specified convention which is cdecl.

Bot Interface

This is the interface for the C and C++ bot.
 #define DllExport __declspec(dllexport) 
 #define DllImport __declspec(dllimport)
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
  DllExport char * __cdecl BotNameVersionDate(void) ; // returns name
  DllExport char * __cdecl BotOwner(void) ; // Eg Fred Blogs, UK, Age 45
  DllExport void __cdecl GetTrades(
   int Minute,
   double Cash,
   char * Companies,
   char * CompanyShares,
   char * Position,
   char * Prices,
   char * Feed, 
   char * Trades,
   char * DebugBuffer) ; // return move
 #ifdef __cplusplus
  }
 #endif
 
It looks complex, but there are just three functions called BotNameVersionDate(), BotOwner() and GetTrades. The first two are easy, you just edit the string they return.

For example

 DllExport char * __cdecl BotNameVersionDate(){
 return "MyBot2 v1.0 Feb 24 2009";
 }
 
and something similar for BotOwner with details of your name etc.
 DllExport char * __cdecl BotOwner(){
 return "David Bolton(UK) Aged 50";
 }
 
The DllExport #define tells the compiler that any function defined with it has the text __declspec(dllexport) added to the function. This means that this function is exported from the dll. If you wish any program to call the dll functions, those functions must be exported. You can also use a .DEF file but this way avoids the need for that.
 DllExport char * __cdecl BotNameVersionDate()
 
This is just a function header for the function char * __cdecl BotNameVersionDate(). The word __stdcall means that the compiler generates special code whenever BotNameVersionDate() is called. Please keep the names clean in BotName. Every time you create a new version of the Bot, change the version number and date. Likewise you should fill in your details for BotOwner().

Game Play

All of the real work must be implemented in the function GetTrades. This is what the C/C++ header looks like (Link at foot of article).
  DllExport void __cdecl GetTrades(
     int Minute,
     double Cash,
     char * Companies,
     char * CompanyShares,
     char * Position,
     char * Prices,
     char * Feed, 
     char * Trades,
     char * DebugBuffer) ; // return move
 
Minute is an integer 1-480. This function is called 480 times, once per game minute with values 1-480. Cash is just an amount of cash you have.

Companies, CompanyShares, Position, Prices and Feed are five strings that supply you with information. Apart from feed, each has 100 values terminated by a pipe char |.

  • Companies - A 1-3 char string saying which sectors that company is in.
  • CompanyShares - How many are available to buy.
  • Position - How many shares of each company you own/owe.
  • Prices - the share price of companies.
  • Feed. A data feed showing which sectors have up/down swings.

      Null values

      If the value is null eg || (as opposed to |100| then treat it as 0. If it's a company then that company is no longer active and shares cannot be bought or sold.

      Prices contains 100 prices (one per company) followed by a pipe char |. This is the shifted character on the key to the left of the z key on your keyboard. There will always be 100 prices and the string will look like 78.8|56.5|...109.0|. If a company goes bust there will be no price so if the second company went bust it might look like 77.6||52.2|...

      Feed is a variable length string made up of a number of sector strings. Each has a sector letter, one of A,B,C,E,F,I,M,R,T,U followed by 1-3 of + or -. These are all the same, so there are 6 of them +, ++, +++, -,-- and ---. Each of these sector strings is separated by a pipe char |. Eg E--T+R++F-

      If you wish to buy or sell shares you must return them in the string Trades as one or more strings each made up of three parts:

      1. Company Number 0-99
      2. A + or -. + = Buy, - = sell
      3. Number of shares.
      So a single trade order will look like 45+100 (Buy 100 shares in company 45) or 0-1 (Sell one share of company 0). If you had these two orders, your string would look like
       45+100|0-1|
       

      DebugBuffer

      I will be publishing all share 480 prices moves (and shares traded) each minute every week as a text file you can download. If you want specific debug information from your bot then this parameter lets you do that. You don't have to use it.

      It is limited to 1 million bytes each minute though I'd really prefer you to not use all of that! Do not overflow the buffer. If your bot bombs out then it will be excluded from this week's trading.

      When you submit a bot for the first time, I'll provide you with a private url that you will be able to download a zipped up file of debug information. Each week, all debug information your bot outputs will be saved into a text file, then that is zipped and uploaded. As soon as the results are live, you can download it.

      You can find exact specifications and downloadable bot dlls for C, C++ and a C# class in the links below.

©2012 About.com. All rights reserved.

A part of The New York Times Company.