Search in sources :

Example 1 with IntFltPair

use of suite.primitive.adt.pair.IntFltPair 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 2 with IntFltPair

use of suite.primitive.adt.pair.IntFltPair in project suite by stupidsing.

the class IntFltMap method forEach.

public void forEach(IntFltSink sink) {
    IntFltPair pair = IntFltPair.of((int) 0, (float) 0);
    IntFltSource source = source_();
    while (source.source2(pair)) sink.sink2(pair.t0, pair.t1);
}
Also used : IntFltPair(suite.primitive.adt.pair.IntFltPair) IntFltSource(suite.primitive.IntFltSource)

Example 3 with IntFltPair

use of suite.primitive.adt.pair.IntFltPair in project suite by stupidsing.

the class Trade_ method collectAcquiredPrices.

public static Map<String, Float> collectAcquiredPrices(Outlet<Trade> outlet) {
    Map<String, List<IntFltPair>> acquireBySymbol = new HashMap<>();
    for (Trade trade : outlet) {
        String symbol = trade.symbol;
        int buySell = trade.buySell;
        float price = trade.price;
        List<IntFltPair> acquires0 = acquireBySymbol.getOrDefault(symbol, List.of());
        List<IntFltPair> acquires1 = new ArrayList<>();
        for (IntFltPair acquire : acquires0) {
            int n0 = acquire.t0;
            int diff = buySell < 0 ? max(0, min(-buySell, n0)) : min(0, max(-buySell, n0));
            int n1 = n0 - diff;
            buySell += diff;
            if (n1 != 0)
                acquires1.add(IntFltPair.of(n1, acquire.t1));
        }
        if (buySell != 0)
            acquires1.add(IntFltPair.of(buySell, price));
        acquireBySymbol.put(symbol, acquires1);
    }
    return // 
    Read.from2(// 
    acquireBySymbol).mapValue(acquires -> {
        IntFltPair sum = IntFltPair.of(0, 0f);
        for (IntFltPair acquire : acquires) sum.update(sum.t0 + acquire.t0, sum.t1 + acquire.t0 * acquire.t1);
        return sum.t1 / sum.t0;
    }).toMap();
}
Also used : IntFltPair(suite.primitive.adt.pair.IntFltPair) 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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with IntFltPair

use of suite.primitive.adt.pair.IntFltPair in project suite by stupidsing.

the class StatisticalArbitrageTest method testPeriod.

// find the period of various stocks using FFT
@Test
public void testPeriod() {
    int minPeriod = 4;
    DctDataSource dctDataSources = dctDataSources();
    for (Pair<String, float[]> e : dctDataSources.dctByKey) {
        float[] dct = e.t1;
        IntFltPair max = IntFltPair.of(Integer.MIN_VALUE, Float.MIN_VALUE);
        for (int i = minPeriod; i < dct.length; i++) {
            float f = Math.abs(dct[i]);
            if (max.t1 < f)
                max = IntFltPair.of(i, f);
        }
        LogUtil.info(e.t0 + " has period " + max.t0);
    }
}
Also used : IntFltPair(suite.primitive.adt.pair.IntFltPair) Test(org.junit.Test)

Aggregations

IntFltPair (suite.primitive.adt.pair.IntFltPair)4 Test (org.junit.Test)2 Pair (suite.adt.pair.Pair)2 Ints_ (suite.primitive.Ints_)2 As (suite.streamlet.As)2 Friends.max (suite.util.Friends.max)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 IntFunction (java.util.function.IntFunction)1 MathUtil (suite.math.MathUtil)1 Tanh (suite.math.Tanh)1 VirtualVector (suite.math.linalg.VirtualVector)1 Statistic (suite.math.numeric.Statistic)1 MeanVariance (suite.math.numeric.Statistic.MeanVariance)1 DiscreteCosineTransform (suite.math.transform.DiscreteCosineTransform)1 LogUtil (suite.os.LogUtil)1