コード例 #1
0
  /** 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;
  }
コード例 #2
0
 /** 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;
 }
コード例 #3
0
 /**
  * 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));
   }
 }
コード例 #4
0
 /** Write [DATE] Buy/Sell [SYMBOL] [NUMBER]sh @[PRICE] CANCELLED (no price on date) * */
 public void orderCancelled(
     TradeOrder order, Date date, StockPosition position, TradingAccount account) {
   String priceString;
   switch (order.getPriceTiming()) {
     case OPEN:
     case CLOSE:
     default:
       priceString = DOLLAR_FORMAT.format(order.projectedPrice);
       break;
     case LIMIT_OR_BELOW:
       priceString = "<=" + DOLLAR_FORMAT.format(order.limit);
       break;
     case LIMIT_OR_ABOVE:
       priceString = ">=" + DOLLAR_FORMAT.format(order.limit);
       break;
   }
   // ATR removed "cancelled order" log. Doesn't make sense when
   // using lots of stop and limit orders that don't fill.
 }