void MyMoneyStorageDump::dumpPriceHistory(QTextStream& s, const equity_price_history history) { if (history.count() != 0) { s << " Price History:\n"; equity_price_history::const_iterator it_price = history.begin(); while (it_price != history.end()) { s << " " << it_price.key().toString() << ": " << it_price.data().toDouble() << "\n"; it_price++; } } }
const QString KInvestmentListItem::calculateGain(const equity_price_history& history, int dayDifference, int monthDifference, bool YTD, bool& bNegative) { bNegative = false; if(history.isEmpty()) { return QString("0.0%"); } else { bool bFoundCurrent = false, bFoundComparison = false; QDate tempDate, comparisonDate = QDate::currentDate(); if(YTD) { //if it is YTD, set the date to 01/01/<current year> comparisonDate.setYMD(comparisonDate.year(), 1, 1); } else { comparisonDate = comparisonDate.addDays(dayDifference); comparisonDate = comparisonDate.addMonths(monthDifference); } MyMoneyMoney comparisonValue, currentValue; //find the current value, or closest to the current value. equity_price_history::ConstIterator itToday = history.end(); for(tempDate = QDate::currentDate(); tempDate >= comparisonDate; ) { itToday = history.find(tempDate); if(itToday != history.end()) { currentValue = itToday.data(); bFoundCurrent = true; break; } tempDate = tempDate.addDays(-1); } if(!bFoundCurrent) { return QString("0.0%"); } //find a date that is closest to a week old, not older, and not today's date. Because its a QMap, this map //should already be sorted earliest to latest. for(equity_price_history::ConstIterator it = history.begin(); it != history.end(); ++it) { if(it.key() >= comparisonDate && it.key() < QDate::currentDate()) { comparisonDate = it.key(); comparisonValue = it.data(); bFoundComparison = true; break; } } if(!bFoundComparison) { return QString("0.0%"); } //qDebug("Current date/value to use is %s/%s, Previous is %s/%s", tempDate.toString().data(), currentValue.toString().data(), comparisonDate.toString().data(), comparisonValue.toString().data()); //compute the percentage difference if(comparisonValue != currentValue) { double result = (currentValue.toDouble() / comparisonValue.toDouble()) * 100.0; result -= 100.0; if(result < 0.0) { bNegative = true; } QString ds = QString("%1%").arg(result, 0, 'f', 3); return ds; /*MyMoneyMoney result = (currentValue / comparisonValue); result = result * 100; result = result - 100; qDebug("final result = %s", result.toString().data()); return QString(result.formatMoney("", 3) + "%");*/ } } return QString(""); }