Browse Source

Buy/sell margin

etomic
jl777 7 years ago
parent
commit
16e6cc7eaf
  1. 43
      iguana/exchanges/LP_portfolio.c
  2. 9
      iguana/exchanges/LP_prices.c

43
iguana/exchanges/LP_portfolio.c

@ -23,7 +23,7 @@ struct LP_portfoliotrade { double metric; char buycoin[65],sellcoin[65]; };
struct LP_autoprice_ref
{
char refbase[65],refrel[65],base[65],rel[65],fundbid[16],fundask[16];
double margin,factor,offset,lastbid,lastask;
double buymargin,sellmargin,factor,offset,lastbid,lastask;
cJSON *fundvalue;
uint32_t count;
} LP_autorefs[1024];
@ -262,8 +262,8 @@ void LP_autopriceset(int32_t ind,void *ctx,int32_t dir,struct LP_priceinfo *base
{
static uint32_t lasttime;
double margin,minprice,newprice,oppomargin,fixedprice,factor,offset; double bid,ask; int32_t changed;
margin = basepp->margins[relpp->ind];
oppomargin = relpp->margins[basepp->ind];
margin = basepp->sellmargins[relpp->ind];
oppomargin = basepp->buymargins[relpp->ind];
if ( (fixedprice= basepp->fixedprices[relpp->ind]) > SMALLVAL )
{
LP_mypriceset(&changed,relpp->symbol,basepp->symbol,fixedprice);
@ -450,7 +450,7 @@ double LP_tickered_price(int32_t bidask,char *base,char *rel,double price,cJSON
void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
{
static cJSON *tickerjson; static uint32_t lasttime;
char *retstr,*base,*rel; cJSON *retjson,*bid,*ask,*fundjson,*argjson; uint64_t bidsatoshis,asksatoshis; int32_t i,changed; double bch_usd,bch_btc,nxtkmd,price,factor,offset,newprice,margin,price_btc,price_usd,kmd_btc,kmd_usd; struct LP_priceinfo *kmdpp,*fiatpp,*nxtpp,*basepp,*relpp;
char *retstr,*base,*rel; cJSON *retjson,*bid,*ask,*fundjson,*argjson; uint64_t bidsatoshis,asksatoshis; int32_t i,changed; double bch_usd,bch_btc,nxtkmd,price,factor,offset,newprice,buymargin,sellmargin,price_btc,price_usd,kmd_btc,kmd_usd; struct LP_priceinfo *kmdpp,*fiatpp,*nxtpp,*basepp,*relpp;
if ( (retstr= issue_curlt("https://bittrex.com/api/v1.1/public/getmarketsummaries",LP_HTTP_TIMEOUT*10)) == 0 )
{
printf("trex error getting marketsummaries\n");
@ -525,7 +525,8 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
{
rel = LP_autorefs[i].rel;
base = LP_autorefs[i].base;
margin = LP_autorefs[i].margin;
buymargin = LP_autorefs[i].buymargin;
sellmargin = LP_autorefs[i].sellmargin;
offset = LP_autorefs[i].offset;
factor = LP_autorefs[i].factor;
if ( (argjson= LP_autorefs[i].fundvalue) != 0 )
@ -538,7 +539,7 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
{
if ( tickerjson != 0 && LP_autorefs[i].count == 0 )
price = LP_tickered_price(0,base,rel,price,tickerjson);
newprice = (1. / price) * (1. + margin);
newprice = (1. / price) * (1. + buymargin);
if ( LP_autorefs[i].lastbid < SMALLVAL )
LP_autorefs[i].lastbid = newprice;
else LP_autorefs[i].lastbid = (LP_autorefs[i].lastbid * 0.9) + (0.1 *newprice);
@ -551,7 +552,7 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
{
if ( tickerjson != 0 && LP_autorefs[i].count == 0 )
price = LP_tickered_price(1,base,rel,price,tickerjson);
newprice = (price * (1. + margin));
newprice = (price * (1. + sellmargin));
if ( LP_autorefs[i].lastask < SMALLVAL )
LP_autorefs[i].lastask = newprice;
else LP_autorefs[i].lastask = (LP_autorefs[i].lastask * 0.9) + (0.1 *newprice);
@ -579,7 +580,7 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
else continue;
if ( factor > 0. )
price = (price * factor) + offset;
newprice = (price * (1. + margin));
newprice = (price * (1. + buymargin));
if ( LP_autorefs[i].lastbid < SMALLVAL )
LP_autorefs[i].lastbid = newprice;
else LP_autorefs[i].lastbid = (LP_autorefs[i].lastbid * 0.9) + (0.1 *newprice);
@ -587,7 +588,7 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
LP_mypriceset(&changed,rel,base,newprice);
LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,rel,base,newprice);
//printf("price %.8f margin %.8f newprice %.8f %.8f\n",price,margin,newprice,(1. / price) * (1. + margin));
newprice = (1. / price) * (1. + margin);
newprice = (1. / price) * (1. + sellmargin);
if ( LP_autorefs[i].lastask < SMALLVAL )
LP_autorefs[i].lastask = newprice;
else LP_autorefs[i].lastask = (LP_autorefs[i].lastask * 0.9) + (0.1 *newprice);
@ -642,20 +643,30 @@ void LP_autoprices_update(char *method,char *base,double basevol,char *rel,doubl
int32_t LP_autoprice(void *ctx,char *base,char *rel,cJSON *argjson)
{
//curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"MNZ\",\"rel\":\"KMD\",\"offset\":0.1,\"refbase\":\"KMD\",\refrel\":\"BTC\",\"factor\":15000,\"margin\":0.01}"
struct LP_priceinfo *basepp,*relpp; int32_t i,retval = -1; char *fundvalue_bid,*fundvalue_ask,*refbase="",*refrel=""; double minprice,margin,offset,factor,fixedprice; cJSON *fundvalue;
struct LP_priceinfo *basepp,*relpp; int32_t i,retval = -1; char *fundvalue_bid,*fundvalue_ask,*refbase="",*refrel=""; double margin,minprice,buymargin,sellmargin,offset,factor,fixedprice; cJSON *fundvalue;
//printf("autoprice.(%s %s) %s\n",base,rel,jprint(argjson,0));
if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 )
{
if ( jobj(argjson,"minprice") != 0 )
minprice = jdouble(argjson,"minprice");
else minprice = 0.;
margin = jdouble(argjson,"margin");
if ( (margin= jdouble(argjson,"margin")) == 0. )
{
buymargin = jdouble(argjson,"buymargin");
sellmargin = jdouble(argjson,"sellmargin");
}
else
{
buymargin = margin;
sellmargin = margin;
}
offset = jdouble(argjson,"offset");
factor = jdouble(argjson,"factor");
fixedprice = jdouble(argjson,"fixed");
basepp->fixedprices[relpp->ind] = fixedprice;
basepp->minprices[relpp->ind] = minprice;
basepp->margins[relpp->ind] = margin;
basepp->buymargins[relpp->ind] = buymargin;
basepp->sellmargins[relpp->ind] = sellmargin;
basepp->offsets[relpp->ind] = offset;
basepp->factors[relpp->ind] = factor;
refbase = jstr(argjson,"refbase");
@ -682,7 +693,8 @@ int32_t LP_autoprice(void *ctx,char *base,char *rel,cJSON *argjson)
safecopy(LP_autorefs[i].fundbid,fundvalue_bid,sizeof(LP_autorefs[i].fundbid));
safecopy(LP_autorefs[i].fundask,fundvalue_ask,sizeof(LP_autorefs[i].fundask));
}
LP_autorefs[i].margin = margin;
LP_autorefs[i].buymargin = buymargin;
LP_autorefs[i].sellmargin = sellmargin;
LP_autorefs[i].factor = factor;
LP_autorefs[i].offset = offset;
safecopy(LP_autorefs[i].refbase,refbase,sizeof(LP_autorefs[i].refbase));
@ -702,14 +714,15 @@ int32_t LP_autoprice(void *ctx,char *base,char *rel,cJSON *argjson)
safecopy(LP_autorefs[num_LP_autorefs].fundbid,fundvalue_bid,sizeof(LP_autorefs[num_LP_autorefs].fundbid));
safecopy(LP_autorefs[num_LP_autorefs].fundask,fundvalue_ask,sizeof(LP_autorefs[num_LP_autorefs].fundask));
}
LP_autorefs[num_LP_autorefs].margin = margin;
LP_autorefs[i].buymargin = buymargin;
LP_autorefs[i].sellmargin = sellmargin;
LP_autorefs[num_LP_autorefs].factor = factor;
LP_autorefs[num_LP_autorefs].offset = offset;
safecopy(LP_autorefs[num_LP_autorefs].refbase,refbase,sizeof(LP_autorefs[num_LP_autorefs].refbase));
safecopy(LP_autorefs[num_LP_autorefs].refrel,refrel,sizeof(LP_autorefs[num_LP_autorefs].refrel));
safecopy(LP_autorefs[num_LP_autorefs].base,base,sizeof(LP_autorefs[num_LP_autorefs].base));
safecopy(LP_autorefs[num_LP_autorefs].rel,rel,sizeof(LP_autorefs[num_LP_autorefs].rel));
printf("%d Using ref %s/%s for %s/%s factor %.8f, offset %.8f, margin %.8f fixed %.8f\n",num_LP_autorefs,refbase,refrel,base,rel,factor,offset,margin,fixedprice);
printf("%d Using ref %s/%s for %s/%s factor %.8f, offset %.8f, margin %.8f/%.8f fixed %.8f\n",num_LP_autorefs,refbase,refrel,base,rel,factor,offset,buymargin,sellmargin,fixedprice);
num_LP_autorefs++;
}
}

9
iguana/exchanges/LP_prices.c

@ -38,7 +38,8 @@ struct LP_priceinfo
double myprices[LP_MAXPRICEINFOS];
double minprices[LP_MAXPRICEINFOS]; // autoprice
double fixedprices[LP_MAXPRICEINFOS]; // fixedprices
double margins[LP_MAXPRICEINFOS];
double buymargins[LP_MAXPRICEINFOS];
double sellmargins[LP_MAXPRICEINFOS];
double offsets[LP_MAXPRICEINFOS];
double factors[LP_MAXPRICEINFOS];
} LP_priceinfos[LP_MAXPRICEINFOS];
@ -62,7 +63,8 @@ void LP_priceinfos_clear()
memset(pp->myprices,0,sizeof(pp->myprices));
memset(pp->minprices,0,sizeof(pp->minprices));
memset(pp->fixedprices,0,sizeof(pp->fixedprices));
memset(pp->margins,0,sizeof(pp->margins));
memset(pp->buymargins,0,sizeof(pp->buymargins));
memset(pp->sellmargins,0,sizeof(pp->sellmargins));
memset(pp->offsets,0,sizeof(pp->offsets));
memset(pp->factors,0,sizeof(pp->factors));
}
@ -520,7 +522,8 @@ int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price)
{
relpp->minprices[basepp->ind] = 0.;
relpp->fixedprices[basepp->ind] = 0.;
relpp->margins[basepp->ind] = 0.;
relpp->buymargins[basepp->ind] = 0.;
relpp->sellmargins[basepp->ind] = 0.;
relpp->offsets[basepp->ind] = 0.;
relpp->factors[basepp->ind] = 0.;
}

Loading…
Cancel
Save