what things are #include’d – c++, java, python

Not just utility functions, but also

* global variables like cin.
** think of them as fully initialized OBJECTS (or services), usable out of the box

* classes – as cookie cutters, very different from cookies the OBJECTS above
** and Families of classes

Java (and c#?), in contrast, only put TYPES in included libraries. To include (and then use) utility _OBJECTS_, they have to be presented as public static fields like System.out. These static member objects get created when the TYPE is loaded by the class loader. In contrast, c++ library objects (like cout) are typically part of a  namespace.

Why the difference? I think reason is the so-called “free(standing)” variable outside any type. Java forbids it. Any language permitting it can package it along with types and free functions into a “unit” (package, module, namespace…) to be #included.

Python (and perl) is more like c++.

Advertisements

How do you identify high workload]the team #XR

These are not the heaviest stressors — see ## 2 heaviest work stressors 

  • Q: Do you need to record how you allocate time to assignments in blocks of 2 hours, 4 hours or 8 hours?
    • Only happened in GS
  • Q: Do you need to everyday describe, verbally, what you did in the previous day? I never did.
  • Q: When neck, back, or eyes feel tired, do you always feel free to get away from computers for a while? I always do
  • Q: Do regular developers feel relaxed enough to write, if they like to, documentation in wiki or for other people? I usually do
  • Q: Do you get time to make 30-minute chitchats with friends on office phone during office hours? I usually do.
  • Q: Do you feel pressure to return to work after 45 minutes at lunch with a friend, assuming no official policy about how long lunch break is.
    • Only happened in GS, BAML, Barclays
  • Q: do you feel relaxed enough to read department newsletter? Exclude brief light periods after a project.
  • Q: do you feel relaxed enough to read technology newsletter? Exclude brief light periods after a project.
  • Q: Do you think twice about going out for lunch once a week?
  • Q: Do you ever work out, however briefly, before leaving office?
    • I did in BAML, Barclays, Macquarie, ICE
  • Q: Suppose you are stuck waiting for an answer or for a build to finish, do you feel you can just stop working for 15 minutes? What if you get such “waiting periods” everyday? Does your manager make it a rule that you should take up some of the numerous other tasks during such a short wait?
    • Only happened in GS
  • Q: As a developer, during office hours do you feel relaxed enough to read business publication related to work? I believe (even lead) developers never have to read such things to do a decent job. Required business knowledge is highly site-specific and accrue during projects. Knowledge about industry is never a must.

market^limit order — market-taker^maker

To some extent, we can say

market maker –uses– limit orders
market taker –uses– market orders

If you have an open position to close[1], you often have a choice to submit a limit order or market order. In a typical security’s market, at any time there are just 2 important quotes i.e. best bid/ask.
$ market makers execute at the “better” of the 2 prices
$ market takers execute at the “worse” of the 2 prices

Market takers “suffer” the inferior price to get immediate execution.

[1] Now, even if you have square position, you still have the 2 basic choices, but situation is less intense.

SoapHttpClientProtocol – capture inbound xml

    /// Decorates the generated SoapHttpClientProtocol subclass with inbound snooper
///
/// Based on http://orbinary.com/blog/2010/01/getting-the-raw-soap-xml-sent-via-soaphttpclientprotocol/
public class ServiceDecoratorI : ServiceDecoratorO
{
private ExposedHttpWebResponse _lastResponseNonNull = new ExposedHttpWebResponse();
protected override WebResponse GetWebResponse(WebRequest request)
{
_lastResponseNonNull = new ExposedHttpWebResponse(request);
return _lastResponseNonNull;
}
public string InBoundXml
{
get
{
return XDocument.Parse(_lastResponseNonNull.Content).ToString();
}
}
}

class ExposedHttpWebResponse : WebResponse
{
private readonly HttpWebResponse _response;
public override Stream GetResponseStream()
{
var readData = new byte[1024];
int readBytes;

Stream contentStream = _response.GetResponseStream();
var result = new MemoryStream();

do
{
readBytes = contentStream.Read(readData, 0, 1024);
result.Write(readData, 0, readBytes);
} while (readBytes > 0);

contentStream.Close();
_response.Close();

var reader = new StreamReader(result);
result.Seek(0, SeekOrigin.Begin);
Content = reader.ReadToEnd();
result.Seek(0, SeekOrigin.Begin);
return result;
}
internal ExposedHttpWebResponse() { Content = ""; }
internal ExposedHttpWebResponse(WebRequest request) { _response = (HttpWebResponse)request.GetResponse(); }
public string Content { get; private set; }
public override long ContentLength { get { return _response.ContentLength; } }
public override WebHeaderCollection Headers { get { return _response.Headers; } }
public override string ContentType { get { return _response.ContentType; } }
public override Uri ResponseUri { get { return _response.ResponseUri; } }
}

SoapHttpClientProtocol – capture outbound xml

