Why do I get an error when building a zero coupon inflation swap helper in Quantlib? - quantlib

I am trying to create a ZC Inflation Swap Helper but I get an error for the below code, does anyone know what the issue is?
import QuantLib as ql
quote = ql.QuoteHandle(ql.SimpleQuote(0.02))
period = ql.Period('3M')
date = ql.Date(15,3,2022)
calendar = ql.TARGET()
convention = ql.ModifiedFollowing
daycounter = ql.Actual360()
index = ql.EUHICPXT(True)
helper = ql.ZeroCouponInflationSwapHelper(quote, period, date, calendar, convention, daycounter, index)
{TypeError}Wrong number or type of arguments for overloaded function 'new_ZeroCouponInflationSwapHelper'.
Possible C/C++ prototypes are:
ZeroCouponInflationSwapHelper::ZeroCouponInflationSwapHelper(Handle< Quote > const &,Period const &,Date const &,Calendar const &,BusinessDayConvention,DayCounter const &,ext::shared_ptr< ZeroInflationIndex > const &,CPI::InterpolationType,Handle< YieldTermStructure > const &)
ZeroCouponInflationSwapHelper::ZeroCouponInflationSwapHelper(Handle< Quote > const &,Period const &,Date const &,Calendar const &,BusinessDayConvention,DayCounter const &,ext::shared_ptr< ZeroInflationIndex > const &,Handle< YieldTermStructure > const &)
This is the same code as the QuantLib userguide...

Posted the issue on Github ... looks like documentation hasn't been updated since the last QuantLib update.
https://github.com/lballabio/QuantLib/issues/1360
The following should work:
import QuantLib as ql
quote = ql.QuoteHandle(ql.SimpleQuote(0.02))
period = ql.Period('6M')
date = ql.Date(15,6,2020)
calendar = ql.TARGET()
convention = ql.ModifiedFollowing
daycounter = ql.Actual360()
index = ql.EUHICPXT(True)
flatForward = ql.FlatForward(ql.Date(15,6,2020), ql.QuoteHandle(ql.SimpleQuote(0.05)), ql.Actual360())
yieldTermStructure = ql.YieldTermStructureHandle(flatForward)
helper = ql.ZeroCouponInflationSwapHelper(quote, period, date, calendar, convention, daycounter, index, yieldTermStructure)

Related

Passing an empty set to a function with type const std::set<std::string>& my_set_that_can_be_empty

