c# log level +! log4net or NLog

I guess c# has builtin support but the log is written to the MSVS output window by default. If you want to also log to a file, you

can add a TextWriterTraceListener

The app.config can contain a TraceLevelSwitch like in DEBUG/INFO/ERROR log levels of log4net. This is probably a runtime change,

since the config can change after compilation.

[ Conditional (“TRACE”) ] also affects the logging, but at compile time.

c# captured var in closures

Local vars may live “beyond” the closing curly bracket. Local struct vars may no longer live on the stack as in java…

Most complicated scenario — local variable declared inside a loop. This is uncommon, considered code smell, and can be avoided. Note for(int index = ….) declares a variable OUTside the loop.

private bank trade/order/quote/execution flow

Remember — Most non-exchange traded products are voice executed. Only a few very dominant, high volume products are electronically executed. Perhaps 1% of the products account for 99% of the trades — by number of trades. By dollar amount, IRS alone is probably 50% of all the trades, and IRS is probably voice-executed, given the large notional amounts.

The products traded between the bank and its clients (not interbank) are often customized private offerings, unavailable from any other bank. (Remember the BofA puttable floats.)

RM / PWA would get live quotes from dealer and give to a client. Sometimes dealer publishes quotes on an internal network, but RFQ is more common. Any time the quote could be executed between RM and client. RM would book the new position into the bank's database. As soon as as executed (before the booking), the bank has a position but dealer knows the position only after the booking, and would hedge quickly.

Dealer initially only responds to RFQ. It's usually executed without her knowledge, just like an ECN flow.

I think in BofA's wealth management platform, many non-equity products (muni bonds are largely sold to retail clients) trade in the same way. Dealer publishes quotes on an intranet website. RM negotiates with client and executes over the phone. During trade booking, the price and quantity would be validated. Occasionally (volatile market), trade fails to go through and RM must inform client to retry. Perhaps requote. Fundamentally, the dealer gets a last look, unlike the exchange flow.

I believe structured products (traded between bank and clients) are usually not fast and volatile — less requote. However, when dealer hedges the position, I think she often uses vanilla instruments.

Terminology warning — some places use “trade” to mean many things including orders. I think in exchange flow, “order” is a precise word.

y learn Trace/Debug classes in dotnet

I believe log4net (and others like NLog) are better than the built-in, but the built-in has one nice feature — In visual studio, the output window conveniently shows the log messages if you use Trace.WriteLine or Debug.WriteLine.

Note output window isn’t the console.

Can log4net write to the output window? See http://angledontech.blogspot.sg/2011/08/direct-log4net-output-to-visual-studio.html. A bit of work. When I start a new project and have no time to set up log4net, then I use Trace/Debug to dump to the output window automatically, by default.

Reason2 – some projects may use the builtin not log4net.

Reason3 – some projects use other logging frameworks, so you may need to learn multiple logging frameworks. If you are lazy and you can decide, then the builtin is a basic solution always available.

y destructors should never throw #le2LinYu

Hi Lin Yu,

You once asked me exactly why destructors should not throw. I just read about it. Compiler would turn a blind eye if your destructor calls some function1 (that in turn calls some function2, that in turn calls some function3 …) that could throw an exception. Over 5 years in production the destructor may not throw, but what happens when it does throw? Maybe nothing bad happens, but maybe one of these things happen

– if the destructor is invoked as part of stack unwinding, then system would probably terminate
– If your object’s destructor has the hidden (destructive) power to throw exceptions, then the STL containers won’t store this object. If you still put this object into an STL container, then I don’t know what happens. Perhaps undefined behavior. If your class is to be used with the STL, then the STL explicitly forbids exception throwing from your class destructor.
– your class can’t be used as member or base-class of another class

Even though the consequence is not documented, the language standard DOES specify when and where destructors should NOT throw. Looks like you might (in theory) get away with an exception thrown from a destructor, but many parts of c++ language do not allow it. In reality, the consequence is probably harmful rather than harmless.

I hope this letter clarifies the issue rather than add to your confusion. Feel free to reply.

Tan Bin

one event delegating to another event – WPF

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030 shows an interesting mini “pattern” of one event (“event-pseudo-field”) delegating all its add/remove logic to another event (pseudo-field).

public event EventHandler CanExecuteChanged{
  add { CommandManager.RequerySuggested += value; }
  remove { CommandManager.RequerySuggested -= value; }

This reveals the true meaning of an event member. If an object studentA (of class Student) has an event pseudo field evt2, this field is basically a chain of callbacks (or notification targets, or event handlers). The evt2 field is nothing but a handle to add/remove on this chain.

In many cases, the callbacks run sequentially on the same thread as the event-firing code.

See also http://bigblog.tanbin.com/2010/06/field-like-event-syntax-restrictions.html

get class file physical location of a java object #home made

<![CDATA[ private static String getMetaData4Obj(Object o) { return getMetaData4Class(o.getClass()); } private static String getMetaData4Class(Class clazz) { String className = clazz.getName(); className = className.replace(“.”, “/”); className += “.class”; URL location = clazz.getClassLoader().getResource(className); return clazz + ” loaded from ” + location; } ]]>

the q[assembly] concept in dotnet – phrasebook

– An “assembly” sounds like purely logical but no no no it is also a physical File — either an EXE or a DLL, which could be an app

or a lib.

– Access modifier “internal” — is defined for an assembly

– unit of deployment

– unit to be versioned

– qualified type name — 2 assemblies “Soccer” and “Guitar” can both export Player class. The fully qualified Player type is

qualified with the host assembly.

– strong name — special subset of qualified type name


– private unshared assembly – used by 1 app

new MyDlg ^ delegate {} – confusing syntax

1a) equivalent forms:
button.Click += new EventHandler(Method1);
button.Click += delegate { /* the code in the body of Method1 */ }

Action action = new Action(Method5);
Action = delegate (int a) {/* the code in Method5 body */ }

new Thread( new ThreadStart(MyMethod2) );
ThreadStart ts = delegate { /* the code in MyMethod2 */ };
new Thread(ts);

The a) form is useful when an existing method hosting the logic is available. The b) form is useful if you have just a few lines of code and want no method created. In both cases, people often simplify this long-winded coding style. However, if it confuses you then you should use the un-abbreviated form.

win service vs console daemon

In the standard 70-536 textbook, Microsoft MVP Tony Northrup related his early experience running a console daemon app as a poor man's win service. He used schedule tasks to start the console app.

In my projects I also keep a console app running forever, as a simple alternative to a win service.

There's no need to open some network service.

## loaded words in c++ literature

“safety” means exception …
“resource” is always represented by an object on heap (accessed via a pointer).
“life cycle” means destructor
“owner” means deleter of a heapy thingy. Non-heap objects need no owner. Owner has just one job — deletion.

“static” is often used without qualification and by default means the most common/popular static object — the locals
“functor” often includes function pointer, at least in some contexts
The “resource” in RAII means any memory allocated from the heap

jar ^ dotnet-assembly

I feel the dotnet “assembly” concept borrows the java “package” and “jar” features. Here we compare assembly with jar

– physical files? Yes both are physical files
– executable? assembly can be EXE or DLL. Jar can come with a main class or without. Jar is more like DLL.
– contains IL bytecode? Yes. However, you could have a native assembly (see ngen.exe)
– unit of deployment? identical
– versioning? assembly versioning is mandatory and builtin. Jar versioning is adhoc, home-made and not always necessary.
– access modifier? No a jar is never used in the access modifier rules
– manifest? identical
– each dotnet class loaded in the VM has a fully qualified name containing the assembly name and namespace. See
http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx. Not in java. Each java class is identified by the package name and unqulified class name.

StopLoss order ineffective when markets crash

In terms of execution price, I feel limit orders are honored by the exchange (FX has no exchange), whereas StopLoss is a “best-effort” promise.  Key is the “rollback” strategy.
* For a limit buy/sell, if the sell-side can’t fulfill your order, you roll back to the original empty position.
* For a SL sell (for simple illustration) order, you already have a long position. When market moves against you, that position has very low MV, potentially $0 MV. If sell-side can’t fulfil your specified price, you can’t rollback to original position. Instead you must grab the next best and exit at an inferior price than specified. 

Yes, your loss will be $2100. The stop loss will not limit your loss to just $1000. These are exception cases which cannot be foreseen. Even for daily trade, if it is volatile and if the currency pair is not actively traded, your loss will depend on the next best price.

Subject: stop loss order ineffective when market crashes

You mentioned FX stoploss order may fail to work. I’d like to learn more from your first hand experience.

This is what I know from reading/discussing.. —

Suppose I have bought USD/SGD at 1.23 so I place a SL sell at 1.22, to limit my loss to S$0.01 (S$1000 loss for a USD100k notional). When market crashes on a Monday morning after a weekend, it opens at 1.211/1/212 (bid/ask), so my SL and other people’s SL execute at whatever best bid price there is, which is 1.211 at Monday open and drops further. My long is eventually unwound at 1.209. Loss is  S$2100. In this story, the StopLoss order doesn’t limit my loss at S$1000.

Is this similar to your experience?

ISerializable in java^dotnet

In java ISerializable is a marker interface. I think dotnet replaced (all?) marker interfaces with class-level attributes. Serializable is such an attribute.

In the same vein, java’s transient keyword is replaced in dotnet by a field-level attribute NonSerialized

Dotnet ISerializable interface does exist but specifies a method and means something else.

##Some Categories@IV questions are evergreen

Background – older developers often lament that generations of tech churn threaten our marketability/survival and devalues our expertise on an old generation of technology.

Key is the job interview. Think about the categories of tech questions. Some categories are immune to the churn… If you invest months (years? impractical) preparing for these questions, your investments may, counter-intuitively, endure. Here are some categories —

#1a) Algo + data structure? Yes. The primary focus of Top tech firm like FB, google, MS, Amazon. However, in coding tests they also need working code, therefore you need substantial coding experience with
** STL (or collections) +
** strings — non-trivial
** pointers

#1b) algo quizzes — dense recursion/loops (eg quicksort). Think fast and make it work. Hardcore coding ability. I think you don’t even need STL for this.

#2) Threading (only c/c#/java)? Not really un-changing. They do ask java thread pools new features.
** threading algorithms using standard constructs? Yes. often among the toughest IV questions.

SQL? Yes

OO principles as implemented in a specific language? Yes

Socket? not sure

Design patterns? No. a fad.

System.Nullable struct # essentials

Let’s take Nullable as a case study.

Physically, represented by a 64-bit object + some bytes for the boolean flag. See http://stackoverflow.com/questions/3559843/what-is-the-size-of-a-nullableint32

Accessing the “Value” property triggers exception if HasValue == false, but GetValueOrDefault property would return 0.0. I guess we usually don’t want to use GetValueOrDefault when it’s null.

common nested types – dotnet^java^c++

Enum – is a common nested type in c++, java and dotnet. In java it’s typically a static nested “class” . In c++, non-nested enum is more common than nested enum

Delegate – is a common nested type in dotnet. Many textbook examples declare custom delegates nested in a class. However, in practice, it’s more common to declare it outside all classes – more reusable.

Local typedef – is a common nested “type” in c++

COM interop, learning ntoes

Dotnet interop (2-way) — allows COM-based applications to use .NET components, and .NET applications to use COM components

Dotnet wrapers — Microsoft now focuses much of its marketing efforts on .NET, with .NET providing wrappers to the most commonly used COM controls. However, your own custom COM classes don't have wrappers yet. You can consider COM interop in order to use them in dotnet.

regasm – is needed when your new COM needs an existing dotnet assembly

regasm.exe, learning notes

I keep forgetting that regasm is needed when your new COM needs an existing dotnet assembly. Therefore you regasm a dotnet assembly file, not an existing COM DLL.

COM types are defined in a type library. A type lib can be a standalone *.tlb file OR embedded in a non-dotnet dll or exe file.

regasm.exe reads the metadata within a dotnet assembly and writes into the registry, which allows COM clients to create .NET Framework classes. After that COM clients can use it as though the class were a COM class. Instances of classes within the dotnet assembly can be created from within dotnet but not from COM until they are registered.

regasm.exe can optionally otuput a TLB file. Tlbexp.exe does the same. A COM client can then use the TLB file to instantiate the a dotnet class, as it instantiates a COM class.

NPE and other exceptions in SL OOB

I’m no expert on SL OOB. I realize unlike in other environments, when an exception is thrown in a SL OOB, the consequence is unexpected. Blank screen is common.

Most common exception is NPE. If I get to see a log msg that’s good, but it doesn’t say in which thread on which line.

In other contexts I often throw exception to indicate error. At least the stack trace is logged. I now avoid throwing any exception. Rather I create an exception object and log it without throwing. Then I either close the application or continue.

For non-NPE, I enable MSVS exception-debug to help me locate the bug.

I feel the Aplication_UnhandledException even handler doesn’t help much. It does get triggered when I “throw”, but I still get blank screen.

IRS valuation, again

When I see “value of a swap” I first remind myself this is about the value of a series of cash flows, like the combined value of “out $19, in $13, out $2, in $5 …”. This value could be positive or negative. Next I ask myself “value of the cash flows to side 1 or 2”? [1]

A) First, you are always one of A) fixed-receiver or B) float-receiver. I feel for a beginner like me, better focus on one side. [[Hull]] example focuses on the fixed receiver. If IRS is important to your job, you will see both sides so many times and they will become quick and intuitive. From now on, Let’s focus on the fixed receiver.

B) The “position” on our book is valued as a series of cash flows including Inflow and outflow. The valuation calc basically
1) predict the size of each inflow and outflow at each future payment date [1]
2) discounts the net Inflow to PV like $81, -$12, -$25, $52..
3) add up the PV to a single dollar amount like $99

If total inflow PV is positive, then the position is like an asset (as of today). Devil is in the details. For now, step back from the gory details and notice the few important details —

Q: key question (per Kuznetsov) is how to estimate today the rate [1] to be announced in 3 months (by the BBA). It’s not fixed on sign-up but “floating” like the future temperature in Dublin 3 months in the future. How is it estimated?
A: basically use the IR numbers known today to back out (RN?) the 3×9 fwd rate “6M rate 3M forward”. I guess this is like the UIP (not the CIP) — treating the RN expected spot rate as the natural expected spot rate. See http://en.wikipedia.org/wiki/Forward_rate_agreement

Note the very first payment date is fixed, not some “unknown future temperature”. See [[Hull]].

Q: what kinda input data are needed to produce the soft market data which are needed for IRS valuation?
A: Lin Yu pointed out the key soft market data is the libor/swap curve, which often uses Libor, ED Futures , Swap rates from the market. But that’s a bit involved. For now, let’s look at the simple calc in [[Hull]]
A: Libor deposit rates. (A bit tricky!) If valuation-date is denoted T, and the payment dates are T+3M, T+9M .. then we need rate for a loan from T to T+3M.
** I’d say from Libor deposit rates T to T+…., we could estimate all the floating payment.
** note ED future rates are not used in this simple calc.

[1] A minor point — if the value of the cash flows nets to positive for one side, it must be negative for the opposite side.

important IV/GTD skills for algo trading developer

Traditional (“real”) algo trading is still confined to buy side. (Sell side algo trading includes things like RFQ auto pricer, limit order auto pricer etc. Dr. Chou points out execution cost reduction algos…) Such shops look for the “strategist” role which is often (usually?) a different role from the implementation engineer’s role. Allow me to focus on the latter.

In my opinion, the really important skills are all about optimization, latency, performance Engineering

#1 [IV] algorithm/data structure optimization
#2 instrumentation (incl. debugging) — profilers, memory leak detector
# [IV] threading — for multiprocessor
# network/socket programming, including NIO
# fast messaging – like RV or solace
# memory management (c++), GC tweaking (java)
# OS tuning including memory, net and CPU
# cache products – like coherence
# exchange conn beyond FIX – understand its idiosyncrasies

Other skills are important to the team but less important to the engineer
– market data gateway
– back testing
– architectures
– OO designs
– automated pricing. No need to understand BS or bond math, or hedging techniques
– basic trading algorithms
– scripting for system automation
– statistics software

For sys admin, I prefer command line to windowing GUI

I prefer GUI text editors (including IDE) to command line editors. I prefer GUI browsers to command line browsers. Most office applications (word processing, spreadsheet etc) must use a windowing GUI. However, for administration, like many power users I prefer the window-less command/text interface (CTI) to the Microsoft suite of control-panel screens. Note Microsoft also offers many admin tools as CTI.

– CTI output is easier to search. GUI can present hierarchical output better but requires you to click many times and you need to know where to drill down. For complex CTI tools you may need to remember a bunch of switches. Switches are easy to document. I find them easier to remember. It’s a long tradition among CTI tools to offer a combination of switches that shows just about everything in pages of text and expect users to grep. Personally I find it rather effective dealing with large-volume data output.
– CTI much easier to script and automate
– CTI easier to integrate with programming languages
– CTI generally easier to remote-execute. For windows, you need to go through a few layers of windows.
– CTI trades mouse for keyboard. Many (probably majority) Power users prefer keyboard.
– CTI often helps us see errors. In MS-win, the error can be hidden somewhere in the GUI or in the event log. To be fair, most administration tools (CTI or GUI) have difficulty communicating errors effectively.
– CTI (result) easier to compare, across versions or across machines. You just save the output in a text file and diff.
– CTI easier to redirect input/output to files. Some GUI have that support but often fail to capture Complete output, which is easy and natural in CTI. Every unix CTI lets you merge stderr and stdout to a single file.
– CTI easier to collaborate with tech support or remote colleagues
– CTI has a cleaner configuration (a kind of input). GUI uses nested panels — often hidden from view. In contrast, CTI uses either config file or nested command line switches — easily documented.

books on MSVS

Most books are too thick and duplicates the very big MSVS documentation

[[ Mastering Visual studio .NET ]], available in Jurong East library, is one of the best but covers VS.net 2003. Covers debugging in-depth.

C++ and c# are the 2 implicit targets.

%%jargon – readonly ^ permanent handles

If we refer to pointer variables and reference variables collectively as “handles” on objects (on heap/stack), then know the difference between a permanent handle and a readonly handle. A permanent handle can’t (compiler enforcement) rebind/reseat to another object. A RO handle can’t (compiler enforcement) be used to edit the target object.

Tip: a stack object can have 0, 1 or multiple handles attached. However, a heap object must have some handle, otherwise it’s unreachable and a memory leak.
Tip: there are detached handles — null pointer and uninitialized pointer
Tip: the pointee/referent of a handle can be another handle

Now Let’s focus on permanent vs readonly handles. There are 4 basic forms — perm, RO, permRO or a plain old handle. Now since there are pointer and reference, we have up to 8 flavors, some illegal.

RO ptr? ptr to const
RO ref? ref to const, widely known as “const-ref”
perm ptr? const ptr
perm ref? the plain vanilla reference
permRO ptr? const ptr to const
permRO ref? same thing as RO ref
plain old ptr? non-const ptr to non-const
plain old ref? rebindable ref is illegal

That’s c++. How about java? pointer variables are illegal. All handles are references.
RO ref? no language support
perm ref? final variable
permRO ref? no language support
plain old ref? yes by default.

mediocre WallSt developers earning WallSt-level salary


A paradox to share with you. For years I often wonder

Q1: why does wall street (a shorthand for “world financial centers”) pay something like double the salary compared to main street, for a senior programmer? (Disclaimer — Wall St is not the only sector; premier tech firms also pay well above main street.) On many teams including my current team, I see people doing main street programming work, using main street technical skills without any financial domain knowledge. Why pay us so much higher than main street?

I feel this is a non-trivial paradox. A deeper understanding helps us manage our career, manage our competitiveness, manage our job security, manage our workload, manage our value-add.

In this letter I use “ibank” as a shorthand for all the employers in the investment-banking and security trading business, sometimes including buy-side. Yes I’m deliberately vague and imprecise.

Factor 1: ibanks have higher margin than main street, even after 2008, and crucially a small percentage of employees are responsible for the profit. Many ibank jobs are, to be honest, not critical to the money-making machinery, but many developer jobs are. To put things into perspective, traders, fund managers, deal-makers, advisors/salesmen … are in the profit center and even more instrumental to the money-making machinery. The closer to the money, the higher is one’s value to the ibank. Highest paid wall street (non-manager) tech roles are probably the high-frequency low-latency experts because they are closest to the money. Their code has a most direct impact on the competitiveness of our trading engine in the arms race. Similarly, our pricing system calculates the prices sent directly to the market and have a direct impact on our competitiveness.

Therefore, many senior programmers are considered instrumental/important for the high margin of an ibank.

Factor 2: these tasks are often fairly complex and easy to mess up, and therefore calls for expertise, backed by track record. (To be fair, these are not the most complex.) For comparison again look at the most important roles in ibank profit centers. There’s usually a lot of decision-making, a huge flood of information to digest. An ibank can hire fresh graduates to create trading applications, but they are likely to fail to meet some of the requirements. In and outside Wall St, software dev is knowledge-intensive, methodical, intricate, even fragile [1], unlike many other jobs in finance.

[1] sensitive to human mistakes. That’s why we say 99.9% of software have bugs.

Therefore ibanks generally recognize in-house app dev as high-value (sometimes instrumental/enabling) and complex. I have heard many times “we can’t introduce this new instrument to the market without the IT system”.

Factor 3: Just a small number of experienced coders are needed. In main street, we see armies of developers for a given project. (Lee Chong Wah may disagree.) On Wall St, team size is cut to a third or a quarter. In so many ibanks I have worked, I always see a lot of senior developers and few junior developers. I feel OCBC (and other traditional banks) see developers as the same “armies”.

Therefore ibanks have high profit, and want to use part of the profit to attract a small number of elite developers. This is a “perfect storm” for higher salary. Now, in reality, a team of 3 to 7 senior developers would have just 1 key developer, so the other guys are not that critical. Perhaps one of them is doing nothing special at all. Low-value, low-importance, mundane code, but still paid well above main street. (I might be one of them now.)

Factor 4: During recruitment, ibanks typically have an adequate budget for a senior developer. If market rate is 140k, and their budget is 90k then they eventually hit the reality that strong candidates (with track record) won’t be interested, so they end up scraping the bottom of the barrel. Mostly tough and unsuccessful.

So managers go get a sufficient budget, screen, select and offer to this fairly strong candidate, but for various reasons he doesn’t get to play that key role. He may even end up with some truly unimportant modules, somewhat over qualified and overpaid. Managers can’t just drop him, but can leave his contract (if contractor) to expire without renewing. If not a contractor, then bonus is in question. If the guy is actually good but the mundane job must be done in the team, then maybe unfair to mistreat him….

That’s my answer to the opening question Q1. But there’s another question, because ibank managers are not stupid….

Q2: For the non-critical roles, why don’t they hire strong main street developers and pay them main street salary, to do whatever main street work there is in the project? No track record needed.

I feel many enlightened and bold ibank managers actually do that, but there’s a risk of uncertainty in following that route. Instead, most managers much prefer candidates with proven track record in a similar project. Also, recruiters are reluctant to submit profiles without relevant experience. Therefore, main street developers are largely excluded from the talent pool.

unboxing allocates mem on stack

We are more familiar with boxing – allocating heap memory for a 8-bit byte object and returning a pointer.

Actually, unboxing also needs to allocate memory, usually on the stack. Without this stack object, we would always access the heap object, which could be modified by another handle. Unboxing should return a pass-by-value object that's not shared with other handles.

my own c# regex examples

Regex _regex = new Regex(“(\<script(.+?)\)|(\<style(.+?)\)”,
            RegexOptions.Singleline | RegexOptions.IgnoreCase);

_regex.Replace(html, String.Empty);

 ret = loginNameQualified.ToUpper();
 ret = Regex.Replace(ret, @”(.+\+)?(.+)”, “$2”);

static Regex REGEX1 = new Regex(@”(https://)(.+)(:d+.*)”);
var ma = REGEX1.Match(strValue);if (ma.Success){
                        var tmp_hostname = ma.Groups[2].Value;

var str = Regex.Replace(upSince.ToString(), “.*(up since.*)”, “$1”, RegexOptions.IgnoreCase);