/** Write [DATE] Buy [SYMBOL] [NUMBER]sh @[PRICE] = [VALUE] --> [NUMBER]sh held * */ public void orderBought( TradeOrder order, Date date, StockPosition position, TradingAccount account) { double profitOrLoss = (order.shares * (order.getExecutedPrice() - position.getCostBasis()) - account.getTradeFees(order.shares)); double percent = profitOrLoss / (position.getCostBasis() * order.shares); this.writer.println( DATE_FORMAT.format(date) + " " + (position.getShares() != 0 ? "Buy " : "Cover ") + order.symbol + " " + order.shares + "sh " + "@" + DOLLAR_FORMAT.format(order.getExecutedPrice()) + " = " + DOLLAR_FORMAT.format(order.getExecutedValue()) + " --> " + position.getShares() + "sh " + (position.getShares() <= 0 ? "left, " + DOLLAR_FORMAT.format(profitOrLoss) + (profitOrLoss > 0 ? " (" + PERCENT_FORMAT.format(percent) + " profit)" : profitOrLoss < 0 ? " (" + PERCENT_FORMAT.format(percent) + "loss)" : " (even)") : "long")); this.lastTraceDate = date; }
/** Write initial cash balance. * */ public void initialized(Date date, TradingAccount account) { writer.println( DATE_FORMAT.format(date) + ": " + "cash=" + DOLLAR_FORMAT.format(account.getCurrentCashBalance())); this.lastTraceDate = date; }
/** * If writing every day, or shares traded on date,<br> * Write [DATE]: cash=[CASHVALUE], stocks=[STOCKVALUE], total=[TOTALVALUE]* */ public void ordersCompleted(Date date, TradingAccount account) { // report new balances only if they changed (buy or sell occurred on date) if (!this.daysTradedOnly || this.lastTraceDate.equals(date)) { double cashBalance = account.getCurrentCashBalance(); double stockValue = account.getCurrentStockValue(); double total = cashBalance + stockValue; this.writer.println( DATE_FORMAT.format(date) + ": " + "cash=" + DOLLAR_FORMAT.format(cashBalance) + ", " + "stocks=" + DOLLAR_FORMAT.format(stockValue) + ", " + "total=" + DOLLAR_FORMAT.format(total)); } }