Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

144
LINES

< > BotCompany Repo | #1036184 // TickerSequences - multiple ticker sequences, usually of one coin and sorted by time

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (17377L/98K).

persistable sclass TickerSequences is ByteIO, IntSize, Iterable<TickerSequence>  {
  gettable S market; // e.g. "TRBUSDT"
  
  gettable new L<TickerSequence> tickers;
  
  *(Iterable<TickerSequence> sequences) {
    main addAll(tickers, sequences);
  }
  
  *(TickerSequence... sequences) {
    main addAll(tickers, sequences);
  }
  
  void add(TickerSequence ticker) {
    tickers.add(ticker);
  }
  
  TickerSequence get(int i) { ret tickers.get(i); }
  
  void sort {
    sortInPlaceByCalculatedField(tickers, ->. startTime());
  }
  
  public int size() { ret tickers.size(); }
  
  toString {
    ret commaCombine(
      spaceCombine(
        n2(tickers, " ticker sequence"),
        empty(market) ? null : "for " + market),
      formatDays(totalTime()),
      n2(nPricePoints(), "price point")
    );
  }
  
  long nPricePoints() {
    ret longSum(map(tickers, t -> (long) t.size()));
  }
  
  long totalTime() {
    ret longSum(map(tickers, ticker -> { var tr = ticker.timeRange(); ret tr == null ? 0 : tr.length(); }));
  }
  
  selfType dropOverlaps() {
    sort();
    
    for (int i = 0; i+1 < l(tickers); i++) {
      var t1 = tickers.get(i);
      var t2 = tickers.get(i+1);
      long time2 = t2.startTime();
      while (!t1.isEmpty() && t1.endTime() >= time2)
        t1.dropLastPricePoint();
      t1.trimToSize();
    }
    
    this;
  }
  
  selfType merge(double maximumGapSeconds default 120) {
    sort();
    
    for (int i = 0; i+1 < l(tickers); i++) {
      var t1 = tickers.get(i);
      var t2 = tickers.get(i+1);
      if (t2.startTime()-t1.endTime() <= fromSeconds(maximumGapSeconds)) {
        t1.add(t2);
        tickers.remove(i+1);
        --i;
      }
    }
    
    this;
  }
  
  double firstPrice() {
    var ticker = first(tickers);
    ret ticker == null ? Double.NaN : ticker.firstPrice();
  }
  
  ItIt<Double> prices() {
    ret nestedIterator(tickers, ticker -> ticker.prices.iterator());
  }
  
    
  long startTime() { ret empty(tickers) ? null : first(tickers).startTime(); }
  long endTime() { ret empty(tickers) ? null : last(tickers).endTime(); }
  
  TickerSequence longest() {
    ret highestBy(tickers(), ticker -> duration(ticker.timeRange()));
  }
  
  TickerSequences mapTickers(IF1<TickerSequence> f) {
    new TickerSequences ts;
    for (t : tickers())
      ts.add(f.get(t));
    ret ts;
  }
  
  TickerSequences filterTickers(IPred<TickerSequence> pred) {
    new TickerSequences ts;
    for (t : tickers())
      if (pred.get(t))
        ts.add(t);
    ret ts;
  }
  
  /*TickerSequences digitizeToPercent(double basePrice, double cellSizeInPercent) {
    ret mapTickers(ts -> ts.digitizeToPercent(basePrice, cellSizeInPercent));
  }*/
  
  selfType marketIfEmpty(S market) {
    if (empty(this.market)) market(market);
    this;
  }
  
  selfType market(S market) {
    this.market = market;
    for (t : tickers)
      t.market(market);
    this;
  }
  
  public void write(ByteHead head) {
    for (t : tickers)
      t.write(head);
  }
  
  public void read(ByteHead head) {
    TickerSequence t;
    while ((t = TickerSequence.read(head)) != null)
      add(t);
    if (empty(market) && nempty(tickers))
      market(first(tickers).market);
  }
  
  public void readWrite(ByteHead head) {
    if (head.readMode()) read(head);
    if (head.writeMode()) write(head);
  }
  
  public Iterator<TickerSequence> iterator() {
    ret tickers.iterator();
  }
}

Author comment

Began life as a copy of #1036138

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036184
Snippet name: TickerSequences - multiple ticker sequences, usually of one coin and sorted by time
Eternal ID of this version: #1036184/37
Text MD5: 34fe4a203a2e97665ede5635e0840956
Transpilation MD5: c0dc3f19f907831e4d7f568516283026
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-21 20:26:15
Source code size: 3567 bytes / 144 lines
Pitched / IR pitched: No / No
Views / Downloads: 226 / 462
Version history: 36 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)