diff --git a/crypto777/OS_portable.h b/crypto777/OS_portable.h index 5229414a6..84ba44794 100755 --- a/crypto777/OS_portable.h +++ b/crypto777/OS_portable.h @@ -198,6 +198,8 @@ int32_t OS_nonportable_init(); void OS_portable_init(); void OS_init(); +int32_t sortds(double *buf,uint32_t num,int32_t size); +int32_t revsortds(double *buf,uint32_t num,int32_t size); double OS_portable_milliseconds(); void OS_portable_randombytes(uint8_t *x,long xlen); diff --git a/crypto777/iguana_utils.c b/crypto777/iguana_utils.c index 47ab6f5a4..2d3ba301c 100755 --- a/crypto777/iguana_utils.c +++ b/crypto777/iguana_utils.c @@ -522,11 +522,24 @@ 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); + if ( double_b > double_a ) + return(-1); + else if ( double_b < double_a ) + return(1); + return(0); +#undef double_a +#undef double_b +} + +static int _decreasing_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 } @@ -572,8 +585,14 @@ static int _decreasing_uint32(const void *a,const void *b) int32_t sortds(double *buf,uint32_t num,int32_t size) { - qsort(buf,num,size,_increasing_double); - return(0); + qsort(buf,num,size,_increasing_double); + return(0); +} + +int32_t refsortds(double *buf,uint32_t num,int32_t size) +{ + qsort(buf,num,size,_decreasing_double); + return(0); } int32_t sort64s(uint64_t *buf,uint32_t num,int32_t size) diff --git a/iguana/exchanges/LP_portfolio.c b/iguana/exchanges/LP_portfolio.c index 2dd82b5ff..231f9b8f6 100644 --- a/iguana/exchanges/LP_portfolio.c +++ b/iguana/exchanges/LP_portfolio.c @@ -416,9 +416,111 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp) } } +int32_t LP_portfolio_trade(void *ctx,uint32_t *requestidp,uint32_t *quoteidp,struct iguana_info *buy,struct iguana_info *sell,double relvolume,int32_t setbaserel) +{ + char *retstr2; double bid,ask,maxprice; uint32_t requestid,quoteid,iter; cJSON *retjson2; + requestid = quoteid = 0; + LP_myprice(&bid,&ask,buy->symbol,sell->symbol); + maxprice = ask; + if ( setbaserel != 0 ) + { + strcpy(LP_portfolio_base,""); + strcpy(LP_portfolio_rel,""); + LP_portfolio_relvolume = 0.; + } + if ( LP_pricevalid(maxprice) > 0 ) + { + printf("pending.%d base buy.%s, rel sell.%s relvolume %f maxprice %.8f (%.8f %.8f)\n",LP_pendingswaps,buy->symbol,sell->symbol,sell->relvolume,maxprice,bid,ask); + relvolume = sell->relvolume; + for (iter=0; iter<3; iter++) + { + if ( relvolume < dstr(LP_MIN_TXFEE) ) + break; + if ( LP_utxo_bestfit(sell->symbol,SATOSHIDEN * relvolume) != 0 ) + { + if ( (retstr2= LP_autotrade(ctx,"127.0.0.1",-1,buy->symbol,sell->symbol,maxprice,relvolume,60,24*3600)) != 0 ) + { + if ( (retjson2= cJSON_Parse(retstr2)) != 0 ) + { + if ( (requestid= juint(retjson2,"requestid")) != 0 && (quoteid= juint(retjson2,"quoteid")) != 0 ) + { + + } + free_json(retjson2); + } + printf("%s relvolume %.8f LP_autotrade.(%s)\n",sell->symbol,relvolume,retstr2); + free(retstr2); + } + if ( requestid != 0 && quoteid != 0 ) + break; + } else printf("cant find alice %.8f %s\n",relvolume,sell->symbol); + relvolume /= 3.; + } + } + else if ( setbaserel != 0 ) + { + strcpy(LP_portfolio_base,buy->symbol); + strcpy(LP_portfolio_rel,sell->symbol); + LP_portfolio_relvolume = sell->relvolume; + } + *requestidp = requestid; + *quoteidp = quoteid; + if ( requestid != 0 && quoteid != 0 ) + return(0); + else return(-1); +} + +struct LP_portfoliotrade { double metric; char buycoin[16],sellcoin[16]; }; + +int32_t LP_portfolio_order(struct LP_portfoliotrade *trades,int32_t max,cJSON *array) +{ + int32_t i,j,m,n = 0; cJSON *item; struct LP_portfoliotrade coins[256]; + memset(coins,0,sizeof(coins)); + if ( (m= cJSON_GetArraySize(array)) > 0 ) + { + for (i=j=0; i SMALLVAL && coins[j].buycoin[0] != 0 ) + j++; + } + if ( (m= j) > 1 ) + { + for (i=n=0; i 0. ) + { + trades[n].metric = (coins[i].metric - coins[j].metric); + strcpy(trades[n].buycoin,coins[i].buycoin); + strcpy(trades[n].sellcoin,coins[j].buycoin); + printf("buy %s %f, sell %s %f -> %f\n",trades[n].buycoin,coins[i].metric,trades[n].sellcoin,coins[j].metric,trades[n].metric); + } + else + { + trades[n].metric = (coins[j].metric - coins[i].metric); + strcpy(trades[n].buycoin,coins[j].buycoin); + strcpy(trades[n].sellcoin,coins[i].buycoin); + printf("buy %s %f, sell %s %f -> %f\n",trades[n].buycoin,coins[j].metric,trades[n].sellcoin,coins[i].metric,trades[n].metric); + } + n++; + if ( n >= max ) + break; + } + revsortds((void *)trades,n,sizeof(*trades)); + for (i=0; i %f\n",i,trades[i].buycoin,trades[i].sellcoin,trades[i].metric); + } + } + return(n); +} + void prices_loop(void *ignore) { - char *buycoin,*sellcoin,*retstr,*retstr2; double bid,ask,maxprice,relvolume; struct iguana_info *buy,*sell; uint32_t requestid,quoteid,iter; cJSON *retjson,*retjson2; struct LP_priceinfo *btcpp; void *ctx = bitcoin_ctx(); + char *retstr; cJSON *retjson,*array; char *buycoin,*sellcoin; struct iguana_info *buy,*sell; uint32_t requestid,quoteid; int32_t i,n,m; struct LP_portfoliotrade trades[256]; struct LP_priceinfo *btcpp; void *ctx = bitcoin_ctx(); while ( 1 ) { if ( (btcpp= LP_priceinfofind("BTC")) == 0 ) @@ -434,51 +536,24 @@ void prices_loop(void *ignore) { if ( (buycoin= jstr(retjson,"buycoin")) != 0 && (buy= LP_coinfind(buycoin)) != 0 && (sellcoin= jstr(retjson,"sellcoin")) != 0 && (sell= LP_coinfind(sellcoin)) != 0 && buy->inactive == 0 && sell->inactive == 0 ) { - LP_myprice(&bid,&ask,buycoin,sellcoin); - maxprice = ask; - strcpy(LP_portfolio_base,""); - strcpy(LP_portfolio_rel,""); - LP_portfolio_relvolume = 0.; - if ( LP_pricevalid(maxprice) > 0 ) + if ( LP_portfolio_trade(ctx,&requestid,"eid,buy,sell,sell->relvolume,1) < 0 ) { - printf("pending.%d base buy.%s force %f, rel sell.%s force %f relvolume %f maxprice %.8f (%.8f %.8f)\n",LP_pendingswaps,buycoin,jdouble(retjson,"buyforce"),sellcoin,jdouble(retjson,"sellforce"),sell->relvolume,maxprice,bid,ask); - //if ( LP_pendingswaps == 0 ) + array = jarray(&m,retjson,"portfolio"); + if ( array != 0 && (n= LP_portfolio_order(trades,(int32_t)(sizeof(trades)/sizeof(*trades)),array)) > 0 ) { - relvolume = sell->relvolume; - for (iter=0; iter<2; iter++) + for (i=0; irelvolume,0) == 0 ) break; - } else printf("cant find alice %.8f %s\n",relvolume,sellcoin); - relvolume *= 0.1; + } } } } - else - { - strcpy(LP_portfolio_base,buycoin); - strcpy(LP_portfolio_rel,sellcoin); - LP_portfolio_relvolume = sell->relvolume; - } - } //else printf("buy or sell missing.(%s)\n",jprint(retjson,0)); + } free_json(retjson); } free(retstr);