use of com.datastax.demo.portfolio.Position in project brisk by riptano.
the class PortfolioMgrHandler method addHistInformation.
private void addHistInformation(List<Portfolio> portfolios) {
for (Portfolio p : portfolios) {
ByteBuffer name = ByteBufferUtil.bytes(p.name);
List<ByteBuffer> tickers = new ArrayList<ByteBuffer>();
for (Position position : p.constituents) {
tickers.add(ByteBufferUtil.bytes(position.ticker));
}
try {
Map<ByteBuffer, List<ColumnOrSuperColumn>> result = getClient().multiget_slice(tickers, hcp, hsp, ConsistencyLevel.ONE);
Map<String, Double> histPrices = new LinkedHashMap<String, Double>();
for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> entry : result.entrySet()) {
for (ColumnOrSuperColumn col : entry.getValue()) {
Double price = histPrices.get(ByteBufferUtil.string(col.column.name));
if (price == null)
price = 0.0;
price = +Double.valueOf(ByteBufferUtil.string(col.column.value));
histPrices.put(ByteBufferUtil.string(col.column.name), price);
}
}
p.setHist_prices(Arrays.asList(histPrices.values().toArray(new Double[] {})));
} catch (InvalidRequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimedOutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CharacterCodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
use of com.datastax.demo.portfolio.Position in project brisk by riptano.
the class PortfolioMgrHandler method buildPorfoliosFromRangeSlices.
private List<Portfolio> buildPorfoliosFromRangeSlices(List<KeySlice> kslices) throws Exception {
List<Portfolio> portfolios = new ArrayList<Portfolio>();
for (KeySlice ks : kslices) {
Portfolio p = new Portfolio();
p.setName(new String(ks.getKey()));
Map<ByteBuffer, Long> tickerLookup = new HashMap<ByteBuffer, Long>();
List<ByteBuffer> tickers = new ArrayList<ByteBuffer>();
for (ColumnOrSuperColumn cosc : ks.getColumns()) {
tickers.add(cosc.getColumn().name);
tickerLookup.put(cosc.getColumn().name, ByteBufferUtil.toLong(cosc.getColumn().value));
}
Map<ByteBuffer, List<ColumnOrSuperColumn>> prices = getClient().multiget_slice(tickers, scp, sp, ConsistencyLevel.ONE);
double total = 0;
double basis = 0;
Random r = new Random(Long.valueOf(new String(ks.getKey())));
for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> entry : prices.entrySet()) {
if (!entry.getValue().isEmpty()) {
Double price = Double.valueOf(ByteBufferUtil.string(entry.getValue().get(0).column.value));
Position s = new Position(ByteBufferUtil.string(entry.getKey()), price, tickerLookup.get(entry.getKey()));
p.addToConstituents(s);
total += price * tickerLookup.get(entry.getKey());
basis += r.nextDouble() * 100 * tickerLookup.get(entry.getKey());
}
}
p.setPrice(total);
p.setBasis(basis);
portfolios.add(p);
}
return portfolios;
}
use of com.datastax.demo.portfolio.Position in project brisk by riptano.
the class PortfolioMgrHandler method buildPorfoliosFromCqlResult.
private List<Portfolio> buildPorfoliosFromCqlResult(CqlResult result) throws Exception {
List<Portfolio> portfolios = new ArrayList<Portfolio>();
for (CqlRow row : result.rows) {
Portfolio p = new Portfolio();
p.setName(new String(row.getKey()));
Map<ByteBuffer, Long> tickerLookup = new HashMap<ByteBuffer, Long>();
List<ByteBuffer> tickers = new ArrayList<ByteBuffer>();
for (Column cosc : row.getColumns()) {
tickers.add(cosc.name);
tickerLookup.put(cosc.name, ByteBufferUtil.toLong(cosc.value));
}
double total = 0;
double basis = 0;
Random r = new Random(Long.valueOf(new String(row.getKey())));
for (ByteBuffer ticker : tickers) {
CqlResult tResult = getClient().execute_cql_query(ByteBufferUtil.bytes(buildStocksQuery(ticker)), Compression.NONE);
CqlRow tRow = tResult.getRowsIterator().hasNext() ? tResult.getRowsIterator().next() : null;
if (tRow != null) {
Double price = Double.valueOf(ByteBufferUtil.string(tRow.columns.get(0).value));
Position s = new Position(ByteBufferUtil.string(tRow.key), price, tickerLookup.get(tRow.key));
p.addToConstituents(s);
total += price * tickerLookup.get(tRow.key);
basis += r.nextDouble() * 100 * tickerLookup.get(tRow.key);
}
}
p.setPrice(total);
p.setBasis(basis);
portfolios.add(p);
}
return portfolios;
}
Aggregations