Search in sources :

Example 6 with String_

use of suite.util.String_ in project suite by stupidsing.

the class Trade_ method collectBrokeredTrades.

public static Streamlet<Trade> collectBrokeredTrades(Outlet<Trade> outlet) {
    Trade[] trades0 = outlet.toArray(Trade.class);
    List<Trade> trades1 = new ArrayList<>();
    int length0 = trades0.length;
    int i0 = 0;
    IntIntSink tx = (i0_, i1_) -> {
        if (Ints_.range(i0_, i1_).mapInt(i -> trades0[i].buySell).sum() != 0)
            while (i0_ < i1_) {
                Trade trade0 = trades0[i0_++];
                if (!String_.equals(trade0.remark, "#"))
                    trades1.add(trade0);
            }
    };
    for (int i = 1; i < length0; i++) {
        Trade trade0 = trades0[i0];
        Trade trade1 = trades0[i];
        boolean isGroup = // 
        true && // 
        String_.equals(trade0.date, trade1.date) && // 
        String_.equals(trade0.symbol, trade1.symbol) && trade0.price == trade1.price;
        if (!isGroup) {
            tx.sink2(i0, i);
            i0 = i;
        }
    }
    tx.sink2(i0, length0);
    return Read.from(trades1);
}
Also used : 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) IntIntSink(suite.primitive.IntIntSink) ArrayList(java.util.ArrayList)

Example 7 with String_

use of suite.util.String_ in project suite by stupidsing.

the class Fun23TreeTest method test.

@Test
public void test() throws IOException {
    int n = 100;
    String list100 = "0 until " + n + " | map {insert} | apply | {Tree (9999, Empty;)}";
    Node fp0 = Suite.substitute("use 23-TREE >> " + list100);
    Node result0 = Suite.evaluateFun(Suite.fcc(fp0, false));
    String out0 = Formatter.dump(result0);
    System.out.println("OUT:\n" + out0);
    int nPars0 = Read.from(String_.chars(out0)).filter(c -> c == '(').size();
    assertTrue(3 <= nPars0);
    Node fp1 = Suite.substitute("use 23-TREE >> 0 until " + n / 2 + " | map {remove} | apply | {" + list100 + "}");
    Node result1 = Suite.evaluateFun(Suite.fcc(fp1, false));
    String out1 = Formatter.dump(result1);
    System.out.println("OUT:\n" + out1);
    int nPars1 = Read.from(String_.chars(out1)).filter(c -> c == '(').size();
    assertTrue(3 <= nPars1);
}
Also used : Suite(suite.Suite) Formatter(suite.node.io.Formatter) Read(suite.streamlet.Read) String_(suite.util.String_) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Node(suite.node.Node) Node(suite.node.Node) Test(org.junit.Test)

Example 8 with String_

use of suite.util.String_ 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 9 with String_

use of suite.util.String_ in project suite by stupidsing.

the class Hkex method queryBoardLot_.

private int queryBoardLot_(String symbol) {
    if (String_.equals(symbol, "0700.HK"))
        return 100;
    else {
        JsonNode json = query(// 
        "" + // 
        "https://www.hkex.com.hk/eng/csm/ws/Company.asmx/GetData" + // 
        "?location=companySearch" + // 
        "&SearchMethod=1" + // 
        "&LangCode=en" + "&StockCode=" + // 
        HkexUtil.toStockCode(symbol) + // 
        "&StockName=" + // 
        "&mkt=hk" + // 
        "&x=" + "&y=");
        CompanyInfo companyInfo = mapper.convertValue(json, CompanyInfo.class);
        String boardLotStr = // 
        Read.each(// 
        companyInfo).flatMap(// 
        ci -> ci.data).concatMap(// 
        Data::tableEntries).filter(// 
        td -> String_.equals(td.get(0), "Board lot")).uniqueResult().get(// 
        1).replace(",", "");
        return Integer.parseInt(boardLotStr);
    }
}
Also used : HttpUtil(suite.http.HttpUtil) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) SerializedStoreCache(suite.os.SerializedStoreCache) Source(suite.util.FunUtil.Source) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) To(suite.util.To) Serialize(suite.util.Serialize) HashSet(java.util.HashSet) Execute(suite.os.Execute) List(java.util.List) Streamlet(suite.streamlet.Streamlet) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Map(java.util.Map) As(suite.streamlet.As) JsonNode(com.fasterxml.jackson.databind.JsonNode) Asset(suite.trade.Asset) Fail(suite.util.Fail) InputStream(java.io.InputStream) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 10 with String_

use of suite.util.String_ in project suite by stupidsing.

the class Summarize method summarize.

