From 47f2069490780624642adf8d4350da9033b1cfbf Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 04:29:54 -1100 Subject: [PATCH] Weight correlated price --- src/komodo_gateway.h | 33 +++++++++++++++++++-------------- src/rpc/blockchain.cpp | 9 ++++++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 7df402d69..66e9fae78 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2231,7 +2231,7 @@ char *komodo_pricename(char *name,int32_t ind) return(0); } -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow) +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow,uint32_t *rawprices2) { int32_t i,j,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den,mult,refprice,lowprice,highprice; if ( daywindow < 2 ) @@ -2239,13 +2239,15 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( ind < 36 ) mult = 10000; else mult = 1; + if ( memcmp(rawprices,rawprices2,daywindow*sizeof(*rawprices)) != 0 ) + fprintf(stderr,"ind.%d rawprices2 != rawprices\n",ind); for (iter=0; iter (daywindow>>1) ) { - /*n = 0; + n = 0; i = (j + seed) % daywindow; for (j=0; j= daywindow ) i = 0; if ( n >= (daywindow>>1) ) - rawprices[i] = 0; + rawprices2[i] = 0; else { price = rawprices[i]; if ( price < lowprice || price > highprice ) - rawprices[i] = 0; - else n++; + rawprices2[i] = 0; + else + { + rawprices2[i] = price; + n++; + } } - }*/ - //fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); - /*if ( n != correlation ) + } + fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); + if ( n != correlation ) return(-1); sum = den = n = 0; for (i=0; i %.8f\n",((double)sum / den) / COIN); - return(sum / den);*/ - return(sum / correlation); + fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); + return((sum * mult) / den); } } } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index c68d4b14b..c41803908 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1177,7 +1177,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 numprices); -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices); +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices,uint32_t *rawprices2); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1186,7 +1186,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( fHelp || params.size() != 1 ) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); - UniValue ret(UniValue::VOBJ); uint64_t seed; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + UniValue ret(UniValue::VOBJ); uint64_t seed; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; if ( ASSETCHAINS_CBOPRET == 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); @@ -1203,6 +1203,7 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "illegal numpricefeeds"); prices = (uint32_t *)calloc(sizeof(*prices),width*numpricefeeds); correlated = (int64_t *)calloc(sizeof(*correlated),width); + prices2 = (int64_t *)calloc(sizeof(*prices2),width); for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) @@ -1238,7 +1239,8 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i