- See Write a Bot in C or C++ for more details
- Download the C++ Bot cppbot.zip which contains bot.h, bot.cpp and test.cpp.
Though identical in the way it works (Parse inputs, store data, calculates buys or sells), I tried to make this a C++ bot by using STL classes and a function template. It was converted directly from the C code, so it makes an interesting example of C to C++ code. I'm not saying it's the best C++ bot, I'll leave that up to you!
Because it has the same interface as the C bot, its had to keep the char * parameters in the dll and only switches them to string when appropriate.
The std::vector class is one of the easiest to use and if you're wanting to make your code more C++, it'ss a nobrainer. It's basically an extensible array that only supports one type of object, be it a simple type like an int or a more complicated struct/class. The 4 typedefs below define the main structure types for holding data. So dvalues is an array of doubles and is used to hold prices.
typedef std::vector<double> dvalues;
typedef std::vector<int> ivalues;
typedef std::vector<tip> tips;
typedef std::vector<company> allcompanies;
This simplifies the parsing functions which now have the same name, using overloading. I put these in a class Parser, and they are accessed via the private member P in the Bot class. A public template ParseStr function is called to parse a string into the appropriate vector type. This is just to save on code size. It calls the SplitStr function that converts the Pipe separated values into a vector string array.
If you're unfamiliar with the string member functions find_first_of and find_first_not_of member functions, these search for the specified values and return string::npos (basically the highest unsigned 32 bit value about) if they're not found.
This is then processed using an iterator to walk through the items in the string vector and parse it, calling the appropriate doParse() function of which there are 4 overloads. (int, double, tip and company).
ParseStr() clears each of the vectors and then each of the four parser functions pushes each value on the end of the array. This lets the new value be accessed by the vector back() method which retrieves the last element of the array. You could of course just set the vector to have 100 elements and then index it using at(). Eg
vector<int> ivalues(100) ;
Prices.at(60) ;
Things to be aware of: When accessing a value via its iterator you must treat the iterator as a pointer. Also vector back(), at() etc returns a reference to the object. Both
company & c = retval.back() ;
and
company c = retval.back() ;
will compile and run but only the first has changes to c's members update the values held in the vector, the 2nd just copies values from the vector.
Conversion of string to double or int is performed by the strtoint() and strtodouble() functions. These use the std library stringstream class.
The remainder of the dll functionality is contained in the bot class and the main processing is done in the exported GetTrades() function. On Minute = 1, the Company names are parsed in InitData and on minute = 480, all existing positions are closed. If the Bot b declaration were moved inside the GetTrades() function then it should be made static (to survive repeated calls) or else InitData() would have to be called for every minute to reinitialize the company names.
This is not an optimal bot, that's your job! It has some slight complexity through the use of the overloaded template functions in the Parser and extensive use of the vector class. Also, rather than declaring this
using namespace std,
I explicitly declared each string and vector as std::string and std::vector and also std::cout and std::endl but feel free to use the namespace declaration if you wish. Note the test program just exercises the dll functions and does not put it through a full 480 minute test but feel free to modify it as you wish.
Testing Your DLL
The cppbot.zip contain the source of a test program. You need to compile the dll so it generates a .dll and .lib file then copy both those files into where you'll build the test program. Make sure you add the .lib to the project or it will not link without that.When you run the compiled exe, it will call both of the 2 Information functions first and then call GetTrades(). It will output the results of one call to GetTrades().
If it runs ok, you can submit the source code file (no binaries please, just source code, preferably with project files!) to cplus.guide@about.com?subject=Share Trading Bot Entry
Have fun and don't forget to check the Weekly Results.

