Search in sources :

Example 1 with UpdatePortfolio

use of suite.trade.Trade_.UpdatePortfolio in project suite by stupidsing.

the class WalkForwardAllocTester method tick.

public String tick(Time time, Map<String, Float> priceBySymbol) {
    int last = windowSize - 1;
    System.arraycopy(times, 0, times, 1, last);
    times[last] = time.epochSec();
    for (Entry<String, DataSource> e : dsBySymbol.entrySet()) {
        String symbol = e.getKey();
        float[] prices = e.getValue().prices;
        System.arraycopy(prices, 0, prices, 1, last);
        prices[last] = priceBySymbol.get(symbol);
    }
    AlignKeyDataSource<String> akds = new AlignKeyDataSource<>(times, Read.from2(dsBySymbol));
    List<Pair<String, Double>> ratioBySymbol = wfa.allocate(akds, windowSize);
    UpdatePortfolio up = Trade_.updatePortfolio(time.ymdHms(), account, ratioBySymbol, assetBySymbol, Read.from2(priceBySymbol).mapValue(Eod::of).toMap());
    float valuation_;
    valuations.append(valuation_ = up.valuation0);
    for (Pair<String, Float> e : up.val0.streamlet()) holdBySymbol.compute(e.t0, (s, h) -> e.t1 / valuation_ + (h != null ? h : 0d));
    List<Trade> trades_ = up.trades;
    String actions;
    if (windowSize <= valuations.size())
        actions = play(trades_);
    else
        actions = "wait";
    return // 
    time.ymdHms() + ", valuation = " + // 
    valuation_ + ", portfolio = " + // 
    account + ", actions = " + actions;
}
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) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) Trade(suite.trade.Trade) UpdatePortfolio(suite.trade.Trade_.UpdatePortfolio) Eod(suite.trade.data.DataSource.Eod) Pair(suite.adt.pair.Pair)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Pair (suite.adt.pair.Pair)1 Statistic (suite.math.numeric.Statistic)1 FloatsBuilder (suite.primitive.Floats.FloatsBuilder)1 Read (suite.streamlet.Read)1 Streamlet (suite.streamlet.Streamlet)1 Account (suite.trade.Account)1 Asset (suite.trade.Asset)1 Time (suite.trade.Time)1 Trade (suite.trade.Trade)1 Trade_ (suite.trade.Trade_)1 UpdatePortfolio (suite.trade.Trade_.UpdatePortfolio)1 Configuration (suite.trade.data.Configuration)1 DataSource (suite.trade.data.DataSource)1 AlignKeyDataSource (suite.trade.data.DataSource.AlignKeyDataSource)1 Eod (suite.trade.data.DataSource.Eod)1