fixes to how commodities are looked up using quotes.cc
This commit is contained in:
parent
9350433499
commit
b030416982
6 changed files with 36 additions and 25 deletions
|
|
@ -980,7 +980,8 @@ amount_t commodity_t::value(const std::time_t moment)
|
|||
}
|
||||
|
||||
if (updater)
|
||||
(*updater)(this, moment, age, price);
|
||||
(*updater)(this, moment, age,
|
||||
history.size() > 0 ? (*history.rbegin()).first : 0, price);
|
||||
|
||||
return price;
|
||||
}
|
||||
|
|
|
|||
8
amount.h
8
amount.h
|
|
@ -192,8 +192,7 @@ std::ostream& operator<<(std::ostream& out, const amount_t& amt);
|
|||
#define COMMODITY_STYLE_SEPARATED 0x0002
|
||||
#define COMMODITY_STYLE_EUROPEAN 0x0004
|
||||
#define COMMODITY_STYLE_THOUSANDS 0x0008
|
||||
#define COMMODITY_STYLE_CONSULTED 0x0010
|
||||
#define COMMODITY_STYLE_NOMARKET 0x0020
|
||||
#define COMMODITY_STYLE_NOMARKET 0x0010
|
||||
|
||||
typedef std::map<const std::time_t, amount_t> history_map;
|
||||
typedef std::pair<const std::time_t, amount_t> history_pair;
|
||||
|
|
@ -210,6 +209,7 @@ class commodity_t
|
|||
virtual void operator()(commodity_t * commodity,
|
||||
const std::time_t moment,
|
||||
const std::time_t date,
|
||||
const std::time_t last,
|
||||
amount_t& price) = 0;
|
||||
};
|
||||
|
||||
|
|
@ -222,6 +222,7 @@ class commodity_t
|
|||
unsigned short precision;
|
||||
unsigned short flags;
|
||||
history_map history;
|
||||
std::time_t last_lookup;
|
||||
amount_t conversion;
|
||||
ident_t ident;
|
||||
|
||||
|
|
@ -254,7 +255,8 @@ class commodity_t
|
|||
commodity_t(const std::string& _symbol = "",
|
||||
unsigned int _precision = 0,
|
||||
unsigned int _flags = COMMODITY_STYLE_DEFAULTS)
|
||||
: symbol(_symbol), quote(false), precision(_precision), flags(_flags) {
|
||||
: symbol(_symbol), quote(false), precision(_precision),
|
||||
flags(_flags), last_lookup(0) {
|
||||
check_symbol();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
namespace ledger {
|
||||
|
||||
const unsigned long binary_magic_number = 0xFFEED765;
|
||||
static const unsigned long format_version = 0x0002000c;
|
||||
static const unsigned long format_version = 0x0002000e;
|
||||
|
||||
bool binary_parser_t::test(std::istream& in) const
|
||||
{
|
||||
|
|
@ -151,6 +151,7 @@ commodity_t * read_binary_commodity(std::istream& in)
|
|||
read_binary_amount(in, amt);
|
||||
commodity->history.insert(history_pair(when, amt));
|
||||
}
|
||||
read_binary_number(in, commodity->last_lookup);
|
||||
|
||||
read_binary_amount(in, commodity->conversion);
|
||||
|
||||
|
|
@ -351,6 +352,7 @@ void write_binary_commodity(std::ostream& out, commodity_t * commodity)
|
|||
write_binary_number(out, (*i).first);
|
||||
write_binary_amount(out, (*i).second);
|
||||
}
|
||||
write_binary_number(out, commodity->last_lookup);
|
||||
|
||||
write_binary_amount(out, commodity->conversion);
|
||||
}
|
||||
|
|
|
|||
39
quotes.cc
39
quotes.cc
|
|
@ -8,47 +8,54 @@ namespace ledger {
|
|||
void quotes_by_script::operator()(commodity_t * commodity,
|
||||
const std::time_t moment,
|
||||
const std::time_t date,
|
||||
const std::time_t last,
|
||||
amount_t& price)
|
||||
{
|
||||
if (commodity->flags & COMMODITY_STYLE_CONSULTED ||
|
||||
std::difftime(moment, now) < pricing_leeway ||
|
||||
DEBUG_CLASS("ledger.quotes.download");
|
||||
|
||||
DEBUG_PRINT_("commodity: " << commodity->symbol);
|
||||
DEBUG_PRINT_TIME_(now);
|
||||
DEBUG_PRINT_TIME_(moment);
|
||||
DEBUG_PRINT_TIME_(date);
|
||||
DEBUG_PRINT_TIME_(last);
|
||||
DEBUG_PRINT_TIME_(commodity->last_lookup);
|
||||
DEBUG_PRINT_("pricing_leeway is " << pricing_leeway);
|
||||
|
||||
if (std::difftime(now, commodity->last_lookup) < pricing_leeway ||
|
||||
std::difftime(now, last) < pricing_leeway ||
|
||||
(price && std::difftime(moment, date) <= pricing_leeway))
|
||||
return;
|
||||
|
||||
using namespace std;
|
||||
|
||||
DEBUG_PRINT("ledger.quotes.download",
|
||||
"downloading quote for symbol " << commodity->symbol);
|
||||
DEBUG_PRINT("ledger.quotes.download",
|
||||
"pricing_leeway is " << pricing_leeway);
|
||||
DEBUG_PRINT_TIME("ledger.quotes.download", now);
|
||||
DEBUG_PRINT_TIME("ledger.quotes.download", moment);
|
||||
DEBUG_PRINT_TIME("ledger.quotes.download", date);
|
||||
DEBUG_PRINT_("downloading quote for symbol " << commodity->symbol);
|
||||
|
||||
// Only consult the Internet once for any commodity
|
||||
commodity->flags |= COMMODITY_STYLE_CONSULTED;
|
||||
commodity->last_lookup = now;
|
||||
cache_dirty = true;
|
||||
|
||||
char buf[256];
|
||||
buf[0] = '\0';
|
||||
|
||||
bool success = true;
|
||||
|
||||
if (FILE * fp = popen((string("getquote ") +
|
||||
commodity->symbol).c_str(), "r")) {
|
||||
if (feof(fp) || ! fgets(buf, 255, fp)) {
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
if (feof(fp) || ! fgets(buf, 255, fp))
|
||||
success = false;
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
if (buf[0]) {
|
||||
if (success && buf[0]) {
|
||||
char * p = strchr(buf, '\n');
|
||||
if (p) *p = '\0';
|
||||
|
||||
DEBUG_PRINT_("downloaded quote: " << buf);
|
||||
|
||||
price.parse(buf);
|
||||
commodity->add_price(now, price);
|
||||
|
||||
if (! price_db.empty()) {
|
||||
if (price && ! price_db.empty()) {
|
||||
char buf[128];
|
||||
strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now));
|
||||
ofstream database(price_db.c_str(), ios_base::out | ios_base::app);
|
||||
|
|
|
|||
1
quotes.h
1
quotes.h
|
|
@ -21,6 +21,7 @@ class quotes_by_script : public commodity_t::updater_t
|
|||
virtual void operator()(commodity_t * commodity,
|
||||
const std::time_t moment,
|
||||
const std::time_t date,
|
||||
const std::time_t last,
|
||||
amount_t& price);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -462,8 +462,7 @@ unsigned int textual_parser_t::parse(std::istream& in,
|
|||
parse_commodity(in, symbol);
|
||||
|
||||
commodity_t * commodity = commodity_t::find_commodity(symbol, true);
|
||||
commodity->flags |= (COMMODITY_STYLE_CONSULTED |
|
||||
COMMODITY_STYLE_NOMARKET);
|
||||
commodity->flags |= COMMODITY_STYLE_NOMARKET;
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -569,8 +568,7 @@ unsigned int textual_parser_t::parse(std::istream& in,
|
|||
done:
|
||||
if (time_commodity) {
|
||||
time_commodity->precision = 2;
|
||||
time_commodity->flags |= (COMMODITY_STYLE_CONSULTED |
|
||||
COMMODITY_STYLE_NOMARKET);
|
||||
time_commodity->flags |= COMMODITY_STYLE_NOMARKET;
|
||||
}
|
||||
|
||||
if (errors > 0) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue