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();
}
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;
}
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));
}
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);
}
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;
}
Aggregations