<![CDATA[ // Decorates the generated SoapHttpClientProtocol subclass with outbound snooper public class ServiceDecoratorO : MainRequestResponse__HTTP { private XmlWriterSpy _writer; public string OutBoundXml { get { return (_writer == null ? null : _writer.Xml); } } protected override XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize) { _writer = new XmlWriterSpyE(base.GetWriterForMessage(message, bufferSize)); return _writer; } } ///

/// This class has special behavior when writing end-elements ///

class XmlWriterSpyE : XmlWriterSpy { public XmlWriterSpyE(XmlWriter implementation) : base(implementation) { } public override void WriteEndDocument() { _me.WriteEndDocument(); _bu.WriteEndDocument(); return; } public override void WriteEndElement() { _me.WriteEndElement(); _bu.WriteEndElement(); var xmlStr = _sw.ToString(); //if (xmlStr.Contains(BBGviaSOA.ticker.id)) // Log1.log(xmlStr); } } /// Wrapper class for XmlWriter that “spies” on the Xml being written /// and later makes that Xml available as a string to anyone who wants /// to inspect it. See also my blog description of this class: /// http://orbinary.com/blog/2010/01/getting-the-raw-soap-xml-sent-via-soaphttpclientprotocol/ public class XmlWriterSpy : XmlWriter { protected XmlWriter _me; protected XmlTextWriter _bu; protected StringWriter _sw; public XmlWriterSpy(XmlWriter implementation) { _me = implementation; _sw = new StringWriter(); _bu = new XmlTextWriter(_sw); _bu.Formatting = Formatting.Indented; } public override void Flush() { _me.Flush(); _bu.Flush(); _sw.Flush(); } public string Xml { get { return (_sw == null ? null : _sw.ToString()); } } public override void Close() { _me.Close(); _bu.Close(); } public override string LookupPrefix(string ns) { return _me.LookupPrefix(ns); } public override void WriteBase64(byte[] buffer, int index, int count) { _me.WriteBase64(buffer, index, count); _bu.WriteBase64(buffer, index, count); } public override void WriteCData(string text) { _me.WriteCData(text); _bu.WriteCData(text); } public override void WriteCharEntity(char ch) { _me.WriteCharEntity(ch); _bu.WriteCharEntity(ch); } public override void WriteChars(char[] buffer, int index, int count) { _me.WriteChars(buffer, index, count); _bu.WriteChars(buffer, index, count); } public override void WriteComment(string text) { _me.WriteComment(text); _bu.WriteComment(text); } public override void WriteDocType(string name, string pubid, string sysid, string subset) { _me.WriteDocType(name, pubid, sysid, subset); _bu.WriteDocType(name, pubid, sysid, subset); } public override void WriteEndAttribute() { _me.WriteEndAttribute(); _bu.WriteEndAttribute(); } public override void WriteEndDocument() { _me.WriteEndDocument(); _bu.WriteEndDocument(); } public override void WriteEndElement() { _me.WriteEndElement(); _bu.WriteEndElement(); } public override void WriteEntityRef(string name) { _me.WriteEntityRef(name); _bu.WriteEntityRef(name); } public override void WriteFullEndElement() { _me.WriteFullEndElement(); _bu.WriteFullEndElement(); } public override void WriteProcessingInstruction(string name, string text) { _me.WriteProcessingInstruction(name, text); _bu.WriteProcessingInstruction(name, text); } public override void WriteRaw(string data) { _me.WriteRaw(data); _bu.WriteRaw(data); } public override void WriteRaw(char[] buffer, int index, int count) { _me.WriteRaw(buffer, index, count); _bu.WriteRaw(buffer, index, count); } public override void WriteStartAttribute(string prefix, string localName, string ns) { _me.WriteStartAttribute(prefix, localName, ns); _bu.WriteStartAttribute(prefix, localName, ns); } public override void WriteStartDocument(bool standalone) { _me.WriteStartDocument(standalone); _bu.WriteStartDocument(standalone); } public override void WriteStartDocument() { _me.WriteStartDocument(); _bu.WriteStartDocument(); } public override void WriteStartElement(string prefix, string localName, string ns) { _me.WriteStartElement(prefix, localName, ns); _bu.WriteStartElement(prefix, localName, ns); } public override WriteState WriteState { get { return _me.WriteState; } } public override void WriteString(string text) { _me.WriteString(text); _bu.WriteString(text); } public override void WriteSurrogateCharEntity(char lowChar, char highChar) { _me.WriteSurrogateCharEntity(lowChar, highChar); _bu.WriteSurrogateCharEntity(lowChar, highChar); } public override void WriteWhitespace(string ws) { _me.WriteWhitespace(ws); _bu.WriteWhitespace(ws); } } ]]>

trade commission exists in any security market

Many authorities claim that “unlike stock market, this … market has no trade commission”. Those authorities often say you just pay for the bid/ask spread and the dealer just earns that spread.

In reality, any transaction (even if just buyer/seller without middleman) can involve commission or transaction fee. The bigger player or Whoever with more power can more or less “arbitrarily” impose a transaction fee to cover her expenses, whatever they are.

#1essential classificationS@variables]each language: java/c++/c#

In java, a variable is exactly one of 2 — reference var or primitive var. Therefore, a thingy/entity (more precisely an”allocation”) is either an OBJECT or a primitive. Clean and simple.

C++ doesn’t differentiate between primitives and composite objects. However, it does differentiate between allocation locations. In C++, an entity either lives on heap, on stack or global area, with important differences. The word “object” has no special meaning.

See also http://bigblog.tanbin.com/2009/06/primitive-vs-object-dichotomy-not-in-c.html
See also http://bigblog.tanbin.com/2011/04/pbref-pbclone-in-any-language.html

In C#, avoid the word “object” if possible. The real, deep and precise (less clean than java) dichotomy is value-type vs reference type.
* a reference type variable occupies 32 bits on a 32-bit machine. The pointee is always on heap, and could be 99 Bytes.
* a value type variable is like a nonref in c++ and occupies 99B. Unlike reference type variables, there’s no separate storage for the variable.
* See http://bigblog.tanbin.com/2011/10/rooted-vs-reseat-able-variables-c-c.html

– assignment to a reference type var copies the RHS 32 bit address and reseats the LHS pointer. Same as java.
– assignment to a value type var bulldozes the LHS and clones the 99B into it.
**Java only clones primitives, up to 64 bits.
**C++ can clone any variable if you pass-by-value.

invokeLater() – impossible to refactor@@

Q: if there's a block inside my method, we can wrap an invokeLater() around it — anon inner class. But is this always possible, assuming local vars are final.

What if a local var must be non-final, but this variable is in the target block? I feel we can create a “final” clone of this variable and use it in the target block.

What if a variable is modified in the target block? I feel we can create a mutable clone and make it a local-var, local to the invokeLater() (actually the run method).

What if a variable is modified in the target block but needed after the block? What if the target block has side effects on local variables?

What if the target block creates objects to be used after the block?

MOM server sends data + thread to client runtime

On many (if not all major) aspects, a market data feed is implemented exactly as MOM, but our focus today is market data systems, not MOM.

A market data vendor almost always ships 2 pieces of software — a server and a client-runtime. Server is a standalone (but embeddable) process, a JVM or C application. This server process could be managed by the vendor (like NYSE). Alternatively a subscriber bank could run its own server process.

I borrowed the term from JMS literature — “client runtime” is never standalone, but an embedded jar or dynamically loadable library (*.so files in unix).

We all know that server PROCESS sends data to client runtime, but someone pointed out that server also sends the thread(s) along with the data. Here’s my interpretation —

Initially in the client JVM (or C process) there’s just one thread blocked in some kind of wait(). I feel it has to be a server socket blocked in accept(). When data comes down the wire, an additional socket is created in client JVM to receive it. This requires a new thread, since the old thread must go back to waiting.

Q1: what does the new thread do?
%%A: newSocket.getInputStream().readxxx() and then onMsg(). I believe system maintains a list of registered listener objects, each with its own STATE. Each listener instance can also unsubscribe and leave the “club”. Upon a single message arrival, the new thread calls listener1.onMsg(…), listern2.onMsg(…) and so on, where listener-N are interested in this message.

Q2: are these called in sequence or in parallel?
%%A: both possible. Since the new thread is recently created for this message, i believe it’s possible to create multiple threads. It’s also possible to reuse a thread pool.

Q3: what are the pros and cons of single vs multiple thread?
%%A: if real time, and onMsg() is time consuming (quite common), then no choice — multiple.

Markov chain – dice problem in XinfengZhou’s book

I find it instructive to give distinct colors to the 11 distinct outcomes {2,3,4,5,6,7,8,9,10,11,12}. It’s important to simplify notations to reduce numbers.

How do we choose the colors? Since we only care about 7 and 12, I give Tomato color to 7, Blue to 12, and white to all other outcomes. From now on, we work with 3 concrete and memorable colors Tomato/Blue/White and not {2,3,4,5,6,7,8,9,10,11,12}. Much simpler.

Each toss produces the 3 colors with fixed probabilities. At the initial stage, it’s not important to compute those 3 probabilities, but I don’t like 3 abstract variables like p(tomato), p(blue), p(white). I find it extremely convenient to use some estimate number like p(getting a tomato)=5%, p(blue)=11%, p(white)=1-p(T)-p(B)=84%

Now, we construct a Markov chain diagram. Suppose a robotic ant moves between the stations. The probability of choosing each “exit” is programmed into the ant. From any station, if you add up the prob on each exit they add up to 100%. On P109, at means the prob(ultimately reaching absorbing Station_b | ant is currently at Station_t).
att = 0 because when ant is at Station_tt it can’t escape so prob(reaching Station_b) = 0%
aw = p(ant taking exit to station_b) * 100%
+ p(ant taking exit to station_t) * at
+ p(ant taking exit to station_w) * aw
at = p(ant taking exit to Station_b) * 100%
+ p(ant taking exit to Station_tt) * 0%
+ p(ant taking exit to Station_w) * aw
Now with these 2 equations, the 2 unknowns at and aw can be solved. But the equations are rather abstract. That’s part of the learning curve on Markov chain

pbref^pbclone in c#, c++ and java, again

Let’s Assume a 32-bit machine.

– java primitive param in a method? Value (up to 64bit) is copied — to the call stack
– java reference param? the 32-bit virtual address of the heapy thingy is copied. Note the caller must provide a tuple consisting of
*** a heapy thingy
*** a 32-bit address, possibly anonymous (How? [1])

C++ by-reference param? the 32-bit address is copied into the function’s stack frame
C++ non-reference param? entire chunk of memory of the argument object is copied, from stack/heap[2] to stack
C++ pointer param? Reference to pointer is illegal.  Declaration is f(someType *). Caller must plug in a 32-bit address of a someType instance. The 32-bit pointer is copied.
C++ double-pointer param? Declaration is f(someType **). See http://bigblog.tanbin.com/2011/04/double-pointer-usage-2-special.html
Note C++ has no rule differentiating a basic type (like char/float) vs a user-defined class.

C# is more complicated
c# by-value on simple/struct type? Entire chunk of memory is copied into the method stack frame
c# by-value on reference-type? 32-bit address copied, just like java reference param
c# ref-param on reference-type? See diagram on P71 [[C#Precisely]] (Value type easier)
** this is true call-by-reference, unsupported in Java
** like C++ double pointer param
** The caller must provide a 32-bit address of a reference variable [3] myvar. Literals like 123 is not a variable therefore unacceptable. Since myvar is a 32-bit pointer, it may point to a 32-bit address 0xAA112233 on heap OR stack
** Let’s say myvar itself has address 0xFFEEFF12. This address is copied into the method as parameter myParam
** myParam becomes a permanent __alias__ of myvar. Effectively 2 names at the same address. myParam may not get a 32-bit storage but it doesn’t matter.
** in the method, assignment to myParam _reseats_ myvar, to 0xAA110000 for example.
** after the method, myvar still points to 0xAA110000

Note when a 32-bit address is copied (pbref), then any change to the pointee is visible after the call; invisible for pbclone.

Note in general, pointee can be on stack or heap

[1] if you pass in new MyType()
[2] how would a nonref param passing involve copying from heap? Granted parameter is nonref, but the argument could be an unwrapped pointer. Another case — a heap object’s field is passed into the function.
[3] myvar can be a primitive variable. Look at P71 [[c#precisely]] to see the difference.

when does implicit cloning occur – c#/c++/java

Background — When we use an existing “object” to write into a variable, we usually [1] get either by-reference-copy, or by-value-copy i.e. cloning, typically bitwise. (C++ allows us to make it non-bitwise via op= and copy ctor.) Here are the contexts that triggers such cloning —

Java is cleanest — only primitives and all primitives get cloned when passing in/out from methods

c# is messier — all Value types and only Value types are pass-by-clone (pbclone). Includes primitives and structs.
* passing in/out from methods
* initializing a Value type variable
* assigning to an existing Value type variable — bulldozes then clones

C++ is most customizable but there are rules too.
* nonref is always pass-by-clone, but you need to look at function prototype. The original argument object could be a reference variable but may still pass-by-clone
* nonref variable init or assignment clones
* unwrapped pointer assignment clones
* reference variable assignment (not init) clones, since reference vars refuse re-seating.

[1] let’s not discuss why “usually”

rooted vs re-bindable variables – c#, c++, java, python

Q: What kinds of variables can re-bind (reseated) to a different object at run-time and what kinds can’t? This understanding is not academic but helps programmers remember ground rules.

—-Python moves further towards rebinding. Even a simple myInt variable can rebind. I feel the fundamental distinction in python world is between immutable vs mutable “Objects” (defined as storage-locations).
* Python Immutables are reference-counted, probably copy-on-write. Therefore variables bound to immutable Objects are reseat-able.
* What python variables are rooted? Well I believe the first element (other elements too) in a tuple is, though the tuple variable itself can rebind.

—-In java, all primitive variables are “rooted”. All reference variables are reseat-able.
+ Assigning to a primitive variable writes into “the ultimate” memory location;
+ Assigning to a reference variable reseats the pointer, without cloning any object.
– There’s a Separate 32-bit storage for every reference variable, distinct from pointee’s storage.[1]
– There’s no separate storage for a primitive variable. Variable name is a nickname of the storage address. Compiler translates variable name into storage address. Run-time access to variable is one-hit. In contrast, Reference variables’ access is 2-hit – following the pointer.

[1] Evidence? See memory layout of any MyClass having a non-primitive field. How much memory (like sizeof(MyClass)) is allocated by new MyClass()?
—-In c#, all Value variables (including structs) are rooted. Assignment clones, including pass-by-value into a method.
All reference variables can be rebound.
—-C is simple and clean
All pointer variables can be re-seated but non-pointer variables are rooted. When a variable is on the LHS, it either rebinds or the Object is “edited”. See post on “Immutable, initialize..” to see the difference between Object vs Variable.
—-C++ feels more complicated.
In C++, all nonref and reference variables are rooted. Assignment writes directly into the object’s “stomach”. Pointers are reseat-able.

However, a C++ reference variable (like pointer variable) has a separate 32-bit storage (address hidden) distinct from pointee/referent storage. Some writers say “referent” but I find “pointee” more distinct and less ambiguous.

Biggest ECN markets in retail-friendly credit markets?

Hi Youwei,

I guess your biggest product types are corporate bonds, muni bonds,
mortgage backed, but not US treasuries, interest swaps nor credit
default swaps?

Looks like Bloomberg is the most popular ECN overall in the FI space?

Is TradeWeb the 2nd most popular?

How about market-axess?

Is ION also an ECN or mostly a data-source/data-storage. I find it
hard to “define” the primary role of ION. I believe it's a first and
foremost a consolidator of market data. Top 5 products supported on
ION?
1) Government Bonds,
2) Corporate Bonds,
Emerging markets (I believe mostly credit products),
Repos,

IV questions/answers on trading system testing

See post on biz-scenario [S] ^ implementation error [I].

More and more interviewers seem to ask increasingly serious questions about trading system testing. I feel that’s part of our professional growth towards senior positions (though many Wall St lead developers I know aren’t big fans). In some cases, the higher you are paid, the deeper testing experience is expected of you.

Testing skill is a complement to design skill, both of which become increasingly important as we move up. These Interview questions tend to be open-ended. Interviewer gives us a chance to sell and convince them that we do have real experience and personal insight. Here are some pointers for myself. Your inputs welcome.
–At the Tools level, we can easily talk about Fitnesse, Mockito, JUnit, DbUnit etc [I]. Easy to read up about these. Easy to tell real stories. Nothing unique about trading systems.
–Another level of easy topics – regression testing, integrated testing. Again nothing unique about trading system. Since i have personal experience [S] i usually spend a lot of time at this level.
* scenario testing — essential in larger systems. Without scenarios, if you ask a rookie to list all the combination of various inputs, the list is endless and useless. Instead, I use assertions to reduce the combination.
* matrix testing — i often draw up some spreadsheet with rows and columns of input values.
* production data — essential if we were to involve users. The earlier we involve users, the better.
* I use JUnit to drive integrated tests with real DB, real MOM, real cache servers, real web servers

–At another level, special to trading systems are load testing, performance testing, thread testing, MOM testing. MOM and esp. thread testing uses special techniques. But in reality these are seldom adopted. An interesting question I like to ask is “at what specific (number please) load level will my system crash?” It’s good if we can predict that magic number. If we can’t then we might get caught with our pants down, which is quite common. I feel trading system testing can be divided into logic testing and performance testing. In reality, most small enhancements need only logic testing.
–Perhaps the highest level is UAT [S]. I would mention Fitnesse. I would make it clear that in GS, users or BA are called upon to generate test cases.
–At a methodology level, a tougher question is something like “How do you ensure adequate testing before release?” or “How do you decide how much testing is enough?”. Since I never used test coverage [I] tools, I won’t mention it. Frankly, I don’t believe 80% test coverage means the code is more reliable than 0% test coverage
— The most productive GS team members do no automated testing at all. They rely on experience to know what scenarios are worth testing, and then test those manually, without automation. When we realize bugs are released to production, in reality we almost always find our test scenarios incomplete. Automated tests won’t help us. But I can’t really say these things in interviews — emperor’s new dress.

user-defined implicit type conversion – uptake in C#

Q: in real c# applications, do folks really create user-defined conversion in their own classes? Is this a rarely “used” feature like java vararg, java nested interfaces, java annotations, java dynamic proxy..? By “rarely used” i mean java developers seldom _create_ such features in their classes, though they may use these features in “off-the-shelf” libraries.

A c# veteran answered — “It would likely be used primarily if you are creating an app framework ((library? not the same thing)), which occurs quite often still in each application silo .. We have a framework ((wrapping)) around Sql Server Analysis Services with Silverlight UI.  There are calculation engines which deal with Matrices and Vectors of data, so these types of structures would include some type conversion for the people using the framework.”

((everything in double paranthses are my annotations))

common technical challenges in buy-side software systems

10 – 30% of wall st IT jobs are on the buy-side such as funds, portfolio and asset management.

* Core challenge – sub ledger. In one system there was more than 100,000 client accounts in the sub ledger. Each is managed by professional financial advisors. I believe each account on average could have hundreds of positions. (Thousands would be overwhelming I feel.) Since clients (and FA) need instant access to their “portfolio”, there are too many positions to keep up to date. Core of the entire IT infrastructure is the subledger.

** Number of trades per day is a 2nd challenge. These aren’t high-frequency traders, but many Asian clients do nothing but brokerage (equity) trades. Per-account not many, but all the accounts combined is a lot of processing overnight. These must add to the sub ledger by next day.

* quarterly performance reporting
** per-fund, per-account, per-position
** YTD, quarterly, annual etc

I guess there is also monthly performance reporting requirement in some cases.

* asset allocation and periodic portfolio re-balancing — for each client. Key differentiators. Investors get a large “menu” of funds, products … For comparison, they may want performance metrics.

– VaR or realtime risk? Probably important to large funds
– pricing? Probably important to large funds

– swing/wpf not really required. Web is adequate.
– trade booking? not a challenge

valuation@various options: complexity imt…

“imt” = is more than

I only know bonds, FX, futures. I feel their valuation is simpler than options.

As stated in http://bigblog.tanbin.com/2011/01/fi-is-more-complex-due-to-time.html, all derivatives have the added complexity of time to maturity. In the same vein, many derivative positions are held “open” longer than cash positions. While open, sensitivity to a lot of variables must be monitored. This is the essence of risk management. Consequence is grave if you neglect these exposures. Valuation is key.

option valuation depends on many (fairly fast-moving) variables
– underlier price swing
– passage of time – theta
– volatility, which is unknown and must be inferred
** volatility itself is a random variable and has a volatility

Option traders not only monitor option valuation, they must monitor delta (#1 sensitivity) and delta’s sensitivity. Delta is affected by
– underlier price swing. If you have an open position, and its Delta value is 68%, it won’t be valid when underlier drops
– volatility, which is unknown and a guesstimate
– time to maturity. Your 68% will not stay the same tomorrow, even if underlier stays constant

I recently picked up a market maker’s brochure on variance swap. Designed for buy-side clients, not quants.
Even the entry-level concepts involve stdev. Since there’s a time element, all the vol values need adjustment. Even pnl involves sigma squared.

I was told fixed income is also complex in terms of math, but I feel the entry barrier is a bit higher in this space.

returning handle to class internal data #effC++

Never quizzed in QQ, but how about BP? Might be a code smell.

Since c++ compiler is permissive and assumes you know what you wrote I believe it will compile.

Returning a handle (ref/ptr) to a class’s internal data is an everyday reality and inevitable, but tricky (– like online shopping) [[EffC++]] has a nice chapter /devoted/ to this topic. I don’t have the book now so here are my personal views.

Note — in contrast returning by value (pbclone) is safe.

java programmers feel safe to return “internal data”. I know from experience it’s seldom an issue. Therefore most of those c++ issues below are somehow “fixed” in java, largely because java is a simple_clean language, compared to the other 2 superpowers — C# and c++.

Issue: java immutable classes should not expose fields (except final primitives??). Ditto for c++, but more fine prints … C++ immutable is almost impossible. Constness can be cast away. A double-pointer field… (Java “final myField” means a field of either const-object or const-pointer to non-const object)

Issue: if an object is const or a method is const, then exposing that handle can break that “promise”. Apparently, c++ compiler can’t detect the “leak”. Java doesn’t make this kind of const “promise”

Issue: Once a handle is returned and “cached”, the original object had better (but often can’t) stay clear of the bulldozer, either on the stack or the heap. Fixed in java…(obviously)

Issue (java too): if a field is an integral and inseparable half of a pair which must be manipulated as a whole, then exposing the field alone is questionable. (Let’s not go into details) Any example?

threading features – c# improving over java

C# “adapted” many java designs, but by different magnitudes.
+ Where c# creators found _robust_ designs in java, they made minimal adjustments — like in threading. Basically wholesale borrow.
+ Where they found problematic/controversial designs, they made bigger adaptations.

Java’s *language* support for threading is rather clean and comes in only 3 foundation building blocks – creation, locking and signal. So in these 3 areas what are the adjustments by c# creators?

– signaling (wait/notify) – identical
– locking – identical
** synchronized keyword replaced by lock keyword
** static synchronized similarly adapted

– thread creation – Fundamentally identical. Superficial changes
** delegate to replace Runnable interface

Beyond these 3, java language includes some important (but less central) features, largely borrowed wholesale by C# creators.
– join
– interrupt
– sleep

Other java threading support is largely “superstructure”.
+ read-write lock
+ thread pool
+ callable tasks and future results — http://bigblog.tanbin.com/2010/09/callable-tasks-future-results.html

y a nested java class can only access final local variables

The explanation is tedious because it has to cover primitive/reference type local variables.

1) Primitive is easier. Say there’s local_boolean isGood variable. If it’s final, then the nested class can simply get an implicit isGood final_private_boolean_field when the nested instance (I’m not saying “class”) is allocated. Probably 1 byte added. All final primitive variables are immutable, which makes things simple.

2) Suppose this local variable is not final, and it’s accessed by a static or non-static NestedClass.m1(). At run time, when m1() runs, isGood is quite possibly out of scope if the stack frame is wiped out. The “last” value of isGood is lost.

3) Contrast 2) with a final_local_reference variable acct, with a mutable Boolean field acct.isGood. Final, so NestedClass instantiation can allocate a 32-bit immutable pointer to the object behind acct. This will put off the garbage collector. When NestedClass.m1() runs, the real time value of acct.isGood will be used, even though it’s mutable.

4) Now consider non-final_local_reference variable acct. acct can point at one object then another object. These objects are on heap, but the 32-bit local pointer is on stack. Stack frame could be gone when m1() runs. The address of the “last” object referenced by acct would be gone.

## practical app documentation in front office

+ Email? default “documentation” system, grossly inefficient but #1 documentation tool in practice.
+ Source code comments? default documentation system

— above are “organic” products of regular work; below are special documentation effort — tend to be neglected —-

+ Jira? rarely but can be used as organic documentation. easy to link
+ [jsw] Cheatsheet, run book and quick guide? same thing, very popular.
+ [jsw] Troubleshooting guide on common production issues
+ Design document? standard practice but often get out of date. A lot of effort. Often too high-level and not detailed enough for developers/implementation guys — written for another audience. Some people tell me a requirement spec should be detailed enough so developers could write code against it, but in reality, i have see extremely detailed specs but developers always have questions needing real time answers.

+ Unit test as documentation? Not sure. In theory only.
+ Wiki? widespread. Easy to link
+ sharepoint? less widely used than wiki as documentation tool. organized into a file tree.

The more “organic”, the easier, and the better its take-up rate; The more artificial, the more resistance from developers.

[w = wiki]
[j = jira]
[s = sharepoint]

Examples of "Structured_Products"

I believe “structured” and “exotic” mean the same — tailor-made or customized. (However, non-listed products could be quite common. There are so many common OTC instruments but I will give just a few — CDS, IRS, FRA, FX swaps, currency swaps, muni bonds, corp bonds… These aren’t tailor-made.)

* variance swaps — some are flow products, but I guess others are structured
* binary options
* Exotic FI notes
* Snowballs
* Callable range accruals
* Dual Range Accruals
* TaRNs
* STAR Index Linked Notes
* Quanto’s Steepeners
* Interest Rate Hybrids
* Spread Options
* Reverse Floaters and Callable Capped Floaters
* And other popular trade types
* structures linked to a wide range of assets including:
**Equity
**Interest Rates
**Funds
**Commodities
**FX

char const * — string OR ptr-to-single-char?#my take

see also my blog – http://bigblog.tanbin.com/2011/09/qq-char-reflex.html
see http://www.codeguru.com/forum/archive/index.php/t-227977.html and http://cboard.cprogramming.com/c-programming/106063-difference-between-const-char-*-char-const-*.html

First thing first — q(const char*) is better written as q(char const *) which reads left-to-right “non-const pointer to const char”

Q: If this is a common C-string, is the entire string const? Some say The const is on the first char only.
%%A:You can advance this (non-const) pointer through an array of chars, but you can’t edit _any_ of the chars using this (const!) pointer – like changing ‘X’ to ‘Y’.
 
Q: Do we need to explicitly remove constness of a q(char const *)?
A: Yes. Constness radiates left. Use const_cast to remove const
A: I saw this in the Macqurie coding test.

char const * ptr
– often used to represent a c-string, but technically a pointer to a single char. The real thing in memory could be a char-array or could be a single char. You can’t tell. If you don’t know the length of the array, then you don’t know where the array ends, even if there’s a continuous stream of 9999999999 chars.
– This pointer can’t be used to state-edit the char, but the char is possibly state-editable by other pointers.

Q: how long is this c-string?
A: you can’t tell. The original thingy
– could be a single char + null
– could be a single char without a null
– could be nothing but the null char
– could be a regular null-terminating c-string
– could be a bunch or chars without a null. This ptr may advance forever without a null character.
– or this pointer may point to a heap location already deallocated – dangling pointer
– or this pointer may be uninitialized

dual purpose of EDT – up/downward updates

A} I used to know EDT as the only thread to “put THINGS on screen”.

B} Now I feel EDT is the only thread to process all user inputs.

}}} In a nutshell, I feel EDT is the choke point for upward/downward updates to/from screen (+ keyboard + mouse)

— For Upward To screen
I guess it relies on paint() and lieutenants

— For Downward From screen,
Q: do user inputs always require 1) events and 2) listeners?
%%A: I think so. Well we know all events are in EDT Queue and all callbacks are invoked on EDT.

how important are QA/BA/DBA/RTB ..#my take

This is a frank, honest but personal observation/reflection of the reality I witnessed. We should always bear in mind how (in)dispensable we really are to the business.

+ Many wall st trading system development teams have no QA personnel so developers do that. I did that.
+ Many wall st trading system development teams have no BA, so the project lead or key developer do that. I did that.
+ Many wall st system development teams have no RTB (Run-The-Bank, as the opposite side from Build-The-Bank) support team, so developers do
that. I did that. RTB can probably handle major FO (front office) apps but a lot of apps aren’t in that league.

+ Each wall st trading system development team I have seen never has a hands-off architect. If there’s an architect this person is first a coder then an architect.

+ Some wall st trading system development teams have no need for a DBA or unix SA (system admin). Without going into specifics, much of the expertise isn’t needed before release to production if (not a big if) developers _can_ build their apps themselves. In rare cases developers need some tuning advice.See also http://bigblog.tanbin.com/2010/12/hands-off-manager-according-to-lego.html

– However, proj mgr is crucial, every time. Most wall st systems are interdependent and extremely quick turnaround, requiring non-trivial communication, coordination, estimation, monitoring. I feel half the time the key developers don’t play the PM role.

Fixed Income -more complex due to time dimension

FI is more complex than eq / FX / commodities, largely due to the time dimension.

There’s a maturity in Every fixed income contract.
There’s a maturity in Every derivative contract. For clarity Let’s use the alternative term “expiration”.
There’re 2 distinct maturities in a typical Fixed Income derivative contract.

Do eq/FX/comm spot trades have a time dimension? I don’t know many. FX spot trades have valuation dates, typically T+2, but T+1 for CAD…

Now, every derivative contract has an expiry date. This time-dimension factor interacts with maturity, in a way that’s unique to Fixed Income instruments.

The time dimension is critical
– because prices change over time.
– because volatility changes over time. Roughly half of all derivatives involve vol/optionality.
– because interest rate and credit spread change over time, but more slowly
– because many investors borrow money to trade, at an interest.
– because of time value of money. Money received in the future is worth less than money received today, and the difference is mathematics.
– because interest rate has a term structure
– because der contracts are often held open for longer timeframe than cash positions. Reason?
** der is often used to hedge away risk;
** many contracts can’t be transferred or closed out early.

foundation — spot rate (&&discount factor)

This post is based on zeros. See also post on spot, FRA … based on Libor.

Assumption: semi-annual compounding. Most US bonds follow it.

Suppose spot rate == 500 bps/year for a 2 year term (ie 4 x 6 months), it means on the market, people are willing to close deals like “Take my $1M today. Repay in 2 years (1.025*1.025*1.025*1.025)*$1M = $1,103,800”, which is 10.38% [2] more than the loan amount.

If i know people agree today to borrow $1M and repay in 2 years $1.1038M, then I can derive the semi-annual compound rate to be 2.5%/semi, or 5%pa but compounded-semiannually. In fact, an instrument exists that pays the $1.1038M in a 2-year term. This instrument is known as a zero-coupon bond. Discount factor for this 2-year term is 1/(1.025 * 1.025 * 1.025 * 1.025). Having $1,103.800 in 2 years is as desirable as having $1M today.

[2] Note spot rate is not 10.38%. Spot rate is a reflection of market sentiments and is directly reflected in the price of zero-coupon STRIPS.

— spot rate (SR) and (DF) discount factor —
For a given future date, spot rate can be derived from discount factor. Since discount factor is a market rate, spot rate is a reflection of market sentiment too. A measure of market sentiments on the time value of money.

In low inflaciton/interest years, spot rate is low, i.e. discount is “light”. Personally, i feel the discount factor concept is simpler than spot rate.

Starting from market prices, it’s simper to derive discount factors than spot rates. Both discount factor and spot rate are functions of length or maturity. Forward rate is more complicated. I think mathematically you can derive SR and DF from each other. SR fully describes time value of money (over various terms) on a given day on the market.

double ptr Usage #2 – special reseating (c#

Double pointer has many usages in c++. Usage #1 is array of pointers. Here’s Usage #2

Q: in a function, can you reseat a pointer object (32bit) created outside the function?
A: possible in C. pointer-to-pointer is a C-specific construct that can reseat the “nested” pointer.

int var3 = 3, var9 = 9;
void reseat(int ** ptr){ *ptr =  &var9; }
int main(){
    int * intp = &var3;
    reseat(&intp); //intp is a pointer-to-int, now reseated to &var9
}

C# has a similar feature in “ref” params.

In java, a reference param to a method is always a local ptr Variable pointing to the argument Object. Once you reseat this local ptr Variable, you lose the argument object. You can’t reseat the original ptr Variable declared outside your method.

void metho1(Object param){
  param=new Object(); // reseating

isShowing(), isDisplayable(), paint(), native visual object

There’s a link between a JComponent and its corresponding “native visual object” on screen.

1) http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Component.html#isDisplayable() mentions the native object

2) http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Component.html#isShowing() is among the most reliable indications of the link.

3) http://java.sun.com/products/jfc/tsc/articles/painting/ seems to hint that paint() is one of the lower-level methods connected to the mysterious native object. Low-level operations such as the screen rendering when

* a previously obscured portion of the component has become exposed. — we can debug this process
* a button determines that it needs to paint a “depressed” button visual.

4) http://bigblog.tanbin.com/2010/06/state-of-data-model-vs-state-of-visual.html describes the link.

class loaders — [[ weblogic definitive]]

P382 [[ weblogic definitive ]] is a good intro to java class loading. Generally concise and detailed. Still there are A few unclear points to bear in mind when studying it:

– pass up and pass down — when a class-loading request (for class J) comes to a child classloader C, C checks “its own memory” to see if J is loaded. Failing that, it sends the class-finding job UPSTAIRS to parent class loader P (and further up). If root class loader R can’t find it then R tries to load the class J. Failing that, R returns the class-loading job downstairs to P and to C.
– – > 1st classloader to attempt loading is always root classloader.

– The classpath classloader can ONLY load from the classpath. The extensions classloader is limited by JVM to only load from /jre/lib/ext/. In general most [2] classloaders are restricted to read a specific group of class files. Fundamental to delegation.
– – > corollary: Every parent is limited in its capability. When a child delegates to her parent some job that’s out of parent’s limits, child will do it herself.

– When a class-loading “job” comes in, it comes to a particular classloader. It doesn’t “come to the system”.

– When a class-loading request comes in, it comes with only a full classname. Each classloader [3] must *search* for the class file in jars and directories. Some beginners may assume the request comes labelled with physical address of the class file.

– It’s common to put a class file in 2 places, each visible to a classloader. Usually (if not always) only one classloader reads the class file and loads it into memory. If these 2 loaders are parent and child, then the parent loads it.

[1] by jvm
[2] if not every [3] the immediate parent of the receiving classloader will try first, followed by the grandparent… A few summary points: – tree. 1:m mapping. one-parent-many-children

a java thread doesn’t map to an object

Some java developers intuitively think a thread maps to an object in JVM (or kernel), and we can control it by calling methods on it (similar to how we control a JDBC connection or JMS session). For both novice and experienced java developers, it can be hard to unlearn that idea.

java.lang.Thread static methods obviously don’t affect any particular “object” [1], since they are static method. java.lang.Thread instance methods do affect the target thread, in limited ways. The stop/suspend/resume methods are direct actions on an an “instance” but are deprecated. The most fundamential programmatic controls on a thread involve locks and wait/notify, which paradoxically never name the threads they affect. Using these constructs demands that we visualize the affected threads as unassociated with any jvm object including the java.lang.Thread instances.

All experienced multi-threading developers eventually get the idea that a thread is not really associated with any object we create in our source code.

Java confuses us by giving us object-oriented thread creation constructs + thread pool. These make us feel threads are just objects. Well, are ebooks real books?

As explained in my blog on [[programmatic control on threads]], a VM thread has its memory structure residing in the stack space, including program counters and per-thread registers. The Thread.java object we create is a JVM heap object having a pointer to the VM thread. This Thread object is unrelated [2] to those objects accessible on the VM thread.

VM thread is basically a stack of open method CALLS. Each non-static method call like m1() has a host object (myAccount for eg) but that host object is UNRELATED[2] to the vm thread. However, myAccount can become problematic to our vm thread due to data corruption if
* m1() read/write myAccount fields
* and other vm threads also have method CALLs that read/write fields of this same myAccount instance.
* In addition, m1() often read/write other shared objects like herAccount, while other threads may also read/write herAccount

It’s exactly due to this kind of concurrent access to shared objects (like myAccount) that we end up with locks, wait/notify and all the consistency/liveness issues.

Among vm thread t1, corresponding Thread instance T1, and a regular object U1 on t1, we can say
+ T1 has a pointer to t1, but never to U1
+ t1 has no pointer to T1 or U1. This t1 is not a java object with fields and methods.
+ U1 has no pointer to t1, but the can get a pointer to T1 using Thread.currentThread(). But can it access fields of T1 if you put custom fields into T1? YES. Thread.currentThread() returns the same Thread instance every time.

In conclusion, the Thread.java object is a poor handle on the vm thread. The vm thread is not an object but a call stack. You don’t have a good java object (with fields and methods) manipulating the vm thread. Crucially, the method calls on a vm thread often access shared objects, but these objects are unrelated[2] to the vm thread

[1] Actually Thread.sleep does affect the current thread but that’s a minor point.
[2] there’s no pointer between the vm thread and myAccount. If you know myAccount.m1() is running on a vm thread, myAccount.m1() might simultaneously be running on antoher vm thread too.

developer caliber requirement in FI vs eq drv

If I compare developer requirement in FI vs eq derivatives

* latency, volume — easier in FI, but the fastest IR markets are faster than listed options.
* market data — easier in FI
* pricing — developers need slightly less pricing knowledge in the FI space. In EQD, volatility math is an everyday reality
* domain knowledge — FI space has more complex products but I don't see deeper knowledge in FI developers.
* trade execution, position management, pnl — about the same level of skill requirement

Overall, I feel FI has lower requirement but pays high. Perhaps the desk is more profitable.

Best online intro to swing Realization

http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Component.html#isDisplayable() says

Determines whether this component is displayable. A component is displayable when it is connected to a __native__ screen resource.

A component is made displayable either when it is added to a displayable containment hierarchy or when its containment hierarchy is made displayable. A containment hierarchy is made displayable when its ancestor window is either packed or made visible.

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html is less precise
Realized means that the component’s paint() method has been or might be called. A Swing component that’s a top-level window is realized by having one of these methods invoked on it: setVisible(true), show(), or (this might surprise you) pack(). Once a window is realized, all components that it contains are realized. Another way to realize a component is to add it to a container that’s already realized. You’ll see examples of realizing components later.

–specfic example for some text component

public boolean isShowing() {
  return getTextComponent().isShowing();
}

This code determines if the object is Showing. This is determined by checking the visibility of the object and its ancestors. Note: this will return true even if the object is obscured by another (for example, it is underneath a menu that was pulled down).

public boolean isVisible(){
  return getTextComponent().isVisible();
}

This code determines if the object is Visible. Note: this means that the object intends to be visible; however, it may not be showing on the screen because one of the objects that this object is contained by is currently not visible. To determine if an object is showing on the screen, use isShowing().

real time risk monitor ^ real time quote repricer (vol trading)

–Most used app in EQD — real time risk monitor
A) #1 workhorse — tick driven position updater with real time greek/unrealizedPNL. Real time unrealized pnl is still important, but EQD trader’s #1 concern is the potential gain/loss in MV of her open derivative positions — real time exposure. Most of the trading decisions are based on this exposure assessment. Real time unrealized pnl doesn’t address questions like VAR or “what if”.

B) Just like Autoreo, EQD real time risk engine manages entire books of open positions, real time mark-to-market/pnl and risk assessment. However, no automatic adjustment of offer/bid on inventories — #1 function of Autoreo

C) As another comparison, BofA Quartz is first a risk engine, and 2nd a pricing engine. In many asset classes, the risk engine doubles as a pre-trade pricing engine.

–Most important business factor in EQD — volatility
In the interest rates business, every player is constantly trying to forecast (and hedge) interest rate fluctuations over various timeframes ranging from 24 hours to 30 years — the entire yield curve. That forecast drives the entire market and the rates business of every bank. In eq der biz, #1 factor is not IR, not the stock market per se, but volatilities (of stocks and ETF and indices). Volatility as a  concept measures how “wild” is the swing (over a timeframe like 30 days) of any fluctuating price. Over the next 30 days, there’s just one such vol, but using bid/ask at different strike prices, you derive different implied-vol values — welcome to the Smile. A smile is one curve. Using different curves at different expiration timeframes, you get a 3D surface.

Every bank needs to forecast vol using such a surface. Each bank uses a proprietary algorithm, because vol forecast is never straightforward. Yet we must forecast, because vol is the real driver.

Typically, an eq der trader has a lot (hundreds) of open positions to manage throughout the day. (A structured eq der trader has even more long-standing positions.) The main market data of interest is tick data i.e. live bid/ask quotes. Tick drives implied vol surfaces, which drive valuation of non-listed options. Not sure about variance swaps and other eq der instruments, but volatility is the #1 driver.

Q: beside vol, what other business factors are important?
A: open position MV is also sensitive to projected dividends, but they change less often.

In a nutshell, eq der traders need to monitor sensitivity/VAR/pnl-impact in their open positions. These positions change value in response to market data “swings”.

y i like eq and drv

I like equities systems because

* volume – upstream
* latency – upstream
* algo – upstream
* connectivity – upstream
* messaging – upstream
* technology-wise, upstream to FX, rates, treasuries…

I like derivatives (esp. options) because

* math, pricing. More mature and entrenched than other derivative instruments – upstream
* m-risk. Non-derivative positions go flat soon. – upstream
* life cycle management

These are mature products and mature technical implementations. Ahead of the curve.

In contrast,

– IRS is extremely popular (even in Asia) and growing, but volume and math is “downstream”.
– CDS is low volume. Mathematically, I still feel option pricing is upstream.
– Mortgage is big business, but not in Asia.

controllers vs IT — whose responsibility for garbage-out?

Controllers are paid to validate and attest the numbers [1]. They (not IT) have the knowledge and the entitlement to confidential information to affirm that the numbers make sense. When (not “if”) mistakes happen, then controllers must answer.

90% (in some places 100%) of the developers are expected to have limited knowledge (so that they can focus on well-defined requirements) but build automated systems very quickly, often with lots of obvioius “garbage out” i.e. unreliable, inaccurate output.

In most (not just “many”) cases, Developers simply don’t know enough to recognize “garbge out”. Through experience supporting a local (not a GENERIC) system, some senior IT guys may gain that judgement, insight and knowledge.

There are mistakes controllers are unable to detect — if developers release something obscure without informing controllers. Out of 500 changes, which ones do we need to “inform”? Initially be very cautious. Over time our judgement grows.

[1] what numbers? Let’s not get into the specifics as we could get carried away.

intuitive – quick reflex with option-WRITING, again

See also http://bigblog.tanbin.com/2011/04/get-intuitive-with-put-option.html. Imprecisely —
+ writing a call, I guarantee to “give” IBM when my counter-party “calls away” the asset.
+ writing a put, I guarantee to “take in” the dump, unloaded by the counter-party. Put holder has the right to “unload” the asset (IBM share) at a fixed price — a high price perhaps [1]

An in-out intuitive reflex —
– If I write a call, I must give OUT assets when option holder calls IN the asset;
– If I write a put, I must take IN when option holder “throws OUT” the junk

[1] in reality, put buyers usually buy puts at low strikes (OTM) therefore cheaper insurance.

CV-worthy buzzword trading domain knowledge

Many extremely important and central systems aren’t worth mentioning in your CV because most readers don’t understand their value. Examples — real time credit limit check; tiered quote pricer; commission…

The most prestigious, recognizable “star players” are
* real time, market-data-driven pricing and valuation
* curve-based pricing
* algo trading — vwap/twap becoming commoditized, but other algos?
* VaR
* execution – order crossing
* trade capture, trade booking
* OMS in a trading desk (The OMS inside an exchange is a different animal)
* connectivity to exchanges and ECN
* real time risk
* market data gateways
* anything low latency, high frequency
* Monte Carlo simulation — probably a risk/stress-test rather than a real-time component

[[all about high frequency trading]] explains many other components of a HFT buy-side system.

cancels by ECN vs exchange vs dealers

—ECN—

Both TMC and HotSpot have the authority to cancel trades after execution…

ECN members often have a last-look in the form of the final ack. If (connectivity or …) final ack if missing, these ECN’s can’t assume “no news means good news”, as exchanges can.

–Exchange—

Exchange has final authority on when to “bust” an matched/executed trade. Exchange members can submit a request to cancel a “ClearlyErroneous”, but actual cancellations are very rare and often announced in public. SEC has specific rules on when and how to correct ClearlyErroneous executions, usually within 60 minutes.

In my  NYSE market data feed, I don’t see any cancellation… probably very rare.

Outside the exchange, a stock broker(actually a dealer) can and do cancel trades with a client (like your grandma) but these cancellations aren’t visible to the exchange.