|
| 1 | +char heap_start[8192]; |
| 2 | +#define PC_HEAP_START (heap_start) |
| 3 | + |
| 4 | +#include <stdint.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <oracle/oracle.h> |
| 8 | +#include <oracle/upd_aggregate.h> |
| 9 | +#include <pc/mem_map.hpp> |
| 10 | +#include <math.h> |
| 11 | +#include <ctype.h> |
| 12 | +#include <iostream> |
| 13 | + |
| 14 | +using namespace pc; |
| 15 | + |
| 16 | +int usage() |
| 17 | +{ |
| 18 | + std::cerr << "usage: test_twap <twap.csv>" << std::endl; |
| 19 | + return 1; |
| 20 | +} |
| 21 | + |
| 22 | +class csv_parser |
| 23 | +{ |
| 24 | +public: |
| 25 | + csv_parser( const char *buf, size_t len ) |
| 26 | + : ptr_( buf ), |
| 27 | + eol_( buf ), |
| 28 | + end_( &buf[len] ), |
| 29 | + first_( false ) { |
| 30 | + } |
| 31 | + bool fetch_line() { |
| 32 | + for( ptr_ = eol_;;++ptr_ ) { |
| 33 | + if ( ptr_ == end_ ) return false; |
| 34 | + if ( *ptr_ != '\n' && *ptr_ != '\r' ) break; |
| 35 | + } |
| 36 | + for( eol_ = ptr_;ptr_ != end_ && *eol_ != '\n' && *eol_ != '\r';++eol_ ); |
| 37 | + first_ = true; |
| 38 | + return true; |
| 39 | + } |
| 40 | + void fetch( int64_t& val ) { |
| 41 | + if ( ptr_ != eol_ && *ptr_ == ',' && !first_ ) { |
| 42 | + ++ptr_; |
| 43 | + } |
| 44 | + first_ = false; |
| 45 | + const char *ptxt = ptr_; |
| 46 | + for( ; ptr_ != eol_ && *ptr_ != ','; ++ptr_ ); |
| 47 | + char *etxt[1]; |
| 48 | + etxt[0] = (char*)ptr_; |
| 49 | + val = strtol( ptxt, etxt, 10 ); |
| 50 | + } |
| 51 | +private: |
| 52 | + const char *ptr_; |
| 53 | + const char *eol_; |
| 54 | + const char *end_; |
| 55 | + bool first_; |
| 56 | +}; |
| 57 | + |
| 58 | +int main( int argc,char** argv ) |
| 59 | +{ |
| 60 | + if ( argc < 2 ) { |
| 61 | + return usage(); |
| 62 | + } |
| 63 | + |
| 64 | + // read price file |
| 65 | + mem_map mf; |
| 66 | + std::string file = argv[1]; |
| 67 | + mf.set_file(file ); |
| 68 | + if ( !mf.init() ) { |
| 69 | + std::cerr << "test_qset: failed to read file[" << file << "]" |
| 70 | + << std::endl; |
| 71 | + return 1; |
| 72 | + } |
| 73 | + pc_price_t px[1]; |
| 74 | + __builtin_memset( px, 0, sizeof( pc_price_t ) ); |
| 75 | + uint64_t slot = 1000; |
| 76 | + px->last_slot_ = slot; |
| 77 | + px->agg_.pub_slot_ = slot; |
| 78 | + px->num_ = 0; |
| 79 | + upd_aggregate( px, slot+1 ); |
| 80 | + pc_qset_t *qs = qset_new( px->expo_ ); |
| 81 | + |
| 82 | + // skip first line |
| 83 | + csv_parser cp( mf.data(), mf.size( )); |
| 84 | + |
| 85 | + // update twap for each update |
| 86 | + std::cout << "price,conf,expo,nslots,twap,twac" << std::endl; |
| 87 | + for( cp.fetch_line(); cp.fetch_line(); ) { |
| 88 | + int64_t nslots = 0, price = 0, conf = 0, expo = 0; |
| 89 | + cp.fetch( price ); |
| 90 | + cp.fetch( conf ); |
| 91 | + cp.fetch( expo ); |
| 92 | + cp.fetch( nslots ); |
| 93 | + px->expo_ = expo; |
| 94 | + px->agg_.price_ = price; |
| 95 | + px->agg_.conf_ = conf; |
| 96 | + upd_twap( px, nslots, qs ); |
| 97 | + std::cout << price << ',' |
| 98 | + << conf << ',' |
| 99 | + << expo << ',' |
| 100 | + << nslots << ',' |
| 101 | + << px->twap_.val_ << ',' |
| 102 | + << px->twac_.val_ |
| 103 | + << std::endl; |
| 104 | + } |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
0 commit comments