Search in sources :

Example 1 with BackAllocator

use of suite.trade.backalloc.BackAllocator in project suite by stupidsing.

the class DailyMain method alloc.

private Result alloc(String tag, float fund, BackAllocator backAllocator, Streamlet<Asset> assets) {
    TimeRange period = TimeRange.daysBefore(64);
    Simulate sim = BackAllocTester.of(cfg, period, assets, backAllocator, log).simulate(fund);
    Account account0 = Account.ofPortfolio(cfg.queryHistory().filter(r -> String_.equals(r.strategy, tag)));
    Account account1 = sim.account;
    Map<String, Integer> assets0 = account0.assets();
    Map<String, Integer> assets1 = account1.assets();
    Set<String> symbols = Set_.union(assets0.keySet(), assets1.keySet());
    Map<String, Float> priceBySymbol = cfg.quote(symbols);
    List<Trade> trades = Trade_.diff(Trade.NA, assets0, assets1, priceBySymbol::get).toList();
    sb.append("\nstrategy = " + tag + ", " + sim.conclusion());
    return new Result(tag, trades);
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) LogUtil(suite.os.LogUtil) SerializedStoreCache(suite.os.SerializedStoreCache) Trade_(suite.trade.Trade_) RunUtil(suite.util.RunUtil) ArrayList(java.util.ArrayList) Bacs(suite.trade.backalloc.BackAllocConfigurations.Bacs) ConfigurationImpl(suite.trade.data.ConfigurationImpl) String_(suite.util.String_) BackAllocatorOld(suite.trade.backalloc.strategy.BackAllocatorOld) Map(java.util.Map) BackAllocTester(suite.trade.backalloc.BackAllocTester) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) Set_(suite.util.Set_) BuySellStrategy(suite.trade.singlealloc.BuySellStrategy) Streamlet2(suite.streamlet.Streamlet2) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) Summarize(suite.trade.analysis.Summarize) Set(java.util.Set) To(suite.util.To) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Serialize(suite.util.Serialize) Strategos(suite.trade.singlealloc.Strategos) Pair(suite.adt.pair.Pair) List(java.util.List) Trade(suite.trade.Trade) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Configuration(suite.trade.data.Configuration) MathUtil(suite.math.MathUtil) Account(suite.trade.Account) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) SingleAllocBackTest(suite.trade.singlealloc.SingleAllocBackTest) DblStreamlet(suite.primitive.streamlet.DblStreamlet) Asset(suite.trade.Asset) SmtpSslGmail(suite.smtp.SmtpSslGmail) TimeRange(suite.trade.TimeRange) SummarizeByStrategy(suite.trade.analysis.Summarize.SummarizeByStrategy) Account(suite.trade.Account) TimeRange(suite.trade.TimeRange) Trade(suite.trade.Trade) Simulate(suite.trade.backalloc.BackAllocTester.Simulate)

Example 2 with BackAllocator

use of suite.trade.backalloc.BackAllocator in project suite by stupidsing.

the class BackAllocatorOld method questoQuella.

public BackAllocator questoQuella(String symbol0, String symbol1) {
    int tor = 64;
    double threshold = 0d;
    BackAllocator ba0 = (akds, indices) -> {
        Streamlet2<String, DataSource> dsBySymbol = akds.dsByKey;
        Map<String, DataSource> dsBySymbol_ = dsBySymbol.toMap();
        DataSource ds0 = dsBySymbol_.get(symbol0);
        DataSource ds1 = dsBySymbol_.get(symbol1);
        return index -> {
            int ix = index - 1;
            int i0 = ix - tor;
            double p0 = ds0.get(i0).t1, px = ds0.get(ix).t1;
            double q0 = ds1.get(i0).t1, qx = ds1.get(ix).t1;
            double pdiff = Quant.return_(p0, px);
            double qdiff = Quant.return_(q0, qx);
            if (threshold < Math.abs(pdiff - qdiff))
                return // 
                List.of(// 
                Pair.of(pdiff < qdiff ? symbol0 : symbol1, 1d), Pair.of(pdiff < qdiff ? symbol1 : symbol0, -1d));
            else
                return List.of();
        };
    };
    return ba0.filterByAsset(symbol -> String_.equals(symbol, symbol0) || String_.equals(symbol, symbol1));
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Arrays(java.util.Arrays) Streamlet2(suite.streamlet.Streamlet2) Statistic(suite.math.numeric.Statistic) MovingAverage(suite.trade.analysis.MovingAverage) Quant(ts.Quant) BollingerBands(ts.BollingerBands) MovingRange(suite.trade.analysis.MovingAverage.MovingRange) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) MeanVariance(suite.math.numeric.Statistic.MeanVariance) String_(suite.util.String_) Configuration(suite.trade.data.Configuration) Map(java.util.Map) DataSource(suite.trade.data.DataSource) TimeSeries(ts.TimeSeries) BackAllocator(suite.trade.backalloc.BackAllocator) Streamlet2(suite.streamlet.Streamlet2) Map(java.util.Map) DataSource(suite.trade.data.DataSource)

Example 3 with BackAllocator

use of suite.trade.backalloc.BackAllocator 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)

Example 4 with BackAllocator

use of suite.trade.backalloc.BackAllocator in project suite by stupidsing.

the class DailyMain method pairs.

public BackAllocConfiguration pairs(String symbol0, String symbol1) {
    Streamlet<Asset> assets = Read.each(symbol0, symbol1).map(cfg::queryCompany).collect(As::streamlet);
    BackAllocator backAllocator = BackAllocatorOld.me.pairs(cfg, symbol0, symbol1).unleverage();
    return new BackAllocConfiguration(time -> assets, backAllocator);
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) As(suite.streamlet.As) Asset(suite.trade.Asset) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration)

Aggregations

BackAllocator (suite.trade.backalloc.BackAllocator)4 Map (java.util.Map)3 Asset (suite.trade.Asset)3 DataSource (suite.trade.data.DataSource)3 List (java.util.List)2 Pair (suite.adt.pair.Pair)2 As (suite.streamlet.As)2 Read (suite.streamlet.Read)2 Streamlet2 (suite.streamlet.Streamlet2)2 TimeRange (suite.trade.TimeRange)2 Trade_ (suite.trade.Trade_)2 MovingAverage (suite.trade.analysis.MovingAverage)2 BackAllocConfiguration (suite.trade.backalloc.BackAllocConfiguration)2 Configuration (suite.trade.data.Configuration)2 TimeSeries (ts.TimeSeries)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 MathUtil (suite.math.MathUtil)1 Statistic (suite.math.numeric.Statistic)1