From 18f884c56f6108d803e6daf0e9e7fd345c957dea Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 11 Apr 2019 02:48:20 -1100 Subject: [PATCH] Calc correlated and smoothed each block --- src/komodo_gateway.h | 66 +++++++++++++++++++++++++++++++++++------- src/rpc/blockchain.cpp | 23 ++++++++------- 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 625cdc6b5..837a82570 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2516,9 +2516,11 @@ void smooth64(int64_t dest[],int64_t src[],int32_t width,int32_t smoothiters) } else memcpy(dest,src,width*sizeof(*dest)); } -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *rawprices,int32_t numprices) +// http://www.holoborodko.com/pavel/numerical-methods/noise-robust-smoothing-filter/ +//const int64_t coeffs[7] = { -2, 0, 18, 32, 18, 0, -2 }; + +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip) { - //const int64_t coeffs[7] = { -2, 0, 18, 32, 18, 0, -2 }; int32_t i; int64_t sum=0,nonzprice,price; if ( PRICES_DAYWINDOW < 2 ) return(0); @@ -2533,12 +2535,9 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *rawprice { if ( (price= correlated[i*cskip]) != 0 ) nonzprice = price; - //correlated2[i] = nonzprice / PRICES_DAYWINDOW; // reduce precision sum += nonzprice; } price = sum / PRICES_DAYWINDOW; - // improve smoothing with correlated2 processing - // price = smooth(correlated2,PRICES_DAYWINDOW,price/daywindow) * PRICES_DAYWINDOW; return(price); } @@ -2573,20 +2572,67 @@ void komodo_pricesinit() void komodo_pricesupdate(int32_t height,CBlock *pblock) { - static int numprices; - int32_t i; uint64_t seed; uint32_t rawprices[KOMODO_MAXPRICES]; + static int numprices; static uint32_t *ptr32,*ptr64; + int32_t ind,offset,width; int64_t correlated,smoothed; uint64_t seed,rngval; uint32_t rawprices[KOMODO_MAXPRICES],buf[4]; + width = (2*PRICES_DAYWINDOW + PRICES_SMOOTHWIDTH); if ( numprices == 0 ) + { numprices = (int32_t)(komodo_cbopretsize(ASSETCHAINS_CBOPRET) / sizeof(uint32_t)); + ptr32 = (uint32_t *)calloc(sizeof(uint32_t),numprices * width); + ptr64 = (int64_t *)calloc(sizeof(int64_t),PRICES_DAYWINDOW*3); + } if ( _komodo_heightpricebits(&seed,rawprices,pblock) == numprices ) { - for (i=0; i width ) + { + fseek(PRICES[0].fp,(height-width+1) * numprices * sizeof(uint32_t),SEEK_SET); + if ( fread(ptr32,sizeof(uint32_t),width*numprices,PRICES[0].fp) == width*numprices ) + { + rngval = seed; + for (ind=1; ind 0 ) + { + fseek(PRICES[ind].fp,height * sizeof(int64_t) * 3,SEEK_SET); + buf[0] = rawprices[ind]; + buf[1] = rawprices[0]; // timestamp + memcpy(&buf[2],&correlated,sizeof(correlated)); + if ( fwrite(buf,1,sizeof(buf),PRICES[ind].fp) != sizeof(buf) ) + fprintf(stderr,"error fwrite buf for ht.%d ind.%d\n",height,ind); + else + { + fseek(PRICES[ind].fp,(height-PRICES_DAYWINDOW+1) * 3 * sizeof(int64_t),SEEK_SET); + if ( fread(ptr64,sizeof(int64_t),PRICES_DAYWINDOW*3,PRICES[ind].fp) == PRICES_DAYWINDOW*3 ) + { + if ( (smoothed= komodo_pricesmoothed(&ptr64[PRICES_DAYWINDOW*3-1],-3)) > 0 ) + { + fseek(PRICES[ind].fp,(height * 3 + 2) * sizeof(int64_t),SEEK_SET); + if ( fwrite(&smoothed,1,sizeof(smoothed),PRICES[ind].fp) != sizeof(smoothed) ) + fprintf(stderr,"error fwrite smoothed for ht.%d ind.%d\n",height,ind); + else + { + fprintf(stderr,"%.4f ",(double)smoothed/COIN); + fflush(PRICES[ind].fp); + } + } else fprintf(stderr,"error price_smoothed ht.%d ind.%d\n",height,ind); + } else fprintf(stderr,"error fread ptr64 for ht.%d ind.%d\n",height,ind); + } + } else fprintf(stderr,"error komodo_pricecorrelated for ht.%d ind.%d\n",height,ind); + } + fprintf(stderr,"height.%d\n",height); + } else fprintf(stderr,"error reading rawprices for ht.%d\n",height); + } } } else fprintf(stderr,"numprices mismatch\n"); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index fc9a71fe2..87f2658cd 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1176,7 +1176,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *correlated2,int32_t numprices); +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip); int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t rawskip,uint32_t *nonzprices,int32_t smoothwidth); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1185,7 +1185,7 @@ int64_t komodo_pricemult(int32_t ind); int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind) { - int32_t height,i,n,width,numpricefeeds = -1; uint64_t seed,ignore,rngval; int64_t *correlated2; uint32_t rawprices[1440*6],*ptr; + int32_t height,i,n,width,numpricefeeds = -1; uint64_t seed,ignore,rngval; uint32_t rawprices[1440*6],*ptr; //daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; //pricedata = (uint32_t *)calloc(sizeof(*prices)*3,numblocks + daywindow*2 + PRICES_SMOOTHWIDTH); width = numblocks+PRICES_DAYWINDOW*2+PRICES_SMOOTHWIDTH; @@ -1205,20 +1205,21 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks, ptr[1] = rawprices[0]; // timestamp } rngval = seed; - correlated2 = (int64_t *)calloc(sizeof(*correlated2),width); + //correlated2 = (int64_t *)calloc(sizeof(*correlated2),width); for (i=0; i2; i++,ht--) @@ -1287,14 +1288,14 @@ UniValue prices(const UniValue& params, bool fHelp) { offset = j*width + i; rngval = (rngval*11109 + 13849); - correlated2[i] = prices[offset]; + //correlated2[i] = prices[offset]; if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],1,0,PRICES_SMOOTHWIDTH)) < 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price"); } for (i=0; i