Browse Source

Weight correlated price

z_createrawtransaction
jl777 5 years ago
parent
commit
47f2069490
  1. 33
      src/komodo_gateway.h
  2. 9
      src/rpc/blockchain.cpp

33
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; iter++)
{
correlation = 0;
i = (j + seed) % daywindow;
refprice = rawprices[i];
highprice = (refprice * (COIN + PRICES_MAXCHANGE*2)) / COIN;
lowprice = (refprice * (COIN - PRICES_MAXCHANGE*2)) / COIN;
highprice = (refprice * (COIN + PRICES_MAXCHANGE*3)) / COIN;
lowprice = (refprice * (COIN - PRICES_MAXCHANGE*3)) / COIN;
if ( highprice == refprice )
highprice++;
if ( lowprice == refprice )
@ -2263,29 +2265,33 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int
correlation++;
if ( correlation > (daywindow>>1) )
{
/*n = 0;
n = 0;
i = (j + seed) % daywindow;
for (j=0; j<daywindow; j++,i++)
{
if ( i >= 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<daywindow; i++)
{
if ( (price= rawprices[i]) != 0 )
if ( (price= rawprices2[i]) != 0 )
{
den += (daywindow - i);
sum += (daywindow - i) * price;
@ -2294,9 +2300,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int
}
if ( n != correlation || sum == 0 || den == 0 )
return(-1);
fprintf(stderr,"weighted -> %.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);
}
}
}

9
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; i<width&&ht>2*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<maxsamples+daywindow; i++)
{
offset = j*width + i;
if ( (correlated[i]= komodo_pricecorrelated(seed,j,&prices[offset],daywindow)) < 0 )
memcpy(prices2,&prices[offset],sizeof(*prices2)*daywindow);
if ( (correlated[i]= komodo_pricecorrelated(seed,j,&prices[offset],daywindow,prices2)) < 0 )
throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price");
}
for (i=0; i<maxsamples; i++)
@ -1264,6 +1266,7 @@ UniValue prices(const UniValue& params, bool fHelp)
ret.push_back(Pair("daywindow",(int64_t)daywindow));
ret.push_back(Pair("numpricefeeds",(int64_t)numpricefeeds));
free(prices);
free(prices2);
free(correlated);
return ret;
}

Loading…
Cancel
Save