public BackTestFileWriter(Strategy strategy) throws JArbitragerException { decimalFormat = NumberFormatterFactory.getNumberFormatter(5); dateFormat = new SimpleDateFormat("MMddyy,HHmmss"); dateFormat.setTimeZone(strategy.getTradingSchedule().getTimeZone()); File marketDataDir = new File(MARKET_DATA_DIR); if (!marketDataDir.exists()) { marketDataDir.mkdir(); } String fullFileName = MARKET_DATA_DIR + FILE_SEP + strategy.getName() + ".txt"; try { boolean fileExisted = new File(fullFileName).exists(); writer = new PrintWriter(new BufferedWriter(new FileWriter(fullFileName, true))); if (!fileExisted) { StringBuilder header = getHeader( strategy.getInstrument1().getContract().m_symbol, strategy.getInstrument2().getContract().m_symbol); writer.println(header); } } catch (IOException ioe) { throw new JArbitragerException("Could not write to file " + strategy.getName()); } }
public void write(long time, Instrument instrument1, Instrument instrument2) { double bid1 = instrument1.getBid(); double ask1 = instrument1.getAsk(); double bid2 = instrument2.getBid(); double ask2 = instrument2.getAsk(); if (bid1 > 0 && ask1 > 0 && bid2 > 0 && ask2 > 0) { StringBuilder sb = new StringBuilder(); sb.append(dateFormat.format(new Date(time))).append(","); sb.append(decimalFormat.format(bid1)).append(","); sb.append(decimalFormat.format(ask1)).append(","); sb.append(decimalFormat.format(bid2)).append(","); sb.append(decimalFormat.format(ask2)); writer.println(sb); writer.flush(); } }