use of suite.adt.pair.Pair in project suite by stupidsing.
the class ChrDblFunUtil method map2.
public static <K1, V1, T> Source2<K1, V1> map2(ChrDbl_Obj<K1> kf0, ChrDbl_Obj<V1> vf0, ChrDblSource source2) {
ChrDbl_Obj<K1> kf1 = kf0.rethrow();
ChrDbl_Obj<V1> vf1 = vf0.rethrow();
ChrDblPair pair1 = ChrDblPair.of((char) 0, (double) 0);
return pair -> {
boolean b = source2.source2(pair1);
if (b)
pair.update(kf1.apply(pair1.t0, pair1.t1), vf1.apply(pair1.t0, pair1.t1));
return b;
};
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class ChrDblFunUtil method fold.
public static <K, V, R> R fold(Fun<Pair<R, ChrDblPair>, R> fun0, R init, ChrDblSource source2) {
Fun<Pair<R, ChrDblPair>, R> fun1 = fun0.rethrow();
ChrDblPair pair = ChrDblPair.of((char) 0, (double) 0);
while (source2.source2(pair)) init = fun1.apply(Pair.of(init, pair));
return init;
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class BackTestMain method run.
@Override
protected boolean run(String[] args) {
// BEGIN
// END
String arg0 = 0 < args.length ? args[0] : "";
String arg1 = 1 < args.length ? args[1] : "";
String arg2 = 2 < args.length ? args[2] : "";
Streamlet<String> strategyMatches = !arg0.isEmpty() ? Read.from(arg0.split(",")) : null;
Streamlet<Integer> years = !arg1.isEmpty() ? //
Read.from(//
arg1.split(",")).concatMap(s -> {
Pair<String, String> pair = ParseUtil.search(s, "-", Assoc.RIGHT);
return //
pair != null ? //
Ints_.range(Integer.valueOf(pair.t0), Integer.valueOf(pair.t1)).map(i -> i) : Read.each(Integer.valueOf(s));
}) : //
Ints_.range(2007, Trade_.thisYear).map(i -> i);
Fun<Time, Streamlet<Asset>> fun = //
!arg2.isEmpty() ? //
time -> Read.from(arg2.split(",")).map(cfg::queryCompany).collect(As::streamlet) : cfg::queryCompaniesByMarketCap;
BackAllocConfigurations bac_ = new BackAllocConfigurations(cfg, fun);
Streamlet2<String, BackAllocConfiguration> bacByTag = bac_.bacs().bacByName;
Streamlet2<String, Simulate> simulationByKey = //
bacByTag.filterKey(//
n -> strategyMatches == null || strategyMatches.isAny(sm -> Wildcard.match(sm, n) != null)).map(//
Pair::of).join2(//
years.sort(Object_::compare).map(TimeRange::ofYear)).map2((pair, period) -> pair.t0, (pair, period) -> {
BackAllocConfiguration bac = pair.t1;
Streamlet<Asset> assets = bac.assetsFun.apply(period.from);
return runner.backTest(bac.backAllocator, period, assets);
}).collect(As::streamlet2);
String content0 = //
Read.bytes(//
Paths.get("src/main/java/" + getClass().getName().replace('.', '/') + ".java")).collect(//
As::utf8decode).map(//
Chars::toString).collect(As::joined);
String content1 = ParseUtil.fit(content0, "// BEGIN", "// END")[1];
System.out.println(content1);
System.out.println(runner.conclude(simulationByKey));
return true;
}
use of suite.adt.pair.Pair 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;
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class Nodify method newNodifier.
@SuppressWarnings("unchecked")
private Nodifier newNodifier(Type type) {
Nodifier nodifier;
if (type instanceof Class) {
Class<?> clazz = (Class<?>) type;
if (clazz == boolean.class)
nodifier = new Nodifier(object -> Atom.of(object.toString()), node -> node == Atom.TRUE);
else if (clazz == int.class)
nodifier = new Nodifier(object -> Int.of((Integer) object), node -> ((Int) node).number);
else if (clazz == Chars.class)
nodifier = new Nodifier(object -> new Str(object.toString()), node -> To.chars(((Str) node).value));
else if (clazz == String.class)
nodifier = new Nodifier(object -> new Str(object.toString()), node -> ((Str) node).value);
else if (clazz.isEnum())
nodifier = new Nodifier(object -> Atom.of(object.toString()), Read.from(clazz.getEnumConstants()).toMap(e -> Atom.of(e.toString()))::get);
else if (clazz.isArray()) {
Class<?> componentType = clazz.getComponentType();
Nodifier nodifier1 = getNodifier(componentType);
Fun<Object, Node> forward = object -> {
Node node = Atom.NIL;
for (int i = Array.getLength(object) - 1; 0 <= i; i--) node = Tree.of(TermOp.OR____, apply_(nodifier1, Array.get(object, i)), node);
return node;
};
nodifier = new Nodifier(forward, node -> {
List<Object> list = //
Read.from(//
Tree.iter(node, TermOp.OR____)).map(//
n -> apply_(nodifier1, n)).toList();
int size = list.size();
Object objects = Array.newInstance(componentType, size);
for (int i = 0; i < size; i++) Array.set(objects, i, list.get(i));
return objects;
});
} else if (// polymorphism
clazz.isInterface())
nodifier = new Nodifier(object -> {
Class<?> clazz1 = object.getClass();
Node n = apply_(getNodifier(clazz1), object);
return Tree.of(TermOp.COLON_, Atom.of(clazz1.getName()), n);
}, node -> {
Tree tree = Tree.decompose(node, TermOp.COLON_);
if (tree != null) {
Class<?> clazz1;
try {
clazz1 = Class.forName(((Atom) tree.getLeft()).name);
} catch (ClassNotFoundException ex) {
clazz1 = Fail.t(ex);
}
return apply_(getNodifier(clazz1), tree.getRight());
} else
// happens when an enum implements an interface
return Fail.t("cannot instantiate enum from interfaces");
});
else {
List<FieldInfo> fieldInfos = //
Read.from(//
inspect.fields(clazz)).map(field -> {
Type type1 = field.getGenericType();
return new FieldInfo(field, field.getName(), getNodifier(type1));
}).toList();
List<Pair<Atom, FieldInfo>> pairs = Read.from(fieldInfos).map(f -> Pair.of(Atom.of(f.name), f)).toList();
nodifier = new Nodifier(object -> Rethrow.ex(() -> {
Dict dict = new Dict();
for (Pair<Atom, FieldInfo> pair : pairs) {
FieldInfo fieldInfo = pair.t1;
Node value = apply_(fieldInfo.nodifier, fieldInfo.field.get(object));
dict.map.put(pair.t0, Reference.of(value));
}
return dict;
}), node -> Rethrow.ex(() -> {
Map<Node, Reference> map = ((Dict) node).map;
Object object1 = Object_.new_(clazz);
for (Pair<Atom, FieldInfo> pair : pairs) {
FieldInfo fieldInfo = pair.t1;
Node value = map.get(pair.t0).finalNode();
fieldInfo.field.set(object1, apply_(fieldInfo.nodifier, value));
}
return object1;
}));
}
} else if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type rawType = pt.getRawType();
Type[] typeArgs = pt.getActualTypeArguments();
Class<?> clazz = rawType instanceof Class ? (Class<?>) rawType : null;
if (collectionClasses.contains(clazz)) {
Nodifier nodifier1 = getNodifier(typeArgs[0]);
nodifier = new Nodifier(object -> {
Tree start = Tree.of(null, null, null), tree = start;
for (Object o : (Collection<?>) object) {
Tree tree0 = tree;
Tree.forceSetRight(tree0, tree = Tree.of(TermOp.OR____, apply_(nodifier1, o), null));
}
Tree.forceSetRight(tree, Atom.NIL);
return start.getRight();
}, node -> {
List<Object> list = Read.from(Tree.iter(node, TermOp.OR____)).map(n -> apply_(nodifier1, n)).toList();
Collection<Object> object1 = (Collection<Object>) instantiate(clazz);
object1.addAll(list);
return object1;
});
} else if (mapClasses.contains(clazz)) {
Nodifier kn = getNodifier(typeArgs[0]);
Nodifier vn = getNodifier(typeArgs[1]);
nodifier = new Nodifier(object -> {
Dict dict = new Dict();
for (Entry<?, ?> e : ((Map<?, ?>) object).entrySet()) dict.map.put(apply_(kn, e.getKey()), Reference.of(apply_(vn, e.getValue())));
return dict;
}, node -> {
Map<Node, Reference> map = ((Dict) node).map;
Map<Object, Object> object1 = (Map<Object, Object>) instantiate(clazz);
for (Entry<Node, Reference> e : map.entrySet()) object1.put(apply_(kn, e.getKey()), apply_(vn, e.getValue().finalNode()));
return object1;
});
} else
nodifier = getNodifier(rawType);
} else
nodifier = Fail.t("unrecognized type " + type);
return nodifier;
}
Aggregations