use of suite.primitive.Ints_ 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.primitive.Ints_ 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.primitive.Ints_ in project suite by stupidsing.
the class Arima method armaBackcast.
// http://math.unice.fr/~frapetti/CorsoP/Chapitre_4_IMEA_1.pdf
// "Least squares estimation using backcasting procedure"
public Arima_ armaBackcast(float[] xs, float[] ars, float[] mas) {
int length = xs.length;
int p = ars.length;
int q = mas.length;
float[] xsp = Floats_.concat(new float[p], xs);
float[] epq = new float[length + q];
Arma arma = new Arma(ars, mas);
for (int iter = 0; iter < 64; iter++) {
// backcast
// ep[t]
// = (xs[t + q] - ep[t + q]
// - ars[0] * xs[t + q - 1] - ... - ars[p - 1] * xs[t + q - p]
// - mas[0] * ep[t + q - 1] - ... - mas[q - 2] * ep[t + 1]
// ) / mas[q - 1]
arma.backcast(xsp, epq);
// forward recursion
// ep[t] = xs[t]
// - ars[0] * xs[t - 1] - ... - ars[p - 1] * xs[t - p]
// - mas[0] * ep[t - 1] - ... - mas[q - 1] * ep[t - q]
double error = arma.forwardRecursion(xsp, epq);
// minimization
// xs[t]
// = ars[0] * xs[t - 1] + ... + ars[p - 1] * xs[t - p]
// + mas[0] * ep[t - 1] + ... + mas[q - 1] * ep[t - q]
// + ep[t]
LinearRegression lr = stat.linearRegression(//
Ints_.range(//
length).map(t -> {
int tp = t + p, tpm1 = tp - 1;
int tq = t + q, tqm1 = tq - 1;
FltStreamlet lrxs0 = Ints_.range(p).collect(Int_Flt.lift(i -> xsp[tpm1 - i]));
FltStreamlet lrxs1 = Ints_.range(q).collect(Int_Flt.lift(i -> epq[tqm1 - i]));
return FltObjPair.of(xsp[tp], Floats_.concat(lrxs0, lrxs1).toArray());
}));
System.out.println("iter " + iter + ", error = " + To.string(error) + lr);
System.out.println();
float[] coefficients = lr.coefficients();
Floats_.copy(coefficients, 0, ars, 0, p);
Floats_.copy(coefficients, p, mas, 0, q);
}
double x1 = arma.sum(xsp, epq);
return new Arima_(ars, mas, (float) x1);
}
use of suite.primitive.Ints_ in project suite by stupidsing.
the class TimeSeries method hurst.
// epchan
public double hurst(float[] ys, int tor) {
float[] logys = To.vector(ys, Math::log);
int[] tors = Ints_.toArray(tor, t -> t + 1);
float[] logVrs = To.vector(tor, t -> {
float[] diffs = dropDiff_(tors[t], logys);
float[] diffs2 = To.vector(diffs, diff -> diff * diff);
return Math.log(stat.variance(diffs2));
});
LinearRegression lr = stat.linearRegression(//
Ints_.range(//
logVrs.length).map(i -> FltObjPair.of((float) Math.log(tors[i]), new float[] { logVrs[i], 1f })));
float beta0 = lr.coefficients[0];
return beta0 / 2d;
}
use of suite.primitive.Ints_ 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;
}
Aggregations