public <K> SummarizeByStrategy<K> summarize(Fun<Trade, K> fun) {
    Streamlet2<K, Summarize_> summaryByKey = // 
    trades.groupBy(fun, // 
    trades_ -> summarize_(trades_, priceBySymbol, s -> null)).filterKey(// 
    key -> key != null).collect(As::streamlet2);
    Map<String, Map<K, Integer>> nSharesByKeyBySymbol = // 
    summaryByKey.concatMap((key, summary) -> // 
    summary.account.portfolio().map(// 
    (symbol, n) -> Fixie.of(symbol, key, n))).groupBy(Fixie3::get0, fixies0 -> // 
    fixies0.groupBy(Fixie3::get1, fixies1 -> // 
    fixies1.map(Fixie3::get2).uniqueResult()).toMap()).toMap();
    Map<String, Float> acquiredPrices = trades.collect(Trade_::collectBrokeredTrades).collect(Trade_::collectAcquiredPrices);
    Time now = Time.now();
    Summarize_ overall = summarize_(trades, priceBySymbol, symbol -> {
        boolean isMarketOpen = // 
        false || // 
        HkexUtil.isMarketOpen(now) || HkexUtil.isMarketOpen(now.addHours(1));
        DataSource ds = cfg.dataSource(symbol);
        // acquisition price
        float price0 = acquiredPrices.get(symbol);
        // previous close
        float price1 = ds.get(isMarketOpen ? -1 : -2).t1;
        // now
        float pricex = isMarketOpen ? priceBySymbol.get(symbol) : ds.get(-1).t1;
        String keys = // 
        Read.from2(// 
        nSharesByKeyBySymbol.getOrDefault(symbol, Map.ofEntries())).keys().map(// 
        Object::toString).sort(// 
        String_::compare).collect(As.joinedBy("/"));
        return // 
        percent(price1, pricex) + ", " + // 
        percent(price0, pricex) + (!keys.isEmpty() ? ", " + keys : "");
    });
    Map<K, String> outByKey = summaryByKey.mapValue(Summarize_::out0).toMap();
    StringBuilder sb = new StringBuilder();
    Sink<String> log = sb::append;
    for (Entry<K, String> e : outByKey.entrySet()) log.sink("\nFor strategy " + e.getKey() + ":" + e.getValue());
    log.sink(FormatUtil.tablize("\nOverall:\t" + Time.now().ymdHms() + overall.out1()));
    // profit and loss
    Map<K, Double> pnlByKey = // 
    sellAll(trades, priceBySymbol).groupBy(fun, // 
    t -> (double) Account.ofHistory(t).cash()).toMap();
    return new SummarizeByStrategy<>(sb.toString(), overall.account, pnlByKey);
}
Also used : Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) Fun(suite.util.FunUtil.Fun) Yahoo(suite.trade.data.Yahoo) String_(suite.util.String_) Dbl_Dbl(suite.primitive.Dbl_Dbl) Map(java.util.Map) Fixie3(suite.adt.pair.Fixie_.Fixie3) LngFltPair(suite.primitive.adt.pair.LngFltPair) TransactionSummary(suite.trade.Account.TransactionSummary) Streamlet2(suite.streamlet.Streamlet2) HkexUtil(suite.trade.data.HkexUtil) Object_(suite.util.Object_) Hsbc(suite.trade.data.Broker.Hsbc) To(suite.util.To) Quant(ts.Quant) Iterate(suite.util.FunUtil.Iterate) Trade(suite.trade.Trade) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Fixie(suite.adt.pair.Fixie) Configuration(suite.trade.data.Configuration) Account(suite.trade.Account) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) Asset(suite.trade.Asset) FormatUtil(suite.util.FormatUtil) Time(suite.trade.Time) DataSource(suite.trade.data.DataSource) Trade_(suite.trade.Trade_) As(suite.streamlet.As) Map(java.util.Map)

Aggregations

String_ (suite.util.String_)14 Map (java.util.Map)12 Read (suite.streamlet.Read)12 List (java.util.List)11 As (suite.streamlet.As)10 Pair (suite.adt.pair.Pair)9 Streamlet (suite.streamlet.Streamlet)9 ArrayList (java.util.ArrayList)8 Set (java.util.Set)8 To (suite.util.To)7 HashMap (java.util.HashMap)6 LogUtil (suite.os.LogUtil)6 Streamlet2 (suite.streamlet.Streamlet2)6 Fail (suite.util.Fail)6 Sink (suite.util.FunUtil.Sink)6 Suite (suite.Suite)5 Node (suite.node.Node)5 Time (suite.trade.Time)5 Rethrow (suite.util.Rethrow)5 MathUtil (suite.math.MathUtil)4