diff --git a/OSlibs/linux/x86_64/libnanomsg-static.a b/OSlibs/linux/x86_64/libnanomsg-static.a new file mode 100644 index 000000000..064e32591 Binary files /dev/null and b/OSlibs/linux/x86_64/libnanomsg-static.a differ diff --git a/basilisk/basilisk.c b/basilisk/basilisk.c index 82803763a..0c7651db2 100755 --- a/basilisk/basilisk.c +++ b/basilisk/basilisk.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -14,6 +14,7 @@ ******************************************************************************/ #include "../iguana/iguana777.h" +#include "../iguana/exchanges777.h" typedef char *basilisk_servicefunc(struct supernet_info *myinfo,char *CMD,void *addr,char *remoteaddr,uint32_t basilisktag,cJSON *valsobj,uint8_t *data,int32_t datalen,bits256 hash,int32_t from_basilisk); @@ -482,6 +483,7 @@ int32_t basilisk_relayid(struct supernet_info *myinfo,uint32_t ipbits) #include "basilisk_lisk.c" #include "basilisk_MSG.c" +#include "tradebots_liquidity.c" #include "basilisk_tradebot.c" #include "basilisk_swap.c" #include "basilisk_DEX.c" @@ -875,39 +877,55 @@ void basilisks_loop(void *arg) if ( relay == 0 ) relay = iguana_coinfind("RELAY"); startmilli = OS_milliseconds(); + endmilli = startmilli + 1000; + //fprintf(stderr,"A "); basilisk_issued_purge(myinfo,600000); + //fprintf(stderr,"B "); basilisk_p2pQ_process(myinfo,777); + //fprintf(stderr,"C "); if ( myinfo->IAMNOTARY != 0 ) { if ( relay != 0 ) + { + //fprintf(stderr,"D "); basilisk_ping_send(myinfo,relay); + } counter++; + //fprintf(stderr,"E "); if ( myinfo->numdpows == 1 ) { iguana_dPoWupdate(myinfo,&myinfo->DPOWS[0]); - endmilli = startmilli + 50; + endmilli = startmilli + 100; } else if ( myinfo->numdpows > 1 ) { dp = &myinfo->DPOWS[counter % myinfo->numdpows]; iguana_dPoWupdate(myinfo,dp); if ( (counter % myinfo->numdpows) != 0 ) + { + //fprintf(stderr,"F "); iguana_dPoWupdate(myinfo,&myinfo->DPOWS[0]); - endmilli = startmilli + 10; + } + endmilli = startmilli + 30; } } else { + //fprintf(stderr,"G "); dex_updateclient(myinfo); if ( myinfo->IAMLP != 0 ) - endmilli = startmilli + 1000; - else endmilli = startmilli + 2000; + endmilli = startmilli + 500; + else endmilli = startmilli + 1000; } if ( myinfo->expiration != 0 && (myinfo->dexsock >= 0 || myinfo->IAMLP != 0 || myinfo->DEXactive > time(NULL)) ) + { + //fprintf(stderr,"H "); basilisk_requests_poll(myinfo); - //printf("RELAYID.%d endmilli %f vs now %f\n",myinfo->NOTARY.RELAYID,endmilli,OS_milliseconds()); + } + //printf("RELAYID.%d endmilli %f vs now %f\n",myinfo->NOTARY.RELAYID,endmilli,startmilli); while ( OS_milliseconds() < endmilli ) usleep(10000); + //printf("finished waiting numdpow.%d\n",myinfo->numdpows); iter++; } } diff --git a/basilisk/basilisk.h b/basilisk/basilisk.h index e4bd8c5ef..d90679ecd 100755 --- a/basilisk/basilisk.h +++ b/basilisk/basilisk.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -79,6 +79,7 @@ struct basilisk_swapinfo struct basilisk_swap { struct supernet_info *myinfo; struct iguana_info *bobcoin,*alicecoin; + void (*balancingtrade)(struct supernet_info *myinfo,struct basilisk_swap *swap,int32_t iambob); struct basilisk_swapinfo I; struct basilisk_rawtx bobdeposit,bobpayment,alicepayment,myfee,otherfee,aliceclaim,alicespend,bobreclaim,bobspend,bobrefund,alicereclaim; bits256 privkeys[INSTANTDEX_DECKSIZE]; diff --git a/basilisk/basilisk_CMD.c b/basilisk/basilisk_CMD.c index 858786d3c..5696addc9 100755 --- a/basilisk/basilisk_CMD.c +++ b/basilisk/basilisk_CMD.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/basilisk/basilisk_DEX.c b/basilisk/basilisk_DEX.c index badaee894..b47ac7c72 100755 --- a/basilisk/basilisk_DEX.c +++ b/basilisk/basilisk_DEX.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/basilisk/basilisk_MSG.c b/basilisk/basilisk_MSG.c index e47c2d461..76e019031 100755 --- a/basilisk/basilisk_MSG.c +++ b/basilisk/basilisk_MSG.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/basilisk/basilisk_bitcoin.c b/basilisk/basilisk_bitcoin.c index 014fc121e..4e5ff17cc 100755 --- a/basilisk/basilisk_bitcoin.c +++ b/basilisk/basilisk_bitcoin.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/basilisk/basilisk_ping.c b/basilisk/basilisk_ping.c index c02e805f2..f5e6c30c7 100755 --- a/basilisk/basilisk_ping.c +++ b/basilisk/basilisk_ping.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/basilisk/basilisk_swap.c b/basilisk/basilisk_swap.c index 7b1966bc9..b340b81ac 100755 --- a/basilisk/basilisk_swap.c +++ b/basilisk/basilisk_swap.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -1558,7 +1558,7 @@ int32_t basilisk_swapiteration(struct supernet_info *myinfo,struct basilisk_swap printf("Bob error spending alice payment\n"); else { - basilisk_swap_balancingtrade(myinfo,swap,1); + tradebot_swap_balancingtrade(myinfo,swap,1); printf("Bob spends alicepayment\n"); swap->I.statebits |= 0x40000; while ( basilisk_numconfirms(myinfo,&swap->bobspend) < swap->I.aliceconfirms ) @@ -1629,7 +1629,7 @@ int32_t basilisk_swapiteration(struct supernet_info *myinfo,struct basilisk_swap if ( basilisk_swapget(myinfo,swap,0x8000,data,maxlen,basilisk_verify_bobpaid) == 0 ) { printf("got bobpayment\n"); - basilisk_swap_balancingtrade(myinfo,swap,0); + tradebot_swap_balancingtrade(myinfo,swap,0); // verify payment and submit, set confirmed height swap->I.statebits |= 0x8000; } diff --git a/basilisk/basilisk_tradebot.c b/basilisk/basilisk_tradebot.c index ab606d6fb..19e4401c2 100755 --- a/basilisk/basilisk_tradebot.c +++ b/basilisk/basilisk_tradebot.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -14,60 +14,6 @@ ******************************************************************************/ // included from basilisk.c -void basilisk_swap_balancingtrade(struct supernet_info *myinfo,struct basilisk_swap *swap,int32_t iambob) -{ - // update balance, compare to target balance, issue balancing trade via central exchanges, if needed - double price,volume,srcamount,destamount,profitmargin,dir=0.,dotrade=1.; char base[64],rel[64]; - srcamount = swap->I.req.srcamount; - destamount = swap->I.req.destamount; - profitmargin = (double)swap->I.req.profitmargin / 1000000.; - if ( srcamount <= SMALLVAL || destamount <= SMALLVAL ) - { - printf("illegal amount for balancing %f %f\n",srcamount,destamount); - return; - } - strcpy(rel,"BTC"); - if ( strcmp(swap->I.req.src,"BTC") == 0 ) - { - strcpy(base,swap->I.req.dest); - price = (srcamount / destamount); - volume = destamount / SATOSHIDEN; - dir = -1.; - } - else if ( strcmp(swap->I.req.dest,"BTC") == 0 ) - { - strcpy(base,swap->I.req.src); - price = (destamount / srcamount); - volume = srcamount / SATOSHIDEN; - dir = 1.; - } - else - { - printf("only BTC trades can be balanced, not (%s/%s)\n",swap->I.req.src,swap->I.req.dest); - return; - } - if ( iambob != 0 ) - { - if ( myinfo->IAMLP != 0 ) - { - printf("BOB: price %f * vol %f -> %s newprice %f margin %.2f%%\n",price,volume,dir < 0. ? "buy" : "sell",price + dir * price * profitmargin,100*profitmargin); - if ( dir < 0. ) - InstantDEX_buy(myinfo,0,0,0,"poloniex",base,rel,price,volume,dotrade); - else InstantDEX_sell(myinfo,0,0,0,"poloniex",base,rel,price,volume,dotrade); - } - } - else - { - if ( myinfo->IAMLP != 0 ) - { - printf("ALICE: price %f * vol %f -> %s newprice %f margin %.2f%%\n",price,volume,dir > 0. ? "buy" : "sell",price - dir * price * profitmargin,100*profitmargin); - if ( dir > 0. ) - InstantDEX_buy(myinfo,0,0,0,"poloniex",base,rel,price,volume,dotrade); - else InstantDEX_sell(myinfo,0,0,0,"poloniex",base,rel,price,volume,dotrade); - } - } -} - cJSON *basilisk_rawtxobj(struct supernet_info *myinfo,struct basilisk_swap *swap,struct basilisk_rawtx *rawtx) { @@ -170,12 +116,13 @@ struct basilisk_swap *basilisk_swapstore(struct supernet_info *myinfo,struct bas struct basilisk_swap *basilisk_swapload(struct supernet_info *myinfo,struct basilisk_swap *swap,uint32_t requestid,uint32_t quoteid) { - return(swap); + // set swap fields and return it if found + return(0); } void basilisk_swapstart(struct supernet_info *myinfo) // scan saved tmpswap, purge if complete, else Q { - + // for resuming pending swaps } void basilisk_txlog(struct supernet_info *myinfo,struct basilisk_swap *swap,struct basilisk_rawtx *rawtx,int32_t delay) @@ -329,53 +276,6 @@ int32_t basilisk_request_cmpref(struct basilisk_request *ref,struct basilisk_req } else return(0); } -void tradebot_liquidity_command(struct supernet_info *myinfo,char *base,bits256 hash,cJSON *vals) -{ - struct liquidity_info li,refli; int32_t i; - memset(&li,0,sizeof(li)); - strcpy(li.base,base), strcpy(li.rel,"BTC"); - li.profit = jdouble(vals,"profit"); - li.refprice = jdouble(vals,"refprice"); - for (i=0; ilinfos)/sizeof(*myinfo->linfos); i++) - { - refli = myinfo->linfos[i]; - if ( strcmp(li.rel,refli.base) == 0 && strcmp(li.base,refli.rel) == 0 ) - { - strcpy(li.base,refli.base); - strcpy(li.rel,refli.rel); - if ( fabs(li.refprice) > SMALLVAL ) - li.refprice = (1. / li.refprice); - else li.refprice = 0.; - printf("Set rev linfo[%d] (%s/%s) %.6f %.8f\n",i,li.base,li.rel,li.profit,li.refprice); - myinfo->linfos[i] = li; - return; - } - else if ( refli.base[0] == 0 || (strcmp(li.base,refli.base) == 0 && strcmp(li.rel,refli.rel) == 0) ) - { - myinfo->linfos[i] = li; - printf("Set linfo[%d] (%s/%s) %.6f %.8f\n",i,li.base,li.rel,li.profit,li.refprice); - return; - } - } - printf("ERROR: too many linfos %d\n",i); -} - -double tradebot_liquidity_active(struct supernet_info *myinfo,double *refpricep,char *base,char *rel) -{ - int32_t i; struct liquidity_info refli; - *refpricep = 0.; - for (i=0; ilinfos)/sizeof(*myinfo->linfos); i++) - { - refli = myinfo->linfos[i]; - if ( (strcmp(base,refli.base) == 0 && strcmp(rel,refli.rel) == 0) || (strcmp(rel,refli.base) == 0 && strcmp(base,refli.rel) == 0 )) - { - *refpricep = refli.refprice; - return(refli.profit); - } - } - return(0.); -} - double basilisk_request_listprocess(struct supernet_info *myinfo,struct basilisk_request *issueR,struct basilisk_request *list,int32_t n) { int32_t i,noquoteflag=0,havequoteflag=0,myrequest=0,maxi=-1; int64_t balance=0,destamount,minamount = 0,maxamount = 0; uint32_t pendingid=0; struct basilisk_swap *active; double metric = 0.; @@ -414,7 +314,7 @@ double basilisk_request_listprocess(struct supernet_info *myinfo,struct basilisk } //printf("%s -> %s myrequest.%d pendingid.%u noquoteflag.%d havequoteflag.%d maxi.%d %.8f\n",list[0].src,list[0].dest,myrequest,pendingid,noquoteflag,havequoteflag,maxi,dstr(maxamount)); double retvals[4],refprice,profitmargin,aveprice; cJSON *retjson; char *retstr; - if ( myinfo->IAMLP != 0 && myrequest == 0 && pendingid == 0 && noquoteflag != 0 && (profitmargin= tradebot_liquidity_active(myinfo,&refprice,list[0].src,list[0].dest)) > 0. ) + if ( maxi >= 0 && myinfo->IAMLP != 0 && myrequest == 0 && pendingid == 0 && noquoteflag != 0 && (profitmargin= tradebot_liquidity_active(myinfo,&refprice,"DEX",list[maxi].src,list[maxi].dest,(double)maxamount/SATOSHIDEN)) > 0. ) { if ( (aveprice= instantdex_avehbla(myinfo,retvals,list[0].src,list[0].dest,1.3 * dstr(list[0].srcamount))) == 0. || refprice > aveprice ) aveprice = refprice; diff --git a/basilisk/tradebots_SVM.h b/basilisk/tradebots_SVM.h new file mode 100755 index 000000000..3e7b566cd --- /dev/null +++ b/basilisk/tradebots_SVM.h @@ -0,0 +1,1066 @@ +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +/* + requires *vals and *vars to be initialized + external calls: OS_milliseconds(), get_jfp_features(), get_yval(), set_ocas_model(), update_ocas_preds(), ocasCL_glue() + */ +//#include + +typedef float svmtype; +typedef double double2 __attribute__((ext_vector_type(2))); +#define MAX_VECTORS (1440 * 365 * 5) +#define MAIN_MAXCORES 16 +#define c_to_refc(c) (c) +#define refc_to_c(refc) (refc) + +#define CONDITION(feature) (feature) +#define FEATURE_THRESHOLD 10. +#define HWMPERC_THRESHOLD 101 +#define HWMPERC_THRESHOLD0 HWMPERC_THRESHOLD + +#ifdef INSIDE_OPENCL +#pragma OPENCL EXTENSION cl_khr_fp64: enable +#define local_barrier() barrier(CLK_LOCAL_MEM_FENCE) +#define global_barrier() barrier(CLK_GLOBAL_MEM_FENCE) +#else +//double get_features(register struct ocas_ptrs *PTRS,register int c,register int weekind,register int numfeatures,register double *features,register double *model,register double *addptr,register struct baserel_info *BR,register double wt); +svmtype *get_jfp_features(register int selector,register int numfeatures,register int c,register int weekind); + +#endif + +#define OCAS_INFINITY (-log(0.0)) +#define OCAS_NEGINFINITY (log(0.0)) +#define OCAS_DECAY .1 +#define OCAS_BIAS 1. +#define OCAS_INDEX(ROW,COL,NUM_ROWS) ((COL)*(NUM_ROWS)+(ROW)) +#define QPSOLVER_MAXITER 1000000 +#define QPSOLVER_MINITER (QPSOLVER_MAXITER * .1) +//#define NUM_CUDA_GROUPS 14 +//#define NUM_BUNDLE_ANSWERS 64 +//#define NUM_TOPCOEFFIS 558 +//#define SMALLVAL .00000000000001 +//#define NUM_PRIMARY_FEATURES 4096 +#ifdef __APPLE__ +#define MAX_OCAS_LHS 512 +#else +#define MAX_OCAS_LHS 2000 +#endif +#define MAX_OCAS_FEATURES (1024 * 1024) + +struct ptrhdr { long allocsize; void *ptr; int devid; char str[64]; }; + +struct ocas_lhsbuffers +{ + double H[MAX_OCAS_LHS * MAX_OCAS_LHS]; + double alpha[MAX_OCAS_LHS],b[MAX_OCAS_LHS],diag_H[MAX_OCAS_LHS],A0[MAX_OCAS_LHS],d[MAX_OCAS_LHS]; + double full_A[]; +}; + +struct ocas_CLtmpspace +{ + double2 hpfb[MAX_VECTORS]; + int poslist[MAX_VECTORS],neglist[MAX_VECTORS]; +}; + +struct ocas_CLbuffers +{ + double output_pred[MAX_VECTORS],old_output[MAX_VECTORS]; + double W[MAX_OCAS_FEATURES+4],oldW[MAX_OCAS_FEATURES+4],new_a[MAX_OCAS_FEATURES+4]; +}; + +struct ocas_vars +{ + struct ptrhdr pH[256]; + double Q_P[TRADEBOTS_NUMANSWERS],Q_D[TRADEBOTS_NUMANSWERS]; + double netcuts[TRADEBOTS_NUMANSWERS],perc[TRADEBOTS_NUMANSWERS],hwmperc[TRADEBOTS_NUMANSWERS],lastmetrics[TRADEBOTS_NUMANSWERS][2]; + double learningrates[TRADEBOTS_NUMANSWERS][2],maxiters[TRADEBOTS_NUMANSWERS],dot_prod_WoldW[TRADEBOTS_NUMANSWERS],cutsum[TRADEBOTS_NUMANSWERS]; + double sq_norm_oldW[TRADEBOTS_NUMANSWERS],oldW0[TRADEBOTS_NUMANSWERS],W0[TRADEBOTS_NUMANSWERS],sq_norm_W[TRADEBOTS_NUMANSWERS]; + double predabs[TRADEBOTS_NUMANSWERS],predsum[TRADEBOTS_NUMANSWERS],dist[TRADEBOTS_NUMANSWERS]; + double xi[TRADEBOTS_NUMANSWERS],pratio[TRADEBOTS_NUMANSWERS],errperc[TRADEBOTS_NUMANSWERS],hwmdist[TRADEBOTS_NUMANSWERS]; + double answerabsaves[TRADEBOTS_NUMANSWERS],answeraves[TRADEBOTS_NUMANSWERS]; + int answercounts,firstweekinds[TRADEBOTS_NUMANSWERS]; + int posA[TRADEBOTS_NUMANSWERS],negA[TRADEBOTS_NUMANSWERS]; + int numIt[TRADEBOTS_NUMANSWERS],numlhs[TRADEBOTS_NUMANSWERS],nNZAlpha[TRADEBOTS_NUMANSWERS],trn_err[TRADEBOTS_NUMANSWERS]; + int qp_exitflag[TRADEBOTS_NUMANSWERS],exitflag[TRADEBOTS_NUMANSWERS],len[TRADEBOTS_NUMANSWERS]; + int have_pendingmodel[TRADEBOTS_NUMANSWERS],cutlen[TRADEBOTS_NUMANSWERS],good[TRADEBOTS_NUMANSWERS],bad[TRADEBOTS_NUMANSWERS]; + int nohwm[TRADEBOTS_NUMANSWERS],numposcuts[TRADEBOTS_NUMANSWERS],numnegcuts[TRADEBOTS_NUMANSWERS]; + struct ocas_CLbuffers *CLspaces[TRADEBOTS_NUMANSWERS]; + struct ocas_lhsbuffers *lhs[TRADEBOTS_NUMANSWERS]; + int *weekinds[TRADEBOTS_NUMANSWERS]; + float *answers,**features; + //unsigned long CLallocsize,tmpallocsize,tmpCLallocsize; + double C,TolRel,TolAbs,MaxTime,QPBound,QPSolverTolAbs,QPSolverTolRel; + double output_time,sort_time,add_time,w_time,qp_solver_time,ocas_time; // total time spent in svm_ocas_solver + int selector,numthreads,starti,modelind,c,refc,maxlen,numfeatures,firstweekind,startweekind,endweekind,numptrs,maxlhs; +}; + +/////////////////////////// Most of runtime is in the add/dot functions +#ifdef INSIDE_OPENCL +typedef double svmtype; + +// numCLthreads: NUM_CUDA_CORES, numgroups: (numdocs + NUM_CUDA_CORES-1)/NUM_CUDA_CORES +__kernel void dot_featuresCL(__local void *lp,__global double *preds,int numfeatures,__global double *W,double W0,__global svmtype *matrix,int numdocs) +{ + register int i,j,docid; + register double sum; + register __global svmtype *features; + if ( (docid = (int)get_global_id(0)) < numdocs ) + { + sum = 0.; + features = &matrix[docid * numfeatures]; + for (i=0; i>= 1; + if ( docid < numdocs ) + { + features = &matrix[docid * numfeatures]; + if ( dir == 0 ) + sum += CONDITION(features[j]); + else sum -= CONDITION(features[j]); + } + } + new_a[j] = sum; + } +} + +#else + +void ocas_purge(struct ocas_vars *vars) +{ + int32_t i; + free(vars->answers); + for (i=0; imaxlen; i++) + if ( vars->features[i] != 0 ) + free(vars->features[i]); + free(vars->features); + for (i=0; iCLspaces[i] != 0 ) + myaligned_free(vars->CLspaces[i],sizeof(*vars->CLspaces[i])); + if ( vars->lhs[i] != 0 ) + myaligned_free(vars->lhs[i],sizeof(*vars->lhs[i]) + vars->numfeatures*vars->maxlhs*sizeof(double)); + if ( vars->weekinds[i] != 0 ) + free(vars->weekinds[i]); + } + free(vars); +} + +/*static inline double dot_expanded_features(register double *W,register int c,register int selector,register int numfeatures) + { + fatal("dot_expanded_features not implemented"); + return(0); + } + + static inline void add_expanded_features(register double *W,register double y,register int c,register int selector,register int numfeatures) + { + fatal("add_expanded_features not implemented"); + }*/ + +static inline double calc_ocas_output(register struct ocas_vars *vars,register int selector,register int c,register int weekind,register int answerind,register double *W,register double W0,register int numfeatures) +{ + register svmtype *features; + register double feature,y,sum = 0.; + register int coeffi; + if ( (y= vars->answers[(weekind-vars->starti)*TRADEBOTS_NUMANSWERS + answerind]) != 0.f ) + { + if ( (features= vars->features[weekind-vars->starti]) != 0 )//get_jfp_features(vars->selector,numfeatures,c,weekind)) != 0 ) + { +#ifdef OCAS_USE_TOPCOEFFIS + for (int i=0; iselector,numfeatures); + sum = y * (W0 + sum); + } + // printf("%f ",sum); + return(sum); +} + +static inline void add_ocas_output(register double y,register struct ocas_vars *vars,register int selector,register int c,register int weekind,register int answerind,register double *W,register double *new_a,register int numfeatures) +{ + register int coeffi; + register svmtype *features,feature; + if ( y != 0 ) + { + if ( (features= vars->features[weekind-vars->starti]) != 0 )//get_jfp_features(vars->selector,numfeatures,c,weekind)) != 0 ) + { + //features = get_jfp_features(vars->selector,numfeatures,c,weekind); +#ifdef OCAS_USE_TOPCOEFFIS + int32_t i; + for (i=0; iselector,numfeatures); + } +} + +static inline void STocas_calc_outputs(register struct ocas_vars *vars,register int c,register int answerind,register double *output,register double *old_output,register double *W,register double W0,register int numfeatures,register int *weekinds,register int numdocs) +{ + register int i,j; + //vars->good[answerind] = vars->bad[answerind] = 0; + //printf("start STocas_calc_outputs.(%p %s.A%d %p) %p %p %p\n",vars,CONTRACTS[c],answerind,weekinds,output,old_output,W); + for (i=0; iselector,c,weekinds[i],answerind,W,W0,numfeatures); + if ( 1 && isnan(output[i]) != 0 ) + { + svmtype *features = vars->features[weekinds[i]-vars->starti];//get_jfp_features(vars->selector,numfeatures,c,weekinds[i]); + if ( features != 0 ) + { + for (j=0; janswers[(i-vars->starti)*TRADEBOTS_NUMANSWERS + answerind]*output[i] <= 0 ) + { + if ( vars->answers[(i-vars->starti)*TRADEBOTS_NUMANSWERS + answerind] != 0.f ) + printf("(%f %f) ",vars->answers[(i-vars->starti)*TRADEBOTS_NUMANSWERS + answerind],output[i]); + } + //printf("[%f %f] ",vars->answers[(i-vars->starti)*TRADEBOTS_NUMANSWERS + answerind],output[i]); + } + } + //printf("finish STocas_calc_outputs\n"); +} + +static inline void STocas_add_newcuts(register struct ocas_vars *vars,register int answerind,register int numfeatures,register int *weekinds,register int *new_cut,register int numcuts,register double *W,register double *new_a) +{ + register int weekind,dir,i,c = vars->c; + memset(new_a,0,sizeof(*new_a) * numfeatures); + //printf("STocas_add_newcuts numcuts.%d numfeatures.%d\n",numcuts,numfeatures); + for (i=0; i>= 1; + add_ocas_output(dir==0?1:-1,vars,vars->selector,c,weekind,answerind,W,new_a,numfeatures); + } +} +//////////////////////////// end of add/dot functions +static inline double _dbufave(register double *buf,register int len) +{ + register int i,n; + register double sum; + sum = 0.; + n = 0; + for (i=0; i 0.0000000001 ) + { + n++; + sum += buf[i]; + } + } + if ( n != 0 ) + sum /= n; + if ( fabs(sum) <= 0.0000000001 ) + sum = 0.; + return(sum); +} + +static inline void add_newcut_entry(register struct ocas_vars *vars,register int answerind,register int *new_cut,register int i,register int weekind,register double y) +{ + weekind <<= 1; + if ( y > 0 ) vars->numposcuts[answerind]++; + else if ( y < 0 ) vars->numnegcuts[answerind]++, weekind |= 1; + new_cut[vars->cutlen[answerind]++] = weekind; +} + +static inline double validate_ocas_model(register struct ocas_vars *vars,register int answerind,register double *output_pred,register double *old_output,register int *weekinds,register int numdocs,register double *W,register double W0,register int numfeatures,register int paranoid) +{ + register svmtype *features; + register double y,pred,perc,answer=0.,feature; + register int i,j,pos,neg,good,bad,oldcuts,training_errors,weekind,nonz,posA,negA; + for (i=pos=neg=good=bad=oldcuts=training_errors=posA=negA=0; ianswers[(weekind-vars->starti)*TRADEBOTS_NUMANSWERS + answerind]) != 0. ) + { + if ( y > 0 ) posA++; + else if ( y < 0 ) negA++; + if ( paranoid != 0 ) + { + pred = 0.; + if ( (features= vars->features[weekind-vars->starti]) != 0 )//get_jfp_features(vars->selector,numfeatures,vars->c,weekind)) != 0 ) + { + for (j=nonz=0; j SMALLVAL ) + pred += W0; + } + else pred = 0;//dot_expanded_features(W,c,selector,numfeatures); + if ( output_pred[i] != 0 && fabs(pred - output_pred[i]) > .000001 ) + // if ( (rand() % 10000) == 0 ) + printf("i.%d A %9.6f pred %9.6f != output_pred %9.6f [%14.10f]\n",i,answer,pred,output_pred[i],pred-output_pred[i]); + } + else pred = output_pred[i], nonz = numfeatures; + if ( nonz != 0 ) + { + if ( pred > 0 ) pos++; + else if ( pred < 0 ) neg++; + if ( pred*y > 0 ) good++; + else if ( pred*y < 0 ) bad++; + } + if ( old_output[i] <= 1. ) + { + oldcuts++; + if ( old_output[i] <= 0. ) + training_errors++; + } + } + } + nonz = 0; + for (i=0; i>>>>> %d.A%02d.(+%-6d -%-6d oc.%-6d | good.%-6d bad.%-6d >>>>> %6.2f%% <<<<<).%-6d | W0 %9.6f W[%d] %9.6f | A +%-6d -%-6d | paranoid.%d numdocs.%d\n",c_to_refc(vars->c),answerind,pos,neg,oldcuts,good,bad,perc,training_errors,W0,nonz,_dbufave(W,numfeatures),posA,negA,paranoid,numdocs); + return(perc); +} + +static int _increasing_double(const void *a,const void *b) +{ +#define double_a (*(double *)a) +#define double_b (*(double *)b) + if ( double_b > double_a ) + return(-1); + else if ( double_b < double_a ) + return(1); + return(0); +#undef double_a +#undef double_b +} + +static inline void calc_ocas_strategy(register struct ocas_vars *vars,register int answerind,register double C,register int numfeatures,register int len,register int *weekinds,register int *new_cut,register double *W,register double *oldW,register double *output,register double *old_output,register double2 *hpfb) +{ + double answermag; + register int i,j,num_hp,good,bad,zero; + register double Bval,Cval,newoutput,W0,oldW0,sq_norm_W,A0val,B0,dist,GradVal,t,t1,t2,t_new,val,GradVal_new,y,starttime,*preds = output; + num_hp = 0; + W0 = vars->W0[answerind]; oldW0 = vars->oldW0[answerind]; + A0val = vars->sq_norm_W[answerind] - (2. * vars->dot_prod_WoldW[answerind]) + vars->sq_norm_oldW[answerind]; + B0 = (vars->dot_prod_WoldW[answerind] - vars->sq_norm_oldW[answerind]); + GradVal = B0; + for (i=0; ianswers[(weekinds[i]-vars->starti)*TRADEBOTS_NUMANSWERS + answerind]) != 0.f ) + { + svmtype *features = vars->features[weekinds[i]-vars->starti];//get_jfp_features(vars->selector,numfeatures,vars->c,weekinds[i]); + //printf("i.%d weekind.%d starti.%d y %f %p\n",i,weekinds[i],vars->starti,y,features); + if ( 0 && features != 0 ) + { + double oldsum=oldW0,sum=W0; + for (j=0; j .000001 ) + { + printf("A%d numIt.%d docid.%-6d sum %11.7f * y%2.0f %11.7f != %11.7f output [%11.7f] W0 %11.7f oldW0 %11.7f\n",answerind,vars->numIt[answerind],i,sum,y,sum*y,output[i],output[i]-sum*y,W0,oldW0); + //output[i] = sum*y; + } + if ( fabs(oldsum*y - old_output[i]) > .000001 ) + { + if ( old_output[i] != 0 && oldW0 != 0 && (rand() % 1000) == 0 ) + printf("A%d numIt.%d docid.%-6d oldsum %11.7f * y%2.0f %11.7f != %11.7f oldoutput [%11.7f] W0 %11.7f oldW0 %11.7f\n",answerind,vars->numIt[answerind],i,oldsum,y,oldsum*y,old_output[i],old_output[i]-oldsum*y,W0,oldW0); + old_output[i] = oldsum*y; + } + } + Cval = C * (1. - old_output[i]); + Bval = C * (old_output[i] - output[i]); + if ( Bval != 0 ) + val = -(Cval / Bval); + else val = OCAS_NEG_INF; + if ( val > 0 ) + { + hpfb[num_hp].y = Bval; + hpfb[num_hp].x = val; + num_hp++; + } + if ( (Bval < 0 && val > 0) || (Bval > 0 && val <= 0) ) + GradVal += Bval; + } + } + //printf("num_hp.%d\n",num_hp); + t = 0; + if ( GradVal < 0 ) + { + starttime = OS_milliseconds(); + qsort(hpfb,num_hp,sizeof(double2),_increasing_double); + //ocas_sort(hpfb,num_hp); + i = 0; + while ( GradVal < 0 && i < num_hp ) + { + t_new = hpfb[i].x; + GradVal_new = GradVal + fabs(hpfb[i].y) + A0val*(t_new - t); + if ( GradVal_new >= 0 ) + t = t + GradVal * (t - t_new) / (GradVal_new - GradVal); + else t = t_new, i++; + GradVal = GradVal_new; + } + vars->sort_time += OS_milliseconds() - starttime; + } + t = MAX(t,0.); // just sanity check; t < 0 should not occur + t1 = t; // new (best so far) W + t2 = t + OCAS_DECAY*(1. - t); // new cutting plane + W0 = oldW0 * (1. - t) + (t * W0); + sq_norm_W = W0 * W0; + for (j=0; jW0[answerind] = W0; vars->sq_norm_W[answerind] = sq_norm_W; + vars->trn_err[answerind] = 0; dist = 0.; + for (i=good=bad=zero=0; ianswers[(weekinds[i]-vars->starti)*TRADEBOTS_NUMANSWERS + answerind]) != 0.f ) + { + answermag = fabs(y); // 1.; + if ( (old_output[i] * (1. - t2) + t2*output[i]) <= answermag ) //1. + add_newcut_entry(vars,answerind,new_cut,i,weekinds[i],y); + newoutput = (old_output[i] * (1. - t1)) + (t1 * output[i]); + if ( 0 ) // won't match unless old_output corresponds with features*oldW + { + double sum=W0; + svmtype *features = vars->features[weekinds[i]-vars->starti];//get_jfp_features(vars->selector,numfeatures,vars->c,weekinds[i]); + if ( features != 0 ) + { + for (j=0; j .0000001 ) + printf("numIt.%d docid.%-6d w%-6d sum %11.7f * y%2.0f %11.7f != %11.7f newoutput [%11.7f] W0 %11.7f oldW0 %11.7f\n",vars->numIt[answerind],i,weekinds[i],sum,y,sum*y,newoutput,newoutput-sum*y,W0,oldW0); + newoutput = sum*y; + } + } + if ( newoutput <= answermag ) + { + vars->xi[answerind] += (answermag - newoutput); + if ( newoutput <= 0. ) + vars->trn_err[answerind]++; + } + preds[i] = y * newoutput; + dist += fabs(preds[i] - y); + old_output[i] = newoutput; + if ( newoutput > 0. ) good++; + else if ( newoutput < 0. ) + { + bad++; + //printf("(%f %f) ",y,newoutput); + } + } else zero++;//,printf("i.%d -> w%d | zeroes.%d good.%d bad.%d of len.%d\n",i,weekinds[i],zero,good,bad,len); + } + //printf("finished strategy\n"); + vars->good[answerind] = good; vars->bad[answerind] = bad; vars->dist[answerind] = dist / MAX(1,good+bad); + vars->perc[answerind] = (100. * (double)vars->good[answerind]) / MAX(1,good+bad); + if ( vars->perc[answerind] > vars->hwmperc[answerind] || (vars->perc[answerind] == vars->hwmperc[answerind] && (vars->hwmdist[answerind] == 0 || vars->dist[answerind] < vars->hwmdist[answerind])) ) + { + double set_ocas_model(int refc,int answerind,double *W,double W0,int numfeatures,int firstweekind,int len,int bad,double dist,double predabs,int posA,int negA,double answerabs,double aveanswer); + vars->W0[answerind] = set_ocas_model(vars->refc,answerind,vars->CLspaces[answerind]->W,vars->W0[answerind],vars->numfeatures,vars->firstweekind,vars->len[answerind],vars->trn_err[answerind],vars->dist[answerind],vars->predabs[answerind],vars->posA[answerind],vars->negA[answerind],vars->answerabsaves[answerind],0.); + vars->nohwm[answerind] = 0; + vars->hwmperc[answerind] = vars->perc[answerind]; vars->hwmdist[answerind] = vars->dist[answerind]; + } + else vars->nohwm[answerind]++; + //printf("good.%d bad.%d zero.%d errors.%d | selector.%d\n",good,bad,zero,vars->trn_err[answerind],vars->selector); +} + +static inline double ocas_splx_solver(register int *nonzalphap,register int maxlhs,register double *d,register double *activeH,register double *diag_H,register double *f,register double C,register double *alpha,register int n,register int MaxIter,register double TolAbs,register double TolRel,register double QP_TH) +{ + register double *col_u,*col_v; + register double QP,QD,lastQD,tmp,diff,distA,distB,etaA,etaB,improv,tmp_num,delta,x_neq0,xval,dval,diagval,tmp_den,tau; + register int u=0,v=0,i,j,iter,nonzalpha,unlikely = 0; + QP = distA = distB = OCAS_PLUS_INF; lastQD = QD = OCAS_NEG_INF; + x_neq0 = C; + etaA = etaB = 0.; + for (i=0; i 0. ) + { + col_u = &activeH[maxlhs * i]; + for (j=0; j 10*(MaxIter-iter) && etaB > 10*(MaxIter-iter) ) + unlikely++; + else unlikely = 0; + } else unlikely = 0; + diff = (QP - QD); + if ( 0 && (diff <= fabs(QP)*TolRel || diff <= TolAbs || QP <= QP_TH || unlikely > 100) ) + { + if ( 0 ) + { + if ( diff <= fabs(QP)*TolRel ) + printf("caseA %f | ",diff - fabs(QP)*TolRel); + else if ( diff <= TolAbs ) + printf("caseB %f | ",diff - TolAbs); + else if ( etaA > 2*(MaxIter-iter) && etaB > 2*(MaxIter-iter) ) + printf("caseC etas %f %f | ",etaA,etaB); + printf("%d: QP %f QD %f diff %f n.%d d0 %9.6f dA %9.6f %9.6f dB %9.6f %9.6f\n",iter,QP,QD,QP-QD,n,d[0],distA,etaA,distB,etaB); + } + break; + } + distA = (diff - fabs(QP)*TolRel); + distB = (diff - TolAbs); + lastQD = QD; + if ( d[u] > 0 ) + u = -1; + else delta -= C * d[u]; + // if satisfied then k-th block of variables needs update + if ( delta > TolAbs && delta > (TolRel * fabs(QP)) ) + { + // for fixed u select v = argmax_{i in I_k} Improvement(i) + improv = OCAS_NEG_INF; + for (i=0; i<=n; i++) + { + if ( i == u || (xval = ((i 0 ) + { + tmp_num = xval * dval; + if ( tmp_num < tmp_den ) + tmp = tmp_num*tmp_num / tmp_den; + else tmp = tmp_num - .5 * tmp_den; + if ( 0 && i < n ) // jimbo tweak + { + tmp = alpha[i] * MIN(1.,tmp_num/tmp_den); + alpha[i] -= tmp; + if ( u == -1 ) + x_neq0 += tmp; + else alpha[u] += tmp; + } + if ( tmp > improv ) + { + improv = tmp; + tau = MIN(1.,tmp_num/tmp_den); + v = i; + } + } + } + // update d = H*x + f + if ( v < n ) + { + tau *= alpha[v]; + alpha[v] -= tau; + if ( u != -1 ) + { + alpha[u] += tau; + col_v = &activeH[maxlhs * v]; + for (i=0; isq_norm_oldW[answerind] = vars->sq_norm_W[answerind]; + oldW0 = vars->oldW0[answerind] = vars->W0[answerind]; + W0 = 0.; + for (i=0; ialpha[i]) > 0 ) + { + //printf("%9.6f ",alpha); + for (j=0; jfull_A[OCAS_INDEX(j,i,numfeatures)]; + W0 += lhs->A0[i] * alpha; + } + } + vars->W0[answerind] = W0; + sq_norm_W = W0 * W0; + dot_prod_WoldW = W0 * oldW0; + for (j=0; jalpha,numlhs),W0,sq_norm_W,answerind); + vars->dot_prod_WoldW[answerind] = dot_prod_WoldW; + vars->sq_norm_W[answerind] = sq_norm_W; +} + +static inline void ocas_update_Lspace(register struct ocas_vars *vars,register int answerind,register double netcuts,register double cut_length,register int numfeatures,register double C,register double QPSolverTolAbs,register double QPSolverTolRel) +{ + register struct ocas_CLbuffers *ptr = vars->CLspaces[answerind]; + register struct ocas_lhsbuffers *lhs = vars->lhs[answerind]; + register double *new_col_H; + register double sq_norm_a,maxiters,metric,tmp; + register int i,j,iters,numlhs,maxlhs = vars->maxlhs; + numlhs = vars->numlhs[answerind]; + new_col_H = &lhs->H[OCAS_INDEX(0,numlhs,maxlhs)]; + lhs->A0[numlhs] = netcuts; + lhs->b[numlhs] = -cut_length; + sq_norm_a = lhs->A0[numlhs] * lhs->A0[numlhs]; + for (j=0; jfull_A[OCAS_INDEX(j,numlhs,numfeatures)] = ptr->new_a[j]; + if ( fabs(ptr->new_a[j]) > 1000 ) + { + //printf("(%d %9.6f %f) ",j,ptr->new_a[j],sq_norm_a); + ptr->new_a[j] = 0.; + } + else + sq_norm_a += ptr->new_a[j] * ptr->new_a[j]; + ptr->oldW[j] = ptr->W[j]; ptr->W[j] = 0.; + } + new_col_H[numlhs] = sq_norm_a; + //printf("QPsolver.A%02d: ABS %f Rel %.11f numlhs.%d cutlen.%f netcuts.%f sq_norm_a %f netcuts.%f\n",answerind,QPSolverTolAbs,QPSolverTolRel,vars->numlhs[answerind],cut_length,lhs->A0[numlhs],sq_norm_a,netcuts); + for (i=0; iA0[numlhs] * lhs->A0[i]; + for (j=0; jnew_a[j] * lhs->full_A[OCAS_INDEX(j,i,numfeatures)]; + new_col_H[i] = tmp; + } + lhs->d[numlhs] = lhs->alpha[numlhs] = 0.; + lhs->diag_H[numlhs] = lhs->H[OCAS_INDEX(numlhs,numlhs,maxlhs)]; + for (i=0; i H[OCAS_INDEX(numlhs,i,maxlhs)] = lhs->H[OCAS_INDEX(i,numlhs,maxlhs)]; + numlhs = ++vars->numlhs[answerind]; + iters = vars->numIt[answerind]; + if ( vars->nohwm[answerind] > 3 ) + vars->maxiters[answerind] *= 1 + sqrt(vars->nohwm[answerind])/100; + else if ( vars->nohwm[answerind] == 0 ) + vars->maxiters[answerind] *= .5; + if ( vars->maxiters[answerind] > QPSOLVER_MAXITER ) + vars->maxiters[answerind] = QPSOLVER_MAXITER; + if ( vars->maxiters[answerind] < QPSOLVER_MINITER ) + vars->maxiters[answerind] = QPSOLVER_MINITER; + maxiters = MAX(QPSOLVER_MINITER,vars->maxiters[answerind]); + vars->Q_D[answerind] = ocas_splx_solver(&vars->nNZAlpha[answerind],maxlhs,lhs->d,lhs->H,lhs->diag_H,lhs->b,C,lhs->alpha,vars->numlhs[answerind],MIN(maxiters,QPSOLVER_MAXITER),QPSolverTolAbs,QPSolverTolRel,OCAS_NEG_INF); + metric = ((double)vars->len[answerind] / MAX(1,vars->trn_err[answerind])) / 1.; + vars->lastmetrics[answerind][iters & 1] = metric; + update_ocas_model(ptr->W,ptr->oldW,vars,numfeatures,answerind,lhs,numlhs); +} + +static inline void start_ocas_iter(register struct ocas_vars *vars,register int c,register int answerind) +{ + if ( vars->pratio[answerind] == 0. ) + vars->pratio[answerind] = vars->answerabsaves[answerind]; + vars->good[answerind] = -1; + vars->bad[answerind] = vars->trn_err[answerind] = vars->cutlen[answerind] = vars->numposcuts[answerind] = vars->numnegcuts[answerind] = 0; + vars->xi[answerind] = vars->predsum[answerind] = vars->dist[answerind] = vars->cutsum[answerind] = vars->netcuts[answerind] = 0.; +} + +static void ocas_print(struct ocas_vars *vars,int answerind,int ishwm,double C) +{ + int i; + double dispvals[4]; + //printf("ocas_print.A%d\n",answerind); + //printf("%s.A%02d %4d %8.2f | QP %9.3f QD %9.3f [%9.4f %7.3f] SV.%d %4d | M%9.6f (%9.6f max %8.1f %9.6f) %s.A%02d %9.6f%%\n", + dispvals[0] = vars->Q_P[answerind]/1000000000.; dispvals[1] = (C * vars->Q_D[answerind])/1000000000.; + dispvals[2] = (vars->Q_P[answerind]-C * vars->Q_D[answerind]) / 1000000000; + dispvals[3] = (vars->Q_P[answerind]-C * vars->Q_D[answerind]) / MAX(1,fabs(vars->Q_P[answerind])); + printf("%3d %d.A%02d +%d -%d",vars->nohwm[answerind],vars->refc,answerind,vars->good[answerind],vars->bad[answerind]); + printf(" %4d %8.2f |QP %9.3f QD %10.2f [%11.2f %9.1f] SV.%3d %3d |M%9.3f errs.%-6d %-8.0f %5.2f%% errs %6.5f A%9.6f W0%9.6f D%11.9f\n",//[%7.4f%%]\n", + vars->numIt[answerind],vars->ocas_time/1000,dispvals[0],dispvals[1],dispvals[2],dispvals[3], + vars->nNZAlpha[answerind], vars->numlhs[answerind], + // PTRS->lastmetrics[answerind],PTRS->learningrates[answerind][0],PTRS->maxiters[answerind],PTRS->learningrates[answerind][1], + vars->lastmetrics[answerind][0],vars->trn_err[answerind],vars->maxiters[answerind],vars->perc[answerind], + vars->errperc[answerind]/100,_dbufave(vars->CLspaces[answerind]->new_a,vars->numfeatures),vars->W0[answerind], + vars->dist[answerind]/vars->answerabsaves[answerind]);//_dbufave(vars->hwmperc,81));//,vars->errperc+vars->perc); + for (i=0; i<4; i++) + if ( isnan(dispvals[i]) != 0 ) + break; + if ( vars->lastmetrics[answerind][0] > 10 ) + usleep(vars->lastmetrics[answerind][0] * vars->lastmetrics[answerind][0]); + /*if ( 0 && i < 4 )//|| (vars->answerind >= 32 && fabs(vars->W0) > .9) ) + { + int save_model(int refc,int answerind,double *W,int numfeatures,double W0,double perc,int posA,int negA); + memset(&vars->CLspaces[answerind],0,sizeof(vars->CLspaces[answerind])); + vars->W0[answerind] = vars->oldW0[answerind] = 0.; + vars->numIt[answerind] = 0; + vars->perc[answerind] = vars->hwmperc[answerind] = 0.; + printf("reset model %s.A%02d\n",CONTRACTS[vars->selector!=3?vars->refc:NUM_COMBINED],answerind); + save_model(vars->refc,answerind,vars->CLspaces[answerind]->W,vars->numfeatures,vars->W0[answerind],vars->perc[answerind]); + }*/ +} + +static inline void finish_ocasiter(register int answerind,register struct ocas_vars *vars,register double C) +{ + register double den; + vars->have_pendingmodel[answerind] = 0; + if ( vars->good[answerind] == 0 && vars->bad[answerind] == 0 ) + { + vars->bad[answerind] = vars->trn_err[answerind]; + vars->good[answerind] = (vars->len[answerind] - vars->trn_err[answerind]); + } + den = MAX(1.,vars->good[answerind]+vars->bad[answerind]); + if ( (vars->predabs[answerind] = (vars->predsum[answerind] / den)) != 0. ) + vars->pratio[answerind] = (vars->answerabsaves[answerind] / vars->predabs[answerind]); + else vars->pratio[answerind] = 0.; + vars->dist[answerind] = sqrt(vars->dist[answerind] / den); + //printf("W0 %9.6f pred sum %f %f | pratio %f distsum %f (%f vs hwm %f)\n",vars->W0[answerind],vars->predsum[answerind],vars->predabs[answerind],vars->pratio[answerind],vars->dist[answerind],vars->perc[answerind],vars->hwmperc[answerind]); + vars->errperc[answerind] = (100 * (double)vars->trn_err[answerind])/(double)MAX(1,vars->len[answerind]); + vars->Q_P[answerind] = 0.5*vars->sq_norm_W[answerind] + (C * vars->xi[answerind]); + vars->ocas_time = (vars->output_time + vars->w_time + vars->add_time + vars->sort_time + vars->qp_solver_time); + ocas_print(vars,answerind,0,C); +} + +static inline int ocas_iter(struct ocas_vars *vars,int max_nohwm) +{ + int Method = 1; + int min_nohwm = 1; + int skipflags[84]; + static int new_cut[MAX_VECTORS]; + static double2 hpfb[MAX_VECTORS]; + int inactives[81]; + register struct ocas_CLbuffers *ptr; + register double netcuts,startmilli,y,psum,pcount,nosum; + register int i,numfeatures,cutlen,lastanswerind,lwm,numactive,numthreads,answerind,*weekinds; + numactive = 0; + if ( (numfeatures= vars->numfeatures) > MAX_OCAS_FEATURES ) + { + printf("numfeatures > MAX_OCAS_FEATURES\n"); + exit(-1); + } + psum = pcount = nosum = 0; + { + //printf("c.%d mask %lx %p\n",c,contractmask,PTRS->ocas[c]); + memset(inactives,0,sizeof(inactives)); + lastanswerind = TRADEBOTS_NUMANSWERS; + numfeatures = vars->numfeatures; + answerind = 0; + //printf("numIt.%d ocas iter.%s A.mask%lx len.%d CLspace.%p lhs.%p | vars.%p\n",vars->numIt[answerind],CONTRACTS[c_to_refc(c)],answerindmask,vars->len[answerind],vars->CLspaces[answerind],vars->lhs[answerind],vars); + memset(skipflags,0,sizeof(skipflags)); + for (answerind=0; answerindCLspaces[answerind],vars->weekinds[answerind],vars->nohwm[answerind],max_nohwm); + if ( vars->hwmperc[answerind] != 0 ) + { + nosum += vars->nohwm[answerind]; + pcount++, psum += vars->hwmperc[answerind]; + } + //printf("answerind.%d\n",answerind); + if ( vars->len[answerind] == 0 || vars->CLspaces[answerind] == 0 || (vars->nohwm[answerind] > min_nohwm && vars->hwmperc[answerind] > ((answerind==0) ? HWMPERC_THRESHOLD0 : HWMPERC_THRESHOLD)) ) + { + inactives[answerind] = 1; + continue; + } + if ( vars->nohwm[answerind] < max_nohwm ) + { + numactive++; + if ( vars->numIt[answerind]++ == 0 ) + { + for (i=0; iCLspaces[answerind]->W[i] != 0 ) + break; + if ( i == numfeatures ) + skipflags[answerind] = 1; + } + ptr = vars->CLspaces[answerind]; + weekinds = vars->weekinds[answerind]; + //printf("start iter %p %p\n",ptr,weekinds); + start_ocas_iter(vars,vars->c,answerind); + numthreads = vars->numthreads; + if ( skipflags[answerind] != 0 ) + { + for (i=0; ilen[answerind]; i++) + { + if ( (y= vars->answers[(weekinds[i]-vars->starti)*TRADEBOTS_NUMANSWERS + answerind]) != 0.f ) + { + ptr->output_pred[i] = 0; + add_newcut_entry(vars,answerind,new_cut,i,weekinds[i],y); + } + } + fprintf(stderr,"skip %d.A%02d cuts +%d -%d, ",c_to_refc(vars->c),answerind,vars->numposcuts[answerind],vars->numnegcuts[answerind]); + } + else + { + startmilli = OS_milliseconds(); + //printf("%s ocas_calc_outputs.A%d len.%d | numthreads.%d\n",CONTRACTS[c_to_refc(c)],answerind,vars->len[answerind],numthreads); + STocas_calc_outputs(vars,vars->c,answerind,ptr->output_pred,ptr->old_output,ptr->W,vars->W0[answerind],numfeatures,weekinds,vars->len[answerind]); + //ocas_calc_outputs(PTRS,numthreads,vars,c,answerind,ptr->output_pred,ptr->old_output,ptr->W,vars->W0[answerind],numfeatures,weekinds,vars->len[answerind]); + vars->output_time += (OS_milliseconds() - startmilli); + if ( Method != 0 ) + { + startmilli = OS_milliseconds(); + //printf("%d calc_ocas_strategy.A%d len.%d | numthreads.%d\n",c_to_refc(vars->c),answerind,vars->len[answerind],numthreads); + calc_ocas_strategy(vars,answerind,vars->C,numfeatures,vars->maxlen,weekinds,new_cut,ptr->W,ptr->oldW,ptr->output_pred,ptr->old_output,hpfb); + vars->w_time += (OS_milliseconds() - startmilli); + } + finish_ocasiter(answerind,vars,vars->C); + } + //printf("%s calc ocas_add_newcuts.A%d poscuts.%d negcuts.%d | numthreads.%d\n",CONTRACTS[c_to_refc(c)],answerind,vars->numposcuts[answerind],vars->numnegcuts[answerind],vars->numthreads); + startmilli = OS_milliseconds(); + //if ( vars->nohwm[answerind] > 13 ) + // numthreads = vars->numthreads;///MAIN_MAXCORES; + memset(ptr->new_a,0,sizeof(ptr->new_a)); + //ocas_add_newcuts(PTRS,numthreads,vars,answerind,numfeatures,weekinds,new_cut,vars->numposcuts[answerind]+vars->numnegcuts[answerind],ptr->W,ptr->new_a); + STocas_add_newcuts(vars,answerind,numfeatures,weekinds,new_cut,vars->numposcuts[answerind]+vars->numnegcuts[answerind],ptr->W,ptr->new_a); + vars->add_time += (OS_milliseconds() - startmilli); +//printf("done %d calc ocas_add_newcuts.A%d poscuts.%d negcuts.%d | good.%d bad.%d\n",c_to_refc(vars->c),answerind,vars->numposcuts[answerind],vars->numnegcuts[answerind],vars->good[answerind],vars->bad[answerind]); + } else inactives[answerind] = 1;//, printf("maxnohwm.%d\n",max_nohwm); + } + startmilli = OS_milliseconds(); + for (answerind=0; answerindnumposcuts[answerind] - vars->numnegcuts[answerind]); + cutlen = (vars->numposcuts[answerind] + vars->numnegcuts[answerind]); + if ( vars->nohwm[answerind] < lwm ) + lwm = vars->nohwm[answerind]; + ocas_update_Lspace(vars,answerind,netcuts,cutlen,numfeatures,vars->C,0.,vars->QPSolverTolRel); + } + vars->qp_solver_time += (OS_milliseconds() - startmilli); + } + if ( pcount != 0 ) + printf("numactive.%d %.0f | ave perf %f%% | ave nohwm %.1f\n",numactive,pcount,psum/pcount,nosum/pcount); + return(numactive); +} + +static inline int init_ocas_vars(int numthreads,int selector,long answerindmask,struct ocas_vars *vars,int c,double C,int numfeatures,int maxlhs,int maxlen,int len,double *answerabsave,int *posA,int *negA) +{ + int answerind,lastanswerind,retval = 0; + lastanswerind = TRADEBOTS_NUMANSWERS; + vars->maxlen = maxlen; + vars->numthreads = numthreads; + vars->selector = selector; + //printf("init_ocas_vars lastanswerind.%d\n",lastanswerind); + for (answerind=0; answerindlen[answerind]); + if ( vars->len[answerind] > 0 )//&& (answerindmask == -1L || ((1L<refc = c_to_refc(c); vars->c = c; vars->C = C; + vars->numfeatures = numfeatures; vars->maxlhs = maxlhs; + if ( vars->CLspaces[answerind] == 0 ) + vars->CLspaces[answerind] = myaligned_alloc(sizeof(*vars->CLspaces[answerind])); + vars->answerabsaves[answerind] = answerabsave[answerind]; + vars->posA[answerind] = posA[answerind]; vars->negA[answerind] = negA[answerind]; + if ( vars->lhs[answerind] == 0 ) + vars->lhs[answerind] = myaligned_alloc(sizeof(*vars->lhs[answerind]) + numfeatures*maxlhs*sizeof(double)); + vars->maxiters[answerind] = QPSOLVER_MINITER; vars->trn_err[answerind] = vars->len[answerind]; vars->Q_P[answerind] = 0.5*vars->sq_norm_W[answerind] + C*vars->len[answerind]; + vars->perc[answerind] = vars->hwmperc[answerind] = vars->dist[answerind] = vars->hwmdist[answerind] = 0.; vars->numIt[answerind] = 0; + //printf("init.A%d %d | %p %p weekinds.%p\n",answerind,vars->len[answerind],vars->CLspaces[answerind],vars->lhs[answerind],vars->weekinds[answerind]); + } + } + //printf("mask.%lx init_ocas_vars selector.%d weekinds[0].%p\n",answerindmask,selector,vars->weekinds[0]); + return(retval); +} + +void ocas_init(struct ocas_vars *vars,int32_t c,int32_t numfeatures,int32_t starti,int32_t endi) +{ + struct ocas_CLbuffers *ptr; struct ocas_lhsbuffers *lhs; + int32_t nonz,weekind,answerind; double answer,y; + if ( numfeatures < 0 ) + return; + vars->maxlhs = MAX_OCAS_LHS; + vars->numfeatures = numfeatures; + vars->maxlen = (endi - starti + 1); + vars->C = 1.; + vars->c = c; + vars->TolRel = 0.01; + vars->TolAbs = 0.0; + vars->QPSolverTolRel = vars->TolRel*0.5; + vars->QPSolverTolAbs = vars->TolAbs*0.5; + vars->MaxTime = OCAS_INFINITY; + vars->QPBound = 0.0; + memset(vars->posA,0,sizeof(vars->posA)); + memset(vars->negA,0,sizeof(vars->negA)); + memset(vars->answeraves,0,sizeof(vars->answeraves)); + memset(vars->answerabsaves,0,sizeof(vars->answerabsaves)); + memset(vars->firstweekinds,0,sizeof(vars->firstweekinds)); + vars->starti = starti; + vars->answers = calloc(TRADEBOTS_NUMANSWERS,sizeof(vars->answers)*vars->maxlen); + vars->features = calloc(vars->maxlen,sizeof(*vars->features)); + for (answerind=0; answerindweekinds[answerind] = calloc(vars->maxlen,sizeof(*vars->weekinds[answerind])); + for (weekind=starti; weekind<=endi; weekind++) + { + if ( (vars->features[weekind - starti]= get_features(numfeatures,c,weekind)) == 0 ) + continue; + for (answerind=0; answerindposA[answerind]+vars->negA[answerind]) >= vars->maxlen ) + continue; + if ( (y= get_yval(&answer,0,weekind,c,answerind)) != 0.f ) + { + vars->answers[(weekind-starti)*TRADEBOTS_NUMANSWERS + answerind] = y; + vars->weekinds[answerind][vars->len[answerind]++] = weekind; + if ( vars->posA[answerind]+vars->negA[answerind] == 0 ) + vars->firstweekinds[answerind] = weekind; + vars->answeraves[answerind] += answer; + if ( answer > 0 ) + { + vars->posA[answerind]++; + vars->answerabsaves[answerind] += answer; + } + else if ( answer < 0 ) + { + vars->negA[answerind]++; + vars->answerabsaves[answerind] -= answer; + } + } + } + } + for (answerind=0; answerindposA[answerind]+vars->negA[answerind])) != 0 ) + { + vars->answerabsaves[answerind] /= nonz; + vars->answeraves[answerind] /= nonz; + printf("A%02d.(%9.6f %d %d) ",answerind,vars->answerabsaves[answerind],vars->posA[answerind],vars->negA[answerind]); + } + init_ocas_vars(1,0,-1,vars,c,vars->C,numfeatures,MAX_OCAS_LHS,vars->maxlen,vars->maxlen,vars->answerabsaves,vars->posA,vars->negA); + for (answerind=0; answerindlen[answerind]); + lhs = vars->lhs[answerind]; + ptr = vars->CLspaces[answerind]; + //printf("%d.A%d call init ocas vars weekinds[0] %p numfeatures.%d (%p %p)\n",c_to_refc(vars->c),answerind,vars->weekinds[0],numfeatures,lhs,ptr); + if ( lhs == 0 || ptr == 0 ) + continue; + vars->numlhs[answerind] = 0;//init_full_A(lhs->full_A,vars->numfeatures,c,answerind,models); + memset(ptr->W,0,sizeof(*ptr->W) * numfeatures); + memset(ptr->oldW,0,sizeof(*ptr->oldW) * numfeatures); + vars->W0[answerind] = vars->oldW0[answerind] = 0; +#ifdef ENABLE_EXISTINGMODEL + double init_model(double *percp,double *W,double *oldW,int c,int answerind,int numfeatures); + vars->W0[answerind] = init_model(&vars->hwmperc[answerind],ptr->W,ptr->oldW,c,answerind,vars->numfeatures); + if ( _dbufave(ptr->W,numfeatures) != 0 && bitweight(answerindmask) == 1 ) + validate_ocas_model(vars,answerind,ptr->output_pred,ptr->old_output,vars->weekinds[answerind],vars->len[answerind],ptr->W,vars->W0[answerind],numfeatures,1); +#endif + //printf("%s.A%d call init ocas vars weekinds[0] %p\n",CONTRACTS[c_to_refc(c)],answerind,vars->weekinds[0]); + } + vars->output_time = vars->sort_time = vars->w_time = vars->qp_solver_time = vars->ocas_time = vars->add_time = 0; + vars->startweekind = starti; vars->endweekind = endi; +} + +int32_t ocas_gen(int32_t c,int32_t numfeatures,int32_t starti,int32_t endi) +{ + int32_t i; struct ocas_vars *vars = calloc(1,sizeof(*vars)); + ocas_init(vars,c,numfeatures,starti,endi); + for (i=0; i<100; i++) + ocas_iter(vars,10); + ocas_purge(vars); + return(0); +} +#endif + diff --git a/basilisk/tradebots_liquidity.c b/basilisk/tradebots_liquidity.c new file mode 100755 index 000000000..f2d8a1b5d --- /dev/null +++ b/basilisk/tradebots_liquidity.c @@ -0,0 +1,1129 @@ +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +// included from basilisk.c +/* + In order to provide liquidity from central exchanges, we need to issue balancing trades, however to do this properly, we need to know what the desired balance is. If unspecified, then a neutral balance is assumed. + The liquidity_info interface is quite flexible, there is a single function liquidity_active() which returns non-zero if the LP node should respond. The model is that the liquidity_command() is used to configure the liquidity_active()'s response. + In order for dynamic adaptiveness to work, the liquidity_command/liquidity_active needs to interact with the balancing. + A simplistic default trio of functions are provided, but any level of complexity is possible with the liquidity interface. + */ + +#define TRADEBOTS_NUMANSWERS 8 +#define TRADEBOTS_NUMDECAYS 8 +#define TRADEBOTS_RAWFEATURESINCR 7 +#define TRADEBOTS_MAXPAIRS 1024 + +#define _OCAS_PLUS_INF (-log(0.0)) +double OCAS_PLUS_INF,OCAS_NEG_INF; + +double Tradebots_decays[TRADEBOTS_NUMDECAYS] = { 0.5, 0.666, 0.8, 0.9, 0.95, 0.99, 0.995, 0.999 }; +int32_t Tradebots_answergaps[TRADEBOTS_NUMANSWERS] = { 60, 60, 120, 120, 240, 240, 720, 720 }; + +struct tradebot_arbentry +{ + char exchange[16]; + double price,volume,profitmargin; + uint32_t timestamp; +}; + +struct tradebot_arbexchange +{ + char name[16]; + struct tradebot_arbentry trades[2]; +}; + +struct tradebot_arbpair +{ + char base[32],rel[32]; + uint32_t lasttime,lastanswertime; FILE *fp; + int32_t numexchanges,counter,btccounter,usdcounter,cnycounter,refc; + double highbid,lowask,hblavolume,btcbid,btcask,btcvol,usdbid,usdask,usdvol,cnybid,cnyask,cnyvol; + double bidaves[TRADEBOTS_NUMDECAYS],askaves[TRADEBOTS_NUMDECAYS]; + double bidslopes[TRADEBOTS_NUMDECAYS],askslopes[TRADEBOTS_NUMDECAYS]; + struct tradebot_arbexchange exchanges[16]; + uint8_t dirmasks[2],slopedirs[2]; + char *svmpairs[TRADEBOTS_MAXPAIRS][2]; + int32_t RTgood[TRADEBOTS_NUMANSWERS],RTbad[TRADEBOTS_NUMANSWERS],numrawfeatures,numsvmfeatures,numpairs; + float rawfeatures[TRADEBOTS_NUMANSWERS+64],prevrawfeatures[60 * TRADEBOTS_NUMANSWERS+64],*svms; // svms is coeffs vector[TRADEBOTS_NUMANSWERS] + float RTpreds[TRADEBOTS_NUMANSWERS],svmpreds[TRADEBOTS_NUMANSWERS],answers[TRADEBOTS_NUMANSWERS]; +}; +struct tradebot_arbpair Arbpairs[TRADEBOTS_MAXPAIRS],*Pair_NXTBTC,*Pair_BTCUSD,*Pair_BTCCNY; +int32_t Tradebot_numarbpairs; + +struct tradebot_arbpair *tradebots_arbpair_find(char *base,char *rel) +{ + int32_t i; + for (i=0; i0; i--) + memcpy(&pair->prevrawfeatures[i*72],&pair->prevrawfeatures[(i-1)*72],sizeof(pair->rawfeatures)); + memcpy(pair->prevrawfeatures,pair->rawfeatures,sizeof(pair->rawfeatures)); + memset(pair->rawfeatures,0,sizeof(pair->rawfeatures)); + if ( fabs(pair->highbid) < SMALLVAL || fabs(pair->lowask) < SMALLVAL ) + return(-1); + ave = _pairaved(pair->highbid,pair->lowask); + timestamp = (uint32_t)time(NULL); + n = TRADEBOTS_NUMANSWERS; + memcpy(&pair->rawfeatures[TRADEBOTS_NUMANSWERS],×tamp,sizeof(*pair->rawfeatures)), n++; + pair->rawfeatures[n++] = pair->highbid; + pair->rawfeatures[n++] = pair->lowask; + pair->rawfeatures[n++] = pair->hblavolume / ave; + n = TRADEBOTS_RAWFEATURESINCR + TRADEBOTS_NUMANSWERS; + for (i=0; ibidaves[i]) < SMALLVAL || fabs(pair->askaves[i]) < SMALLVAL ) + return(-1); + starti = n; + pair->rawfeatures[n++] = (pair->bidaves[i] / ave) - 1.; + pair->rawfeatures[n++] = (pair->askaves[i] / ave) - 1.; + pair->rawfeatures[n++] = 10000. * (pair->bidslopes[i] / ave); + pair->rawfeatures[n++] = 10000. * (pair->askslopes[i] / ave); + if ( n < starti+TRADEBOTS_RAWFEATURESINCR ) + n = starti+TRADEBOTS_RAWFEATURESINCR; + } + if ( pair->fp != 0 ) + { + if ( fwrite(pair->rawfeatures,1,sizeof(pair->rawfeatures),pair->fp) != sizeof(pair->rawfeatures) ) + printf("fwrite error for %s/%s rawfeatures[%d]\n",pair->base,pair->rel,n); + else fflush(pair->fp); + } + if ( n > sizeof(pair->rawfeatures)/sizeof(*pair->rawfeatures) ) + { + printf("n.%d too many for rawfeatures %ld\n",n,sizeof(pair->rawfeatures)/sizeof(*pair->rawfeatures)); + exit(-1); + } + return(n); +} + +uint32_t tradebots_featureset(double *highbidp,double *lowaskp,double *avep,double *volp,double *bidaves,double *askaves,double *bidslopes,double *askslopes,float *rawfeatures) +{ + uint32_t timestamp; int32_t i,n,starti; + memcpy(×tamp,&rawfeatures[TRADEBOTS_NUMANSWERS],sizeof(timestamp)); + n = TRADEBOTS_NUMANSWERS + 1; + *highbidp = rawfeatures[n++]; + *lowaskp = rawfeatures[n++]; + *avep = _pairaved(*highbidp,*lowaskp); + *volp = rawfeatures[n++]; + //printf("[%9.6f %9.6f] vol %f t.%u\n",*highbidp,*lowaskp,*volp,timestamp); + n = TRADEBOTS_RAWFEATURESINCR + TRADEBOTS_NUMANSWERS; + for (i=0; irefc = Tradebot_numarbpairs++; + strcpy(pair->rel,rel); + strcpy(pair->base,base); + if ( strcmp(base,"NXT") == 0 && strcmp(rel,"BTC") == 0 ) + Pair_NXTBTC = pair, printf("Pair_NXTBTC <- %p\n",pair); + else if ( strcmp(base,"BTC") == 0 && strcmp(rel,"USD") == 0 ) + Pair_BTCUSD = pair; + else if ( strcmp(base,"BTC") == 0 && strcmp(rel,"CNY") == 0 ) + Pair_BTCCNY = pair; + sprintf(fname,"SVM/rawfeatures/%s_%s",base,rel); + pair->fp = OS_appendfile(fname); + if ( (ftell(pair->fp) % sizeof(pair->rawfeatures)) != 0 ) + { + printf("misalinged rawfeatures %ld %ld\n",ftell(pair->fp),(ftell(pair->fp) % sizeof(pair->rawfeatures))); + } + fseek(pair->fp,(ftell(pair->fp) / sizeof(pair->rawfeatures)) * sizeof(pair->rawfeatures) - sizeof(pair->rawfeatures),SEEK_SET); + if ( fread(pair->rawfeatures,1,sizeof(pair->rawfeatures),pair->fp) == sizeof(pair->rawfeatures) ) + { + pair->lasttime = tradebots_featureset(&pair->highbid,&pair->lowask,&ave,&pair->hblavolume,pair->bidaves,pair->askaves,pair->bidslopes,pair->askslopes,pair->rawfeatures); + printf("%s/%s [%.8f %.8f] %u\n",pair->base,pair->rel,pair->highbid,pair->lowask,pair->lasttime); + } + return(pair); + } else return(0); +} + +int32_t tradebots_expandrawfeatures(double *svmfeatures,float *rawfeatures,uint32_t reftimestamp,float *refrawfeatures) +{ + double factor,highbid,lowask,ave,vol,bidaves[TRADEBOTS_NUMDECAYS],askaves[TRADEBOTS_NUMDECAYS],bidslopes[TRADEBOTS_NUMDECAYS],askslopes[TRADEBOTS_NUMDECAYS]; + double refhighbid,reflowask,refave,refvol,refbidaves[TRADEBOTS_NUMDECAYS],refaskaves[TRADEBOTS_NUMDECAYS],refbidslopes[TRADEBOTS_NUMDECAYS],refaskslopes[TRADEBOTS_NUMDECAYS]; + uint32_t timestamp; int32_t i,j,starti,n = 0; + tradebots_featureset(&refhighbid,&reflowask,&refave,&refvol,refbidaves,refaskaves,refbidslopes,refaskslopes,refrawfeatures); + timestamp = tradebots_featureset(&highbid,&lowask,&ave,&vol,bidaves,askaves,bidslopes,askslopes,rawfeatures); + if ( timestamp == 0 || reftimestamp == 0 || timestamp >= reftimestamp+60 ) + { + //printf("tradebots_expandrawfeatures: timestamp.%u vs reftimestamp.%u\n",timestamp,reftimestamp); + return(-1); + } + factor = sqrt(reftimestamp - timestamp); + if ( factor > 60. ) + factor = 60.; + else if ( factor < 1. ) + factor = 1.; + factor = 1. / factor; + if ( refhighbid == 0. || highbid == 0. || lowask == 0. || reflowask == 0. ) + { + //printf("tradebots_expandrawfeatures: (%f %f) ref (%f %f)\n",highbid,lowask,refhighbid,reflowask); + return(-1); + } + svmfeatures[n++] = highbid; + svmfeatures[n++] = (highbid / ave) - 1.; + svmfeatures[n++] = lowask; + svmfeatures[n++] = (lowask / ave) - 1.; + svmfeatures[n++] = (lowask - highbid); + svmfeatures[n++] = (lowask - highbid) / ave; + svmfeatures[n++] = vol; + starti = n; + svmfeatures[n++] = refhighbid; + svmfeatures[n++] = (refhighbid / refave) - 1.; + svmfeatures[n++] = reflowask; + svmfeatures[n++] = (reflowask / refave) - 1.; + svmfeatures[n++] = (reflowask - refhighbid); + svmfeatures[n++] = (reflowask - refhighbid) / refave; + svmfeatures[n++] = refvol; + for (i=0; i SMALLVAL ) + { + for (i=starti; i 0. ) + svmfeatures[i] = cbrt(svmfeatures[i]); + else svmfeatures[i] = -cbrt(-svmfeatures[i]); + } + } + return(n); +} + +int32_t tradebots_calcsvmfeatures(double *svmfeatures,struct tradebot_arbpair *pair,float *rawfeatures,float *prevrawfeatures) +{ + int32_t i,j,n,numpairfeatures,flag; struct tradebot_arbpair *ptr; uint32_t reftimestamp; + memcpy(&reftimestamp,&rawfeatures[TRADEBOTS_NUMANSWERS],sizeof(reftimestamp)); + if ( reftimestamp == 0 ) + { + printf("reftimestamp.%u is illegal\n",reftimestamp); + return(-1); + } + numpairfeatures = n = tradebots_expandrawfeatures(svmfeatures,prevrawfeatures,reftimestamp,rawfeatures); + if ( n <= 0 ) + return(-1); + for (i=0; i<60; i++,n+=numpairfeatures) + tradebots_expandrawfeatures(&svmfeatures[n],&prevrawfeatures[i*72],reftimestamp,rawfeatures); + if ( 0 && pair->numsvmfeatures != (1+pair->numpairs)*n ) + { + for (i=0; inumpairs; i++) // need to do lookups + { + flag = -1; + if ( (ptr= tradebots_arbpair_find(pair->svmpairs[i][0],pair->svmpairs[i][1])) != 0 ) + flag = tradebots_expandrawfeatures(&svmfeatures[n],ptr->rawfeatures,reftimestamp,rawfeatures); + if ( flag < 0 ) + { + for (j=0; jsvms != 0 ) + { + for (i=n=0; inumsvmfeatures; i++) + { + feature = svmfeatures[i]; + for (j=0; jsvms[n++]; + } + } + return(n); +} + +void tradebots_calcanswers(struct tradebot_arbpair *pair) +{ + double highbid,lowask,futurebid,futureask,ave,vol,bidaves[TRADEBOTS_NUMDECAYS],askaves[TRADEBOTS_NUMDECAYS],bidslopes[TRADEBOTS_NUMDECAYS],askslopes[TRADEBOTS_NUMDECAYS]; + float rawfeatures[sizeof(pair->rawfeatures)/sizeof(*pair->rawfeatures)],futuremin,futuremax,minval,maxval,*hblas = 0; + uint32_t timestamp,firsttime = 0; long fpos,savepos; int32_t flag,i,iter,j,ind,maxi; + OCAS_PLUS_INF = _OCAS_PLUS_INF; OCAS_NEG_INF = -_OCAS_PLUS_INF; + if ( pair->fp != 0 ) + { + for (iter=0; iter<2; iter++) + { + rewind(pair->fp); + fpos = 0; + while ( fread(rawfeatures,1,sizeof(pair->rawfeatures),pair->fp) == sizeof(pair->rawfeatures) ) + { + savepos = ftell(pair->fp); + timestamp = tradebots_featureset(&highbid,&lowask,&ave,&vol,bidaves,askaves,bidslopes,askslopes,rawfeatures); + //printf("timestamp.%u firsttime.%u\n",timestamp,firsttime); + if ( timestamp == 0 ) + continue; + if ( firsttime == 0 ) + { + firsttime = timestamp; + maxi = (int32_t)((time(NULL) - firsttime) / 60 + 1); + hblas = calloc(maxi,sizeof(*hblas)*2); + printf("HBLAS[%d] allocated\n",maxi); + } + if ( (i= (timestamp - firsttime)/60) >= 0 && i < maxi ) + { + if ( iter == 0 ) + { + if ( hblas[i << 1] == 0 ) + { + hblas[i << 1] = highbid; + hblas[(i << 1) + 1] = lowask; + } + else + { + _xblend(&hblas[i << 1],highbid,0.5); + _xblend(&hblas[(i << 1) + 1],lowask,0.5); + } + } + else + { + highbid = hblas[i << 1]; + lowask = hblas[(i << 1) + 1]; + if ( fabs(highbid) > SMALLVAL && fabs(lowask) > SMALLVAL ) + { + memset(pair->answers,0,sizeof(pair->answers)); + flag = 0; + for (j=0; janswers[j] = _pairaved(futuremax,futuremin) - _pairaved(minval,maxval); + else + { + futurebid = hblas[ind << 1]; + futureask = hblas[(ind << 1) + 1]; + minval = MIN(highbid,lowask); + maxval = MAX(highbid,lowask); + futuremin = MIN(futurebid,futureask); + futuremax = MAX(futurebid,futureask); + if ( futuremin > maxval ) + { + if ( futuremax < minval ) + printf("%s/%s A%d: highly volatile minmax.(%f %f) -> (%f %f) %d of %d\n",pair->base,pair->rel,j,minval,maxval,futuremin,futuremax,i,maxi); + else + { + pair->answers[j] = (futuremin - maxval); + flag++; + } + } + else if ( futuremax < minval ) + pair->answers[j] = (futuremax - minval), flag++; + //printf("i.%d j.%d gap.%d ind.%d answer %9.6f (%f %f) -> (%f %f)\n",i,j,Tradebots_answergaps[j],ind,pair->answers[j],minval,maxval,futuremin,futuremax); + } + } + } + if ( flag != 0 ) + { + fseek(pair->fp,fpos,SEEK_SET); + if ( fwrite(pair->answers,1,sizeof(pair->answers),pair->fp) != sizeof(pair->answers) ) + printf("error writing answers for %s/%s t%u i.%d of %d\n",pair->base,pair->rel,timestamp,i,maxi); + else if ( 0 ) + { + for (j=0; janswers[j]); + printf("%s/%s answers %d of %d\n",pair->base,pair->rel,i,maxi); + } + fseek(pair->fp,savepos,SEEK_SET); + } + } + } + } + fpos = ftell(pair->fp); + } + if ( iter == 0 ) + { + if ( hblas == 0 ) + break; + highbid = hblas[0]; + lowask = hblas[1]; + for (i=1; i SMALLVAL && fabs(hblas[(i << 1) + 1]) > SMALLVAL ) + { + highbid = hblas[i << 1]; + lowask = hblas[(i << 1) + 1]; + } + else + { + hblas[i << 1] = highbid; + hblas[(i << 1) + 1] = lowask; + } + //printf("%9.6f ",_pairaved(highbid,lowask)); + } + //printf("maxi.%d\n",maxi); + } + } + if ( hblas != 0 ) + free(hblas); + } + if ( pair->fp != 0 && (ftell(pair->fp) % sizeof(pair->rawfeatures)) != 0 ) + printf("ERROR: %s/%s not on feature boundary\n",pair->base,pair->rel); +} + +double get_yval(double *answerp,int32_t selector,int32_t ind,int32_t refc,int32_t answerind) +{ + float answer; struct tradebot_arbpair *pair; long savepos; + pair = &Arbpairs[refc]; + if ( pair->fp != 0 ) + { + savepos = ftell(pair->fp); + fseek(pair->fp,ind*sizeof(pair->rawfeatures)+answerind*sizeof(*pair->rawfeatures),SEEK_SET); + if ( fread(&answer,1,sizeof(answer),pair->fp) != sizeof(answer) ) + answer = 0; + fseek(pair->fp,savepos,SEEK_SET); + if ( isnan(answer) != 0 ) + return(0); + answer /= 10.; + /*if ( answer > .01 ) + answer = .01; + else if ( answer < -.01 ) + answer = -.01; + if ( answerp != 0 ) + *answerp = answer; + */ + if ( answer > 0. ) + answer = sqrt(answer); + else answer = -sqrt(-answer); + *answerp = answer; + /*if ( answer > 0. ) + return(1.); + else if ( answer < 0. ) + return(-1.);*/ + return(answer); + } + return(0.); +} + +float *get_features(int32_t numfeatures,int32_t refc,int32_t ind) +{ + struct tradebot_arbpair *pair; long savepos; int32_t i,n; double svmfeatures[32768]; + float rawfeatures[sizeof(pair->rawfeatures)],prevrawfeatures[60 * sizeof(pair->rawfeatures)],*svmf=0; + pair = &Arbpairs[refc]; + pair->numsvmfeatures = numfeatures; + if ( pair->fp != 0 && ind > 61 ) + { + savepos = ftell(pair->fp); + fseek(pair->fp,(ind-60)*sizeof(pair->rawfeatures),SEEK_SET); + if ( fread(prevrawfeatures,60,sizeof(pair->rawfeatures),pair->fp) == sizeof(pair->rawfeatures) && fread(&rawfeatures,1,sizeof(pair->rawfeatures),pair->fp) == sizeof(pair->rawfeatures) ) + { + n = tradebots_calcsvmfeatures(svmfeatures,pair,rawfeatures,prevrawfeatures); + //int32_t nonz; for (i=nonz=0; i %d\n",ind,n); + if ( n != pair->numsvmfeatures ) + { + //printf("unexpected numsvmfeatures refc.%d ind.%d %d vs %d\n",refc,ind,n,pair->numsvmfeatures); + return(0); + } + svmf = calloc(n,sizeof(*svmf)); + for (i=0; ifp,savepos,SEEK_SET); + } + return(svmf); +} + +double set_ocas_model(int refc,int answerind,double *W,double W0,int numfeatures,int firstweekind,int len,int bad,double dist,double predabs,int posA,int negA,double answerabs,double aveanswer) +{ + int32_t i,nonz=0; + for (i=0; i perc ) + { + if ( (_posA+_negA) != 0 && _posA >= posA && _negA >= negA ) + { + memcpy(W,tmpW,sizeof(*W)*numfeatures); + printf("%s.A%02d numfeatures.%d posA.%d negA.%d saved model %f is better than %f +A%d -A%d\n",CONTRACTS[refc],answerind,numfeatures,_posA,_negA,fileperc,perc,posA,negA); + return(tmpW[numfeatures]); + } + } +#endif + save_model(refc,answerind,W,numfeatures,W0,perc,posA,negA); + return(W0); +}*/ + +#ifndef _WIN +#include "tradebots_SVM.h" +#endif + +static char *assetids[][2] = +{ + { "12071612744977229797", "UNITY" }, + { "15344649963748848799", "DEX" }, + { "6883271355794806507", "PANGEA" }, + { "17911762572811467637", "JUMBLR" }, + { "17083334802666450484", "BET" }, + { "13476425053110940554", "CRYPTO" }, + { "6932037131189568014", "HODL" }, + { "3006420581923704757", "SHARK" }, + { "17571711292785902558", "BOTS" }, + { "10524562908394749924", "MGW" }, +}; + +uint64_t NXT_assetidfind(char *base) +{ + int32_t i; + for (i=0; iexchange[0] == 0 ) + strcpy(arb->exchange,exchange); + if ( strcmp(arb->exchange,exchange) == 0 ) + { + arb->price = price; + arb->volume = volume; + arb->timestamp = timestamp; + arb->profitmargin = profitmargin; + } else printf("mismatched arbexchange? (%s vs %s)\n",arb->exchange,exchange); +} + +struct tradebot_arbexchange *tradebots_arbexchange_find(struct tradebot_arbpair *pair,char *exchange) +{ + int32_t i; + if ( pair->numexchanges > sizeof(pair->exchanges)/sizeof(*pair->exchanges) ) + { + printf("data corruption pair.%p %s %s/%s numexchanges.%d\n",pair,exchange,pair->base,pair->rel,pair->numexchanges); + return(0); + } + for (i=0; inumexchanges; i++) + if ( strcmp(pair->exchanges[i].name,exchange) == 0 ) + return(&pair->exchanges[i]); + return(0); +} + +struct tradebot_arbexchange *tradebots_arbexchange_create(struct tradebot_arbpair *pair,char *exchange) +{ + if ( pair->numexchanges < sizeof(pair->exchanges)/sizeof(*pair->exchanges) ) + { + strcpy(pair->exchanges[pair->numexchanges].name,exchange); + return(&pair->exchanges[pair->numexchanges++]); + } else return(0); +} + +void tradebot_arbcandidate(struct supernet_info *myinfo,char *exchange,int32_t tradedir,char *base,char *rel,double price,double volume,uint32_t timestamp,double profitmargin) +{ + int32_t i,offset,flag; double highbid,lowask,lastbid,lastask,arbval; uint32_t now; + struct tradebot_arbentry *bid,*ask; struct tradebot_arbexchange *arbex; struct tradebot_arbpair *pair = 0; + if ( strcmp(rel,"BTC") != 0 && strcmp(rel,"NXT") != 0 && strcmp(rel,"USD") != 0 && strcmp(rel,"CNY") != 0 ) + { + printf("reject non-BTC arbcandidate (%s/%s)\n",base,rel); + return; + } + offset = (tradedir > 0) ? 0 : 1; + if ( (pair= tradebots_arbpair_find(base,rel)) == 0 ) + pair = tradebots_arbpair_create(base,rel); + if ( pair == 0 ) + { + printf("cant get pair for %s %s/%s\n",exchange,base,rel); + return; + } + if ( (arbex= tradebots_arbexchange_find(pair,exchange)) == 0 ) + arbex = tradebots_arbexchange_create(pair,exchange); + if ( arbex != 0 ) + { + //printf("cand.%d %16s %s %12.6f (%5s/%-5s) at %12.8f profit %.03f\n",pair->numexchanges,exchange,tradedir<0?"ask":"bid",volume,base,rel,price,profitmargin); + tradebot_arbentry(&arbex->trades[offset],exchange,price,volume,timestamp,profitmargin); + bid = ask = 0; + pair->highbid = pair->lowask = highbid = lowask = 0.; + now = (uint32_t)time(NULL); + //if ( pair->numexchanges >= 2 ) + { + for (i=0; inumexchanges; i++) + { + arbex = &pair->exchanges[i]; + if ( arbex->trades[0].price != 0. && (highbid == 0. || arbex->trades[0].price >= highbid) ) + { + bid = &arbex->trades[0]; + if ( now > bid->timestamp+30 ) + bid->price = 0.; + else highbid = bid->price; + } + if ( arbex->trades[1].price != 0. && (lowask == 0. || arbex->trades[1].price <= lowask) ) + { + ask = &arbex->trades[1]; + if ( now > ask->timestamp+30 ) + ask->price = 0.; + else lowask = ask->price; + } + //printf("%p %s %s %f %f -> %p %p %f %f (%f %f)\n",pair,pair->base,arbex->name,arbex->trades[0].price,arbex->trades[1].price,bid,ask,highbid,lowask,pair->highbid,pair->lowask); + } + flag = 0; + if ( Pair_NXTBTC != 0 && pair->btccounter != Pair_NXTBTC->counter ) + flag |= 1; + if ( Pair_BTCUSD != 0 && pair->usdcounter != Pair_BTCUSD->counter ) + flag |= 2; + if ( Pair_BTCCNY != 0 && pair->cnycounter != Pair_BTCCNY->counter ) + flag |= 4; + //printf("%s %s/%s flag.%d (%d %d) %p %p\n",exchange,base,rel,flag,pair->btccounter,Pair_NXTBTC!=0?Pair_NXTBTC->counter:-1,bid,ask); + if ( bid != 0 && ask != 0 && bid->price != 0. && ask->price != 0 && (now - bid->timestamp) < 30 && (now - ask->timestamp) < 30 && (fabs(bid->price - pair->highbid) > SMALLVAL || fabs(ask->price - pair->lowask) > SMALLVAL || (strcmp(pair->rel,"NXT") == 0 && flag != 0)) ) + { + pair->counter++; + pair->hblavolume = volume = MIN(bid->volume,ask->volume); + arbval = lastbid = lastask = 0.; + memset(pair->dirmasks,0,sizeof(pair->dirmasks)); + memset(pair->slopedirs,0,sizeof(pair->slopedirs)); + if ( strcmp(pair->rel,"NXT") == 0 ) + { + if ( Pair_NXTBTC != 0 && Pair_NXTBTC->highbid != 0. && Pair_NXTBTC->lowask != 0. ) + { + pair->btccounter = Pair_NXTBTC->counter; + pair->btcbid = highbid * Pair_NXTBTC->highbid; + pair->btcask = lowask * Pair_NXTBTC->lowask; + pair->btcvol = volume * _pairaved(pair->btcbid,pair->btcask); + } + } + else if ( strcmp(pair->rel,"BTC") == 0 ) + { + pair->btcbid = highbid; + pair->btcask = lowask; + pair->btcvol = volume; + } + if ( strcmp(pair->rel,"USD") == 0 ) + { + pair->usdbid = highbid; + pair->usdask = lowask; + pair->usdvol = volume; + } + if ( strcmp(pair->rel,"CNY") == 0 ) + { + pair->cnybid = highbid; + pair->cnyask = lowask; + pair->cnyvol = volume; + } + if ( pair->btcbid != 0. && pair->btcask != 0. ) + { + if ( strcmp(pair->rel,"USD") != 0 && Pair_BTCUSD != 0 && Pair_BTCUSD->highbid != 0. && Pair_BTCUSD->lowask != 0. ) + { + pair->usdcounter = Pair_BTCUSD->counter; + pair->usdbid = pair->btcbid * Pair_BTCUSD->highbid; + pair->usdask = pair->btcask * Pair_BTCUSD->lowask; + pair->usdvol = pair->btcvol * _pairaved(pair->usdbid,pair->usdask); + } + if ( strcmp(pair->rel,"CNY") != 0 && Pair_BTCCNY != 0 && Pair_BTCCNY->highbid != 0. && Pair_BTCCNY->lowask != 0. ) + { + pair->cnycounter = Pair_BTCCNY->counter; + pair->cnybid = pair->btcbid * Pair_BTCCNY->highbid; + pair->cnyask = pair->btcask * Pair_BTCCNY->lowask; + pair->cnyvol = pair->btcvol * _pairaved(pair->cnybid,pair->cnyask); + } + } + for (i=0; ibidslopes[i]= dxblend(&pair->bidaves[i],highbid,Tradebots_decays[i])) > 0. ) + pair->slopedirs[0] |= (1 << i); + if ( (pair->askslopes[i]= dxblend(&pair->askaves[i],lowask,Tradebots_decays[i])) > 0. ) + pair->slopedirs[1] |= (1 << i); + lastbid = pair->bidaves[i]; + lastask = pair->askaves[i]; + //printf("(%.8f %.8f) ",lastbid,lastask); + } + for (i=0; i lastbid ) + pair->dirmasks[0] |= (1 << i); + if ( lowask > lastask ) + pair->dirmasks[1] |= (1 << i); + } + else + { + if ( pair->bidaves[i-1] > lastbid ) + pair->dirmasks[0] |= (1 << i); + if ( pair->askaves[i-1] > lastask ) + pair->dirmasks[1] |= (1 << i); + } + } + //printf("%12.6f %7s/%-3s %8s %14.8f %8s %14.8f spread %6.2f%% %02x:%02x %02x:%02x %d\n",volume,base,rel,bid->exchange,highbid,ask->exchange,lowask,100.*(lowask-highbid)/_pairaved(highbid,lowask),pair->dirmasks[0],pair->slopedirs[0],pair->dirmasks[1],pair->slopedirs[1],pair->counter); + //printf("BTC.(%.8f %.8f) %.8f %.8f USD.(%.4f %.4f) CNY.(%.3f %.3f)\n",pair->btcbid,pair->btcask,Pair_BTCUSD!=0?Pair_BTCUSD->highbid:0,Pair_BTCUSD!=0?Pair_BTCUSD->lowask:0,pair->usdbid,pair->usdask,pair->cnybid,pair->cnyask); + } + if ( highbid != 0 ) + pair->highbid = highbid; + if ( lowask != 0 ) + pair->lowask = lowask; + //printf(">>>>>>> %s (%s/%s) BTC %.8f %.8f v%f counter.%d btc.%d (%d)\n",exchange,pair->base,pair->rel,pair->btcbid,pair->btcask,pair->btcvol,pair->counter,pair->btccounter,Pair_NXTBTC!=0?Pair_NXTBTC->counter:-1); + if ( bid != 0 && ask != 0 && highbid != 0. && lowask != 0. && highbid > lowask && strcmp(bid->exchange,ask->exchange) != 0 && strcmp(rel,"BTC") == 0 ) + { + volume = MIN(bid->volume,ask->volume); + if ( volume*_pairaved(highbid,lowask) > 0.1 ) + volume = 0.1 / _pairaved(highbid,lowask); + if ( highbid * (1. - bid->profitmargin) > lowask * (1. + ask->profitmargin) ) + { + arbval = highbid * (1. - bid->profitmargin) - lowask * (1. + ask->profitmargin); + printf(">>>>>>>> FOUND ARB %s/%s highbid.%s %.8f lowask.%s %.8f volume %f (%.8f %.8f) %.4f%%\n",pair->base,pair->rel,bid->exchange,bid->price,ask->exchange,ask->price,volume,highbid,lowask,100.*(highbid-lowask)/_pairaved(highbid,lowask)); + InstantDEX_buy(myinfo,0,0,0,ask->exchange,pair->base,"BTC",ask->price,volume,1); + InstantDEX_sell(myinfo,0,0,0,bid->exchange,pair->base,"BTC",bid->price,volume,1); + printf("finished trades %s/%s volume %f\n",pair->base,pair->rel,volume); + } + } + if ( pair->counter > TRADEBOTS_NUMDECAYS ) + { + if ( pair->lasttime != time(NULL) ) + { + if ( (pair->numrawfeatures= tradebots_calcrawfeatures(pair)) > 0 ) + { + if ( pair->numsvmfeatures != 0 ) + { + if ( myinfo->svmfeatures == 0 ) + myinfo->svmfeatures = calloc(sizeof(*myinfo->svmfeatures),pair->numsvmfeatures); + if ( tradebots_calcsvmfeatures(myinfo->svmfeatures,pair,pair->rawfeatures,pair->prevrawfeatures) > 0 ) + tradebots_calcpreds(pair->RTpreds,pair,myinfo->svmfeatures); + } + } + pair->lasttime = (uint32_t)time(NULL); + } + if ( 0 && time(NULL) > pair->lastanswertime+3600 ) + { + tradebots_calcanswers(pair); + pair->lastanswertime = (uint32_t)time(NULL); + } + } + } + } +} + +void _default_swap_balancingtrade(struct supernet_info *myinfo,struct basilisk_swap *swap,int32_t iambob) +{ + // update balance, compare to target balance, issue balancing trade via central exchanges, if needed + double price,volume,srcamount,destamount,profitmargin,dir=0.,dotrade=1.; char base[64],rel[64]; + srcamount = swap->I.req.srcamount; + destamount = swap->I.req.destamount; + profitmargin = (double)swap->I.req.profitmargin / 1000000.; + if ( srcamount <= SMALLVAL || destamount <= SMALLVAL ) + { + printf("illegal amount for balancing %f %f\n",srcamount,destamount); + return; + } + strcpy(rel,"BTC"); + if ( strcmp(swap->I.req.src,"BTC") == 0 ) + { + strcpy(base,swap->I.req.dest); + price = (srcamount / destamount); + volume = destamount / SATOSHIDEN; + dir = -1.; + } + else if ( strcmp(swap->I.req.dest,"BTC") == 0 ) + { + strcpy(base,swap->I.req.src); + price = (destamount / srcamount); + volume = srcamount / SATOSHIDEN; + dir = 1.; + } + else + { + printf("only BTC trades can be balanced, not (%s/%s)\n",swap->I.req.src,swap->I.req.dest); + return; + } + if ( iambob != 0 ) + { + if ( myinfo->IAMLP != 0 ) + { + printf("BOB: price %f * vol %f -> %s newprice %f margin %.2f%%\n",price,volume,dir < 0. ? "buy" : "sell",price + dir * price * profitmargin,100*profitmargin); + if ( dir < 0. ) + InstantDEX_buy(myinfo,0,0,0,"poloniex",base,rel,price,volume,dotrade); + else InstantDEX_sell(myinfo,0,0,0,"poloniex",base,rel,price,volume,dotrade); + } + } + else + { + if ( myinfo->IAMLP != 0 ) + { + printf("ALICE: price %f * vol %f -> %s newprice %f margin %.2f%%\n",price,volume,dir > 0. ? "buy" : "sell",price - dir * price * profitmargin,100*profitmargin); + if ( dir > 0. ) + InstantDEX_buy(myinfo,0,0,0,"poloniex",base,rel,price,volume,dotrade); + else InstantDEX_sell(myinfo,0,0,0,"poloniex",base,rel,price,volume,dotrade); + } + } +} + +void _default_liquidity_command(struct supernet_info *myinfo,char *base,bits256 hash,cJSON *vals) +{ + struct liquidity_info li,refli; int32_t i; char *exchange,*relstr,numstr[32]; + if ( (exchange= jstr(vals,"exchange")) == 0 ) + exchange = "DEX"; + else if ( strcmp(exchange,"*") == 0 ) + exchange = ""; + else if ( exchanges777_find(exchange) == 0 ) + { + printf("cant find exchange.(%s)\n",exchange); + return; + } + if ( (relstr= jstr(vals,"rel")) == 0 ) + relstr = "BTC"; + if ( base == 0 || base[0] == 0 ) + base = jstr(vals,"base"); + if ( base == 0 || base[0] == 0 ) + return; + memset(&li,0,sizeof(li)); + safecopy(li.base,base,sizeof(li.base)); + safecopy(li.rel,relstr,sizeof(li.rel)); + safecopy(li.exchange,exchange,sizeof(li.exchange)); + li.profit = jdouble(vals,"profit"); + li.refprice = jdouble(vals,"refprice"); + li.dir = jint(vals,"dir"); // positive -> buy, negative -> sell, 0 or missing -> both + // li.theoretical = ... dotproduct + // li.filter = ... + // li.trigger = ... + // PAX response + if ( strcmp("NXT",li.rel) == 0 ) + li.assetid = NXT_assetidfind(base); + else if ( strcmp("UNITY",base) == 0 ) + li.assetid = NXT_assetidfind(base); + if ( strcmp(li.base,"BTC") == 0 && strcmp("USD",li.rel) != 0 && strcmp("CNY",li.rel) != 0 ) + { + printf("unsupported base BTC (%s/%s)\n",li.base,li.rel); + return; + } + if ( strcmp(li.base,"BTC") != 0 && strcmp("BTC",li.rel) != 0 && + strcmp(li.base,"NXT") != 0 && strcmp("NXT",li.rel) != 0 && + strcmp(li.base,"USD") != 0 && strcmp("USD",li.rel) != 0 && + strcmp(li.base,"CNY") != 0 && strcmp("CNY",li.rel) != 0 && + strcmp(li.exchange,"DEX") != 0 ) // filter out most invalids + { + printf("unsupported base/rel %s/%s\n",li.base,li.rel); + return; + } + for (i=0; ilinfos)/sizeof(*myinfo->linfos); i++) + { + refli = myinfo->linfos[i]; + /*if ( strcmp(li.rel,refli.base) == 0 && strcmp(li.base,refli.rel) == 0 ) + { + li = refli; + strcpy(li.base,refli.base); + strcpy(li.rel,refli.rel); + if ( fabs(li.refprice) > SMALLVAL ) + li.refprice = (1. / li.refprice); + else li.refprice = 0.; + li.dir = -li.dir; + printf("Set rev linfo[%d] (%s/%s) %.6f %.8f\n",i,li.base,li.rel,li.profit,li.refprice); + myinfo->linfos[i] = li; + return; + } + else*/ if ( refli.base[0] == 0 || (strcmp(li.base,refli.base) == 0 && strcmp(li.rel,refli.rel) == 0 && strcmp(li.exchange,refli.exchange) == 0) ) + { + if ( refli.base[0] == 0 && li.exchange[0] != 0 && strcmp(li.exchange,"DEX") != 0 ) + { + if ( strcmp("NXT",li.rel) == 0 && li.assetid != 0 ) + { + sprintf(numstr,"%llu",(long long)li.assetid); + printf("monitor %s %s\n",li.rel,numstr); + tradebot_monitor(myinfo,0,0,0,li.exchange,numstr,li.rel,0.); + } else tradebot_monitor(myinfo,0,0,0,li.exchange,li.base,li.rel,0.); + } + myinfo->linfos[i] = li; + printf("Set linfo[%d] %s (%s/%s) %.6f %.8f\n",i,li.exchange,li.base,li.rel,li.profit,li.refprice); + return; + } + } + printf("ERROR: too many linfos %d\n",i); +} + +int32_t _default_volume_ok(struct supernet_info *myinfo,struct liquidity_info *li,int32_t dir,double volume) +{ + // check order exposure + // check cumulative exposure + return(0); +} + +double _default_liquidity_active(struct supernet_info *myinfo,double *refpricep,char *exchange,char *base,char *rel,double volume) +{ + int32_t i,dir; struct liquidity_info refli; + *refpricep = 0.; + //printf("%s %s/%s\n",exchange,base,rel); + for (i=sizeof(myinfo->linfos)/sizeof(*myinfo->linfos)-1; i>=0; i--) + { + refli = myinfo->linfos[i]; + if ( refli.base[0] == 0 ) + continue; + if ( strcmp(base,refli.base) == 0 && strcmp(rel,refli.rel) == 0 ) + dir = 1; + else if ( strcmp(rel,refli.base) == 0 && strcmp(base,refli.rel) == 0 ) + dir = -1; + else dir = 0; + if ( exchange[0] != 0 && refli.exchange[0] != 0 && strcmp(exchange,refli.exchange) != 0 ) + { + //printf("continue %s %s/%s [%d] dir.%d refli.dir %d vs %s %s/%s\n",exchange,base,rel,i,dir,refli.dir,refli.exchange,refli.base,refli.rel); + continue; + } + //printf(">>>>>>>> %s %s/%s [%d] dir.%d refli.dir %d vs %s/%s\n",exchange,base,rel,i,dir,refli.dir,refli.base,refli.rel); + if ( dir != 0 && dir * refli.dir <= 0 ) + { + if ( _default_volume_ok(myinfo,&refli,dir,volume) == 0 ) + { + *refpricep = refli.refprice; + return(refli.profit); + } else break; + } + } + return(0.); +} + +void tradebot_swap_balancingtrade(struct supernet_info *myinfo,struct basilisk_swap *swap,int32_t iambob) +{ + if ( swap->balancingtrade == 0 ) + _default_swap_balancingtrade(myinfo,swap,iambob); + else (*swap->balancingtrade)(myinfo,swap,iambob); +} + +void tradebot_liquidity_command(struct supernet_info *myinfo,char *base,bits256 hash,cJSON *vals) +{ + // processed in LIFO manner which allows to override existing command + if ( myinfo->liquidity_command == 0 ) + _default_liquidity_command(myinfo,base,hash,vals); + else (*myinfo->liquidity_command)(myinfo,base,hash,vals); +} + +double tradebot_liquidity_active(struct supernet_info *myinfo,double *refpricep,char *exchange,char *base,char *rel,double destvolume) +{ + if ( myinfo->liquidity_active == 0 ) + return(_default_liquidity_active(myinfo,refpricep,exchange,base,rel,destvolume)); + else return((*myinfo->liquidity_active)(myinfo,refpricep,exchange,base,rel,destvolume)); +} + +// struct exchange_quote { uint64_t satoshis,orderid,offerNXT,exchangebits; double price,volume; uint32_t timestamp,val; }; + +void tradebots_processprices(struct supernet_info *myinfo,struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t numbids,int32_t numasks) +{ + double price,profitmargin,volume; struct tradebot_arbpair *pair; + if ( strcmp(rel,"NXT") == 0 && strcmp(base,"BTC") != 0 && (base= NXT_assetnamefind(base)) == 0 ) + { + //printf("reject %s %s/%s\n",exchange,base,rel); + return; + } + else if ( strcmp(base,"NXT") == 0 && strcmp(rel,"BTC") != 0 && (rel= NXT_assetnamefind(rel)) == 0 ) + { + //printf("reject %s %s/%s\n",exchange,base,rel); + return; + } + //printf("%s %s/%s bids.%d asks.%d\n",exchange->name,base,rel,numbids,numasks); + if ( numbids > 0 && (volume= bidasks[0].volume) > 0. && (profitmargin= + tradebot_liquidity_active(myinfo,&price,exchange->name,base,rel,volume)) > 0. ) + { + if ( price == 0. ) + price = bidasks[0].price; + tradebot_arbcandidate(myinfo,exchange->name,1,base,rel,price,volume,(uint32_t)time(NULL),profitmargin); + } + if ( numasks > 0 && (volume= bidasks[1].volume) > 0. && (profitmargin= + tradebot_liquidity_active(myinfo,&price,exchange->name,rel,base,volume)) > 0. ) + { + if ( price == 0. ) + price = bidasks[1].price; + tradebot_arbcandidate(myinfo,exchange->name,-1,base,rel,price,volume,(uint32_t)time(NULL),profitmargin); + } + if ( (pair= tradebots_arbpair_find(base,rel)) == 0 ) + pair = tradebots_arbpair_create(base,rel); + if ( pair != 0 ) + { + if ( strcmp(rel,"NXT") == 0 ) + { + if ( pair->btcbid != 0. && pair->btcask != 0. ) + { + tradebot_arbcandidate(myinfo,"arb",1,base,"BTC",pair->btcbid,pair->btcvol,(uint32_t)time(NULL),profitmargin); + tradebot_arbcandidate(myinfo,"arb",-1,base,"BTC",pair->btcask,pair->btcvol,(uint32_t)time(NULL),profitmargin); + } + } + if ( strcmp(rel,"USD") != 0 && pair->usdbid != 0. && pair->usdask != 0. ) + { + tradebot_arbcandidate(myinfo,"arb",1,base,"USD",pair->usdbid,pair->usdvol,(uint32_t)time(NULL),profitmargin); + tradebot_arbcandidate(myinfo,"arb",-1,base,"USD",pair->usdask,pair->usdvol,(uint32_t)time(NULL),profitmargin); + } + if ( strcmp(rel,"CNY") != 0 && pair->cnybid != 0. && pair->cnyask != 0. ) + { + tradebot_arbcandidate(myinfo,"arb",1,base,"CNY",pair->cnybid,pair->cnyvol,(uint32_t)time(NULL),profitmargin); + tradebot_arbcandidate(myinfo,"arb",-1,base,"CNY",pair->cnyask,pair->cnyvol,(uint32_t)time(NULL),profitmargin); + } + } +} + +#include "../includes/iguana_apidefs.h" +#include "../includes/iguana_apideclares.h" + +TWO_STRINGS(tradebots,gensvm,base,rel) +{ +#ifdef _WIN + return(clonestr("{\"error\":\"windows doesnt support SVM\"}")); +#else + int32_t numfeatures = 317*61; + struct tradebot_arbpair *pair; + if ( base[0] != 0 && rel[0] != 0 && (pair= tradebots_arbpair_find(base,rel)) != 0 && pair->fp != 0 ) + { + tradebots_calcanswers(pair); + ocas_gen(pair->refc,numfeatures,0,(int32_t)(ftell(pair->fp) / sizeof(pair->rawfeatures))); + return(clonestr("{\"result\":\"success\"}")); + } else return(clonestr("{\"error\":\"cant find arbpair\"}")); +#endif +} + +#include "../includes/iguana_apiundefs.h" diff --git a/crypto777/OS_nonportable.c b/crypto777/OS_nonportable.c index 52832bba9..9e4e3f281 100755 --- a/crypto777/OS_nonportable.c +++ b/crypto777/OS_nonportable.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/crypto777/OS_portable.c b/crypto777/OS_portable.c index ff16d4a65..7f614b816 100755 --- a/crypto777/OS_portable.c +++ b/crypto777/OS_portable.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -94,6 +94,17 @@ char *OS_portable_path(char *str) #endif } +FILE *OS_appendfile(char *origfname) +{ + char fname[1024]; FILE *fp; + strcpy(fname,origfname); + OS_portable_path(fname); + if ( (fp= fopen(fname,"rb+")) == 0 ) + fp = fopen(fname,"wb"); + else fseek(fp,0,SEEK_END); + return(fp); +} + int32_t OS_portable_renamefile(char *fname,char *newfname) { #ifdef _WIN32 diff --git a/crypto777/OS_portable.h b/crypto777/OS_portable.h index f0d61365e..d1baed4e0 100755 --- a/crypto777/OS_portable.h +++ b/crypto777/OS_portable.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -219,6 +219,7 @@ uint32_t OS_conv_datenum(int32_t datenum,int32_t hour,int32_t minute,int32_t sec int32_t OS_conv_unixtime(struct tai *t,int32_t *secondsp,time_t timestamp); char *OS_compatible_path(char *str); +FILE *OS_appendfile(char *origfname); int32_t OS_compare_files(char *fname,char *fname2); int64_t OS_copyfile(char *src,char *dest,int32_t cmpflag); diff --git a/crypto777/OS_time.c b/crypto777/OS_time.c index e127ad7a5..245da6928 100755 --- a/crypto777/OS_time.c +++ b/crypto777/OS_time.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/crypto777/SaM.c b/crypto777/SaM.c index 3f9c2d092..bfc97df59 100755 --- a/crypto777/SaM.c +++ b/crypto777/SaM.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index ed69962c6..2d31acdd2 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -155,6 +155,7 @@ try_again: curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA, &s); // we pass our 's' struct to the callback curl_easy_setopt(curl_handle,CURLOPT_NOSIGNAL, 1L); // supposed to fix "Alarm clock" and long jump crash curl_easy_setopt(curl_handle,CURLOPT_NOPROGRESS, 1L); // no progress callback + //curl_easy_setopt(curl_handle,CURLOPT_TIMEOUT, 60L); if ( strncmp(url,"https",5) == 0 ) { curl_easy_setopt(curl_handle,CURLOPT_SSL_VERIFYPEER,0); diff --git a/crypto777/cJSON.c b/crypto777/cJSON.c index d67dfa742..719c3eb8e 100755 --- a/crypto777/cJSON.c +++ b/crypto777/cJSON.c @@ -634,7 +634,7 @@ void cJSON_Minify(char *json) // the following written by jl777 /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/crypto777/curve25519.c b/crypto777/curve25519.c index eae51aefc..5d2c24930 100755 --- a/crypto777/curve25519.c +++ b/crypto777/curve25519.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/crypto777/hmac_sha512.c b/crypto777/hmac_sha512.c index 68c428189..e1fe4e523 100755 --- a/crypto777/hmac_sha512.c +++ b/crypto777/hmac_sha512.c @@ -506,7 +506,7 @@ char *hmac_sha512_str(char *dest,char *key,int32_t key_size,char *message) hmac_memory(&sha512_desc,(void *)key,key_size,(void *)message,strlen(message),checkbuf,&size); init_hexbytes_noT(dest,mac,SHA512_DIGEST_SIZE); init_hexbytes_noT(dest2,checkbuf,SHA512_DIGEST_SIZE); - //if ( memcmp(checkbuf,mac,SHA512_DIGEST_SIZE) != 0 ) + if ( memcmp(checkbuf,mac,SHA512_DIGEST_SIZE) != 0 ) printf("hmac_512 : %s vs %s\n",dest,dest2); return(dest); } diff --git a/crypto777/iguana_OS.c b/crypto777/iguana_OS.c index 425e65d73..5363f2135 100755 --- a/crypto777/iguana_OS.c +++ b/crypto777/iguana_OS.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/crypto777/iguana_serdes.c b/crypto777/iguana_serdes.c index eff6f3e5f..996743c78 100755 --- a/crypto777/iguana_serdes.c +++ b/crypto777/iguana_serdes.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/crypto777/iguana_utils.c b/crypto777/iguana_utils.c index 700cd73cc..cc058efd1 100755 --- a/crypto777/iguana_utils.c +++ b/crypto777/iguana_utils.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -193,6 +193,14 @@ void calc_OP_HASH160(char hexstr[41],uint8_t rmd160[20],char *pubkey) init_hexbytes_noT(hexstr,rmd160,20); } +double _xblend(float *destp,double val,double decay) +{ + double oldval; + if ( (oldval = *destp) != 0. ) + return((oldval * decay) + ((1. - decay) * val)); + else return(val); +} + double _dxblend(double *destp,double val,double decay) { double oldval; diff --git a/crypto777/m_LP_StaticNanoMsg b/crypto777/m_LP_StaticNanoMsg new file mode 100755 index 000000000..f6487bc1d --- /dev/null +++ b/crypto777/m_LP_StaticNanoMsg @@ -0,0 +1,30 @@ +# Very basic makefile for compiling iguana in Linux with StaticNanoMsg +# derived from m_LP (used to compile iguana for linux) +# author: fadedreamz + +SRCS=$(wildcard *.c) +SRCS+=$(wildcard jpeg/*.c) +SRCS+=$(wildcard jpeg/unix/*.c) + +OBJS=$(SRCS:%.c=%.o) + +INCLUDE_DIRS+=-I/usr/lib/x86_64-linux-gnu/curl +PREPROCESSORS+=-DLIQUIDITY_PROVIDER=1 -DUSE_STATIC_NANOMSG + +CFLAGS+=-O2 $(PREPROCESSORS) $(INCLUDE_DIRS) + +update_repo: + git pull + +clean: + -rm $(OBJS) + +all: update_repo $(OBJS) static + @echo ' ================' + @echo '| crypto777 [OK] |' + @echo ' ================' + +static: $(OBJS) + ar rc ../agents/libcrypto777.a $(OBJS) + +.PHONY: update_repo clean all static \ No newline at end of file diff --git a/crypto777/ramcoder.c b/crypto777/ramcoder.c index d495914de..86760289f 100755 --- a/crypto777/ramcoder.c +++ b/crypto777/ramcoder.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/datachain/datachain.c b/datachain/datachain.c index d0f661c79..9922e3f78 100755 --- a/datachain/datachain.c +++ b/datachain/datachain.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/deprecated/iguana_instantdex.c b/deprecated/iguana_instantdex.c index 85755175b..5baf88af0 100755 --- a/deprecated/iguana_instantdex.c +++ b/deprecated/iguana_instantdex.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/gecko/gecko.c b/gecko/gecko.c index cd7667089..5a0c2dbaa 100755 --- a/gecko/gecko.c +++ b/gecko/gecko.c @@ -1,6 +1,6 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/SuperNET_keys.c b/iguana/SuperNET_keys.c index be283118e..9eadf21f0 100755 --- a/iguana/SuperNET_keys.c +++ b/iguana/SuperNET_keys.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -84,7 +84,6 @@ bits256 SuperNET_wallet2priv(char *wallet2fname,bits256 wallethash) wallet2str[i] ^= wallethash.bytes[(i + r) % 32]; vcalc_sha256(0,wallet2priv.bytes,(void *)wallet2str,(int32_t)allocsize); free(wallet2str); - //char str[65]; printf("wallet2priv.(%s) from.(%s) crc.%u and passphrase r.%d len.%ld\n",bits256_str(str,wallet2priv),wallet2fname,crc,r,allocsize); } else if ( wallet2fname[0] != 0 ) printf("SuperNET_wallet2priv cant open (%s)\n",wallet2fname); return(wallet2priv); @@ -248,7 +247,6 @@ cJSON *SuperNET_decryptedjson(char *destfname,char *passphrase,int32_t passsize, wallethash = SuperNET_linehash(passphrase); SuperNET_linehash(fname2fa); // maps special chars wallet2priv = SuperNET_wallet2priv(fname2fa,wallethash); - //char str[65],str2[65]; printf("(%s + %s) -> wallethash.%s 2.(%s)\n",passphrase,fname2fa,bits256_str(str,wallethash),bits256_str(str2,wallet2priv)); } first = (bits256_nonz(wallethash) != 0 && bits256_cmp(wallethash,GENESIS_PRIVKEY) != 0); second = (bits256_nonz(wallet2priv) != 0 && bits256_cmp(wallet2priv,GENESIS_PRIVKEY) != 0); @@ -259,7 +257,6 @@ cJSON *SuperNET_decryptedjson(char *destfname,char *passphrase,int32_t passsize, wallet2shared = SuperNET_wallet2shared(wallethash,wallet2priv); wallet2pub = curve25519(wallet2shared,curve25519_basepoint9()); sprintf(destfname,"%s/%s",GLOBAL_CONFSDIR,bits256_str(str,wallet2pub)); - //printf("fname.(%s) wallet2pub.%s < [%s, %s]\n",destfname,bits256_str(str,wallet2pub),passphrase,fname2fa); if ( (confstr= OS_filestr(&allocsize,destfname)) != 0 ) { if ( (filejson= cJSON_Parse(confstr)) != 0 ) @@ -305,12 +302,9 @@ int32_t _SuperNET_encryptjson(struct supernet_info *myinfo,char *destfname,char wallethash = SuperNET_linehash(passphrase); SuperNET_linehash(fname2fa); // maps special chars wallet2priv = SuperNET_wallet2priv(fname2fa,wallethash); - //char str2[65]; printf("ENCRYPT.[%s %s] (%s) 2.%s\n",passphrase,fname2fa,bits256_str(str,wallethash),bits256_str(str2,wallet2priv)); wallet2shared = SuperNET_wallet2shared(wallethash,wallet2priv); wallet2pub = curve25519(wallet2shared,curve25519_basepoint9()); sprintf(destfname,"%s/%s",GLOBAL_CONFSDIR,bits256_str(str,wallet2pub)); - //printf("SAVE ARGJSON.(%s) [%s, %s] -> destfname.(%s)\n",jprint(argjson,0),passphrase,fname2fa,destfname); - //printf("shared.%llx -> pub.%s\n",(long long)wallet2shared.txid,bits256_str(str,wallet2pub)); SuperNET_savejsonfile(myinfo,destfname,wallethash,wallet2pub,argjson); return(0); } @@ -375,7 +369,6 @@ char *SuperNET_keysinit(struct supernet_info *myinfo,char *argjsonstr) passphrase[0] = fname2fa[0] = 0; wallethash = wallet2priv = GENESIS_PRIVKEY; coinargs = SuperNET_parsemainargs(myinfo,&wallethash,&wallet2priv,argjsonstr); - //printf("wallethash.%s 2.(%s)\n",bits256_str(str,wallethash),bits256_str(str2,wallet2priv)); if ( (msgjson= SuperNET_decryptedjson(destfname,passphrase,sizeof(passphrase),wallethash,fname2fa,sizeof(fname2fa),wallet2priv)) != 0 ) { SuperNET_parsemyinfo(myinfo,msgjson); @@ -395,9 +388,7 @@ char *SuperNET_keysinit(struct supernet_info *myinfo,char *argjsonstr) jaddbits256(json,"persistent_pub",myinfo->myaddr.persistent); OS_randombytes((void *)&r,sizeof(r)); jadd64bits(json,"rand",r); - //printf("call SuperNET_encryptjson\n"); _SuperNET_encryptjson(myinfo,destfname,passphrase,sizeof(passphrase),fname2fa,sizeof(fname2fa),json); - //printf("save.(%s)\n",jprint(json,0)); free_json(json); } if ( myinfo->ipaddr[0] == 0 ) diff --git a/iguana/dPoW.h b/iguana/dPoW.h index 4d1c27e4b..d9e3cb1f2 100755 --- a/iguana/dPoW.h +++ b/iguana/dPoW.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/dpow/dpow_fsm.c b/iguana/dpow/dpow_fsm.c index b65ec3a50..edaa8d22c 100755 --- a/iguana/dpow/dpow_fsm.c +++ b/iguana/dpow/dpow_fsm.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -412,7 +412,7 @@ void dpow_statemachinestart(void *ptr) extralen = dpow_paxpending(extras,&bp->paxwdcrc); bp->notaries[bp->myind].paxwdcrc = bp->paxwdcrc; } - sleep(5); + sleep(13); if ( dp->checkpoint.blockhash.height > checkpoint.blockhash.height ) { if ( bp->isratify == 0 ) diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c index be5b16837..aa803d1e8 100755 --- a/iguana/dpow/dpow_network.c +++ b/iguana/dpow/dpow_network.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -163,7 +163,7 @@ char *dex_reqsend(struct supernet_info *myinfo,char *handler,uint8_t *data,int32 printf("%d: subscribe connect (%s)\n",myinfo->numdexipbits,str); } } -//nn_connect(myinfo->reqsock,nanomsg_tcpname(0,str,ipaddr,REP_SOCK)); + nn_connect(myinfo->reqsock,nanomsg_tcpname(0,str,ipaddr,REP_SOCK)); printf("%d: req connect (%s)\n",myinfo->numdexipbits,str); } } @@ -1206,7 +1206,7 @@ int32_t dpow_nanomsg_update(struct supernet_info *myinfo) break; usleep(1000); }*/ - while ( (size= nn_recv(myinfo->dpowsock,&np,NN_MSG,0)) >= 0 && num < 100 ) + while ( (size= nn_recv(myinfo->dpowsock,&np,NN_MSG,0)) >= 0 && num < 1000 ) { if ( size > 0 ) { diff --git a/iguana/dpow/dpow_prices.c b/iguana/dpow/dpow_prices.c index 3b942a953..cc2420201 100755 --- a/iguana/dpow/dpow_prices.c +++ b/iguana/dpow/dpow_prices.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/dpow/dpow_rpc.c b/iguana/dpow/dpow_rpc.c index 54924fc22..0238df031 100755 --- a/iguana/dpow/dpow_rpc.c +++ b/iguana/dpow/dpow_rpc.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -1001,7 +1001,7 @@ int32_t dpow_issuer_block(struct dpow_info *dp,struct iguana_info *coin,int32_t int32_t dpow_issuer_iteration(struct dpow_info *dp,struct iguana_info *coin,int32_t height,uint32_t *isrealtimep) { - char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint16_t port = coin->chain->rpcport; + char *retstr; int32_t i,currentheight=0; cJSON *infoobj,*result; uint16_t port = coin->chain->rpcport; if ( height <= 0 ) height = 1; *isrealtimep = 0; @@ -1009,9 +1009,9 @@ int32_t dpow_issuer_iteration(struct dpow_info *dp,struct iguana_info *coin,int3 { if ( (infoobj= cJSON_Parse(retstr)) != 0 ) { - if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 ) + if ( (result= jobj(infoobj,(char *)"result")) != 0 && (currentheight= jint(result,(char *)"blocks")) != 0 ) { - for (i=0; i<1000 && height<=kmdheight; i++,height++) + for (i=0; i<100 && height<=currentheight; i++,height++) { /*fprintf(stderr,"%s.%d ",coin->symbol,height); if ( (height % 10) == 0 ) @@ -1028,7 +1028,7 @@ int32_t dpow_issuer_iteration(struct dpow_info *dp,struct iguana_info *coin,int3 } usleep(10000); } - if ( height >= kmdheight ) + if ( height >= currentheight ) *isrealtimep = (uint32_t)time(NULL); } free_json(infoobj); @@ -1038,8 +1038,8 @@ int32_t dpow_issuer_iteration(struct dpow_info *dp,struct iguana_info *coin,int3 } else { - //printf("error from %s\n",coin->symbol); - sleep(3); + printf("error from %s height.%d currentheight.%d\n",coin->symbol,height,currentheight); + usleep(100000); } return(height); } diff --git a/iguana/dpow/dpow_tx.c b/iguana/dpow/dpow_tx.c index 653e633c2..79559c10f 100755 --- a/iguana/dpow/dpow_tx.c +++ b/iguana/dpow/dpow_tx.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -558,7 +558,7 @@ int32_t dpow_signedtxgen(struct supernet_info *myinfo,struct dpow_info *dp,struc void dpow_sigscheck(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,int32_t myind,int32_t src_or_dest,int8_t bestk,uint64_t bestmask,uint8_t pubkeys[64][33],int32_t numratified) { - bits256 txid,srchash,zero,signedtxid; struct iguana_info *coin; int32_t j,len,numsigs; char *retstr=0,str[65],str2[65]; uint8_t txdata[32768]; uint32_t channel; + bits256 txid,srchash,zero,signedtxid; struct iguana_info *coin; int32_t j,len,numsigs; char *retstr=0,str[65],str2[65]; uint8_t txdata[32768]; uint32_t channel,state; coin = (src_or_dest != 0) ? bp->destcoin : bp->srccoin; memset(zero.bytes,0,sizeof(zero)); channel = (src_or_dest != 0) ? DPOW_SIGBTCCHANNEL : DPOW_SIGCHANNEL; @@ -586,11 +586,20 @@ void dpow_sigscheck(struct supernet_info *myinfo,struct dpow_info *dp,struct dpo decode_hex(txdata+32,len,bp->signedtx); for (j=0; jhashmsg,(src_or_dest != 0) ? DPOW_BTCTXIDCHANNEL : DPOW_TXIDCHANNEL,bp->height,txdata,len+32); - bp->state = src_or_dest != 0 ? 1000 : 0xffffffff; - printf("complete statemachine.%s ht.%d state.%d (%x %x)\n",coin->symbol,bp->height,bp->state,bp->hashmsg.uints[0],txid.uints[0]); + state = src_or_dest != 0 ? 1000 : 0xffffffff; + if ( bp->state < state ) + { + bp->state = state; + dpow_send(myinfo,dp,bp,txid,bp->hashmsg,(src_or_dest != 0) ? DPOW_BTCTXIDCHANNEL : DPOW_TXIDCHANNEL,bp->height,txdata,len+32); + printf("complete statemachine.%s ht.%d state.%d (%x %x)\n",coin->symbol,bp->height,bp->state,bp->hashmsg.uints[0],txid.uints[0]); + } } else printf("sendtxid mismatch got %s instead of %s\n",bits256_str(str,txid),bits256_str(str2,signedtxid)); } + else + { + bp->state = 0xffffffff; + printf("dpow_sigscheck: mismatched txid.%s vs %s\n",bits256_str(str,txid),retstr); + } free(retstr); retstr = 0; } diff --git a/iguana/exchanges/bitcoin.c b/iguana/exchanges/bitcoin.c index 90d4d4fef..5f8c3ec7b 100755 --- a/iguana/exchanges/bitcoin.c +++ b/iguana/exchanges/bitcoin.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/bitcoin.h b/iguana/exchanges/bitcoin.h index 2a5cf651e..ad362a5a2 100755 --- a/iguana/exchanges/bitcoin.h +++ b/iguana/exchanges/bitcoin.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/bitfinex.c b/iguana/exchanges/bitfinex.c index 764167ca3..6643fff89 100755 --- a/iguana/exchanges/bitfinex.c +++ b/iguana/exchanges/bitfinex.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/bitstamp.c b/iguana/exchanges/bitstamp.c index 30c9b9a5a..36e928a34 100755 --- a/iguana/exchanges/bitstamp.c +++ b/iguana/exchanges/bitstamp.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/bittrex.c b/iguana/exchanges/bittrex.c index f032b764b..155de4cfa 100755 --- a/iguana/exchanges/bittrex.c +++ b/iguana/exchanges/bittrex.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -77,7 +77,7 @@ double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchang { cJSON *json,*obj; char *jsonstr,market[128],url[1024]; double hbla = 0.; sprintf(market,"%s-%s",rel,base); - sprintf(url,"https://bittrex.com/api/v1.1/public/getorderbook?market=%s&type=both&depth=%d",market,maxdepth); + sprintf(url,"http://bittrex.com/api/v1.1/public/getorderbook?market=%s&type=both&depth=%d",market,maxdepth); jsonstr = issue_curl(url); if ( jsonstr != 0 ) { @@ -166,8 +166,11 @@ uint64_t TRADE(int32_t dotrade,char **retstrp,struct exchange_info *exchange,cha return(0); } sprintf(payload,"https://bittrex.com/api/v1.1/market/%slimit?apikey=%s&nonce=%llu&market=%s&rate=%.8f&quantity=%.8f",dir>0?"buy":"sell",exchange->apikey,(long long)exchange_nonce(exchange),pairstr,price,volume); - if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume,argjson) == 0 && (json= SIGNPOST(&exchange->cHandle,dotrade,retstrp,exchange,payload,payload)) != 0 ) + if ( //CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume,argjson) == 0 && + (json= SIGNPOST(&exchange->cHandle,dotrade,retstrp,exchange,payload,payload)) != 0 ) { + if ( *retstrp != 0 ) + printf("SIGNPOST returned.(%s) %s\n",*retstrp,jprint(json,0)); if ( is_cJSON_True(cJSON_GetObjectItem(json,"success")) != 0 && (resultobj= cJSON_GetObjectItem(json,"result")) != 0 ) { copy_cJSON(&uuidstr,cJSON_GetObjectItem(resultobj,"uuid")); diff --git a/iguana/exchanges/btc38.c b/iguana/exchanges/btc38.c index 268dce776..493d3fa0d 100755 --- a/iguana/exchanges/btc38.c +++ b/iguana/exchanges/btc38.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/btce.c b/iguana/exchanges/btce.c index c7a255b06..b1d5a1291 100755 --- a/iguana/exchanges/btce.c +++ b/iguana/exchanges/btce.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/coinbase.c b/iguana/exchanges/coinbase.c index 1a0fd190d..528df90a2 100755 --- a/iguana/exchanges/coinbase.c +++ b/iguana/exchanges/coinbase.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/exchange_supports.h b/iguana/exchanges/exchange_supports.h index 083630bc1..fc7c854d2 100755 --- a/iguana/exchanges/exchange_supports.h +++ b/iguana/exchanges/exchange_supports.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/exchange_undefs.h b/iguana/exchanges/exchange_undefs.h index 20862bce8..6d115147d 100755 --- a/iguana/exchanges/exchange_undefs.h +++ b/iguana/exchanges/exchange_undefs.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/fxcm.c b/iguana/exchanges/fxcm.c index 42807a46f..ac39cbbd1 100755 --- a/iguana/exchanges/fxcm.c +++ b/iguana/exchanges/fxcm.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/huobi.c b/iguana/exchanges/huobi.c index 402049a7c..02cc6f941 100755 --- a/iguana/exchanges/huobi.c +++ b/iguana/exchanges/huobi.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/instaforex.c b/iguana/exchanges/instaforex.c index 3efa2a0e4..babcec86f 100755 --- a/iguana/exchanges/instaforex.c +++ b/iguana/exchanges/instaforex.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/jumblr.c b/iguana/exchanges/jumblr.c index ba3cb4209..9803bbc05 100755 --- a/iguana/exchanges/jumblr.c +++ b/iguana/exchanges/jumblr.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/lakebtc.c b/iguana/exchanges/lakebtc.c index 992665a07..3542ac9ba 100755 --- a/iguana/exchanges/lakebtc.c +++ b/iguana/exchanges/lakebtc.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/nxtae.c b/iguana/exchanges/nxtae.c index 592532f30..03e2c1eee 100755 --- a/iguana/exchanges/nxtae.c +++ b/iguana/exchanges/nxtae.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/okcoin.c b/iguana/exchanges/okcoin.c index 793fa38e6..af2fe5184 100755 --- a/iguana/exchanges/okcoin.c +++ b/iguana/exchanges/okcoin.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/poloniex.c b/iguana/exchanges/poloniex.c index 0e4061d99..a45c3e343 100755 --- a/iguana/exchanges/poloniex.c +++ b/iguana/exchanges/poloniex.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -166,10 +166,11 @@ uint64_t TRADE(int32_t dotrade,char **retstrp,struct exchange_info *exchange,cha typestr = (dir > 0) ? "marginBuy":"marginSell"; else typestr = (dir > 0) ? "buy":"sell"; sprintf(payload,"command=%s&nonce=%lld¤cyPair=%s&rate=%.8f&amount=%.8f",typestr,(long long)nonce,pairstr,price,volume); - if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume,argjson) == 0 && (json= SIGNPOST(&exchange->cHandle,dotrade,retstrp,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + if ( //CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume,argjson) == 0 && + (json= SIGNPOST(&exchange->cHandle,dotrade,retstrp,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) { txid = (get_API_nxt64bits(cJSON_GetObjectItem(json,"orderNumber")) << 32) | get_API_nxt64bits(cJSON_GetObjectItem(json,"tradeID")); - printf("poloniex.%llu (%s)\n",(long long)txid,jprint(json,0)); + printf("poloniex.%llu (%s) %s\n",(long long)txid,jprint(json,0),*retstrp!=0?*retstrp:""); free_json(json); } return(txid); diff --git a/iguana/exchanges/quadriga.c b/iguana/exchanges/quadriga.c index 6347c5118..e686c9787 100755 --- a/iguana/exchanges/quadriga.c +++ b/iguana/exchanges/quadriga.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/truefx.c b/iguana/exchanges/truefx.c index 331eb71d6..1d0049543 100755 --- a/iguana/exchanges/truefx.c +++ b/iguana/exchanges/truefx.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges/unconf.c b/iguana/exchanges/unconf.c index d0cea56ab..951b6419d 100755 --- a/iguana/exchanges/unconf.c +++ b/iguana/exchanges/unconf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/exchanges777.h b/iguana/exchanges777.h index b1b632660..fdefd98b5 100755 --- a/iguana/exchanges777.h +++ b/iguana/exchanges777.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -46,7 +46,7 @@ #define EXCHANGES777_MINPOLLGAP 1 #define EXCHANGES777_MAXDEPTH 200 -#define EXCHANGES777_DEFAULT_TIMEOUT 30 +#define EXCHANGES777_DEFAULT_TIMEOUT 60 typedef void CURL; struct exchange_info; struct exchange_quote; @@ -179,7 +179,7 @@ char *exchanges777_Qtrade(struct exchange_info *exchange,char *base,char *rel,in struct exchange_request *exchanges777_baserelfind(struct exchange_info *exchange,char *base,char *rel,int32_t func); struct exchange_info *exchanges777_find(char *exchangestr); -void prices777_processprice(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth); +void tradebots_processprices(struct supernet_info *myinfo,struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t numbids,int32_t numasks); double truefx_price(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert); double fxcm_price(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert); diff --git a/iguana/iguana777.c b/iguana/iguana777.c index 6176e51b7..f8fc045ce 100755 --- a/iguana/iguana777.c +++ b/iguana/iguana777.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana777.h b/iguana/iguana777.h index 52d136d2f..2c74858b5 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -39,6 +39,9 @@ #endif #endif +#define LOCKTIME_THRESHOLD 500000000 +#define KOMODO_INTEREST ((uint64_t)(0.05 * SATOSHIDEN)) // 5% + //#define BTC2_VERSION #define BTC2_HARDFORK_HEIGHT 444444 #define BTC2_SIGHASH_FORKID 0xcf @@ -51,7 +54,8 @@ #include "nn.h" #include "bus.h" #else*/ -#ifdef __APPLE__ +//#ifdef __APPLE__ +#if defined(__APPLE__) || defined(WIN32) || defined(USE_STATIC_NANOMSG) #include "../crypto777/nanosrc/nn.h" #include "../crypto777/nanosrc/bus.h" #include "../crypto777/nanosrc/pubsub.h" @@ -84,7 +88,7 @@ struct supernet_address char NXTADDR[32],BTC[64],BTCD[64]; }; -struct liquidity_info { char base[64],rel[64]; double profit,refprice; }; +struct liquidity_info { char base[16],rel[16],exchange[16]; uint64_t assetid; double profit,refprice; int dir; }; struct message_info { int32_t msgcount; bits256 refhash,msghashes[64]; uint32_t timestamps[64]; }; struct supernet_info @@ -94,6 +98,8 @@ struct supernet_info uint8_t persistent_pubkey33[33]; char ipaddr[64],NXTAPIURL[512],secret[4096],password[4096],rpcsymbol[64],handle[1024],permanentfile[1024]; char *decryptstr; + void (*liquidity_command)(struct supernet_info *myinfo,char *base,bits256 hash,cJSON *vals); + double (*liquidity_active)(struct supernet_info *myinfo,double *refpricep,char *exchange,char *base,char *rel,double volume); int32_t maxdelay,IAMRELAY,IAMNOTARY,IAMLP,publicRPC,basilisk_busy,genesisresults,remoteorigin; uint32_t expiration,dirty,DEXactive,DEXpoll,totalcoins,nanoinit,dexcrcs[1024]; uint16_t argport,rpcport; @@ -124,6 +130,7 @@ struct supernet_info bits256 pangea_category,instantdex_category; uint8_t logs[256],exps[510]; struct message_info msgids[8192]; + double *svmfeatures; }; #include "../includes/iguana_funcs.h" diff --git a/iguana/iguana_accept.c b/iguana/iguana_accept.c index b3eaa07be..869d0f3bb 100755 --- a/iguana/iguana_accept.c +++ b/iguana/iguana_accept.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_bitmap.c b/iguana/iguana_bitmap.c index 94a63fc72..b690738b5 100755 --- a/iguana/iguana_bitmap.c +++ b/iguana/iguana_bitmap.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_blocks.c b/iguana/iguana_blocks.c index 84bf9c914..9d502494f 100755 --- a/iguana/iguana_blocks.c +++ b/iguana/iguana_blocks.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_bundles.c b/iguana/iguana_bundles.c index 0c9bba65f..d4ad7a81e 100755 --- a/iguana/iguana_bundles.c +++ b/iguana/iguana_bundles.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_chains.c b/iguana/iguana_chains.c index 7a2b1f370..927fc0752 100755 --- a/iguana/iguana_chains.c +++ b/iguana/iguana_chains.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_exchanges.c b/iguana/iguana_exchanges.c index 306d72315..d8d678004 100755 --- a/iguana/iguana_exchanges.c +++ b/iguana/iguana_exchanges.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -184,11 +184,6 @@ double instantdex_avehbla(struct supernet_info *myinfo,double retvals[4],char *_ else return(0); } -void prices777_processprice(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth) -{ - -} - cJSON *exchanges777_allpairs(char *baserels[][2],int32_t num) { int32_t i; char str[32]; cJSON *json,*item,*array = cJSON_CreateArray(); @@ -339,11 +334,11 @@ double exchange_setquote(struct exchange_quote *bidasks,int32_t *numbidsp,int32_ } if ( commission != 0. ) { - //printf("price %f fee %f -> ",price,prices->commission * price); + printf("price %f fee %f -> ",price,commission * price); if ( bidask == 0 ) price -= commission * price; else price += commission * price; - //printf("%f\n",price); + printf("%f\n",price); } quote = (bidask == 0) ? &bidasks[(*numbidsp)<<1] : &bidasks[((*numasksp)<<1) + 1]; quote->price = price, quote->volume = volume, quote->timestamp = timestamp, quote->orderid = orderid, quote->offerNXT = offerNXT; @@ -400,6 +395,7 @@ void exchanges777_json_quotes(struct exchange_info *exchange,double commission,c if ( strcmp(exchange->name,"kraken") == 0 ) timestamp = juint(jitem(item,2),0); else orderid = j64bits(jitem(item,2),0); + //printf("{%s} (%.8f %.8f) %f\n",jprint(item,0),price,volume,commission); } else { @@ -456,7 +452,7 @@ double exchanges777_standardprices(struct exchange_info *exchange,double commiss if ( (jsonstr= issue_curl(url)) != 0 ) { //if ( strcmp(exchangestr,"btc38") == 0 ) - //printf("(%s) -> (%s)\n",url,jsonstr); + //printf("%f (%s) -> (%s)\n",commission,url,jsonstr); if ( (json= cJSON_Parse(jsonstr)) != 0 ) { hbla = exchanges777_json_orderbook(exchange,commission,base,rel,quotes,maxdepth,json,field,"bids","asks",price,volume,invert); @@ -859,7 +855,7 @@ void exchanges777_loop(void *ptr) if ( req->bidasks[(i << 1) + 1].price > SMALLVAL ) req->numasks++; //printf("%-10s %s/%s numbids.%d numasks.%d\n",exchange->name,req->base,req->rel,req->numbids,req->numasks); - prices777_processprice(exchange,req->base,req->rel,req->bidasks,req->depth); + tradebots_processprices(myinfo,exchange,req->base,req->rel,req->bidasks,req->numbids,req->numasks); } queue_enqueue("pricesQ",&exchange->pricesQ,&req->DL); } @@ -954,7 +950,7 @@ char *exchanges777_Qprices(struct exchange_info *exchange,char *base,char *rel,i } if ( base[0] == 0 || rel[0] == 0 || (polarity= (*exchange->issue.supports)(exchange,base,rel,argjson)) == 0 ) { - //printf("%s invalid (%s) or (%s)\n",exchange->name,base,rel); + printf("%s invalid (%s) or (%s)\n",exchange->name,base,rel); return(clonestr("{\"error\":\"invalid base or rel\"}")); } if ( depth <= 0 ) @@ -977,7 +973,7 @@ char *exchanges777_Qprices(struct exchange_info *exchange,char *base,char *rel,i req->commission = exchange->commission; if ( monitor == 0 ) { - printf("%s submit (%s) (%s)\n",exchange->name,base,rel); + //printf("%s submit (%s) (%s)\n",exchange->name,base,rel); return(exchanges777_submit(exchange,req,'Q',maxseconds)); } else diff --git a/iguana/iguana_init.c b/iguana/iguana_init.c index 8d9ccacfd..961996a23 100755 --- a/iguana/iguana_init.c +++ b/iguana/iguana_init.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_interpreter.c b/iguana/iguana_interpreter.c index 4ed277649..d98ac46a8 100755 --- a/iguana/iguana_interpreter.c +++ b/iguana/iguana_interpreter.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_json.c b/iguana/iguana_json.c index ca7a1fea3..6ca3c0074 100755 --- a/iguana/iguana_json.c +++ b/iguana/iguana_json.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_mofn.c b/iguana/iguana_mofn.c index 50d024457..4cca28947 100755 --- a/iguana/iguana_mofn.c +++ b/iguana/iguana_mofn.c @@ -309,7 +309,7 @@ int maingen(int argc,char **argv) } /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_msg.c b/iguana/iguana_msg.c index f9a46eec0..4b7a497d9 100755 --- a/iguana/iguana_msg.c +++ b/iguana/iguana_msg.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_notary.c b/iguana/iguana_notary.c index 03cf58c38..645321ae6 100755 --- a/iguana/iguana_notary.c +++ b/iguana/iguana_notary.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -71,12 +71,14 @@ void dpow_srcupdate(struct supernet_info *myinfo,struct dpow_info *dp,int32_t he else { freq = 1; - minsigs = 7;//(komodo_notaries(dp->symbol,pubkeys,height) >> 1) + 1; + //minsigs = 7;//(komodo_notaries(dp->symbol,pubkeys,height) >> 1) + 1; //if ( minsigs < DPOW_MINSIGS ) - // minsigs = DPOW_MINSIGS; + minsigs = DPOW_MINSIGS; } printf("%s/%s src ht.%d dest.%u nonz.%d %s minsigs.%d\n",dp->symbol,dp->dest,checkpoint.blockhash.height,dp->destupdated,bits256_nonz(checkpoint.blockhash.hash),bits256_str(str,dp->last.blockhash.hash),minsigs); dpow_fifoupdate(myinfo,dp->srcfifo,dp->last); + if ( dp->SRCREALTIME == 0 && strcmp(dp->dest,"KMD") == 0 ) + return; if ( bits256_nonz(checkpoint.blockhash.hash) != 0 && (checkpoint.blockhash.height % freq) == 0 ) { ptrs = calloc(1,sizeof(void *)*5 + sizeof(struct dpow_checkpoint)); @@ -142,22 +144,23 @@ void dpow_destupdate(struct supernet_info *myinfo,struct dpow_info *dp,int32_t h void iguana_dPoWupdate(struct supernet_info *myinfo,struct dpow_info *dp) { - int32_t i,height,num; uint32_t blocktime; bits256 blockhash; struct iguana_info *src,*dest; - for (i=0; i<3; i++) + int32_t height,num; uint32_t blocktime; bits256 blockhash; struct iguana_info *src,*dest; + if ( strcmp(dp->symbol,"KMD") == 0 ) { - if ( (num= dpow_nanomsg_update(myinfo)) <= 0 ) - break; + num = dpow_nanomsg_update(myinfo); //fprintf(stderr,"%d ",num); } src = iguana_coinfind(dp->symbol); dest = iguana_coinfind(dp->dest); - //fprintf(stderr,"i.%d dp.%p dPoWupdate (%s -> %s)\n",i,dp,dp!=0?dp->symbol:"",dp!=0?dp->dest:""); + //fprintf(stderr,"dp.%p dPoWupdate (%s -> %s)\n",dp,dp!=0?dp->symbol:"",dp!=0?dp->dest:""); if ( src != 0 && dest != 0 ) { dp->numdesttx = sizeof(dp->desttx)/sizeof(*dp->desttx); if ( (height= dpow_getchaintip(myinfo,&blockhash,&blocktime,dp->desttx,&dp->numdesttx,dest)) != dp->destchaintip.blockhash.height && height >= 0 ) { - char str[65]; printf("[%s] %s %s height.%d vs last.%d\n",dp->symbol,dp->dest,bits256_str(str,blockhash),height,dp->destchaintip.blockhash.height); + char str[65]; + if ( strcmp(dp->symbol,"KMD") == 0 || height != dp->destchaintip.blockhash.height+1 ) + printf("[%s].%d %s %s height.%d vs last.%d\n",dp->symbol,dp->SRCHEIGHT,dp->dest,bits256_str(str,blockhash),height,dp->destchaintip.blockhash.height); if ( height <= dp->destchaintip.blockhash.height ) { printf("iguana_dPoWupdate dest.%s reorg detected %d vs %d\n",dp->dest,height,dp->destchaintip.blockhash.height); @@ -169,8 +172,12 @@ void iguana_dPoWupdate(struct supernet_info *myinfo,struct dpow_info *dp) if ( (height= dpow_getchaintip(myinfo,&blockhash,&blocktime,dp->srctx,&dp->numsrctx,src)) != dp->last.blockhash.height && height >= 0 ) { if ( strcmp(dp->dest,"KMD") == 0 ) + { + //fprintf(stderr,"[I "); dp->SRCHEIGHT = dpow_issuer_iteration(dp,src,dp->SRCHEIGHT,&dp->SRCREALTIME); - char str[65]; printf("[%s] %s %s height.%d vs last.%d\n",dp->dest,dp->symbol,bits256_str(str,blockhash),height,dp->last.blockhash.height); + //fprintf(stderr," %d] ",dp->SRCHEIGHT); + } + char str[65]; printf("[%s].%d %s %s height.%d vs last.%d\n",dp->dest,dp->SRCHEIGHT,dp->symbol,bits256_str(str,blockhash),height,dp->last.blockhash.height); if ( dp->lastheight == 0 ) dp->lastheight = height-1; if ( height < dp->last.blockhash.height ) diff --git a/iguana/iguana_payments.c b/iguana/iguana_payments.c index db778a1f6..9aa807dc0 100755 --- a/iguana/iguana_payments.c +++ b/iguana/iguana_payments.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -334,6 +334,63 @@ bits256 iguana_sendrawtransaction(struct supernet_info *myinfo,struct iguana_inf return(txid); } +uint64_t iguana_interest(struct supernet_info *myinfo,struct iguana_info *coin,bits256 txid,int32_t vout,uint64_t value) +{ + char *retstr; int32_t height; cJSON *retjson; struct iguana_txid T,*tx; + int32_t minutes; uint64_t numerator,denominator,interest = 0; + if ( coin->FULLNODE < 0 ) // komodod is running + { + if ( (retjson= dpow_gettxout(myinfo,coin,txid,vout)) != 0 ) + { + interest = jdouble(retjson,"interest") * SATOSHIDEN; + free_json(retjson); + } + } + else if ( coin->FULLNODE == 0 ) // basilisk mode -> use DEX* API + { + if ( (retstr= _dex_gettxout(myinfo,coin->symbol,txid,vout)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + interest = jdouble(retjson,"interest") * SATOSHIDEN; + free_json(retjson); + } + free(retstr); + } + } + else // we have it local + { + if ( (tx= iguana_txidfind(coin,&height,&T,txid,coin->bundlescount)) != 0 && tx->locktime >LOCKTIME_THRESHOLD ) + { + if ( (minutes= ((uint32_t)time(NULL) - 60 - tx->locktime) / 60) >= 60 ) + { + numerator = (value * KOMODO_INTEREST); + denominator = (((uint64_t)365 * 24 * 60) / minutes); + if ( denominator == 0 ) + denominator = 1; // max KOMODO_INTEREST per transfer, do it at least annually! + interest = (numerator / denominator) / SATOSHIDEN; + fprintf(stderr,"komodo_interest %lld %.8f nLockTime.%u tiptime.%u minutes.%d interest %lld %.8f (%llu / %llu)\n",(long long)value,(double)value/SATOSHIDEN,tx->locktime,(uint32_t)time(NULL),minutes,(long long)interest,(double)interest/SATOSHIDEN,(long long)numerator,(long long)denominator); + } + } + } + char str[65]; printf("interest for %s.v%d %.8f %.8f\n",bits256_str(str,txid),vout,dstr(value),dstr(interest)); + return(interest); +} + +uint64_t iguana_interests(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *vins) +{ + int32_t i,n; cJSON *item; uint64_t interest = 0; + if ( is_cJSON_Array(vins) != 0 && (n= cJSON_GetArraySize(vins)) > 0 ) + { + for (i=0; isymbol,"KMD") == 0 ) + total += iguana_interests(myinfo,coin,vins); if ( total < (satoshis + txfee) ) { free_json(vins); diff --git a/iguana/iguana_peers.c b/iguana/iguana_peers.c index f981cc82d..bedda833f 100755 --- a/iguana/iguana_peers.c +++ b/iguana/iguana_peers.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_ramchain.c b/iguana/iguana_ramchain.c index d94078751..7ed8867fc 100755 --- a/iguana/iguana_ramchain.c +++ b/iguana/iguana_ramchain.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_realtime.c b/iguana/iguana_realtime.c index b54a82e39..bedc4b911 100755 --- a/iguana/iguana_realtime.c +++ b/iguana/iguana_realtime.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_recv.c b/iguana/iguana_recv.c index f7bd3f598..c310b0a12 100755 --- a/iguana/iguana_recv.c +++ b/iguana/iguana_recv.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_rpc.c b/iguana/iguana_rpc.c index 70c072490..c36a6bce7 100755 --- a/iguana/iguana_rpc.c +++ b/iguana/iguana_rpc.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -52,7 +52,6 @@ char *sglue(GLUEARGS,char *agent,char *method) { if ( (wallet= iguana_walletjson(myinfo)) != 0 ) { - //printf("WALLETSTR.(%s)\n",jprint(wallet,0)); if ( (walletstr= SuperNET_login(myinfo,coin,json,remoteaddr,myinfo->handle,myinfo->secret,myinfo->permanentfile,0)) != 0 ) { free(walletstr); diff --git a/iguana/iguana_scripts.c b/iguana/iguana_scripts.c index 7a79fed39..4374c50ea 100755 --- a/iguana/iguana_scripts.c +++ b/iguana/iguana_scripts.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_secp.c b/iguana/iguana_secp.c index b69929408..984379f3a 100755 --- a/iguana/iguana_secp.c +++ b/iguana/iguana_secp.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_sign.c b/iguana/iguana_sign.c index 5f34def90..f8790f003 100755 --- a/iguana/iguana_sign.c +++ b/iguana/iguana_sign.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_spendvectors.c b/iguana/iguana_spendvectors.c index 0667d429e..e1d9a9479 100755 --- a/iguana/iguana_spendvectors.c +++ b/iguana/iguana_spendvectors.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_tradebots.c b/iguana/iguana_tradebots.c index f6755e71d..8134c9125 100755 --- a/iguana/iguana_tradebots.c +++ b/iguana/iguana_tradebots.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_tx.c b/iguana/iguana_tx.c index c58b7c56d..36972a0c6 100755 --- a/iguana/iguana_tx.c +++ b/iguana/iguana_tx.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_txidfind.c b/iguana/iguana_txidfind.c index cf49872ef..6db6d03ed 100755 --- a/iguana/iguana_txidfind.c +++ b/iguana/iguana_txidfind.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_unspents.c b/iguana/iguana_unspents.c index 4d2581d69..5e53c357d 100755 --- a/iguana/iguana_unspents.c +++ b/iguana/iguana_unspents.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_volatiles.c b/iguana/iguana_volatiles.c index 5ea221167..8bf4923f1 100755 --- a/iguana/iguana_volatiles.c +++ b/iguana/iguana_volatiles.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/iguana_wallet.c b/iguana/iguana_wallet.c index f452e3695..14c8aadc6 100755 --- a/iguana/iguana_wallet.c +++ b/iguana/iguana_wallet.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -1505,12 +1505,10 @@ TWOSTRINGS_AND_INT(bitcoinrpc,importprivkey,wif,account,rescan) { free(retstr); retstr = myinfo->decryptstr, myinfo->decryptstr = 0; - //printf("DECRYPT.(%s)\n",retstr); iguana_waddresscalc(myinfo,coin->chain->pubtype,coin->chain->wiftype,waddr,privkey); iguana_waccountswitch(myinfo,coin,account,waddr->coinaddr,0); waddr->privkey = privkey; retjson = iguana_walletadd(myinfo,0,coin,retstr,account,waddr,0,0); - //printf("AFTERADD.(%s)\n",jprint(retjson,0)); if ( retstr != 0 ) scrubfree(retstr); return(jprint(retjson,1)); diff --git a/iguana/m_LP b/iguana/m_LP index fdd45659b..d09ee9373 100755 --- a/iguana/m_LP +++ b/iguana/m_LP @@ -4,6 +4,6 @@ rm -f ../agents/iguana *.o git pull cd secp256k1; ./m_unix; cd .. cd ../crypto777; ./m_LP; cd ../iguana -gcc -g -fno-aggressive-loop-optimizations -Wno-deprecated -c -O2 -DLIQUIDITY_PROVIDER=1 *.c ../basilisk/basilisk.c ../gecko/gecko.c ../datachain/datachain.c -gcc -g -fno-aggressive-loop-optimizations -Wno-deprecated -c -DLIQUIDITY_PROVIDER=1 main.c iguana777.c iguana_bundles.c ../basilisk/basilisk.c -gcc -g -o ../agents/iguana *.o ../agents/libcrypto777.a -lnanomsg -lcurl -lssl -lcrypto -lpthread -lz -lm +clang -g -Wno-deprecated -c -O2 -DLIQUIDITY_PROVIDER=1 *.c ../basilisk/basilisk.c ../gecko/gecko.c ../datachain/datachain.c +clang -g -Wno-deprecated -c -DLIQUIDITY_PROVIDER=1 main.c iguana777.c iguana_bundles.c ../basilisk/basilisk.c +clang -g -o ../agents/iguana *.o ../agents/libcrypto777.a /usr/local/lib/libnanomsg.so -lcurl -lssl -lcrypto -lpthread -lz -lm diff --git a/iguana/m_LP_StaticNanoMsg b/iguana/m_LP_StaticNanoMsg new file mode 100755 index 000000000..b88e05e45 --- /dev/null +++ b/iguana/m_LP_StaticNanoMsg @@ -0,0 +1,12 @@ +#!/bin/bash +#./configure --enable-endomorphism --enable-module-ecdh --enable-module-schnorr --enable-module-rangeproof --enable-experimental --enable-module_recovery +rm -f ../agents/iguana *.o +git pull +cd secp256k1; ./m_unix; cd .. +cd ../crypto777; make -f m_LP_StaticNanoMsg all; make -f m_LP_StaticNanoMsg clean; cd ../iguana + +clang -g -Wno-deprecated -c -O2 -DLIQUIDITY_PROVIDER=1 -DUSE_STATIC_NANOMSG *.c ../basilisk/basilisk.c ../gecko/gecko.c ../datachain/datachain.c +clang -g -Wno-deprecated -c -DLIQUIDITY_PROVIDER=1 -DUSE_STATIC_NANOMSG main.c iguana777.c iguana_bundles.c ../basilisk/basilisk.c + + +clang -g -o ../agents/iguana *.o ../agents/libcrypto777.a ../OSlibs/linux/$(uname -m)/libnanomsg-static.a -lcurl -lssl -lcrypto -lpthread -lz -lm -lanl -lrt -lnsl diff --git a/iguana/m_notary b/iguana/m_notary index dd0794507..cf6fdaaf0 100755 --- a/iguana/m_notary +++ b/iguana/m_notary @@ -4,9 +4,11 @@ rm -f ../agents/iguana *.o git pull cd secp256k1; ./m_unix; cd .. cd ../crypto777; ./m_LP; cd ../iguana -gcc -g -fno-aggressive-loop-optimizations -Wno-deprecated -c -O2 -DISNOTARYNODE=1 -DLIQUIDITY_PROVIDER=1 *.c ../basilisk/basilisk.c ../gecko/gecko.c ../datachain/datachain.c -gcc -g -fno-aggressive-loop-optimizations -Wno-deprecated -c -DISNOTARYNODE=1 -DLIQUIDITY_PROVIDER=1 main.c iguana777.c iguana_bundles.c ../basilisk/basilisk.c -gcc -g -o ../agents/iguana *.o ../agents/libcrypto777.a -lnanomsg -lcurl -lssl -lcrypto -lpthread -lz -lm +#gcc -g -fno-aggressive-loop-optimizations -Wno-deprecated -c -O2 -DISNOTARYNODE=1 -DLIQUIDITY_PROVIDER=1 *.c ../basilisk/basilisk.c ../gecko/gecko.c ../datachain/datachain.c +clang -g -Wno-deprecated -c -O2 -DISNOTARYNODE=1 -DLIQUIDITY_PROVIDER=1 *.c ../basilisk/basilisk.c ../gecko/gecko.c ../datachain/datachain.c +#gcc -g -fno-aggressive-loop-optimizations -Wno-deprecated -c -DISNOTARYNODE=1 -DLIQUIDITY_PROVIDER=1 main.c iguana777.c iguana_bundles.c ../basilisk/basilisk.c +clang -g -Wno-deprecated -c -DISNOTARYNODE=1 -DLIQUIDITY_PROVIDER=1 main.c iguana777.c iguana_bundles.c ../basilisk/basilisk.c +clang -g -o ../agents/iguana *.o ../agents/libcrypto777.a -lnanomsg -lcurl -lssl -lcrypto -lpthread -lz -lm ../agents/iguana notary & #> iguana.log 2> error.log & diff --git a/iguana/main.c b/iguana/main.c index fbbdf2a63..eccec5306 100755 --- a/iguana/main.c +++ b/iguana/main.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -676,6 +676,8 @@ void iguana_ensuredirs() sprintf(dirname,"%s/ECB",GLOBAL_DBDIR), OS_ensure_directory(dirname); sprintf(dirname,"%s/BTC",GLOBAL_VALIDATEDIR), OS_ensure_directory(dirname); sprintf(dirname,"%s/BTCD",GLOBAL_VALIDATEDIR), OS_ensure_directory(dirname); + sprintf(dirname,"SVM"), OS_ensure_directory(dirname); + sprintf(dirname,"SVM/rawfeatures"), OS_ensure_directory(dirname); } void iguana_Qinit() @@ -1558,10 +1560,8 @@ FOUR_STRINGS(SuperNET,login,handle,password,permanentfile,passphrase) if ( (str= SuperNET_encryptjson(myinfo,coin,argjson,remoteaddr,password,myinfo->permanentfile,myinfo->decryptstr == 0 ? "" : myinfo->decryptstr)) != 0 ) free(str); myinfo->expiration = (uint32_t)(time(NULL) + 3600); - printf("(%s) logged into (%s) %s %s\n",password,myinfo->myaddr.NXTADDR,myinfo->myaddr.BTC,myinfo->myaddr.BTCD); return(SuperNET_activehandle(IGUANA_CALLARGS)); } else return(clonestr("{\"error\":\"need passphrase\"}")); - printf("(%s) logged into (%s) %s %s\n",password,myinfo->myaddr.NXTADDR,myinfo->myaddr.BTC,myinfo->myaddr.BTCD); return(SuperNET_activehandle(IGUANA_CALLARGS)); } @@ -1619,9 +1619,10 @@ void iguana_main(void *arg) portable_mutex_init(&myinfo->dpowmutex); portable_mutex_init(&myinfo->notarymutex); #if LIQUIDITY_PROVIDER + myinfo->tradingexchanges[myinfo->numexchanges++] = exchange_create(clonestr("nxtae"),0); myinfo->tradingexchanges[myinfo->numexchanges++] = exchange_create(clonestr("bitcoin"),0); myinfo->tradingexchanges[myinfo->numexchanges++] = exchange_create(clonestr("poloniex"),0); - //myinfo->tradingexchanges[myinfo->numexchanges++] = exchange_create(clonestr("bittrex"),0); + myinfo->tradingexchanges[myinfo->numexchanges++] = exchange_create(clonestr("bittrex"),0); myinfo->tradingexchanges[myinfo->numexchanges++] = exchange_create(clonestr("btc38"),0); myinfo->tradingexchanges[myinfo->numexchanges++] = exchange_create(clonestr("huobi"),0); myinfo->tradingexchanges[myinfo->numexchanges++] = exchange_create(clonestr("coinbase"),0); diff --git a/iguana/ramchain_api.c b/iguana/ramchain_api.c index 59fe53824..e14d744d4 100755 --- a/iguana/ramchain_api.c +++ b/iguana/ramchain_api.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/iguana/tests/arbinit b/iguana/tests/arbinit new file mode 100755 index 000000000..991c3fa70 --- /dev/null +++ b/iguana/tests/arbinit @@ -0,0 +1,75 @@ +#!/bin/bash +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"amlp\"}" + +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTC\",\"vals\":{\"rel\":\"USD\",\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTC\",\"vals\":{\"rel\":\"USD\",\"profit\":0.004,\"exchange\":\"coinbase\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTC\",\"vals\":{\"rel\":\"USD\",\"profit\":0.004,\"exchange\":\"lakebtc\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTC\",\"vals\":{\"rel\":\"USD\",\"profit\":0.004,\"exchange\":\"quadriga\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTC\",\"vals\":{\"rel\":\"USD\",\"profit\":0.004,\"exchange\":\"btcd\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTC\",\"vals\":{\"rel\":\"USD\",\"profit\":0.004,\"exchange\":\"bitstamp\"}}" + +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTC\",\"vals\":{\"rel\":\"CNY\",\"profit\":0.004,\"exchange\":\"huobi\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTC\",\"vals\":{\"rel\":\"CNY\",\"profit\":0.004,\"exchange\":\"btc38\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"amlp\"}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTCD\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTCD\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"CLOAK\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"CLOAK\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"SYS\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"SYS\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"GAME\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"GAME\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"XMR\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"XMR\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"ETH\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"ETH\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"ETC\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"ETC\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"DASH\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"DASH\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"ZEC\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"ZEC\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"FCT\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"FCT\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"STRAT\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"STRAT\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"DGD\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"DGD\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"NAV\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"NAV\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"LTC\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"LTC\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"SDC\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"SDC\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"STEEM\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"STEEM\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"SBD\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"SBD\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"XRP\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"XRP\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"LSK\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"LSK\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"NXT\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"NXT\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"MAID\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"MAID\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BLK\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BLK\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"POT\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"POT\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"XEM\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"XEM\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"VRC\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"VRC\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"VTC\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"VTC\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTS\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BTS\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"NAUT\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"NAUT\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"AUR\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"AUR\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"REP\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"REP\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"AMP\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"AMP\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" diff --git a/iguana/tests/arbnxt b/iguana/tests/arbnxt new file mode 100755 index 000000000..0e7b25cf3 --- /dev/null +++ b/iguana/tests/arbnxt @@ -0,0 +1,16 @@ +#!/bin/bash +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"amlp\"}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"NXT\",\"vals\":{\"profit\":0.004,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"NXT\",\"vals\":{\"profit\":0.004,\"exchange\":\"bittrex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"UNITY\",\"vals\":{\"profit\":0.01,\"exchange\":\"poloniex\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"UNITY\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"DEX\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"PANGEA\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"JUMBLR\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BET\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"CRYPTO\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"HODL\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"SHARK\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"BOTS\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebot\",\"method\":\"liquidity\",\"targetcoin\":\"MGW\",\"vals\":{\"rel\":\"NXT\",\"profit\":0.01,\"exchange\":\"nxtae\"}}" + diff --git a/iguana/tests/gensvm b/iguana/tests/gensvm new file mode 100755 index 000000000..b5b6625a9 --- /dev/null +++ b/iguana/tests/gensvm @@ -0,0 +1,3 @@ +#!/bin/bash +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebots\",\"method\":\"gensvm\",\"base\":\"BTC\",\"rel\":\"USD\"}" +#curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"tradebots\",\"method\":\"gensvm\",\"base\":\"BTCD\",\"rel\":\"BTC\"}" diff --git a/includes/iguana_apideclares.h b/includes/iguana_apideclares.h index b3ff9e271..bf81a3f7b 100755 --- a/includes/iguana_apideclares.h +++ b/includes/iguana_apideclares.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -43,6 +43,7 @@ ZERO_ARGS(pax,start); HASH_ARRAY_STRING(tradebot,liquidity,hash,vals,targetcoin); ZERO_ARGS(tradebot,amlp); ZERO_ARGS(tradebot,notlp); +TWO_STRINGS(tradebots,gensvm,base,rel); INT_AND_ARRAY(iguana,rates,unused,quotes); TWO_STRINGS(iguana,rate,base,rel); diff --git a/includes/iguana_defines.h b/includes/iguana_defines.h index 7fbf0a817..942336699 100755 --- a/includes/iguana_defines.h +++ b/includes/iguana_defines.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/includes/iguana_funcs.h b/includes/iguana_funcs.h index a90ca45b8..efba8cba3 100755 --- a/includes/iguana_funcs.h +++ b/includes/iguana_funcs.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * @@ -134,6 +134,7 @@ double PoW_from_compact(uint32_t nBits,uint8_t unitval); void calc_rmd160(char *hexstr,uint8_t buf[20],uint8_t *msg,int32_t len); void calc_OP_HASH160(char *hexstr,uint8_t hash160[20],char *msg); double dxblend(double *destp,double val,double decay); +double _xblend(float *destp,double val,double decay); // json int32_t iguana_processjsonQ(struct iguana_info *coin); // reentrant, can be called during any idletime diff --git a/includes/iguana_globals.h b/includes/iguana_globals.h index 5d8c57f94..cc0a13359 100755 --- a/includes/iguana_globals.h +++ b/includes/iguana_globals.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/includes/iguana_structs.h b/includes/iguana_structs.h index 6cf3766a3..0552c6854 100755 --- a/includes/iguana_structs.h +++ b/includes/iguana_structs.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * diff --git a/includes/iguana_types.h b/includes/iguana_types.h index 7b953cea1..4a1b4647e 100755 --- a/includes/iguana_types.h +++ b/includes/iguana_types.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright © 2014-2016 The SuperNET Developers. * + * Copyright © 2014-2017 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright *