Search in sources :

Example 6 with As

use of suite.streamlet.As in project suite by stupidsing.

the class FunctionalTemplateRendererTest method test.

@Test
public void test() {
    List<Node> fruits = Arrays.<Node>asList(new Str("orange"), new Str("apple"), new Str("pear"));
    Map<String, Node> map = // 
    Read.from2(// 
    List.of(// 
    Pair.of("list", Tree.of(TermOp.OR____, fruits)), // 
    Pair.of("title", new Str("My favourite things")))).collect(As::map);
    System.out.println(// 
    new FunctionalTemplateRenderer().render(// 
    "" + // 
    "<html> \n" + // 
    "    <head> \n" + // 
    "        <#= title #> \n" + // 
    "    </head> \n" + // 
    "    </body> \n" + // 
    "        Fruits: \n" + // 
    "<# . (list | apply . map {fruit => id#>        <li><#= fruit #></li> \n" + // 
    "<#}) #>    <body> \n" + // 
    "</html> \n", map));
}
Also used : Str(suite.node.Str) As(suite.streamlet.As) Node(suite.node.Node) Test(org.junit.Test)

Example 7 with As

use of suite.streamlet.As in project suite by stupidsing.

the class AnalyzeTimeSeriesTest method analyze.

private void analyze(float[] prices) {
    int length = prices.length;
    int log2 = Quant.log2trunc(length);
    double nYears = length * Trade_.invTradeDaysPerYear;
    float[] fds = dct.dct(Arrays.copyOfRange(prices, length - log2, length));
    float[] returns = ts.returns(prices);
    float[] logPrices = To.vector(prices, Math::log);
    float[] logReturns = ts.differences(1, logPrices);
    MeanVariance rmv = stat.meanVariance(returns);
    double variance = rmv.variance;
    double kelly = rmv.mean / variance;
    IntFltPair max = IntFltPair.of(Integer.MIN_VALUE, Float.MIN_VALUE);
    for (int i = 4; i < fds.length; i++) {
        float f = Math.abs(fds[i]);
        if (max.t1 < f)
            max.update(i, f);
    }
    IntFunction<BuySell> momFun = n -> {
        int d0 = 1 + n;
        int d1 = 1;
        return buySell(d -> Quant.sign(prices[d - d0], prices[d - d1])).start(d0);
    };
    IntFunction<BuySell> revert = d -> momFun.apply(d).scale(0f, -1f);
    IntFunction<BuySell> trend_ = d -> momFun.apply(d).scale(0f, +1f);
    BuySell[] reverts = To.array(8, BuySell.class, revert);
    BuySell[] trends_ = To.array(8, BuySell.class, trend_);
    BuySell tanh = buySell(d -> Tanh.tanh(3.2d * reverts[1].apply(d)));
    float[] holds = mt.hold(prices, 1f, 1f, 1f);
    float[] ma200 = ma.movingAvg(prices, 200);
    BuySell mat = buySell(d -> {
        int last = d - 1;
        return Quant.sign(ma200[last], prices[last]);
    }).start(1).longOnly();
    BuySell mt_ = buySell(d -> holds[d]);
    Pair<float[], float[]> bbmv = bb.meanVariances(VirtualVector.of(logReturns), 9, 0);
    float[] bbmean = bbmv.t0;
    float[] bbvariances = bbmv.t1;
    BuySell ms2 = buySell(d -> {
        int last = d - 1;
        int ref = last - 250;
        float mean = bbmean[last];
        return Quant.sign(logPrices[last], logPrices[ref] - bbvariances[last] / (2d * mean * mean));
    }).start(1 + 250);
    LogUtil.info(// 
    "" + "\nsymbol = " + // 
    symbol + "\nlength = " + // 
    length + "\nnYears = " + // 
    nYears + "\nups = " + // 
    Floats_.of(returns).filter(return_ -> 0f <= return_).size() + "\ndct period = " + // 
    max.t0 + // 
    Ints_.range(// 
    10).map(// 
    d -> "\ndct component [" + d + "d] = " + fds[d]).collect(// 
    As::joined) + "\nreturn yearly sharpe = " + // 
    rmv.mean / Math.sqrt(variance / nYears) + "\nreturn kelly = " + // 
    kelly + "\nreturn skew = " + // 
    stat.skewness(returns) + "\nreturn kurt = " + // 
    stat.kurtosis(returns) + // 
    Ints_.of(1, 2, 4, 8, 16, // 
    32).map(// 
    d -> "\nmean reversion ols [" + d + "d] = " + ts.meanReversion(prices, d).coefficients[0]).collect(// 
    As::joined) + // 
    Ints_.of(4, // 
    16).map(// 
    d -> "\nvariance ratio [" + d + "d over 1d] = " + ts.varianceRatio(prices, d)).collect(// 
    As::joined) + "\nreturn hurst = " + // 
    ts.hurst(prices, prices.length / 2) + "\nhold " + // 
    buySell(d -> 1d).invest(prices) + "\nkelly " + // 
    buySell(d -> kelly).invest(prices) + "\nma200 trend " + // 
    mat.invest(prices) + // 
    Ints_.range(1, // 
    8).map(// 
    d -> "\nrevert [" + d + "d] " + reverts[d].invest(prices)).collect(// 
    As::joined) + // 
    Ints_.range(1, // 
    8).map(// 
    d -> "\ntrend_ [" + d + "d] " + trends_[d].invest(prices)).collect(// 
    As::joined) + // 
    Ints_.range(1, // 
    8).map(// 
    d -> "\nrevert [" + d + "d] long-only " + reverts[d].longOnly().invest(prices)).collect(// 
    As::joined) + // 
    Ints_.range(1, // 
    8).map(// 
    d -> "\ntrend_ [" + d + "d] long-only " + trends_[d].longOnly().invest(prices)).collect(// 
    As::joined) + "\nms2 " + // 
    ms2.invest(prices) + "\nms2 long-only " + // 
    ms2.longOnly().invest(prices) + "\ntanh " + // 
    tanh.invest(prices) + "\ntimed " + // 
    mt_.invest(prices) + "\ntimed long-only " + mt_.longOnly().invest(prices));
}
Also used : Arrays(java.util.Arrays) LogUtil(suite.os.LogUtil) IntFltPair(suite.primitive.adt.pair.IntFltPair) Trade_(suite.trade.Trade_) ConfigurationImpl(suite.trade.data.ConfigurationImpl) TimeSeries(ts.TimeSeries) Ints_(suite.primitive.Ints_) DiscreteCosineTransform(suite.math.transform.DiscreteCosineTransform) IntFunction(java.util.function.IntFunction) Statistic(suite.math.numeric.Statistic) Test(org.junit.Test) To(suite.util.To) Quant(ts.Quant) BollingerBands(ts.BollingerBands) Tanh(suite.math.Tanh) VirtualVector(suite.math.linalg.VirtualVector) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) MeanVariance(suite.math.numeric.Statistic.MeanVariance) Time(suite.trade.Time) Floats_(suite.primitive.Floats_) Configuration(suite.trade.data.Configuration) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) TimeRange(suite.trade.TimeRange) Int_Dbl(suite.primitive.Int_Dbl) Int_Flt(suite.primitive.Int_Flt) IntFltPair(suite.primitive.adt.pair.IntFltPair) MeanVariance(suite.math.numeric.Statistic.MeanVariance) As(suite.streamlet.As)

Example 8 with As

use of suite.streamlet.As in project suite by stupidsing.

the class BackTestMain method run.

@Override
protected boolean run(String[] args) {
    // BEGIN
    // END
    String arg0 = 0 < args.length ? args[0] : "";
    String arg1 = 1 < args.length ? args[1] : "";
    String arg2 = 2 < args.length ? args[2] : "";
    Streamlet<String> strategyMatches = !arg0.isEmpty() ? Read.from(arg0.split(",")) : null;
    Streamlet<Integer> years = !arg1.isEmpty() ? // 
    Read.from(// 
    arg1.split(",")).concatMap(s -> {
        Pair<String, String> pair = ParseUtil.search(s, "-", Assoc.RIGHT);
        return // 
        pair != null ? // 
        Ints_.range(Integer.valueOf(pair.t0), Integer.valueOf(pair.t1)).map(i -> i) : Read.each(Integer.valueOf(s));
    }) : // 
    Ints_.range(2007, Trade_.thisYear).map(i -> i);
    Fun<Time, Streamlet<Asset>> fun = // 
    !arg2.isEmpty() ? // 
    time -> Read.from(arg2.split(",")).map(cfg::queryCompany).collect(As::streamlet) : cfg::queryCompaniesByMarketCap;
    BackAllocConfigurations bac_ = new BackAllocConfigurations(cfg, fun);
    Streamlet2<String, BackAllocConfiguration> bacByTag = bac_.bacs().bacByName;
    Streamlet2<String, Simulate> simulationByKey = // 
    bacByTag.filterKey(// 
    n -> strategyMatches == null || strategyMatches.isAny(sm -> Wildcard.match(sm, n) != null)).map(// 
    Pair::of).join2(// 
    years.sort(Object_::compare).map(TimeRange::ofYear)).map2((pair, period) -> pair.t0, (pair, period) -> {
        BackAllocConfiguration bac = pair.t1;
        Streamlet<Asset> assets = bac.assetsFun.apply(period.from);
        return runner.backTest(bac.backAllocator, period, assets);
    }).collect(As::streamlet2);
    String content0 = // 
    Read.bytes(// 
    Paths.get("src/main/java/" + getClass().getName().replace('.', '/') + ".java")).collect(// 
    As::utf8decode).map(// 
    Chars::toString).collect(As::joined);
    String content1 = ParseUtil.fit(content0, "// BEGIN", "// END")[1];
    System.out.println(content1);
    System.out.println(runner.conclude(simulationByKey));
    return true;
}
Also used : Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Trade_(suite.trade.Trade_) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) Chars(suite.primitive.Chars) Fun(suite.util.FunUtil.Fun) RunUtil(suite.util.RunUtil) Wildcard(suite.parser.Wildcard) ParseUtil(suite.util.ParseUtil) Pair(suite.adt.pair.Pair) ConfigurationImpl(suite.trade.data.ConfigurationImpl) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Paths(java.nio.file.Paths) Configuration(suite.trade.data.Configuration) As(suite.streamlet.As) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) Ints_(suite.primitive.Ints_) Asset(suite.trade.Asset) Assoc(suite.node.io.Operator.Assoc) TimeRange(suite.trade.TimeRange) Time(suite.trade.Time) Object_(suite.util.Object_) As(suite.streamlet.As) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Streamlet(suite.streamlet.Streamlet) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Pair(suite.adt.pair.Pair)

