DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Experiment: Build Your Own Stock Market AI

Experiment: Build Your Own Stock Market AI

Raymond Camden user avatar by
Raymond Camden
·
Oct. 18, 12 · Interview
Like (0)
Save
Tweet
Share
4.80K Views

Join the DZone community and get the full member experience.

Join For Free

I 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.

 

 

 

AI Build (game engine)

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The New DevOps Performance Clusters
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • Data Engineering Trends for 2023
  • Kubernetes vs Docker: Differences Explained

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: