Sort trick
This commit is contained in:
@@ -2353,12 +2353,12 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int
|
||||
int32_t i,j,k,n,iter,correlation,maxcorrelation=0; int64_t firstprice,price,sum,den,mult,refprice,lowprice,highprice;
|
||||
if ( PRICES_DAYWINDOW < 2 || ind >= KOMODO_MAXPRICES )
|
||||
return(-1);
|
||||
mult = PriceMult[ind];
|
||||
mult = komodo_pricemult(ind);
|
||||
if ( nonzprices != 0 )
|
||||
memset(nonzprices,0,sizeof(*nonzprices)*PRICES_DAYWINDOW);
|
||||
for (i=0; i<PRICES_DAYWINDOW; i++)
|
||||
fprintf(stderr,"%u ",rawprices[i*rawskip]);
|
||||
fprintf(stderr,"ind.%d\n",ind);
|
||||
//for (i=0; i<PRICES_DAYWINDOW; i++)
|
||||
// fprintf(stderr,"%u ",rawprices[i*rawskip]);
|
||||
//fprintf(stderr,"ind.%d\n",ind);
|
||||
for (iter=0; iter<PRICES_DAYWINDOW; iter++)
|
||||
{
|
||||
correlation = 0;
|
||||
@@ -2521,10 +2521,33 @@ void smooth64(int64_t dest[],int64_t src[],int32_t width,int32_t smoothiters)
|
||||
|
||||
// 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_priceave(int64_t *correlated,int32_t cskip)
|
||||
static int cmp_llu(const void *a, const void*b)
|
||||
{
|
||||
int32_t i; int64_t sum=0,nonzprice,price;
|
||||
if(*(int64_t *)a < *(int64_t *)b) return -1;
|
||||
if(*(int64_t *)a > *(int64_t *)b) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int64_t sort64(int64_t *l, int32_t llen)
|
||||
{
|
||||
qsort(l,llen,sizeof(uint64_t),cmp_llu);
|
||||
}
|
||||
|
||||
static int revcmp_llu(const void *a, const void*b)
|
||||
{
|
||||
if(*(int64_t *)a < *(int64_t *)b) return 1;
|
||||
if(*(int64_t *)a > *(int64_t *)b) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int64_t revsort64(int64_t *l, int32_t llen)
|
||||
{
|
||||
heapsort(l,llen,sizeof(uint64_t),cmp_llu);
|
||||
}
|
||||
|
||||
int64_t komodo_priceave(int64_t *buf,int64_t *correlated,int32_t cskip)
|
||||
{
|
||||
int32_t i,dir=0; int64_t sum=0,nonzprice,price,halfave,decayprice;
|
||||
if ( PRICES_DAYWINDOW < 2 )
|
||||
return(0);
|
||||
for (i=0; i<PRICES_DAYWINDOW; i++)
|
||||
@@ -2538,10 +2561,37 @@ int64_t komodo_priceave(int64_t *correlated,int32_t cskip)
|
||||
{
|
||||
if ( (price= correlated[i*cskip]) != 0 )
|
||||
nonzprice = price;
|
||||
buf[i] = nonzprice;
|
||||
sum += nonzprice;
|
||||
if ( i == PRICES_DAYWINDOW/2 )
|
||||
halfave = (sum / (PRICES_DAYWINDOW/2));
|
||||
}
|
||||
price = sum / PRICES_DAYWINDOW;
|
||||
return(price);
|
||||
if ( halfave == price )
|
||||
return(price);
|
||||
else if ( halfave > price ) // rising prices
|
||||
{
|
||||
sort64(buf,PRICES_DAYWINDOW);
|
||||
decayprice = buf[0];
|
||||
for (i=0; i<PRICES_DAYWINDOW; i++)
|
||||
{
|
||||
decayprice = ((decayprice * 9) + (buf[i] * 1)) / 10
|
||||
fprintf(stderr,"%.4f ",(double)buf[i]/COIN);
|
||||
}
|
||||
fprintf(stderr,"sort half %.4f vs %.4f -> %.4f\n",(double)halfprice/COIN,(double)price/COIN,(double)decayprice/COIN);
|
||||
}
|
||||
else
|
||||
{
|
||||
revsort64(buf,PRICES_DAYWINDOW);
|
||||
decayprice = buf[0];
|
||||
for (i=0; i<PRICES_DAYWINDOW; i++)
|
||||
{
|
||||
decayprice = ((decayprice * 9) + (buf[i] * 1)) / 10
|
||||
fprintf(stderr,"%.4f ",(double)buf[i]/COIN);
|
||||
}
|
||||
fprintf(stderr,"revsort half %.4f vs %.4f -> %.4f\n",(double)halfprice/COIN,(double)price/COIN,(double)decayprice/COIN);
|
||||
}
|
||||
return(decayprice);
|
||||
}
|
||||
|
||||
void komodo_pricesinit()
|
||||
@@ -2576,7 +2626,7 @@ void komodo_pricesinit()
|
||||
|
||||
void komodo_pricesupdate(int32_t height,CBlock *pblock)
|
||||
{
|
||||
static int numprices; static uint32_t *ptr32; static int64_t *ptr64;
|
||||
static int numprices; static uint32_t *ptr32; static int64_t *ptr64,*tmpbuf;
|
||||
int32_t ind,offset,width; int64_t correlated,smoothed; uint64_t seed,rngval; uint32_t rawprices[KOMODO_MAXPRICES],buf[4];
|
||||
width = PRICES_DAYWINDOW;//(2*PRICES_DAYWINDOW + PRICES_SMOOTHWIDTH);
|
||||
if ( numprices == 0 )
|
||||
@@ -2584,6 +2634,7 @@ void komodo_pricesupdate(int32_t height,CBlock *pblock)
|
||||
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);
|
||||
tmpbuf = (int64_t *)calloc(sizeof(int64_t),PRICES_DAYWINDOW);
|
||||
fprintf(stderr,"prices update: numprices.%d %p %p\n",numprices,ptr32,ptr64);
|
||||
}
|
||||
if ( _komodo_heightpricebits(&seed,rawprices,pblock) == numprices )
|
||||
@@ -2620,7 +2671,7 @@ void komodo_pricesupdate(int32_t height,CBlock *pblock)
|
||||
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_priceave(&ptr64[PRICES_DAYWINDOW*3-1],-3)) > 0 )
|
||||
if ( (smoothed= komodo_priceave(tmpbuf,&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) )
|
||||
|
||||
@@ -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_priceave(int64_t *correlated,int32_t cskip);
|
||||
int64_t komodo_priceave(int64_t *tmpbuf,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; 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; int64_t *tmpbuf;
|
||||
width = numblocks+PRICES_DAYWINDOW*2+PRICES_SMOOTHWIDTH;
|
||||
komodo_heightpricebits(&seed,rawprices,firstheight + numblocks - 1);
|
||||
if ( firstheight < width )
|
||||
@@ -1210,8 +1210,10 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,
|
||||
if ( (pricedata[i*3+1]= komodo_pricecorrelated(rngval,ind,(uint32_t *)&pricedata[i*3],6,0,PRICES_SMOOTHWIDTH)) < 0 )
|
||||
return(-3);
|
||||
}
|
||||
tmpbuf = (int64_t *)calloc(sizeof(int64_t),PRICES_DAYWINDOW);
|
||||
for (i=0; i<numblocks; i++)
|
||||
pricedata[i*3+2] = komodo_priceave(&pricedata[i*3+1],3);
|
||||
pricedata[i*3+2] = komodo_priceave(tmpbuf,&pricedata[i*3+1],3);
|
||||
free(tmpbuf);
|
||||
return(0);
|
||||
}
|
||||
|
||||
@@ -1220,7 +1222,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,rngval; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[1440*6],*prices; uint32_t i,width,j,numpricefeeds=-1,n,numsamples,nextheight,offset,ht;
|
||||
UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t *tmpbuf,smoothed,*correlated; char name[64],*str; uint32_t rawprices[1440*6],*prices; uint32_t i,width,j,numpricefeeds=-1,n,numsamples,nextheight,offset,ht;
|
||||
if ( ASSETCHAINS_CBOPRET == 0 )
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices");
|
||||
|
||||
@@ -1281,10 +1283,11 @@ UniValue prices(const UniValue& params, bool fHelp)
|
||||
if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],1,0,PRICES_SMOOTHWIDTH)) < 0 )
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price");
|
||||
}
|
||||
tmpbuf = (int64_t *)calloc(sizeof(int64_t),PRICES_DAYWINDOW);
|
||||
for (i=0; i<maxsamples&&i<numsamples; i++)
|
||||
{
|
||||
offset = j*width + i;
|
||||
smoothed = komodo_priceave(&correlated[i],1);
|
||||
smoothed = komodo_priceave(tmpbuf,&correlated[i],1);
|
||||
UniValue parr(UniValue::VARR);
|
||||
parr.push_back(ValueFromAmount((int64_t)prices[offset] * komodo_pricemult(j)));
|
||||
parr.push_back(ValueFromAmount(correlated[i]));
|
||||
@@ -1292,6 +1295,7 @@ UniValue prices(const UniValue& params, bool fHelp)
|
||||
// compare to alternate method
|
||||
p.push_back(parr);
|
||||
}
|
||||
free(tmpbuf);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user