Search in sources :

Example 16 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class TextUtil method merge.

public List<Pair<Bytes, Bytes>> merge(List<Pair<Bytes, Bytes>> pairsx, List<Pair<Bytes, Bytes>> pairsy, boolean isDetectSameChanges) throws ConflictException {
    boolean isEmptyx = pairsx.isEmpty();
    boolean isEmptyy = pairsy.isEmpty();
    if (!isEmptyx || !isEmptyy) {
        Pair<Bytes, Bytes> phx = !isEmptyx ? pairsx.get(0) : Pair.of(Bytes.empty, Bytes.empty);
        Pair<Bytes, Bytes> phy = !isEmptyy ? pairsy.get(0) : Pair.of(Bytes.empty, Bytes.empty);
        List<Pair<Bytes, Bytes>> ptx = !isEmptyx ? List_.right(pairsx, 1) : pairsx;
        List<Pair<Bytes, Bytes>> pty = !isEmptyy ? List_.right(pairsy, 1) : pairsy;
        int c = min(phx.t0.size(), phy.t0.size());
        Bytes commonx = phx.t0.range(0, c);
        Bytes commony = phy.t0.range(0, c);
        if (Objects.equals(commonx, commony)) {
            int s0, s1;
            Pair<Bytes, Bytes> pair;
            List<Pair<Bytes, Bytes>> pairs;
            if (// 
            isDetectSameChanges && // 
            phx.t0 != phx.t1 && // 
            phy.t0 != phy.t1 && // 
            0 < (s0 = detectSame(phx.t0, phy.t0)) && 0 < (s1 = detectSame(phx.t1, phy.t1))) {
                pair = Pair.of(phx.t0.range(0, s0), phx.t1.range(0, s1));
                pairs = merge(// 
                cons(Pair.of(phx.t0.range(s0), phx.t1.range(s1)), ptx), // 
                cons(Pair.of(phy.t0.range(s0), phy.t1.range(s1)), pty), isDetectSameChanges);
            } else if (phx.t0 != phx.t1) {
                pair = phx;
                pairs = merge(ptx, cons(skip(phy, c), pty), isDetectSameChanges);
            } else if (phy.t0 != phy.t1) {
                pair = phy;
                pairs = merge(cons(skip(phx, c), ptx), pty, isDetectSameChanges);
            } else {
                pair = Pair.of(commonx, commonx);
                pairs = merge(cons(skip(phx, c), ptx), cons(skip(phy, c), pty), isDetectSameChanges);
            }
            return cons(pair, pairs);
        } else
            throw new ConflictException();
    } else
        return List.of();
}
Also used : Bytes(suite.primitive.Bytes) Pair(suite.adt.pair.Pair)

Example 17 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class Trade_ method dividend.

public static float dividend(Streamlet<Trade> trades, Fun<String, LngFltPair[]> fun, Dbl_Dbl feeFun) {
    float sum = 0f;
    for (Pair<String, List<Trade>> pair : trades.toMultimap(trade -> trade.symbol).listEntries()) {
        LngFltPair[] dividends = fun.apply(pair.t0);
        Outlet<Trade> outlet = Outlet.of(pair.t1);
        LngIntPair tn = LngIntPair.of(0l, 0);
        Source<LngIntPair> tradeSource = () -> {
            Trade trade = outlet.next();
            long t = trade != null ? Time.of(trade.date + " 12:00:00").epochSec(8) : Long.MAX_VALUE;
            return LngIntPair.of(t, tn.t1 + (trade != null ? trade.buySell : 0));
        };
        LngIntPair tn1 = tradeSource.source();
        for (LngFltPair dividend : dividends) {
            while (tn1 != null && tn1.t0 < dividend.t0) {
                tn.update(tn1.t0, tn1.t1);
                tn1 = tradeSource.source();
            }
            float amount = tn.t1 * dividend.t1;
            sum += amount - feeFun.apply(amount);
        }
    }
    return sum;
}
Also used : LngIntPair(suite.primitive.adt.pair.LngIntPair) Outlet(suite.streamlet.Outlet) Read(suite.streamlet.Read) Obj_Flt(suite.primitive.FltPrimitives.Obj_Flt) IntFltPair(suite.primitive.adt.pair.IntFltPair) IntIntSink(suite.primitive.IntIntSink) HashMap(java.util.HashMap) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) String_(suite.util.String_) Dbl_Dbl(suite.primitive.Dbl_Dbl) Map(java.util.Map) Ints_(suite.primitive.Ints_) Valuation(suite.trade.Account.Valuation) Set_(suite.util.Set_) LngFltPair(suite.primitive.adt.pair.LngFltPair) Friends.min(suite.util.Friends.min) Source(suite.util.FunUtil.Source) Set(java.util.Set) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) Streamlet(suite.streamlet.Streamlet) Eod(suite.trade.data.DataSource.Eod) MathUtil(suite.math.MathUtil) As(suite.streamlet.As) LngFltPair(suite.primitive.adt.pair.LngFltPair) LngIntPair(suite.primitive.adt.pair.LngIntPair) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with Pair

use of suite.adt.pair.Pair 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 19 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class BackAllocatorTest method testStop.

@Test
public void testStop() {
    Time start = Time.of(2017, 1, 1);
    String symbol = "S";
    float[] prices = { 1f, .99f, .98f, .5f, .5f, .5f, 0f, 0f, 0f };
    BackAllocator ba0 = (akds, ts) -> index -> List.of(Pair.of(symbol, 1d));
    BackAllocator ba1 = ba0.stopLoss(.98d);
    int length = prices.length;
    long[] ts = Longs_.toArray(length, i -> start.addDays(i).epochSec());
    DataSource ds = DataSource.of(ts, prices);
    AlignKeyDataSource<String> akds = DataSource.alignAll(Read.from2(List.of(Pair.of(symbol, ds))));
    int[] indices = Ints_.toArray(length, i -> i);
    OnDateTime odt = ba1.allocate(akds, indices);
    List<Double> potentials = // 
    Ints_.range(// 
    indices.length).map(// 
    index -> 0 < index ? Read.from(odt.onDateTime(index)) : Read.<Pair<String, Double>>empty()).map(// 
    pairs -> pairs.toDouble(Obj_Dbl.sum(pair -> pair.t1))).toList();
    assertEquals(List.of(0d, 1d, 1d, 1d, 0d, 0d, 0d, 0d, 0d), potentials);
}
Also used : OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Pair(suite.adt.pair.Pair) List(java.util.List) Read(suite.streamlet.Read) Time(suite.trade.Time) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) Test(org.junit.Test) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Ints_(suite.primitive.Ints_) Assert.assertEquals(org.junit.Assert.assertEquals) Longs_(suite.primitive.Longs_) OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Time(suite.trade.Time) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Pair(suite.adt.pair.Pair) Test(org.junit.Test)

Example 20 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class PackageManager method install.

public boolean install(String packageFilename) throws IOException {
    PackageManifest packageManifest = getPackageManifest(packageFilename);
    List<Pair<String, String>> filenameMappings = // 
    Read.from2(// 
    packageManifest.getFilenameMappings()).sort(// 
    (p0, p1) -> p1.t0.length() - p0.t0.length()).toList();
    List<InstallAction> installActions = new ArrayList<>();
    try (ZipFile zipFile = new ZipFile(packageFilename)) {
        installActions.addAll(// 
        Read.from(// 
        FileUtil.listZip(zipFile)).map(filename0 -> {
            String filename1 = filename0;
            for (Pair<String, String> filenameMapping : filenameMappings) {
                String[] match;
                if ((match = Wildcard.match(filenameMapping.t0, filename1)) != null) {
                    filename1 = Wildcard.apply(filenameMapping.t1, match);
                    break;
                }
            }
            return new ExtractFileAction(packageFilename, filename0, filename1);
        }).toList());
    }
    installActions.addAll(// 
    Read.from(// 
    packageManifest.getCommands()).map(// 
    command -> new ExecCommandAction(command.getInstallCommand(), command.getUninstallCommand())).toList());
    int progress = 0;
    boolean isSuccess = true;
    for (; progress < installActions.size(); progress++) try {
        installActions.get(progress).act();
    } catch (Exception ex) {
        log.error("Error during installation", ex);
        isSuccess = false;
        break;
    }
    if (isSuccess)
        keeper.savePackageMemento(new PackageMemento(packageManifest, installActions));
    else
        progress = unact(installActions, progress);
    return isSuccess;
}
Also used : ExecCommandAction(suite.pkgmanager.action.ExecCommandAction) Read(suite.streamlet.Read) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) InstallAction(suite.pkgmanager.action.InstallAction) ArrayList(java.util.ArrayList) Wildcard(suite.parser.Wildcard) Pair(suite.adt.pair.Pair) List(java.util.List) ExtractFileAction(suite.pkgmanager.action.ExtractFileAction) FileUtil(suite.os.FileUtil) ZipFile(java.util.zip.ZipFile) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) InputStream(java.io.InputStream) InstallAction(suite.pkgmanager.action.InstallAction) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExtractFileAction(suite.pkgmanager.action.ExtractFileAction) ZipFile(java.util.zip.ZipFile) ExecCommandAction(suite.pkgmanager.action.ExecCommandAction) Pair(suite.adt.pair.Pair)

Aggregations

Pair (suite.adt.pair.Pair)36 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Read (suite.streamlet.Read)22 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Node (suite.node.Node)14 Fail (suite.util.Fail)13 Fun (suite.util.FunUtil.Fun)12 Reference (suite.node.Reference)11 Tree (suite.node.Tree)10 TermOp (suite.node.io.TermOp)10 Set (java.util.Set)9 Atom (suite.node.Atom)9 Int (suite.node.Int)9 As (suite.streamlet.As)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 Streamlet (suite.streamlet.Streamlet)7 Time (suite.trade.Time)6