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++.

How do you identify high workload? #letter to XR

Q: Do you need to record how you allocate time to assignments in blocks of 2 hours, 4 hours or 8 hours?
Q: Do you need to everyday describe, verbally, what you did in the previous day?
Q: When neck, back, or eyes feel tired, do you always feel free to get away from computers for a while?Q: Do regular developers feel relaxed enough to write documentation in wiki or Word document for other people?
Q: Do you get time to make 30-minute chitchats with friends on office phone during office hours?
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).
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 (briefly) before leaving office?
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?

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