Search in sources :

Example 11 with Ints_

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);
}
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 12 with Ints_

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();
    }
}
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 13 with Ints_

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);
}
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) FltStreamlet(suite.primitive.streamlet.FltStreamlet) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Example 14 with Ints_

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;
}
Also used : Arrays(java.util.Arrays) Read(suite.streamlet.Read) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Trade_(suite.trade.Trade_) CholeskyDecomposition(suite.math.linalg.CholeskyDecomposition) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) ArrayList(java.util.ArrayList) Friends.max(suite.util.Friends.max) List(java.util.List) MeanVariance(suite.math.numeric.Statistic.MeanVariance) Vector(suite.math.linalg.Vector) Floats(suite.primitive.Floats) FltObjPair(suite.primitive.adt.pair.FltObjPair) Ints_(suite.primitive.Ints_) Int_Dbl(suite.primitive.Int_Dbl) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Example 15 with Ints_

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

Aggregations

Ints_ (suite.primitive.Ints_)24 To (suite.util.To)14 Arrays (java.util.Arrays)12 Statistic (suite.math.numeric.Statistic)12 Friends.max (suite.util.Friends.max)12 List (java.util.List)10 LinearRegression (suite.math.numeric.Statistic.LinearRegression)10 FltObjPair (suite.primitive.adt.pair.FltObjPair)10 Pair (suite.adt.pair.Pair)9 Floats_ (suite.primitive.Floats_)9 Read (suite.streamlet.Read)9 Friends.min (suite.util.Friends.min)9 Random (java.util.Random)8 Int_Flt (suite.primitive.Int_Flt)8 As (suite.streamlet.As)8 Test (org.junit.Test)7 Time (suite.trade.Time)6 Trade_ (suite.trade.Trade_)6 Configuration (suite.trade.data.Configuration)6 ConfigurationImpl (suite.trade.data.ConfigurationImpl)6