deephaven / deephaven-core

Deephaven Community Core
Other
237 stars 79 forks source link

Add a deephaven tutorials subpackage in the deephaven package for groovy #1771

Open hythloda opened 2 years ago

hythloda commented 2 years ago

Similar to Integrations/python/deephaven/tutorials/crypto_trades.py for #1444

We need to import functions to simulate tables for a groovy tutorial.

hythloda commented 2 years ago
rand = new Random()
ct_symbols = ['BTC/USD', 'ETH/USD', 'YFI/USD', 'PAXG/USD']
ct_exchanges = ['binance', 'bitfinex', 'coinbase-pro', 'gemini', 'kraken', 'bitstamp']

ct_price_map = ['BTC/USD': 55220.00, 'ETH/USD': 3480.00, 'YFI/USD': 30200.00, 'PAXG/USD': 1761.00]

ct_pricer = { final String instrument, final double E ->
   _aPrice = ct_price_map[instrument]
   if (_aPrice == null) {
      ct_price_map[instrument] = abs((int)E)/100.0
   } else {
      ct_price_map[instrument] = ((int)(_aPrice*100) + (int)(E/2000))/100.0
   }
   return (double)ct_price_map[instrument]
}

distributerFunc = { final int cnt ->
    temp = (int)((cnt*(cnt+1))/2)
    n = rand.nextInt(temp) +1
    for (i = 0; i < cnt; i++) {
      n -= (cnt-i)
      if (n <= 0) {
        return (int)i;
      }
    }
}

ticking_crypto_milliseconds = {  int interval ->
    return timeTable(minus(currentTime(), 1800000000000), '00:00:00.00'+ (interval * 1000).toString()).update(
        'Id=rand.nextInt(1100000000)+12000000', 
        'B=rand.nextInt(2)', 
        'C=rand.nextInt(50)',
        'D= (rand.nextDouble() - 0.5) * 20000.0', 
        'Instrument=ct_symbols[(rand.nextInt(50)%ct_symbols.size())].toString()',
        'Size=Math.max((rand.nextInt(11))*100, (rand.nextInt(149))+1)',
        'Price=(double)ct_pricer(Instrument, D)', 'Exchange = ct_exchanges[C%ct_exchanges.size()].toString()',
        'Date = formatDate(Timestamp, TZ_NY)') \
        .dropColumns('B', 'C', 'D') \
        .moveColumnsUp('Date', 'Timestamp', 'Id', 'Instrument', 'Exchange', 'Price', 'Size')
}