From 0467d13a107548a613f09cc7e598086ea91535db Mon Sep 17 00:00:00 2001 From: dimxy Date: Mon, 29 Apr 2019 21:50:55 +0500 Subject: [PATCH] try use floating point to calc average costbasis --- src/cc/prices.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 30149244a..6066100a9 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1334,18 +1334,20 @@ UniValue PricesInfo(uint256 bettxid, int32_t refheight) int64_t totalbets = 0; int64_t totalprofits = 0; - int64_t costbasis = 0; + double dcostbasis = 0.0; for (auto b : bets) { totalbets += b.amount; totalprofits += b.profits; - costbasis += b.amount * (b.costbasis / PRICES_POINTFACTOR); // prevent int64 overflow - std::cerr << "PricesInfo() acc costbasis=" << costbasis << " b.amount=" << b.amount << " b.costbasis/PRICES_POINTFACTOR=" << (b.costbasis / PRICES_POINTFACTOR) << std::endl; + dcostbasis += b.amount * b.costbasis; + // costbasis += b.amount * (b.costbasis / PRICES_POINTFACTOR); // prevent int64 overflow (but we have underflow for 1/BTC) + // std::cerr << "PricesInfo() acc costbasis=" << costbasis << " b.amount=" << b.amount << " b.costbasis/PRICES_POINTFACTOR=" << (b.costbasis / PRICES_POINTFACTOR) << std::endl; } int64_t equity = totalbets + totalprofits; + int64_t costbasis; if (totalbets != 0) { //prevent zero div - costbasis *= PRICES_POINTFACTOR; // save last 0.0000xxxx positions - costbasis /= totalbets; + // costbasis *= PRICES_POINTFACTOR; // save last 0.0000xxxx positions + costbasis = (int64_t) (dcostbasis / (double)totalbets); } else costbasis = 0;