English 中文(简体)
How should I construct a finance DSL?
原标题:

I ve decided to build a database tracking the credit card offers I get in the mail, and one confounding factor has been how to represent the offers. With minimum finance charges and variable rates and mimimum variable rates it gets a bit more complex than "10.99 percent". I ve decided I need to construct a language to represent these things if I have a shot at comparing or trending these offers.

The question is, how do I represent this? The formulas are far too complex to model by creating fields, so I ll just store the formula in the DB in all it s glory. Yes, this means blurring the line between data and execution, but since this is a personal project I m not so worried.

  1. Embedded python. Represent the formula as Python and just bind relevant variables and call the formula.
  2. Use a Domain Specific Language. I can t be the first person to think of representing things this way, and in fact I know I m not. Are there any free, public languages that can represent this?

Can anyone offer advice on which approach is better?

最佳回答

This sounds somewhat similar to the work by Simon Peyton Jones et al. on representing financial contracts using combinator libraries, including an implementation in Haskell, including the ability to run evaluation functions over them to compute the value of the contract given some model. I don t know enough about finance to tell you if this is exactly what you re looking for, but it sounds like it s in the right ballpark.

As to your specific question, I generally prefer embedded domain specific languages (that is, DSL-like constructs embedded in a host language, such as Ruby with its object oriented metaprogramming, Scheme with its macros, or Haskell combinator libraries), since it give you the full power of the host language for free; usually when writing a domain specific language with its own syntax, execution model, runtime, and so on, you run into things that would be easy to do if you had an existing language available, but are a pain to deal with since you haven t implemented something in your DSL.

问题回答

My vote is for Embedded Python, if you feel you must store the formulas in the database.

A DSL seems like overkill; you already have a perfectly good language for evaluating formulas.

Another option is to use R. It has a nice formula representation; you store it as a string and then evaluate it with the eval() function. There are also extensive financial libraries (see the CRAN finance view), including Rmetrics.

R also interacts very nicely with other languages, including python, java, and C++. You mentioned Python: you can use RPy if you want to write a wrapper script.





相关问题
How to model a many-to-many relationship in App Engine?

I have a question regarding how to model a many-to-many relationship in App Engine: A Blogentry can have many tags, a tag can apply to many blog entries. I see a couple of scenarios: Use a Set of ...

How to emulate tagged union in a database?

What is the best way to emulate Tagged union in databases? I m talking about something like this: create table t1 { vehicle_id INTEGER NOT NULL REFERENCES car(id) OR motor(id) -- not valid ... } ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

How to best implement a 1:1 relationship in a RDBMS?

Yesterday while working on a project I came up on a peculiar 1:1 relationship which left me wondering - how to best implement this (clearly, we had done it wrong :D) The idea is that there are two ...

Automatic filling entity properties

I have some type an architect(?) question I develop an application, based on Spring and Hibernate (annotated configuration) For each table in my database I added 4 fields: createdBy and modifiedBy(...

热门标签