use of suite.util.To in project suite by stupidsing.
the class LibraryMain method run.
protected boolean run(String[] args) {
Pair<Streamlet2<Path, Long>, Streamlet2<Path, Long>> partition = //
FileUtil.findPaths(Paths.get(inputDir)).filter(//
path -> fileExtensions.contains(FileUtil.getFileExtension(path))).map2(//
path -> Rethrow.ex(() -> Files.size(path))).partition((path, size) -> 0 < size);
// remove empty files
partition.t1.sink((path, size) -> {
try {
Files.delete(path);
} catch (IOException ex) {
Fail.t(ex);
}
});
Streamlet2<Path, FileInfo> path_fileInfos = //
partition.t0.map2((path, size) -> {
BasicFileAttributes attrs = Rethrow.ex(() -> Files.readAttributes(path, BasicFileAttributes.class));
// get all file information
List<String> tags = //
Ints_.range(//
path.getNameCount()).map(//
i -> path.getName(i).toString()).cons(//
To.string(attrs.lastModifiedTime().toInstant())).toList();
FileInfo fileInfo = new FileInfo();
fileInfo.md5 = Rethrow.ex(() -> Md5Crypt.md5Crypt(Files.readAllBytes(path)));
fileInfo.tags = tags;
return fileInfo;
});
// construct file listing
try (OutputStream os = FileUtil.out(inputDir + ".listing");
PrintWriter pw = new PrintWriter(os)) {
for (Pair<Path, FileInfo> path_fileInfo : path_fileInfos) pw.println(path_fileInfo.t0 + path_fileInfo.t1.md5);
} catch (IOException ex) {
Fail.t(ex);
}
//
path_fileInfos.map2((path, fileInfo) -> {
// move file to library, by md5
Path path1 = Paths.get(libraryDir, fileInfo.md5.substring(0, 2), fileInfo.md5);
FileUtil.mkdir(path1.getParent());
Rethrow.ex(() -> Files.move(path, path1, StandardCopyOption.REPLACE_EXISTING));
return fileInfo;
}).concatMap((path, fileInfo) -> Read.from(fileInfo.tags).map(tag -> {
// add to tag indices
Path path1 = Paths.get(tagsDir, tag, fileInfo.md5);
return Rethrow.ex(() -> {
Files.newOutputStream(path1).close();
return Pair.of(tag, fileInfo);
});
}));
return true;
}
use of suite.util.To in project suite by stupidsing.
the class Yahoo method dataSourceYql.
public DataSource dataSourceYql(String symbol, TimeRange period) {
String yql = //
"select *" + //
" from yahoo.finance.historicaldata" + " where symbol = \"" + symbol + //
"\"" + " and startDate = \"" + period.from.ymd() + //
"\"" + " and endDate = \"" + period.to.ymd() + "\"";
String urlString = //
"http://query.yahooapis.com/v1/public/yql" + "?q=" + //
encode(yql) + //
"&format=json" + //
"&diagnostics=true" + //
"&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" + "&callback=";
return Rethrow.ex(() -> {
try (InputStream is = Singleton.me.storeCache.http(urlString).collect(To::inputStream)) {
JsonNode json = mapper.readTree(is);
Streamlet<JsonNode> quotes = //
Read.each(json).flatMap(//
json_ -> json_.path("query")).flatMap(//
json_ -> json_.path("results")).flatMap(//
json_ -> json_.path("quote")).collect(As::streamlet);
Streamlet<String[]> arrays = //
quotes.map(json_ -> new String[] { //
json_.path("Date").textValue(), //
json_.path("Open").textValue(), //
json_.path("Close").textValue(), //
json_.path("Low").textValue(), //
json_.path("High").textValue() }).collect(As::streamlet);
long[] ts = arrays.collect(Obj_Lng.lift(array -> closeTs(array[0]))).toArray();
float[] opens = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[1]))).toArray();
float[] closes = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[2]))).toArray();
float[] lows = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[3]))).toArray();
float[] highs = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[4]))).toArray();
float[] volumes = new float[ts.length];
return DataSource.ofOhlcv(ts, opens, closes, lows, highs, volumes);
}
});
}
use of suite.util.To in project suite by stupidsing.
the class Arima method maIa.
// "High Frequency Trading - A Practical Guide to Algorithmic Strategies and
// Trading Systems", Irene Aldridge, page 100
// xs[t]
// = mas[0] * 1 + mas[1] * ep[t - 1] + ... + mas[q] * ep[t - q]
// + ep[t]
@SuppressWarnings("unused")
private float[] maIa(float[] xs, int q) {
int length = xs.length;
float[][] epqByIter = new float[q][];
int iter = 0;
int qm1 = q - 1;
while (true) {
int iter_ = iter;
LinearRegression lr = stat.linearRegression(//
Ints_.range(//
length).map(t -> {
int tqm1 = t + qm1;
float[] lrxs = Floats_.concat(Floats_.of(1f), Ints_.range(iter_).collect(Int_Flt.lift(i -> epqByIter[i][tqm1 - i]))).toArray();
return FltObjPair.of(xs[t], lrxs);
}));
if (iter < q)
System.arraycopy(lr.residuals, 0, epqByIter[iter++] = new float[q + length], q, length);
else
return lr.coefficients();
}
}
use of suite.util.To in project suite by stupidsing.
the class Google method quote_.
private synchronized Map<String, Float> quote_(Streamlet<String> symbols) {
if (0 < symbols.size()) {
URL url = To.url(//
"http://finance.google.com/finance/info?client=ig&q=HKEX%3A" + symbols.sort(Object_::compare).map(this::fromSymbol).collect(As.joinedBy(",")));
JsonNode json = Rethrow.ex(() -> {
try (InputStream is = HttpUtil.get(url).out.collect(To::inputStream)) {
for (int i = 0; i < 4; i++) is.read();
return mapper.readTree(is);
}
});
return //
Read.from(//
json).map2(json_ -> toSymbol(json_.get("t").textValue()), //
json_ -> Float.parseFloat(json_.get("l").textValue().replace(",", ""))).toMap();
} else
return new HashMap<>();
}
use of suite.util.To in project suite by stupidsing.
the class Arima method armaIa.
// extended from
// "High Frequency Trading - A Practical Guide to Algorithmic Strategies and
// Trading Systems", Irene Aldridge, page 100
// xs[t]
// = ars[0] * xs[t - 1] + ... + ars[p - 1] * xs[t - p]
// + ep[t]
// + mas[0] * ep[t - 1] + ... + mas[q - 1] * ep[t - q]
private Arima_ armaIa(float[] xs, int p, int q) {
int length = xs.length;
int lengthp = length + p, lengthpm1 = lengthp - 1;
int lengthq = length + q, lengthqm1 = lengthq - 1;
int iter = 0;
float[] xsp = new float[lengthp];
float[][] epqByIter = new float[q][];
Arrays.fill(xsp, 0, p, xs[0]);
System.arraycopy(xs, 0, xsp, p, length);
while (true) {
int iter_ = iter;
LinearRegression lr = stat.linearRegression(//
Ints_.range(//
length).map(t -> {
int tp = t + p;
int tq = t + q, tqm1 = tq - 1;
float[] lrxs = //
Floats_.concat(Floats_.reverse(xsp, t, tp), //
Ints_.range(iter_).collect(Int_Flt.lift(i -> epqByIter[i][tqm1 - i]))).toArray();
return FltObjPair.of(xsp[tp], lrxs);
}));
float[] coeffs = lr.coefficients();
if (iter < q)
System.arraycopy(lr.residuals, 0, epqByIter[iter++] = new float[lengthq], q, length);
else {
float[] ars = Floats.of(coeffs, 0, p).toArray();
float[] mas = Floats.of(coeffs, p).toArray();
double x1 = //
0d + //
Ints_.range(p).toDouble(Int_Dbl.sum(i -> ars[i] * xsp[lengthpm1 - i])) + Ints_.range(q).toDouble(Int_Dbl.sum(i -> mas[i] * epqByIter[i][lengthqm1 - i]));
return new Arima_(ars, mas, (float) x1);
}
}
}
Aggregations