Search in sources :

Example 1 with ReturnsStat

use of ts.TimeSeries.ReturnsStat in project suite by stupidsing.

the class WalkForwardAllocTester method conclusion.

public String conclusion() {
    float[] valuations_ = valuations.toFloats().toArray();
    int length = valuations_.length;
    double deltaMs = (start - System.currentTimeMillis()) / length;
    ReturnsStat rs = ts.returnsStat(valuations_, deltaMs);
    StringBuilder sb = new StringBuilder();
    for (Pair<String, Double> e : Read.from2(holdBySymbol).sortBy((symbol, value) -> -value).take(5)) sb.append(e.t0 + ":" + String.format("%.0f", e.t1 * 100d / length) + "%,");
    return // 
    "nTicks:" + length + " val:" + // 
    (0 < length ? valuations_[length - 1] : "N/A") + " tickRtn:" + // 
    To.string(rs.return_) + " sharpe:" + // 
    To.string(rs.sharpeRatio()) + " skew:" + // 
    To.string(stat.skewness(valuations_)) + " " + // 
    account.transactionSummary(cfg::transactionFee).out0() + " holds::" + sb + "...";
}
Also used : Read(suite.streamlet.Read) Statistic(suite.math.numeric.Statistic) FloatsBuilder(suite.primitive.Floats.FloatsBuilder) Trade_(suite.trade.Trade_) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) HashMap(java.util.HashMap) To(suite.util.To) UpdatePortfolio(suite.trade.Trade_.UpdatePortfolio) ArrayList(java.util.ArrayList) Pair(suite.adt.pair.Pair) List(java.util.List) Trade(suite.trade.Trade) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Eod(suite.trade.data.DataSource.Eod) ReturnsStat(ts.TimeSeries.ReturnsStat) Map(java.util.Map) Account(suite.trade.Account) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) TimeSeries(ts.TimeSeries) Asset(suite.trade.Asset) ReturnsStat(ts.TimeSeries.ReturnsStat)

Example 2 with ReturnsStat

use of ts.TimeSeries.ReturnsStat in project suite by stupidsing.

the class MovingAvgMeanReversionBackAllocator method backAllocator.

public BackAllocator backAllocator() {
    return (akds, indices) -> {
        Map<String, DataSource> dsBySymbol = akds.dsByKey.toMap();
        double dailyRiskFreeInterestRate = Trade_.riskFreeInterestRate(1);
        DataSourceView<String, MeanReversionStat> dsv = // 
        DataSourceView.of(tor, 256, akds, (symbol, ds, period) -> new MeanReversionStat(ds, period));
        return index -> {
            Map<String, MeanReversionStat> mrsBySymbol = // 
            akds.dsByKey.map2(// 
            (symbol, ds) -> dsv.get(symbol, index)).filterValue(// 
            mrsReversionStat -> mrsReversionStat != null).toMap();
            // ensure 0 < half-life: determine investment period
            return // 
            Read.from2(mrsBySymbol).filterValue(mrs -> // 
            mrs.adf < 0d && // 
            mrs.hurst < .5d && // 
            mrs.movingAvgMeanReversionRatio() < 0d).map2((symbol, mrs) -> {
                DataSource ds = dsBySymbol.get(symbol);
                double price = ds.prices[index - 1];
                double lma = mrs.latestMovingAverage();
                double diff = mrs.movingAvgMeanReversion.predict(new float[] { (float) lma, 1f });
                double dailyReturn = diff / price - dailyRiskFreeInterestRate;
                ReturnsStat returnsStat = ts.returnsStatDaily(ds.prices);
                double sharpe = returnsStat.sharpeRatio();
                double kelly = dailyReturn * price * price / mrs.movingAvgMeanReversion.sse;
                return new PotentialStat(dailyReturn, sharpe, kelly);
            }).filterValue(// 
            ps -> 0d < ps.kelly).cons(Asset.cashSymbol, // 
            new PotentialStat(Trade_.riskFreeInterestRate, 1d, 0d)).mapValue(// 
            ps -> ps.kelly).sortBy(// 
            (symbol, potential) -> -potential).take(// 
            top).toList();
        };
    };
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) MovingAverage(suite.trade.analysis.MovingAverage) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) ReturnsStat(ts.TimeSeries.ReturnsStat) Map(java.util.Map) DataSource(suite.trade.data.DataSource) TimeSeries(ts.TimeSeries) Asset(suite.trade.Asset) TimeRange(suite.trade.TimeRange) DataSourceView(suite.trade.data.DataSourceView) DataSourceView(suite.trade.data.DataSourceView) ReturnsStat(ts.TimeSeries.ReturnsStat) Map(java.util.Map) DataSource(suite.trade.data.DataSource)

Aggregations

Map (java.util.Map)2 Read (suite.streamlet.Read)2 Asset (suite.trade.Asset)2 Trade_ (suite.trade.Trade_)2 DataSource (suite.trade.data.DataSource)2 To (suite.util.To)2 TimeSeries (ts.TimeSeries)2 ReturnsStat (ts.TimeSeries.ReturnsStat)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Pair (suite.adt.pair.Pair)1 Statistic (suite.math.numeric.Statistic)1 LinearRegression (suite.math.numeric.Statistic.LinearRegression)1 FloatsBuilder (suite.primitive.Floats.FloatsBuilder)1 Streamlet (suite.streamlet.Streamlet)1 Account (suite.trade.Account)1 Time (suite.trade.Time)1 TimeRange (suite.trade.TimeRange)1