Example 9 with As

use of suite.streamlet.As in project suite by stupidsing.

the class Aastocks method quote_.

private float quote_(String symbol) {
    String urlString = "http://www.aastocks.com/en/mobile/Quote.aspx?symbol=0" + symbol.substring(0, 4);
    URL url = To.url(urlString);
    List<String> lines = HttpUtil.get(url).out.collect(As::lines).toList();
    int i0 = Ints_.range(lines.size()).filter(i -> lines.get(i).contains("text_last")).first();
    return toFloat(lines.get(i0 + 1).replace("0px", "").replace(".png", ""));
}
Also used : HttpUtil(suite.http.HttpUtil) ObjFltMap(suite.primitive.adt.map.ObjFltMap) List(java.util.List) URL(java.net.URL) Trade_(suite.trade.Trade_) Chars_(suite.primitive.Chars_) As(suite.streamlet.As) To(suite.util.To) Ints_(suite.primitive.Ints_) URL(java.net.URL)

Example 10 with As

use of suite.streamlet.As in project suite by stupidsing.

the class Aastocks method hsi.

public float hsi() {
    String urlString = "http://www.aastocks.com/en/mobile/Quote.aspx?symbol=00005";
    URL url = To.url(urlString);
    List<String> lines = HttpUtil.get(url).out.collect(As::lines).toList();
    int i0 = Ints_.range(lines.size()).filter(i -> lines.get(i).contains("HSI")).first();
    return toFloat(lines.get(i0 + 1));
}
Also used : HttpUtil(suite.http.HttpUtil) ObjFltMap(suite.primitive.adt.map.ObjFltMap) List(java.util.List) URL(java.net.URL) Trade_(suite.trade.Trade_) Chars_(suite.primitive.Chars_) As(suite.streamlet.As) To(suite.util.To) Ints_(suite.primitive.Ints_) URL(java.net.URL)

Aggregations

As (suite.streamlet.As)16 List (java.util.List)7 Read (suite.streamlet.Read)7 To (suite.util.To)7 Streamlet (suite.streamlet.Streamlet)6 Asset (suite.trade.Asset)6 Time (suite.trade.Time)6 Map (java.util.Map)5 Trade_ (suite.trade.Trade_)5 URL (java.net.URL)4 Set (java.util.Set)4 Pair (suite.adt.pair.Pair)4 HttpUtil (suite.http.HttpUtil)4 TimeRange (suite.trade.TimeRange)4 String_ (suite.util.String_)4 IOException (java.io.IOException)3 Ints_ (suite.primitive.Ints_)3 Streamlet2 (suite.streamlet.Streamlet2)3 BackAllocConfiguration (suite.trade.backalloc.BackAllocConfiguration)3 Configuration (suite.trade.data.Configuration)3