/** Composes and submits the appropriate order for the given timeslot. */
  private void submitOrder(double neededKWh, int timeslot) {
    double neededMWh = neededKWh / 1000.0;

    MarketPosition posn = broker.getBroker().findMarketPositionByTimeslot(timeslot);
    if (posn != null) neededMWh -= posn.getOverallBalance();
    if (Math.abs(neededMWh) <= minMWh) {
      log.info("no power required in timeslot " + timeslot);
      return;
    }
    Double limitPrice = computeLimitPrice(timeslot, neededMWh);
    log.info("new order for " + neededMWh + " at " + limitPrice + " in timeslot " + timeslot);
    Order order = new Order(broker.getBroker(), timeslot, neededMWh, limitPrice);
    lastOrder.put(timeslot, order);
    broker.sendMessage(order);
  }
 /**
  * Compute needed quantities for each open timeslot, then submit orders for those quantities.
  *
  * @see org.powertac.samplebroker.MarketManager#activate()
  */
 @Override
 public synchronized void activate(int timeslotIndex) {
   double neededKWh = 0.0;
   log.debug("Current timeslot is " + timeslotRepo.currentTimeslot().getSerialNumber());
   for (Timeslot timeslot : timeslotRepo.enabledTimeslots()) {
     int index = (timeslot.getSerialNumber()) % broker.getUsageRecordLength();
     neededKWh = portfolioManager.collectUsage(index);
     submitOrder(neededKWh, timeslot.getSerialNumber());
   }
 }
 /**
  * Receives a MarketBootstrapData message, reporting usage and prices for the bootstrap period. We
  * record the overall weighted mean price, as well as the mean price and usage for a week.
  */
 public synchronized void handleMessage(MarketBootstrapData data) {
   marketMWh = new double[broker.getUsageRecordLength()];
   marketPrice = new double[broker.getUsageRecordLength()];
   double totalUsage = 0.0;
   double totalValue = 0.0;
   for (int i = 0; i < data.getMwh().length; i++) {
     totalUsage += data.getMwh()[i];
     totalValue += data.getMarketPrice()[i] * data.getMwh()[i];
     if (i < broker.getUsageRecordLength()) {
       // first pass, just copy the data
       marketMWh[i] = data.getMwh()[i];
       marketPrice[i] = data.getMarketPrice()[i];
     } else {
       // subsequent passes, accumulate mean values
       int pass = i / broker.getUsageRecordLength();
       int index = i % broker.getUsageRecordLength();
       marketMWh[index] = (marketMWh[index] * pass + data.getMwh()[i]) / (pass + 1);
       marketPrice[index] = (marketPrice[index] * pass + data.getMarketPrice()[i]) / (pass + 1);
     }
   }
   meanMarketPrice = totalValue / totalUsage;
 }
 /* (non-Javadoc)
  * @see org.powertac.samplebroker.MarketManager#init(org.powertac.samplebroker.SampleBroker)
  */
 @Override
 public void initialize(BrokerContext broker) {
   this.broker = broker;
   lastOrder = new HashMap<>();
   propertiesService.configureMe(this);
   System.out.println("  name=" + broker.getBrokerUsername());
   if (seedNumber != null) {
     System.out.println("  seeding=" + seedNumber);
     log.info("Seeding with : " + seedNumber);
     randomGen = new Random(seedNumber);
   } else {
     randomGen = new Random();
   }
 }
 /** Receives a MarketPosition message, representing our commitments on the wholesale market */
 public synchronized void handleMessage(MarketPosition posn) {
   broker.getBroker().addMarketPosition(posn, posn.getTimeslotIndex());
 }