I'm trying to pass a set of strings to a c++ function. That works fine. However, in some cases the set is empty and I get a type error.
It may be that this is a c++ problem or it may also be that it is connected that I call the function fro python via pybind11.
Any suggestions on how this can be elegantly solved are highly appreciated.
I have posted an example here:
https://github.com/dickreuter/neuron_poker/blob/master/tests/test_montecarlo_cpp.py#L44
def test_montecarlo4():
"""Montecarlo test"""
my_cards = {'AS', 'KS'}
cards_on_table = {}
expected_results = 66.1 + 1.6
players = 2
_runner(my_cards, cards_on_table, players, expected_results)
def _runner(my_cards, cards_on_table, players, expected_result):
"""Montecarlo test"""
# if len(cards_on_table) < 3:
# cards_on_table = {'null'}
equity = calculator.montecarlo(my_cards, cards_on_table, players, 5000)
assert equity == pytest.approx(expected_result, abs=1)
calling
https://github.com/dickreuter/neuron_poker/blob/master/tools/Montecarlo.cpp#L266
double montecarlo(const std::set<std::string>& my_cards, const std::set<std::string>& cards_on_table, const int number_of_players, const int iterations) {
resulting in:
E TypeError: montecarlo(): incompatible function arguments. The following argument types are supported:
E 1. (arg0: Set[str], arg1: Set[str], arg2: int, arg3: int) -> float

Update function broken in C++ Azure Mobile Library

I am trying to update a record in one of my Azure Mobile tables using the “update” function in the azure mobile C++ header. But I get an exception. Below is what my code looks like:
void DBUtils::DBQuestion::UpdateQuestionInTable(std::shared_ptr<azure::mobile::table> table)
{
auto obj = json::value::object();
obj[U("id")] = json::value::string(ID);
obj[U("QuestionText")] = json::value::string(QuestionText);
obj[U("AnswerLatitude")] = json::value::number(AnswerLatitude);
obj[U("AnswerLongitude")] = json::value::number(AnswerLongitude);
table->update(obj);
}
I have verified that the ID above is a valid one actually present in the table. A similar insert operation (which doesn’t specify the ID field) actually succeeds:
void DBUtils::DBQuestion::InsertIntoTable(std::shared_ptr<azure::mobile::table> table)
{
auto obj = json::value::object();
obj[U("QuestionText")] = json::value::string(QuestionText);
obj[U("AnswerLatitude")] = json::value::number(AnswerLatitude);
obj[U("AnswerLongitude")] = json::value::number(AnswerLongitude);
table->insert(obj);
}
What am I doing wrong?
Azure Mobile recently updated its table schema so that the Id field is now a string, which gets filled in by the server with a Guid value if the client doesn’t set it.
This change has introduced a bug in the C++ library. As a workaround, you can try calling the other overload for update, the one that takes the ID string and the object.
void DBUtils::DBQuestion::UpdateQuestionInTable(utility::string_t id, std::shared_ptr<azure::mobile::table> table)
{
auto obj = json::value::object();
obj[U("QuestionText")] = json::value::string(QuestionText);
obj[U("AnswerLatitude")] = json::value::number(AnswerLatitude);
obj[U("AnswerLongitude")] = json::value::number(AnswerLongitude);
ID = id;
table->update(ID, obj);
}

a C++ program that loads over 1 billion records into memory?

I have a whole years worth of option prices in a database (along with other bits of information such as strike price, volatility etc) and would like to load it into memory for my program to process (hash maps, program was written in C++). The trouble is it takes over 10 hours to load into memory at startup (from a local database).
Can someone please suggest how i can overcome this issue ? I have workarounds where i only load the portions that i need but it would be great if people can share ideas to the problem. Would loading the data from shared memory after each startup help?
Here is the code so far, in short it queries the database and loads the data into containers
const std::string m_url = "mysql://localhost/test2?user=root&password=";
URL_T optionURL = URL_new(m_url.c_str());
ConnectionPool_T optionPool = ConnectionPool_new(optionURL);
ConnectionPool_setInitialConnections(optionPool,1);
ConnectionPool_setMaxConnections(optionPool,1);
ConnectionPool_start(optionPool);
Connection_T con = ConnectionPool_getConnection(optionPool);
ResultSet_T result = Connection_executeQuery(con,
"SELECT DISTINCT(underlying) FROM Options");
PreparedStatement_T prepareStatement = Connection_prepareStatement(con,"SELECT underlying,underlyingPrice,expiry,strike,issueDate,type,delta,volatility FROM Options o,RiskFreeRate r WHERE underlying = ? AND o.issueDate = r.date");
while(ResultSet_next(result))
{
const std::string symbol = ResultSet_getString(result,1);
PreparedStatement_setString(prepareStatement,1,symbol.c_str());
ResultSet_T resultDetail = PreparedStatement_executeQuery(prepareStatement);
while(ResultSet_next(resultDetail))
{
float strike = ResultSet_getDouble(resultDetail,4);
date expiry = from_string(ResultSet_getString(resultDetail,3));
std::string issueDate = ResultSet_getString(resultDetail,5);
float underlyingPrice = ResultSet_getDouble(resultDetail,2);
float riskFreeRate = 4; //tmp hack
float volatility = ResultSet_getDouble(resultDetail,8);
OptionDateMap optionMap = m_optionMap[symbol];
OptionVec optionVec = optionMap[issueDate];
optionVec.push_back(boost::shared_ptr<WallStreet::FixedIncome::Option::Option> (new WallStreet::FixedIncome::Option::Option(strike,expiry,underlyingPrice, riskFreeRate,volatility)));
optionMap[issueDate] = optionVec;
m_optionMap[symbol] = optionMap;
}
}

string conversion to COleDateTime (CRecordset)

In a CRecordset, i want to store two dates into a database table. The fields on the database are from type datetime (MS SQL).
There are two variables named validfrom and validto from type string. The dateformat from the string-variables are DD.MM.YYYY. m_Validfrom and m_Validto is from type COleDateTime.
At CFSC_FULLTRUCK::Update(), i get a conversion error.
"Invalid character value for cast specification"
void CFSC_FULLTRUCK::setfulltruck(int nr ,CString ort,CString country ,CString zone, double cost ,CString file_index, string validfrom, string validto, double fuelinpercent, CString remark)
{
CFSC_FULLTRUCK::Open();
CFSC_FULLTRUCK::AddNew();
m_NR = nr;
m_ORT =ort;
m_COUNTRY =country;
m_ZONENO = zone;
m_COST = cost;
m_FILE_INDEX = file_index;
if(validfrom == ""){validfrom = "01.01.2001";}
if(validto == ""){validto = "31.12.2020";}
m_Validfrom.ParseDateTime(validfrom);
m_Validto.ParseDateTime(validto);
m_Fuelinpercent = fuelinpercent;
m_Remark = remark;
CFSC_FULLTRUCK::Update();
CFSC_FULLTRUCK::Close();
}

Invalid Argument to getUInt64 when retrieving LAST_INSERT_ID()

I have added a record to my table which auto-increments the primary key. I am having no luck retrieving this new value. The MySQL documents say to use the SELECT LAST_INSERT_ID(); in a query. I have done this, but can't retrieve the results.
According the the metadata of the result set, the data type is BIGINT and the column name is LAST_INSERT_ID(). The C++ connector has a getUInt64() for the result set, which I assume is the correct method to use.
The ResultSet class declaration contains the following:
virtual uint64_t getUInt64(uint32_t columnIndex) const = 0;
virtual uint64_t getUInt64(const std::string& columnLabel) const = 0;
The documentation does not state whether the columnIndex is zero based or one based. I tried both and get sql::InvalidArgumentException for both cases.
Using the result set metadata, I retrieved the column name and passed it directly to the getUInt64 method and still receive the sql::InvalidArgumentException. This not a good indication (when the returned column name doesn't work when fetching the data).
Here is my code fragment:
std::string query_text;
query_text = "SELECT LAST_INSERT_ID();";
boost::shared_ptr<sql::Statement> query(m_db_connection->createStatement());
boost::shared_ptr<sql::ResultSet> query_results(query->executeQuery(query_text));
long id_value = 0;
if (query_results)
{
ResultSetMetaData p_metadata = NULL;
p_metadata = query_results->getMetaData();
unsigned int columns = 0;
columns = p_metadata->getColumnCount();
std::string column_label;
std::string column_name;
std::string column_type;
for (i = 0; i < columns; ++i)
{
column_label = p_metadata->getColumnLabel(i);
column_name = p_metadata->getColumnName(i);
column_type = p_metadata->getColumnTypeName(i);
wxLogDebug("Column label: \"%s\"\nColumn name: \"%s\"\nColumn type: \"%s\"\n",
column_label.c_str(),
column_name.c_str(),
column_type.c_str());
}
unsigned int column_index = 0;
column_index = query_results->findColumn(column_name);
// The value of column_index is 1 (one).
// All of the following will generate sql::InvalidArgumentException
id_value = query_results->getUInt64(column_index);
id_value = query_results->getUInt64(column_name);
id_value = query_results->getUInt64(0);
id_value = query_results->getUInt64(1);
id_record.set_record_id(id_value);
}
Here is the debug output (from wxLogDebug):
10:50:58: Column label: "LAST_INSERT_ID()"
Column name: "LAST_INSERT_ID()"
Column type: "BIGINT"
My Question: How do I retrieve the LAST_INSERT_ID() using the MySQL C++ Connector?
Do I need to use a prepared statement instead?
I am using MySQL Connector C++ 1.0.5 on Windows Vista and Windows XP with Visual Studio 9 (2008).
Resolved.
I inserted a query_results->next() before retrieving the data and that worked.