Search in sources :

Example 1 with To

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;
}
Also used : OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Md5Crypt(org.apache.commons.codec.digest.Md5Crypt) Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) Files(java.nio.file.Files) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) IOException(java.io.IOException) To(suite.util.To) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) RunUtil(suite.util.RunUtil) StandardCopyOption(java.nio.file.StandardCopyOption) Pair(suite.adt.pair.Pair) List(java.util.List) Paths(java.nio.file.Paths) Rethrow(suite.util.Rethrow) FileUtil(suite.os.FileUtil) Ints_(suite.primitive.Ints_) Path(java.nio.file.Path) Fail(suite.util.Fail) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Streamlet2(suite.streamlet.Streamlet2) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PrintWriter(java.io.PrintWriter)

Example 2 with To

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);
        }
    });
}
Also used : Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) Obj_Flt(suite.primitive.FltPrimitives.Obj_Flt) LogUtil(suite.os.LogUtil) URL(java.net.URL) HashMap(java.util.HashMap) Obj_Lng(suite.primitive.LngPrimitives.Obj_Lng) HomeDir(suite.util.HomeDir) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Map(java.util.Map) FileUtil(suite.os.FileUtil) JsonNode(com.fasterxml.jackson.databind.JsonNode) Path(java.nio.file.Path) LngFltPair(suite.primitive.adt.pair.LngFltPair) HttpUtil(suite.http.HttpUtil) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) FoldOp(suite.util.FunUtil2.FoldOp) Constants(suite.Constants) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) To(suite.util.To) URLEncoder(java.net.URLEncoder) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) As(suite.streamlet.As) TimeRange(suite.trade.TimeRange) Fail(suite.util.Fail) InputStream(java.io.InputStream) As(suite.streamlet.As) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) To(suite.util.To)

Example 3 with To

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();
    }
}
Also used : Arrays(java.util.Arrays) DblSource(suite.primitive.DblPrimitives.DblSource) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Random(java.util.Random) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Friends.max(suite.util.Friends.max) Floats_(suite.primitive.Floats_) Vector(suite.math.linalg.Vector) Floats(suite.primitive.Floats) FltObjPair(suite.primitive.adt.pair.FltObjPair) FltStreamlet(suite.primitive.streamlet.FltStreamlet) Ints_(suite.primitive.Ints_) Int_Dbl(suite.primitive.Int_Dbl) Int_Flt(suite.primitive.Int_Flt) DblObjPair(suite.primitive.adt.pair.DblObjPair) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Example 4 with To

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<>();
}
Also used : HttpUtil(suite.http.HttpUtil) Outlet(suite.streamlet.Outlet) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) Object_(suite.util.Object_) URL(java.net.URL) Trade_(suite.trade.Trade_) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) HashMap(java.util.HashMap) To(suite.util.To) ArrayList(java.util.ArrayList) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Fixie(suite.adt.pair.Fixie) Rethrow(suite.util.Rethrow) Map(java.util.Map) Fixie3(suite.adt.pair.Fixie_.Fixie3) As(suite.streamlet.As) JsonNode(com.fasterxml.jackson.databind.JsonNode) Collections(java.util.Collections) Fail(suite.util.Fail) InputStream(java.io.InputStream) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) To(suite.util.To) Object_(suite.util.Object_) URL(java.net.URL)

Example 5 with To

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);
        }
    }
}
Also used : Arrays(java.util.Arrays) DblSource(suite.primitive.DblPrimitives.DblSource) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Random(java.util.Random) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Friends.max(suite.util.Friends.max) Floats_(suite.primitive.Floats_) Vector(suite.math.linalg.Vector) Floats(suite.primitive.Floats) FltObjPair(suite.primitive.adt.pair.FltObjPair) FltStreamlet(suite.primitive.streamlet.FltStreamlet) Ints_(suite.primitive.Ints_) Int_Dbl(suite.primitive.Int_Dbl) Int_Flt(suite.primitive.Int_Flt) DblObjPair(suite.primitive.adt.pair.DblObjPair) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Aggregations

To (suite.util.To)7 List (java.util.List)5 Read (suite.streamlet.Read)5 Map (java.util.Map)3 Set (java.util.Set)3 Singleton (suite.node.util.Singleton)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Random (java.util.Random)2 Pair (suite.adt.pair.Pair)2 HttpUtil (suite.http.HttpUtil)2 Vector (suite.math.linalg.Vector)2 Statistic (suite.math.numeric.Statistic)2