Search in sources :

Example 1 with Schedule

use of suite.os.Schedule in project suite by stupidsing.

the class WalkForwardRecorderMain method run.

@Override
protected boolean run(String[] args) {
    Streamlet<Asset> assets = cfg.queryCompaniesByMarketCap(Time.now());
    float fund0 = 1000000f;
    Trade_.isCacheQuotes = false;
    Trade_.isShortSell = true;
    Trade_.leverageAmount = fund0;
    if (Boolean.FALSE) {
        // record
        String ts = Time.now().ymdHms().replace("-", "").replace(" ", "-").replace(":", "");
        String filename = "wfa." + ts + ".csv";
        Schedule schedule = // 
        Schedule.ofRepeat(5, () -> {
            String ymdHms = Time.now().ymdHms();
            Map<String, Float> priceBySymbol = cfg.quote(assets.map(asset -> asset.symbol).toSet());
            try (OutputStream os = // 
            Files.newOutputStream(// 
            HomeDir.resolve(filename), // 
            StandardOpenOption.APPEND, // 
            StandardOpenOption.CREATE, // 
            StandardOpenOption.WRITE);
                PrintWriter bw = new PrintWriter(os)) {
                for (Entry<String, Float> e : priceBySymbol.entrySet()) bw.println(ymdHms + ", " + e.getKey() + ", " + e.getValue());
            } catch (IOException ex) {
                Fail.t(ex);
            }
        });
        Scheduler.of(schedule.filterTime(dt -> HkexUtil.isMarketOpen(Time.of(dt)))).run();
    } else {
        // replay
        String ts = "20170612-092616";
        String filename = "wfa." + ts + ".csv";
        Map<Time, Map<String, Float>> data = new TreeMap<>();
        try (// 
        InputStream is = Files.newInputStream(HomeDir.resolve(filename));
            // 
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr)) {
            while (br.ready()) {
                String[] array = br.readLine().split(",");
                Time time = Time.of(array[0].trim());
                String symbol = array[1].trim();
                float price = Float.parseFloat(array[2].trim());
                data.computeIfAbsent(time, s -> new HashMap<>()).put(symbol, price);
            }
        } catch (IOException ex) {
            Fail.t(ex);
        }
        WalkForwardAllocConfiguration wfac = new // 
        WalkForwardAllocConfiguration(// 
        cfg.queryCompaniesByMarketCap(Time.now()), bag.rsi.unleverage().walkForwardAllocator());
        WalkForwardAllocTester tester = new WalkForwardAllocTester(cfg, wfac.assets, fund0, wfac.walkForwardAllocator);
        for (Entry<Time, Map<String, Float>> e : data.entrySet()) System.out.println(tester.tick(e.getKey(), e.getValue()));
        System.out.println(tester.conclusion());
    }
    return true;
}
Also used : WalkForwardAllocTester(suite.trade.walkforwardalloc.WalkForwardAllocTester) Schedule(suite.os.Schedule) Trade_(suite.trade.Trade_) HashMap(java.util.HashMap) Scheduler(suite.os.Scheduler) HomeDir(suite.util.HomeDir) RunUtil(suite.util.RunUtil) ConfigurationImpl(suite.trade.data.ConfigurationImpl) Map(java.util.Map) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) HkexUtil(suite.trade.data.HkexUtil) Files(java.nio.file.Files) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) WalkForwardAllocConfiguration(suite.trade.walkforwardalloc.WalkForwardAllocConfiguration) InputStreamReader(java.io.InputStreamReader) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) TreeMap(java.util.TreeMap) Configuration(suite.trade.data.Configuration) Entry(java.util.Map.Entry) BufferedReader(java.io.BufferedReader) Asset(suite.trade.Asset) BackAllocatorGeneral(suite.trade.backalloc.strategy.BackAllocatorGeneral) Fail(suite.util.Fail) InputStream(java.io.InputStream) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) WalkForwardAllocTester(suite.trade.walkforwardalloc.WalkForwardAllocTester) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Time(suite.trade.Time) IOException(java.io.IOException) TreeMap(java.util.TreeMap) WalkForwardAllocConfiguration(suite.trade.walkforwardalloc.WalkForwardAllocConfiguration) Schedule(suite.os.Schedule) BufferedReader(java.io.BufferedReader) Asset(suite.trade.Asset) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) PrintWriter(java.io.PrintWriter)

Example 2 with Schedule

use of suite.os.Schedule in project suite by stupidsing.

the class WalkForwardTestMain method run.

@Override
protected boolean run(String[] args) {
    float fund0 = 1000000f;
    Trade_.isCacheQuotes = false;
    Trade_.isShortSell = true;
    Trade_.leverageAmount = fund0;
    WalkForwardAllocConfiguration wfac = new // 
    WalkForwardAllocConfiguration(// 
    cfg.queryCompaniesByMarketCap(Time.now()), bag.rsi.unleverage().walkForwardAllocator());
    WalkForwardAllocTester tester = new WalkForwardAllocTester(cfg, wfac.assets, fund0, wfac.walkForwardAllocator);
    Schedule schedule = // 
    Schedule.ofRepeat(5, // 
    () -> System.out.println(tester.tick())).filterTime(dt -> HkexUtil.isMarketOpen(Time.of(dt)));
    Scheduler.of(schedule).run();
    return true;
}
Also used : WalkForwardAllocTester(suite.trade.walkforwardalloc.WalkForwardAllocTester) Schedule(suite.os.Schedule) WalkForwardAllocConfiguration(suite.trade.walkforwardalloc.WalkForwardAllocConfiguration)

Aggregations

Schedule (suite.os.Schedule)2 WalkForwardAllocConfiguration (suite.trade.walkforwardalloc.WalkForwardAllocConfiguration)2 WalkForwardAllocTester (suite.trade.walkforwardalloc.WalkForwardAllocTester)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Files (java.nio.file.Files)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1 Scheduler (suite.os.Scheduler)1 Streamlet (suite.streamlet.Streamlet)1 Asset (suite.trade.Asset)1 Time (suite.trade.Time)1 Trade_ (suite.trade.Trade_)1