diff --git a/lib/market.js b/lib/market.js index 6e58939..ef78c81 100644 --- a/lib/market.js +++ b/lib/market.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.4.0 (function() { - var Stream, delay, geekdaq, inspect, pretty, randInt, + var Stream, delay, geekdaq, inspect, isString, log, pretty, randInt, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; @@ -14,13 +14,19 @@ geekdaq = require('geekdaq'); pretty = function(obj) { - return "" + (inspect(obj)); + return "" + (inspect(obj, false, 20, true)); }; randInt = function(min, max) { return Math.round(min + Math.random() * (max - min)); }; + isString = function(obj) { + return !!(obj === '' || (obj && obj.charCodeAt && obj.substr)); + }; + + log = console.log; + module.exports = (function(_super) { __extends(exports, _super); @@ -117,8 +123,12 @@ return this.update(); }; - exports.prototype.ticker = function(code) { - return this.tickers[code]; + exports.prototype.ticker = function(t) { + if (isString(t)) { + return this.tickers[t]; + } else { + return t; + } }; exports.prototype.register = function(account) { @@ -143,7 +153,7 @@ this.emit('debug', "username: " + username + " and account: " + (pretty(account))); for (_i = 0, _len = orders.length; _i < _len; _i++) { order = orders[_i]; - this.emit('debug', "processing " + order.type + " order:"); + this.emit('debug', "going to " + order.type + " " + order.amount + " " + order.ticker + ":"); ticker = this.ticker(order.ticker); switch (order.type) { case 'buy': @@ -151,9 +161,8 @@ total_cost = raw_cost + (raw_cost * this.commissions.buy); this.emit('debug', "buy total cost: " + total_cost); if (account.balance < total_cost) { - msg("" + username + "'s balance is " + account.balance + ", but cost is " + total_cost); + msg = "" + username + "'s balance is " + account.balance + ", but cost is " + total_cost; this.emit('error', 'NOT_ENOUGH_MONEY', msg); - continue; } else { account.balance -= total_cost; if (order.symbol in account.portfolio) { @@ -174,26 +183,30 @@ if (!(order.ticker in account.portfolio)) { msg = "" + username + " doesn't own any " + order.ticker; this.emit('error', 'NOT_IN_PORTFOLIO', msg); - continue; - } - amount = account.portfolio[order.ticker]; - if (amount < order.amount) { - msg = "" + username + " doesn't have enough " + order.ticker + " to sell (want to sell " + order.amount + ", but we have " + amount + ")"; - this.emit('error', 'NOT_ENOUGH_SHARES', msg); - continue; + } else { + amount = account.portfolio[order.ticker]; + if (amount < order.amount) { + msg = "" + username + " doesn't have enough " + order.ticker + " to sell (want to sell " + order.amount + ", but we have " + amount + ")"; + this.emit('error', 'NOT_ENOUGH_SHARES', msg); + } else { + raw_earnings = amount * ticker.price; + total_earnings = raw_earnings - (raw_earnings * this.commissions.sell); + this.emit('debug', "total earnings: " + total_earnings); + account.portfolio[order.ticker] -= order.amount; + account.balance += total_earnings; + account.history.push({ + type: order.type, + ticker: order.ticker, + amount: order.amount, + price: ticker.price, + earnings: total_earnings + }); + } } - raw_earnings = amount * ticker.price; - total_earnings = raw_earnings - (raw_earnings * this.commissions.sell); - this.emit('debug', "total earnings: " + total_earnings); - account.portfolio[order.ticker] -= order.amount; - account.balance += total_earnings; - account.history.push({ - type: order.type, - ticker: order.symbol, - amount: order.amount, - price: ticker.price, - earnings: total_earnings - }); + break; + default: + msg = "unknown order type '" + order.type + "'"; + this.emit('error', 'UNKNOWN_ORDER_TYPE', msg); } } return onComplete(void 0); diff --git a/src/market.coffee b/src/market.coffee index 2d2f4dc..6a3451f 100644 --- a/src/market.coffee +++ b/src/market.coffee @@ -4,9 +4,12 @@ Stream = require 'stream' {delay} = require 'ragtime' geekdaq = require 'geekdaq' -pretty = (obj) -> "#{inspect obj}" +pretty = (obj) -> "#{inspect obj, no, 20, yes}" randInt = (min,max) -> Math.round(min + Math.random() * (max - min)) +isString = (obj) -> !!(obj is '' or (obj and obj.charCodeAt and obj.substr)) + +log = console.log class module.exports extends Stream @@ -63,8 +66,8 @@ class module.exports extends Stream @emit 'started' @update() - ticker: (code) => - @tickers[code] + ticker: (t) => + if isString(t) then @tickers[t] else t register: (account) => @accounts[account.username] = account @@ -83,7 +86,7 @@ class module.exports extends Stream account = @accounts[username] @emit 'debug', "username: #{username} and account: #{pretty account}" for order in orders - @emit 'debug', "processing #{order.type} order:" + @emit 'debug', "going to #{order.type} #{order.amount} #{order.ticker}:" ticker = @ticker order.ticker switch order.type when 'buy' @@ -93,9 +96,8 @@ class module.exports extends Stream @emit 'debug', "buy total cost: #{total_cost}" if account.balance < total_cost - msg "#{username}'s balance is #{account.balance}, but cost is #{total_cost}" + msg = "#{username}'s balance is #{account.balance}, but cost is #{total_cost}" @emit 'error', 'NOT_ENOUGH_MONEY', msg - continue else account.balance -= total_cost #log "order executed, balance is now #{worker.balance}" @@ -113,21 +115,24 @@ class module.exports extends Stream unless order.ticker of account.portfolio msg = "#{username} doesn't own any #{order.ticker}" @emit 'error', 'NOT_IN_PORTFOLIO', msg - continue - amount = account.portfolio[order.ticker] - if amount < order.amount - msg = "#{username} doesn't have enough #{order.ticker} to sell (want to sell #{order.amount}, but we have #{amount})" - @emit 'error', 'NOT_ENOUGH_SHARES', msg - continue - raw_earnings = amount * ticker.price - total_earnings = raw_earnings - (raw_earnings * @commissions.sell) - @emit 'debug', "total earnings: #{total_earnings}" - account.portfolio[order.ticker] -= order.amount - account.balance += total_earnings - account.history.push - type: order.type - ticker: order.symbol - amount: order.amount - price: ticker.price - earnings: total_earnings + else + amount = account.portfolio[order.ticker] + if amount < order.amount + msg = "#{username} doesn't have enough #{order.ticker} to sell (want to sell #{order.amount}, but we have #{amount})" + @emit 'error', 'NOT_ENOUGH_SHARES', msg + else + raw_earnings = amount * ticker.price + total_earnings = raw_earnings - (raw_earnings * @commissions.sell) + @emit 'debug', "total earnings: #{total_earnings}" + account.portfolio[order.ticker] -= order.amount + account.balance += total_earnings + account.history.push + type: order.type + ticker: order.ticker + amount: order.amount + price: ticker.price + earnings: total_earnings + else + msg = "unknown order type '#{order.type}'" + @emit 'error', 'UNKNOWN_ORDER_TYPE', msg onComplete undefined