Experiment: Build Your Own Stock Market AI
Join the DZone community and get the full member experience.
Join For FreeI had an interesting idea last night. I'm not saying it is a good idea - my brain is having some serious weirdness going on with the seven hour time change. But good idea or not I built it and figured I'd share it.
Did you know that you could create functions in JavaScript by passing in a string? I don't see many people using this technique (ok, I've never seen anyone use this) but the gist is that you can create a new Function object by listing any number of arguments followed by a string of code. Here is an example:
x = new Function("a","b", "return a+b");
x(1,2);
This got me thinking about what kind of code would make use of this form of defining functions. About four years ago I asked my readers to participate in a coding contest that required you to write the logic to handle a lemonade stand. The contest had you writing a UDF that responded to daily weather reports. It passed you information about your assets and you simply responded with an action. The idea was that you were building an "intelligent agent" to handle the business.
With this in mind, I decided to build something similar in JavaScript. In my simulation, you are writing an agent that monitors a stock and decides when to buy or sell stock. Your code will be a function that is passed basic data like the stock price, the change, etc. You then handle returning an action: Buy stock, Sell stock, do nothing.
You are given a simple function to start off with:
When you click the Start Simulation button, I take that code and generate a function from it:
$("#startSim").on("click", function(e) {
code = $("#functiontext").val();
if(code === "") return;
$(this).attr("disabled","disabled");
//build the func
try {
userFunc = new Function("value,change,cash,shares,storage", code);
beginSim();
} catch(e) {
handleError(e);
}
});
The try/catch there handles some errors, but not all. As the code runs the simulation, I've got to do more error handling on the processing side. Here is that logic:
function runFunc() {
count++;
if(count > maxCount) { endSim(); return; }
console.log("runFunc");
//Is the stock going up or down?
var x = Math.random();
var change = Math.floor(Math.random() * 10);
if(x > 0.5) {
if((stockvalue - change) < 1) { change = stockvalue-1; }
change = -1 * change;
}
stockvalue += change;
try {
console.log("Calling userfunc",stockvalue,change,usercash,sharesowned);
var result = userFunc(stockvalue,change,usercash,sharesowned,storage);
if(!result) {
throw new Error("Your function did not return a result.");
}
if(!result.hasOwnProperty("action")) {
throw new Error("Your result did not include the action key.");
}
if(!validAction(result.action)) {
throw new Error("Your result did not contain a valid action value.");
}
if(result.action === "sell") {
if(!result.hasOwnProperty("quantity")) {
throw new Error("Your result tried to sell without returning a quantity value.");
} else {
console.log("doing a sale");
if(result.quantity > sharesowned) throw new Error("You tried to sell "+result.quantity+" shares but only owned "+sharesowned);
sharesowned -= result.quantity;
usercash += result.quantity*stockvalue;
}
}
if(result.action === "buy") {
if(!result.hasOwnProperty("quantity")) {
throw new Error("Your result tried to buy without returning a quantity value.");
} else {
if(result.quantity * stockvalue > usercash) throw new Error("You tried to spend "+(result.quantity*stockvalue)+" on shares but only have "+usercash);
sharesowned += result.quantity;
usercash -= result.quantity*stockvalue;
}
}
displayStats();
} catch(e) {
console.log("Error", e);
handleError(e);
}
}function runFunc() {
count++;
if(count > maxCount) { endSim(); return; }
console.log("runFunc");
//Is the stock going up or down?
var x = Math.random();
var change = Math.floor(Math.random() * 10);
if(x > 0.5) {
if((stockvalue - change) < 1) { change = stockvalue-1; }
change = -1 * change;
}
stockvalue += change;
try {
console.log("Calling userfunc",stockvalue,change,usercash,sharesowned);
var result = userFunc(stockvalue,change,usercash,sharesowned,storage);
if(!result) {
throw new Error("Your function did not return a result.");
}
if(!result.hasOwnProperty("action")) {
throw new Error("Your result did not include the action key.");
}
if(!validAction(result.action)) {
throw new Error("Your result did not contain a valid action value.");
}
if(result.action === "sell") {
if(!result.hasOwnProperty("quantity")) {
throw new Error("Your result tried to sell without returning a quantity value.");
} else {
console.log("doing a sale");
if(result.quantity > sharesowned) throw new Error("You tried to sell "+result.quantity+" shares but only owned "+sharesowned);
sharesowned -= result.quantity;
usercash += result.quantity*stockvalue;
}
}
if(result.action === "buy") {
if(!result.hasOwnProperty("quantity")) {
throw new Error("Your result tried to buy without returning a quantity value.");
} else {
if(result.quantity * stockvalue > usercash) throw new Error("You tried to spend "+(result.quantity*stockvalue)+" on shares but only have "+usercash);
sharesowned += result.quantity;
usercash -= result.quantity*stockvalue;
}
}
displayStats();
} catch(e) {
console.log("Error", e);
handleError(e);
}
}
It could use a bit more hardening, but you can see the basics of what I'm doing here. As the simulation runs you see a running report of your stats.
So - want to give it a shot? Stock Market AI Simulator
In case you are curious - you can absolutely cheat with this. I don't look for sneaky things like selling negative amounts of stock. Nor do I block you from reaching out into the global scope and manipulating your values. I'd love to see what people try in the demo. Post your examples as comments below.
Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments