initialize array-of-pointer to all nulls

Suggestion 1: LimitOrder* orders[howManyOrders] = {};

Suggestion 0: P37 [[c++ coding standards]] suggests “.. = {NULL}“, consistent with P115 [[c++primer]].

See also http://bigblog.tanbin.com/2012/07/default-initializevalue-initialize-new.html

See also http://www.informit.com/articles/article.aspx?p=1852519 on c++11 array-init:

For an array (on stack or heap), an empty pair of braces indicates default initialization. Default initialization of POD types usually means initialization to binary zeros, whereas for non-POD types default initialization means default construction

parse FIX msg to key/value pairS #repeating group

http://bigblog.tanbin.com/2012/03/design-considerations-my-first-homemade.html is the context.

    /**
     * Parsing to array is faster than parsing to a HashMap
     */
    public static ArrayList quickParse(String fromServer) {
        ArrayList ret = new ArrayList(Collections.nCopies(
            MAX_FIX_TAG, (String) null));
        for (String pair : fromServer.split(SOH)) {
            String[] tmp = pair.split("=");
            if (tmp.length != 2)  continue;
            int tag = Integer.parseInt(tmp[0]);
            ret.set(tag, tmp[1]); //treat ArrayList as array -- efficient
        }
        return ret;
    }

Repeating group is not supported in this simplistic scheme, but could be accommodated. I can think one simple and one robust design:

Design 1: array<list<string>>, so a regular non-repeating tag will hit a singular list.

Design 2: we ought to know which tags are repeating. The message specification must tell us which tag (NoRoutingIds in the example) signals the arrival of a repeating group. Therefore, at compile time (not runtime) system knows what tags are special i.e. potentially repeating. We will build a separate data structure for them, such as a list<groupMember>. So each FIX message will have one regular array + one list

bond ref data service – practical caching strategy

Data change frequency is lower than market data. JMS-based cache update.

There's a thin database layer, supported by a dedicated developer — minimal (if any) stored proc. There's a thick java api layer, supported by a team of developers.

To avoid hitting the DB too frequently, there's an in-memory cache, to be updated whenever data changes in the DB. Some places use RV, some use JMS.

static nested classes unnecessary@@

As stated in another post, I always start my nested class as “private static” and relax gradually when justified.

Now, some say static nested classes can always be pulled out as first-class citizens i.e. top level classes. No. A major feature (perhaps 2) I rely on everyday is the private access modifier in the context of nested classes.

Nested class can refer to private members (fields/methods) of the enclosing class; enclosing class can refer to private members of the nested class.

virtual inheritance diamond malformed

Both C1 and C2 should virtually derive from Base. You see the D class instantiation only calling Base ctor once. See http://www.parashift.com/c++-faq-lite/virtual-inheritance-where.html

Now, what if we omit one of the “virtual” keywords? Base ctor called twice — tested in GCC

struct B {
    B() {
        cout << "B\n";
    }
};
struct C1: virtual public B {
    C1() {
        cout << "entering C1()\n";
    }
};
struct C2: virtual public B {
    C2() {
        cout << "entering C2()\n";
    }
};
class D: public C1, C2 {
};

int main() {
    D d;

select2perform c++questions #ez

Q: given char * const cp = ….; can you do cp[0] = ‘b’
AA: yes. const is about the pointer (no rebinding), not the char pointee.

Q: can friend-class to Derived access Base protected field?
AA: yes in gcc

Q: what are the narrow-character iostream objects?
A: http://bigblog.tanbin.com/2012/07/narrow-vs-wide-character-iostreams.html

Q[u]: anything wrong with static_cast(MyClass var2)
%%A: This is a silly question. Bad coding style.
AA: illegal in gcc

Q[u]: if a ctor takes an int arg, can you call it with a char literal?
AA (confirmed): implicit conversion from char to int

Q: can a C struct be subCLASSed?
AA: yes struct differs from class in one way only

Q: if u catch by value, modify the exception object, then do an empty throw all within your catch block, then the does downstream catch the original or the modified object?

Q[u]: what’s wrong with

char * charArray = someString.c_str()

%%A (confirmed): c_str() returns a pointer to const char. Const radiates leftward

Q[u]: when you pass std::less to list.sort(), must you append “()” as “std::less()”?
AA: illegal without ().
%%A: std::less is a class, so we are instantiating it.

Q[u]: can catch(…) be the first among many catchers?
AA: must be last — illegal otherwise

Q[u]: a class template’s static function uses the T as parameter type. Callable without ?
AA: this follows the CLASS template instantiation rule, not the function template instantiation rule. No implicit detection by compiler. You must provide explicit type argument when referring to the class template

Q: bind1st or bind2nd on std::less when used as find_if() filter
AA: here’s the idiom. We are binding 2nd parameter to the literal 1000.
Note std::less is a Class template not function template, so the template type arg is mandatory.

bind2nd(std::less(),1000)

central bank rate hikes bring ccy up or down@@

Higher interest rate helps a currency short-term. Inflation (sometimes associated with increasing IR) is sure to weaken a currency. According to Wikipedia — By increasing interest rates a central bank stimulate traders to buy their currency (carry trades) as it provides a high return on investment and this would strengthen the currency against others.

I feel this might reduce inflation and increase purchasing power.When inflation is perceived to rise (easy borrowing), central banks dampen it by rate hikes (expensive to borrow).

Very roughly, Interest rate is 70% influenced by gov; FX rate is 30% influenced by gov. Central bank has 90% control on the supply of their currency (but not other currencies). FX adjustment is one of the goals of Central bank IR actions. IR actions have other serious consequences not to be ignored. In this sense, FX trading desk needs IR expertise.

The interest rate is higher on some currency because there is a probability that it will depreciate. As long as the depreciation does not materialize, the carry trade is profitable, but makes big losses when it does. High yielder currencies aren’t always safe bets.

credit risk analysis in Singapore

(a blog post. Your comments are appreciated.)

To me, credit risk is all about default risk. There's a whole industry around the rating, measurement/analysis, monitoring, hedging and control of default risk. As such, Credit risk is relevant to both investment banking (buy/sell, underwriting, M&A etc) and commercial banking (ie lending), but how relevant? I feel credit risk is one of many components of market risk in investment-banking, but credit risk is absolutely central to commercial banking.

For the Singapore financial industry, commercial banking generates (much) larger revenue than i-banking, and is a far more important industry to the national economy. Most S'pore businesses need to borrow from banks.

I guess credit risk analysis is more important than market risk analysis in S'pore.

challenges when using JNI

Recently I was asked about the challenges when developing apps using JNI.

I mentioned the “native” keyword, mentioned GC, type safety(???) but missed the portability issue. The bond calculator is in C. C is platform specific, so the java app can't run on windows.

Wikibook says:

To complete writing native method, you need to process your class with javah tool that will generate a header code in C. You then need to provide implementation of the header code, produce dynamically loadable library (.so under Linux, .dll under Windows) and load it with System.load(library_file_name) — as seen in reo.

##what mkt-data feed your autoReo pricing engine@@

background — every algo pricing/trading engine needs market data feeds.

For my algo engine,
– Libor, ED futures
– Treasury futures
– Treasury ticks
– Bloomberg
– credit rating updates
– material event feed
– ION mkt view
– JJ Kenny
– MSRB, NASD TRACE

There are a few essential INTERNAL feeds to my algo engines —
– positions,
– marks by other desks,  and other algo engines.

mkt-risk systems — front office or back office@@

Some say the risk manager's job begins when the trader's job is done. Robert said no. Better trading firms maintain real time risk system so risk can be assessed before execution. Most pre-trade calculations (pricing and quantity) are risk-bound. How much can you lend to anyone and at what rate and repayment frequency? What price do you buy/sell/bid/offer anything? But these are not the most well-known risk systems — consider the opening remark.

I believe the meanings of “market risk system” are wide ranging. Looks like the #1 core component is valuation, and other fundamental components include

* VaR
* scenario/stress testing — GSS risk

IV — UBS apac

difficult users?
leading a small team?
how did you deal with failures?
biggest challenges in your current system?
what’s IRS and cross-currency IRS?
use vs require in perl?
excel VBA
JNI
what do u see yourself doing in 3-5 years? “we do have people in their 40’s. we wont’ push you out of tech.”

2 threads using a singleton servlet

Get this into your blood — 2 threads can simultaneously run the same instance method (un-synchronized) of a singleton, on separate processors.

* Only 1 copy of the method throughout entire memory — Say method1() is at memory 0x289c2. No difference between static or instance methods
* each thread knows 0x289c2 and keeps a private instruction counter.
* Only 1 object in memory
* for a non-static method, threads access 0x289c2 only via the object. A thread can't invoke method1 without a pointer to the object.

A thread is… (loosely) a call stack. Each method call can access various objects in memory. Objects live on HEAP not STACK.

Very loosely, you can think of each servlet as a singleton. Hundreds of concurrent call stacks (on separate processors) have access to this object. Object should avoid mutable fields.

credit risk analytics important to trading desks@@

Now I realize the entire credit business (all those credit desks) in any trading firm operates around credit risk or “default risk” of individual issuing companies. I guess credit risk is at the center of all the securitization, CDS trading and straight buying ans selling of corporate bonds.

Am still unsure how precise a mathematician can be about calculating, predicting, measuring default probability. They probably take in tons of reported company financial data and feed them into some complex model.

Muni bonds have non-zero credit risk. In fact many muni bonds are below AAA rating. However, I don’t hear about credit risk analytics in the muni business. In fact, one high yield muni trader I know always prices his offers by hand, without any automatic calculation.

High yield means low rating, right? So credit risk is higher and should be factored into the pricing logic. The trader seems to go by gut feelings?

Q: I know credit risk modeling is used in consumer and corporate loan pricing, but is it also used in fixed income trading? In such areas as interest rate swap, credit default swap, corporate bond trading?
A (from a practitioner): yes

Q: people tell me market risk and credit risk are 2 main focus areas for any bank, but if a bank holds lots of credit instruments (high yield bonds, IRS, CDS securities…) then i feel market risk measurement must depend on credit risk measurement. Right?
A (from a practitioner): yes

eq listed drv desk

Some basic info from a friend –

Equity Listed derivatives – mostly options on single stocks or options on index/future, but also variance-swaps. Even if a stock has no listed options, we would still create a vol surface so as to price OTC options on it, but the technique would be different — The standard technique if given many pairs of {expiration, strike} is to fit a curve on a single expiration, then create similar curves for other expirations on the same underlyer (say IBM), then try to consolidate all IBM curves into a smooth IBM vol surface. Each “point” on the surface is an implied vol value. I was told some of the more advanced “fitting” math is extracted out into a C++ quant lib.

Instrument pricing has to be fast, not multi-second. I guess this is pre-trade, RFQ bid/offer pricing, similar to bond markets’ bid-wanted. In contrast, the more “real” need for vol surface is position pricing (or mark-to-market), which provides unrealized PnL. I feel this is usually end-of-day, but some traders actually want it real time. Beside the traders on the flow[3]/listed/OTC derivative desks, the vol surface is also used by many other systems such as structured derivatives, which are entirely OTC.

It’s quite hard to be really event-driven since they are too frequent, instruments too numerous, and pricing algo non-trivial, exactly like FX option real time risk. Instead, you can schedule periodic repricing batches once a few minutes.

About 3500 underliers and about 450,000 derivative instruments. Average 100 derivatives on each underlier (100 combinations of strike/tenor). S&P500 has more than 1000 derivatives on it.

Market data vendors — Reuterss, Wombat, Bloomberg.

Inputs to vol calculation — product reference (strike/tenor), live market quotes, dividend, interest rate …

One of the most common OTC equity derivatives is barrier option.

Pricing and risk tend to be the most mathematically challenging.

Exchange connectivity is usually c++, client connectivity (clients to send orders or receive market data) is usually java.

[3] Flow means agency trading, most for institutional clients. Retail clients are very wealthy. Those ordinary retail investors won’t use an investment bank. Flow equity derivative can be listed or OTC.

dark side of hash tables: mutable objects

If YourClass.java instance is to go into in a HashSet, then think of how you login to online banking. (Yes they are similar!)

JVM identifies each instance not by the physical address (necessarily unique), but by hashCode() and equals(). If another physical instance matches the first instance you put into the hashset, then system thinks they are identical. Similarly, if your wife knows your password, she can masquerade as you.

Now The darker side — if you forget your password, then you can’t access your money. For a given physical object, if state changes as to affect hashCode() or equals(), then JVM will think this instance is not the first instance put in. hashCode() must return exactly the same int, since that int determines which bucket … — think of separate chaining.

In the test below, once object changes state, it’s lost in the Set forever. The remove() and contains() operations can’t find it. Worse still, I managed to put the same object in it twice!

public class SetTest {
static Date d1 = new Date();
static Date d2 = new Date();
static Set Date> set = new HashSet Date>();
public static void main (String a[]) {
set.add(d1);
set.remove(d1);
dump();
set.clear();

set.add(d2);
printIfContains(d2);
d2.setHours(4);
printIfContains(d2);
set.remove(d2);
dump();
set.add(d2);
dump();
}
static void printIfContains(Date incoming) {
out.println("set.contians() === \t " + set.contains(incoming));
}
static void dump() {
out.println("size ==== \t" + set.size());
for (Date d: set) {
out.println(d);
}
}
}

swing PLAF, native peer object ^ UI delegate

LAF concept (and implementations) is at the heart of the swing MVC. It is beneath just about every JComponent and UI delegate class. I think the concept of swing component’s painting and realization are relevant, if not a cornerstone of LAF implementation. UI delegates are instantiated when JComponent is instantiated, according to my debugging. When a component is “realized”, i believe its corresponding UI delegate “enters” a new state as it is painted on screen and can take user input.

http://java.sun.com/products/jfc/tsc/articles/painting/#background
“lightweight” component is one that reuses the native window of its closest heavyweight ancestor.

I think that ancestor is usually a jframe instance

http://jan.newmarch.name/i18n/input/input.html
a Swing object does not rely on an associated peer object. This article also covers event queue.

http://java.sun.com/products/jfc/tsc/articles/architecture/
As an application developer, you should think of a component’s view/controller responsibilities as being handled by JButton, JTree,.. The component class then delegates the look-and-feel-specific aspects
of those responsibilities to the UI object of the look-and-feel.

Pluggable look-and-feel means the portion of a component’s implementation that deals with the presentation (the look) and event-handling (the feel) is delegated to a separate UI object of the look-and-feel,
com.sun.java.swing.plaf.windows.WindowsScrollBarUI is a concrete class for such an UI object

The UIManager is the API through which components and programs access look-and-feel information (they should rarely, if ever, talk directly to a LookAndFeel *instance*). UIManager is responsible for keeping
track of which LookAndFeel classes are available, which are installed…

mkt-data subscription engine java IV #barc eq drv

Say you have market data feeds from Reuters, Wombat, Bloomberg, eSpeed, BrokerTec, ION… Data covers some 4000 underliers and about half a million derivative instruments on these underliers. For each instrument, there can be new bid/offer/trade ticks at any millisecond mark[1]. Volume is similar to option data feed like OPRA.

Say you have institutional clients (in additional to in-house systems) who register to receive IBM ticks when a combination of conditions occur, like “when bid/ask spread reaches X, and when some other pricing pattern occurs”. There are other conditions like “send me the 11am IBM snapshot best bid/ask”, but let’s put those aside. For each of the instruments, there are probably a few combination of conditions, but each client could have a different target value for a condition — 2% for u, 2.5% for me. Assuming just 10 combination for each instrument, we have 5 million combination to monitor. To fulfill clients, we must continuously evaluate these conditions. CEP and Gemfire continuous query have this functionality.

I proposed a heavily multi-threaded architecture. Each thread is event-driven (primary event) and wakes up to reevaluate a bunch of conditions and generate secondary events to be sent out. It can drop the new 2ndary event into a queue so as to quickly return. The “consumer” can pick up the 2ndary events and send out by multicast.

Each market data vendor (Reuters, e-speed, ION, even tibrv) provides a “client-runtime” in the form of a jar or DLL. You embed the client-runtime into your VM, and it may create private threads dedicated to communicating with the remote publisher.

[1] Each IBM tick actually has about 10 fields, but each IBM update from vendor only contains 2 fields if the other field the symbol didn’t change. So we need something like Gemfire to reconstruct the entire 10-field object.

what 80% volatility means

( http://lvb.wiwi.hu-berlin.de/members/personalpages/wh/talks/DetHaeSlidesVarianceSwapForecast060531.pdf has the precise math definition of Realized Variance )

We need to differentiate between i-vol vs h-vol …..

Q: what does 24% volatility mean for an option expiring in 3 months?
A: it means the stdev for an observable is —-> 24% * √ 3m/12m = 24% * 1/2 = 12%.
See P277 [[Complete guide ]]

Now let’s define that observable. If today’s closing price is $100, and closing price in 3 months is X, then (X-100)/100 is the observable.

Therefore, 3-month forward price is likely (two-thirds likelihood) to fall between ±12% of current price, or between $88 and $112. Here we ignore interest rate and dividend.

Now forget about options. Consider a stock like IBM.

Q: what does a vol of 80% means for IBM?
A: see P 76 [[Options Vol trading]]. Average day will see a 5% move (80%/√ 252. More precisely 66.666% of the days will see moves under 5%; 33.333% of the days will see wider/wilder moves.

Q: a longer example — what does 25% volatility mean for IBM’s closing prices tomorrow vs today?
A: it means the stdev for IBM daily percentage return is 25% * √ 1 / 252days = 25% / 15.9 = 1.57% [1]

A longer answer: Take the underlier’s daily closing price for today ($100) vs tomorrow (X). The daily percentage return (X-100)/100  could be 1%, -2%, 0.5% .., but for clarity I’d rather compute PriceRelative to get 1.01, 0.98, 1.005…

Now if we simulate 1000 times and plot a histogram of daily returns defined as log(PR), then we get a mean. The mean is most likely very close to 0. The histogram is likely to resemble a normal curve. If today closes at $100, and if IBM does follow an annualized vol of 25%, then tomorrow’s close is likely (two-thirds likelihood) to fall within $98.43 to $101.57. Note the numbers are imprecise because we are assuming IBM is equally like to lose 1.57% or gain 1.57%, but this is the naive model. It also predicts “equally likely to gain 101% or lose 101%” to become NEGATIVE. BS model assumes log(PR) is normal so 1.57% gain is as likely as a decline to 98.454%.

IF (big IF) IBM continues to *follow* the 25% annualized vol, and if we observe its daily PR for 5 years, we will see that most of the time (two-third of the time), daily PR falls somewhere between ±1.57% using the naive model. See P34 [[Trading Options in Turbulent Markets ]]

[1] There are 252 trading days in a year. Our 25% vol is an annualized vol.

java nested class — static^non-static

See post on [[philosophy of nested classes]].

Let’s put aside anon classes.

By default, all my nested classes start with the “static” keyword.

Practically, whenever the nested class needs access to enclosing class’s instance fields (outerInt1 for eg) i remove the “static” keyword and make the class non-static, effectively adding an implicit “outer.this” field into the nested class. Nested class can then access outerInt1 via outer.this.outerInt1

 

yield^generator^list-comprehension

  1. LC (list comp) builds up a physical list in memory. LC needs you, the creator, to put the brackets [….] around it. Now it looks like a list (and really IS a list)
  2. GE (generator expression) doesn’t create a list in memory. GE “yields” one “fruit” at a time on demand. GE needs the parentheses (…).

Declarative programming — a generator Expression is a specification for producing a stream of items.

LC example — range()
GE example — xrange()

What if you remove the brackets around an LC? I think it won’t compile.

Now we focus on the “yield” keyword

  • Any function using yield is a generator-function and should not [3] use “return”.
  • The unique capability of a generator function is “stop, save state, resume [1]”. To start with, you may think of it as a stateful function with its own private memory.
  • generator function often take arguments to initialize or customize itself

if you happen to print the return value of myYieldFunc() , you realize it’s a generator object with an address. Basically, this is the uncommon way [2] to use myYieldFunc.  Instead, myYieldFunc() should be called, with parentheses, in “iteration context”. In iteration context, under the hood the compiled code basically instantiates (once) and queries (repeatedly) the generator object.

Q: do I always use myYieldFunc with parentheses?
%%A: I think so.

[1] [[py cookbook]] says this is such a simple yet powerful idea that it was pushed to the limit (just like the unix pipe).

https://www.programiz.com/python-programming/generator compares generator functions to regular functions.

This “yield” (also present in c#) serves a similar purpose to generator expressions and can express more complex logic. https://stackoverflow.com/questions/16780434/yield-vs-generator-expression-different-type-returned sheds some lights but is on python3

— [2] using the returned generator object directly
Most of the time we use myYieldFunc() in iteration context, but we can (but why) do this:

myGenObj = myYieldFunc(); myGenObj.next()
myGenObj = myYieldFunc(); next(myGenObj) # builtin free function

— [3] combining yield and return
A generator function like myYieldFunc() should almost never use q[ return ]. However, a regular function f2() can do a return myYieldFunc() with or without args. I think this f2() should be used as a generator function, in an iteration context.

If you do call return inside myYieldFunc(), better document it. The “return” is like break. Usually you can avoid “return”.

https://stackabuse.com/python-generators/ has more details on return. See also P698 [[cookbook]]

keep a live JTable sorted/filtered, again

After updating an existing row, you need to fireTableDataChanged(). This loses user selection. It trigger a re-filter and re-sort. To keep user selection, try

fireTableRowsUpdated(0, getRowCount() – 1) // a broken alternative as it re-sorts and keeps user selection, but doesn’t trigger re-filter, so the updated row may fail to show/hide.

See other post on the performance implications of frequent sorting on a large live table.

global variables in (financial) rapid development

In my financial projects, I realized people do worse things than global vars – copy/paste, super long method signatures, poor null checks, returning nulls…

My professional perl programs successfully use lots of globals. Be practical. Get Real.

C++ lets you easily use tons of globals, so I’d say “avoid it, but if no time then use it.”

In java, someone might want to create a GlobalVars.java to hold public static vars. This is actually better than scattering lots of mutable[1] non-private static variables into multiple classes. So before you dismiss someone for using GlobalVars.java, look for those mutable shared static vars.

For a bit of access control, you can use an abcSingleton.java class to hold such shared variables — Some classes will not get a reference to this singleton, so they can’t access the vars.

[1] Most final variables are mutable in java, unless the class is designed to be immutable.

every cell update event triggers getTableCellRendererComponent()

Note WPF is similar — PropertyChanged event is an invitation to “query ViewModel and display updates

If you use a swing timer to fire table update events, you will see, in log or debugger, that after every fireTableCellUpdated(a single cell), getTableCellRendererComponent() runs exactly once, on that single cell.

If you don’t use swing timer but use scheduleAtFixedRate() to call invokeLater(), which asynchronously calls fireTableCellUpdated(), you will see the same in the log file. However, debug session will likely show many fire() followed by a getTableCellRendererComponent(). This is illusion. It depends where you break-point — (Let’s say in both cases the event is scheduled to happen once a second. )

* If you break inside invokeLater’s Runnable.run(), you get confusing results. Scheduled tasks continue to call invokeLater() once a second, but your debug may suspend the EDT, so you might see many fire()
* If you break before invokeLater(), you should see one fire() followed by one getTableCellRendererComponent() — ABABABAB

No break point, then no surprise. Bottom line, each fire() is followed by a getTableCellRendererComponent() call. In plain English, after we fire each update event on a cell, that single “cell model” will get queried and rendered exactly once.

As in WPF, our code aren’t allowed to modify the visual component directly. Only way to effect a visual change is to update the model and fire events to invite “Hollywood” to requery the model and Notice the update.

what C/C++ can do but Java can’t@@

See also post on c++ vs real time jvm.
* java generics has non-trivial limitations. c++ generics is more complete IMHO. Some say c++ generics leads to code bloat but it’s not intuitive to me.
* I believe some financial packages were written for C/C++. On the other hand, some packages might be written for java.
* c/c++ api are more easily usable across by java, dotnet and Excel
* Function pointers. Java has Method objects (and interfaces), but not widely used.
* operator overloading
* preprocessor is mighty. I feel java has some code generators but not popular.
* JVM and java compiler is written in c/c++, on most OS versions.
* A large part of windows was written in C++ and C.
* Most OS system API is in C. C++ can easily call them.
* most shell utilities in Unix and Windows are written in C/C++. Java is too slow and bulky. To run a simple grep, (I guess) you need to start a JVM.
* Most database engines are probably written in C/C++. If for any reason you want to build your own database engine then c++ is probably better than java.
* fastest web servers are in c/c++.
* Here’s one last thing c++ can provide, but this is my personal view. C++ knowledge, if deep enough, provides some kind of window (from a lower angle) into the performance limitations/strengths of java and c#. This insight is valuable but hard to achieve if I only know java.
I don’t know the details, but a large part of c++ power comes from pointers. 
Java/c# are high-level languages. c/c++ are mid-level languages; while assembly is truly low-level. Some requirements (DB, OS, system utilities, compiler…) are best implemented in mid-level languages. Do financial apps fall into that category? I don’t think so, that’s why C++ is severely challenged by java and C#.

real-world OMS c++data structure for large collection of orders

class OrderMap{
struct data_holder {/*fields only. no methods*/};
unordered_map<int, shared_ptr<data_holder> > orders; // keyed by orderID
… //methods to access the orders
};

Each data_holder is instantiated on heap, and the new pointer goes into a shared_ptr. No need to worry about memory.

Do you see ANY drawback, from ANY angle?

pre1.6 jtable sorter – a simple impl

In a high-speed trading GUI, it’s not uncommon to have 2 tables showing the same live data, but in different sort orders. As data stream in, the 2 tables must auto-refresh (either periodically or event-driven).

This requires a separate-view-shared-model design. Whenever content of the model (object) is updated, the updater method would call myTableModel.fireXXX() on EDT. Since the 2 (JTable) views are always among the listeners of myTableModel, fireXXX() method always invokes the callback on each, sequentially, on EDT. When the callback event handler runs, it basically queries myTableModel and displays its content sorted. This could take many millisec on each table if content is huge. If you have 5 tables on myTableModel, the latency adds up. Use paging if necessary.

Don’t do the naïve sort — recreate a table model instance, and setModel(). This way the 2 views will show inconsistent data — detached.

Table sorting should not change the data, should change the presentation instead. Here’s one simple way to keep a live table (or two) sorted —

Provide a decorator class/object implementing TableModel. This is the object you put into mySortedJtable.setModel(). It holds a decoratee TableModel instance. As you query it via getValueAt(int,int), you go through a row index translator, which translates view-index to physical-index.

* Physical index is the row-index into decoratee.getValueAt(). The sequencing inside decoratee doesn’t change during sort
* view index is the row-index into this.getValueAt()

So when the table is re-drawn, this.getValueAt() gets called 22 times if there are 22 rows. First call (row 0) will hit physical-index 21 in a reverse sort.

This is enough to present a static content sorted. But our data is live. Every 2 seconds, or whenever MOM delivers new data, some method updates the underlying data structure inside decoratee. Now the view won’t show it. The same method would trigger (perhaps via invokeLater or timer event) a decorator.sort() and decorator.fireXXX(). The sort() would update the row-index translator. The fireXXX() would make the view query its model (i.e. decorator) via decorator.getValueAt().

TableModel fireXXX() skips event queue

Upon mouse click, hardware/OS pumps in a low-level event into EDT queue. After some delay the mouse event is picked up on EDT. The event is examined to get the source which is typically some jcomponent [1]. Then the list of listeners is examined. Each listener object’s mouseClicked() method is invoked sequentially on EDT.

However, in a TableModel’s fireXXX() method, the listener object’s [2] event handler method is invoked directly — on the same thread (as part of) fireXXX(), without pumping event into the queue.

Hold your breath — if you call fireXXX() on any non-EDT, the view (java + native screen objects) would be modified on that thread — disaster. Therefore TableModel should be modified only on EDT.

[1] remember when user clicks, there has to be some screen object she’s hitting.
[2] This object is the JTable instance, i.e. the view object. This object is __automatically__ registered as one of the listeners on the table model object. If the table model instance is a celebrity with a fan club, then JTable instance is her twin sister and the founding member of the fan club.

java private nested classes: my habits

A major justification and usage of nested class is constructor access control. To appreciate, first appreciate immutables, final, singletons, protected, …. Note you can finish most projects without using these access control devices, but I tend to spend extra time adding such access controls. In complex concurrent systems, they reduce risk and add much-needed reassurance. Constructor access control is one of the controls.

Majority of my nested classes have only private constructors. I usually call them from the enclosing class.

Furthermore, all my nested classes start off as private static classes, and become protected only when necessary.

y delta overshadows other greeks

If your own money is at stake, which risk factor do you worry most? Which variable do you watch most? I bet #2 is theta, not vega, but #1 is delta/gamma i.e. how underlier moves.

One reason is, underlier changes much faster.
– Implied vol doesn’t change by the minute. I think people re-calibrate vol surfaces as frequently as every few minutes. However, imp vol is a guesstimate “soft” market data, with a lot of guessing.
– realized vol changes usually by the day.
– TTL doesn’t change by the hour. We typically treat it as changing by the day.

In contrast, Underlier spot price changes by the second, and is Directly observable. Therefore, among all of the input variables to option valuation models, spot price changes fastest.

Note spot isn’t always the most significant input in every pricing formula. Volatility and TTL (when expiring) are sometimes more significant.

Another reason — hedging. An option/swap often exists along with a position in underlier. In that case it’s paramount to analize the nett impact of an underlier move. No such thing with the other greeks.

pace of change — for TTL pace is constant, whereas pace of change in S can be very fast in a crash. Sigma can change fast, but not as fast as S.

I feel theta matters mostly in the last few months. “Decay” is a slow process until the dying days.

explict cloning in c++, java, collections…

Say you have a collection holding Errors, Exceptions .. which are subclasses. Let’s clone them.

In a C++ container, you need Throwable pointers to Error objects. To clone each object, you would need a virtual clone() method.

In a java collection, if Error doesn’t implement clone(), then the Throwable clone() won’t help as it can’t access subclass’s additional fields (molecules on a single onion layer). I gave the UBS interviewer a workaround — collection’s iterating loop to use reflection to get the class of each element (Errors), then call constructor reflectively, finally set each field reflectively.

Serialization is another solution, as described in http://en.wikipedia.org/wiki/Clone_(Java_method)

Can you create methods using reflection? I said no you need bytecode instrumentation or perhaps dynamic proxy.

Unlike assignment, clone() should create a brand new object. (Try Object.java javadoc.) Stack-var would get out of scope. So cloned objects should use new() and pointer and heap.

Can clone() return the new pointer unwrapped (i.e. deferenced)? Put another way, do you return the object by pbref or pbclone? We already made a clone, so pbref is more efficient.

ack in JMS flow – which leg@@

The term “acknowledgement” is ambiguous in the JMS context. I always ask “which leg”.

Leg 1 sender-broker. Ack is from broker to sender.
* for persistent messages, broker always persists the message Before sending ack to sender
* for nonpersistent messages, broker may send ack then crash, losing the message.

Leg 2 broker-receiver. Ack is from receiver to broker.
* for unicast,
** for unicast nondurable subscriber A, messages published during A’s downtime are ignored and won’t be delivered to A
** for unicast durable subscriber, … obvious
* for p2p queue, delivery is guaranteed. Similar to unicast Durable.

Future.java designed for …

IMO, Future is designed for

* cancel, if pending i.e. not completed
* retrieve return value of a callable task, if completed
* detect exception thrown on the worker/executor thread.

3 tricky requirements! For any one of them, Future.java is the standard solution.

The CompletionService javadoc shows that a Future can represent either a 1) pending or 2) completed task.

UBS IV emerging market credit/rates: java

Q: How do you create a deep copy of a java List where each element could be an Integer, Float …?
A: see the email I sent on 4 Aug 2018.

Q1: how do you price an interest rate swap? I think this can mean at least two things
Q1a: how do you compute the p/l of an existing IRS?
Q1b: how do you determine the fixed rate on a new IRS?

Q: how do you call c from java?

Q: what does a credit spread mean?

Q: Challenges of multithreading?
%%A: one feature of java can restrict the number of threads to a number smaller than the number of processors? Can’t name that feature.

Q: Say a master producer thread puts objects (like tasks or …) into a shared collection, and worker threads pick them up and put results objects into a shared place. How do you ensure data integrity/consistency.

–perl QQ topics — by using python or perl day to day i won’t know these topics 😦

Q: Say your perl program needs to create 15 concurrent child processes.. How do you wait for all children to finish?

Q: How do you connect your (perl) STDIN to a child process?

credit desk — CDS, corp bonds, MBS
rate desk — IRS, treasuries
c++ is used by quants but not really others.

JTable sorter – mkt-data streaming tips

http://stackoverflow.com/questions/996948/live-sorting-of-jtable shows a subtle difference between

fireTableDataChanged(); // triggers re-sort, but ….loses current row selection 😦
fireTableRowsUpdated(0, getRowCount() – 1); // keeps selection 🙂

That’s all about the subtle difference. Here’s a separate issue.

Both of these send some kind of “update” events. To react to them, TableSorter source code reveals a special field —

private boolean sortsOnUpdates; // false by default, so updates to existing rows will NOT trigger re-sort.

http://stackoverflow.com/questions/996948/live-sorting-of-jtable shows a simple fix. This is all you need to add this powerful feature into your trading apps — keep a live table sorted