objA querying objB – vague OO-speak

Update — I think architects and “savvy communicators” often practice vague OO-speak to perfection. We may need to practice it.

In OO discussions, we often say “Look here, objectA will query objectB”, by calling getTimestamp(). This is a typical example of vague/misleading OO-speak. It’s as vague as a presidential candidate promising to “reduce deficit in 4 years” without any concrete details.  Note non-OO programmers don’t develope such vague communication habits.

Here are some of the fundamental amgiuities.

* A must get a handle on B before calling B’s non-static methods. The way to get the correct B instance is often non-trivial. That sentence invariably glosses over such details.

* It’s a method in A — methodA1 — not just “object A”, that queries B. This is a crucial point glossed over in that sentence.

** The original sentence implies A is some kind of active object that can initiate actions, but actually method A is called by another method, so the real initiator is often an external entity such as a GUI user, an incoming message, the main thread of a batch job, or a web-service/EJB request. ObjectA is NOT the initiator entity.

* Return value of the query is either saved in A or used on the fly in the methodA1. The original sentence doesn’t mention it and implies objectA has some kind of intelligence/bizlogic about how to use the return value. In some cases indeed the method in objectA has that “intelligence”, but it should not be glossed over. More often, methodA1 simplify passes the query result to the upstairs method above methodA1. The upstairs method may “belong” to a different object. All the details are glossed over in the original sentence.

Here’s an even more vague phrase – “A calling B”. This glosses over the essentials of the arguments and where they come from.

Advertisements

pbref^pbclone DEFINED : 3QnA any lang, Part1

pbref include 2 categories – pbcref (const-ref) and pbrref (regular ref). It’s best to update all posts …

  lhs_var =: rhs_var // pseudo code

Reading this assignment in ANY language, we face the same fundamental question –

Q1: copying object address (assume 32 bit) or are we cloning the object? Note input/output for a function call is a trickier form of assignment. In most languages, this is essentially identical to regular LHS/RHS assignments. [1]
Q2: “Is LHS memory pre-allocated (to be bulldozed) or yet to be allocated (malloc)?”
Q3: does the variable occupy a different storage than the referent/pointee object? See http://bigblog.tanbin.com/2011/10/rooted-vs-reseat-able-variables-c-c.html

With these questions,  we will see 2 different types of assignment. I call the them pass-by-reference — pbref vs pbclone — pass-by-cloning (including op= and copy-ctor). These questions are part of my Lesson 1 in Python and C++. Here are some answers

java primitives – pbclone
java non-primitives – pbref
c# value types – pbclone
c# reference types – pbref
c++ nonref by default – pbclone
c++ nonref passing into function ref-param — pbref
c++ pointer – usually pbclone on the 32-bit pointer
c++ literal – pbclone?
c++ literal passing into function ref-param?
php func by default – pbclone
php func ref-param – pbref
php func ref-return – pbref
perl subroutine using my $newVar — pbcloneperl subroutine using $_[0] — pbref
python immutables (string, tuple, literals, frozenset) – pbref — copy-on-write
** python primitive — no such thing. “myInt=3” is implemented same as string. See post on Immutable, initialize etc
python list, dict, set …- pbref

Q: what if RHS of pbref is a literal?
A: then pbref makes no sense.

Q: what if RHS is a function returning by reference?
A: you can get pbref or pbclone

[1] C++ and Perl parameter passing is multifaceted.

equivalence of array^pointer? my take

Update — the spreadsheet is a newer post.
————
First let’s clarify what we mean by “pointer”.

Q: an array name is comparable to a pointer ….? Nickname? PointerObject? or PureAddress?
%%A: an array name like myArray is nickname for a pure address. See post on 3 meanings of “pointer”. However, in this post we are comparing myArray with a myPointer, where myPointer is … either a PointerObject or PureAddress
%%A: myArray is like a const ptr “int * const myConstPtr”

The apparent “equivalence” of array vs pointer is a constant source of confusion. Best (one-paragraph) pocket-sized summary — http://c-faq.com/aryptr/practdiff.html

http://c-faq.com/aryptr/aryptrequiv.html is a longer answer.

#1 (principle dealing with the confusion) physical layout. When in doubt always go back to fundamentals.

A simple array on the stack is a permanent name plate [2] on a permanent block of memory, physically dissimilar to a pointer Variable. However, Pointer Arithmetic and Array Indexing are equivalent. I feel AI is implemented using PA.

#2) syntax

Pointer variable is syntactically a more _flexible_ and more powerful construct than array. However, the strong syntactical restriction on array is a feature not a limitation. Look at strongly typed vs dynamic languages.

I feel most operations on an array is implemented using pointer operations. (Not conversely — Many pointer operations are syntactically unavailable on arrays). Specifically, Passing an array into a function
is implemented by pointer bitwise copy.

That’s the situation with arrays on the Stack. On the heap situation is more confusing. Consider “new int[5]” — return value Must be saved in a pointer variable, but the underlying is a nameless array. In contrast, see [2] above. Also, that pointer variable could rebind/reseat.

attributes of an option instrument

When we say “I am short 30 OTM July IBM put option struck at 103”, we mix static instrument attributes with a non-static attribute

A call option is always a call option (unless in FX). The underlier and the call/put attribute are part of the product static data.
The strike/expiry are also part of the product static data. These are the 4 defining attributes of an option instrument in real
business conversation. It’s important to notice the difference between static and dynamic attributes.

Moneyness (ITM/OTM) is a dynamic attribute. Moneyness is like indebtedness. You can be in-debt now but later become debt-free.

A negative vega position is always negative vega. This characteristic never changes. It’s like a person’s ethnicity. However, this
is not an instrument attribute, but a position attribute. Ditto for the quantity.

SL – how to grab dispatcher, random tips

Q: how many dispatchers are there in one SL process?
%%A: 1, according to many c# guys. However, a lot of literature mentions “a dispatcher”

Q: can you get hold of the dispatcher from Any thread Always?
A: http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/ says not always

Q: which source is best to get the dispatcher?
A: Deployment.Current.Dispatcher is preferred over Application.Current.RootVisual.Dispatcher. See http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/ and other sites

Q: Is SynchronizationContext better than dispatcher?
A: http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/ says dispatcher is easier
A: http://dotnetslackers.com/articles/silverlight/Multithread-Programming-in-Silverlight-4.aspx demonstrates both.

Q: how many UI threads are there?
%%A: 1, according to many c# guys.

Stop orders — LimitOrder, StopLoss, TakeProfit …

See posts on limit^stop orders…

To understand stop orders, it’s instructional to contrast Limit orders vs Stop orders.

Your Limit B/S order gets executed when market moves … in your favor.
Your Stop B/S order gets executed when market moves  … against you, regardless whether you have an open position or not.
** Stop S is a Sell. You prepare a Stop S Below the mkt. It’s executed when mkt moves Down, but before Down too much. (If you buy and then quickly Execute a Stop Sell, you end up buy-high-sell-low. This would be due to a miscalculated buy and you want to cut loss quickly.)
** Stop B is a Buy. You prepare a Stop B Above the mkt. It’s executed when mkt moves Up, but before Up too much.

Stop orders are mostly used for SL. Other uses tend to be related to SL.

For example, after an FX trade at 1.22, you often prepare an offset trade SL and a similar offset trade TP. You prepare both but only one of them will get executed. SL and TP will box up your 1.22 “position open price”, i.e. one of them placed at a higher and the other at a lower price around your position.

For a buy@1.22
* the StopLoss is a sell at Lower price — at a Loss (buy high sell low)
* the TakeProfit is a sell at Higher price.

Now, the TP is just a regular limit order, while the SL is a stop order.

I think the exchange only knows LO and market orders. I think the exchange order book consists of LO only.

Here’s a fundamental difference between LO and SO. When triggered, stop orders become a market order available for execution at the next available market price. Stop orders guarantee execution but do not guarantee a particular price. Therefore, stop orders may incur slippage — There is a substantial risk that stop-loss orders left to protect open positions held overnight may be executed significantly worse than their specified price. I guess this would hapen when mkt moves against you too fast and too many like-minded investors have a SL order at the same price. Therefore, Saxo only told me SL order “usually” does limit the losses.

In contrast, LO is price-guaranteed by the exchange (though not sure about FX where there’s no exchange).

icon in SL-OOB causing silent death

icon.png
icon.png
icon.png
icon.png

I realized if the icon.png is zero-byte, then a SL OOB will fail with inconsistent behavior. Launched from the desktop shortcut, blank screen would show up. From MSVS, F5 or Ctrl-F5 would show no screen no error no log even if you enable exception debugging. Even the constructor in App.xaml.cs won’t get invoked.

GarbageCollect in JVM ^ .NET

Disclaimer — I’m no developer of garbage collector and have no real insight. This is just a collection of hearsay online comments.

Diff: Large Object heap

Diff: config parameter? JVM has (far) more. One of the very few dotnet config param is gcConcurrent.
http://msdn.microsoft.com/en-us/library/at1stbec.aspx points out

By default, VM (i.e. the runtime) uses concurrent garbage collection, which is optimized for latency. If you set gcConcurrent to false, VM uses non-concurrent garbage collection, which is optimized for throughput.

See first paragraph of http://msdn.microsoft.com/en-us/library/at1stbec.aspx

c# programmatically retrieve file path of runtime objects

From a Method object you can get the host Type object.

From a Type object you can get the host Assembly object.

From Assembly object you can get the physical assembly file.

1) (tested) Assembly.GetExecutingAssembly().Location; // gives the dll fullpath
1c) new T().GetType().Assembly; //returns the Assembly object. Works even though T is a template dummy type!

2) Process.GetCurrentProcess().MainModule.FileName

3) AppDomain.CurrentDomain.BaseDirectory; // Similar to 1)

user-editable XML config file for silverlight OOB app

Requirement: user-editable XML config file (like app.config or yourAppName.config) to control my silverlight OOB app.

Solution: Here are the key steps based on http://www.mohamedibrahim.net/blog/2010/01/27/creating-and-using-silverlight-app-settings-webconfig-configuration-applicatioin-settings-to-change-wcf-service-address-after-deployment-servicereferencesclientconfig-servicereferences-clientconfig/

1) move your app.config content into ServiceReferences.ClientConfig. This file is editable post-install, unlike the app.config which gets converted to binary and embedded in the DLL during build-deploy.

The blog above describes how to pragmatically parse the xml config — nothing fancy.

2) After the install (using sllauncher), you can easily locate the xap file. On my winXP PC it’s in C:\Documents and Settings\myLogin\Local Settings\Application Data\Microsoft\Silverlight\OutOfBrowser\1352946853.theOriginHostname

C:\Users\a5113359\AppData\Local\Microsoft\Silverlight\OutOfBrowser on my win7.

As you would expect, this xap gets overwritten whenever you re-install (with sllauncher). This is one way to confirm any code change took effect.

3) edit the ServiceReferences.ClientConfig file inside the xap. The blog above describes the technique. Actually no need to rename. Just use 7z to open the xap.

4) restart the OOB app. See new config value.

renaming folder in svn

C) Conservative approach – clone the folder, verify it, check in, observe [3], then schedule to remove obsolete folder.

** Caveat – Before removal, everyone needs to know the old folder is obsolete and not to be used.
** Caveat – history lost

[3] it can take months to uncover a problem induced by the renaming.

D) The tortise “rename” is convenient but potentially dangerous. Usually works. Acceptable on unimportant svn folders. Use your judgement.

install SL-OOB app by xcopy

Based on http://www.silverlightshow.net/items/How-to-distribute-a-Silverlight-OOB-Application.aspx

In short, install the XAP file via sllauncher.exe.

Q: What files are needed?

%%A: nothing but the XAP file. The MSVS project build creates the XAP in the Bin/Debug folder. You can give it to users.

Q: Can I send the file(s) to a user by email attachment?

%%A: I think so. User need to have Silverlight installed.

Q: User needs to go online?

A: no

tech jobs | new territories acquired

If we are too honest, we could portray ourselves as a junior java developer — only 2009 – 2012. In fact many java veterans stop learning after the first 3 years. Domain knowledge? Only bond pricing/trade capture + vol fitter – mostly non-mainstream domains in the Asia context, since bond and option markets are really mature and developed only in the US.

That would be the situation if we were too honest. However, right now I feel confident to claim many other territories thanks to my “ECA” learning.

* FX – I described several FX projects and I feel confident talking about them in details
* eq
* vol – I studied most of these things on my own before the Barclays interview
* threading — obvious
* swing – I took up some swing projects in Citi and did some personal projects while in BofA. Then I learnt key tricks from Guillaume and practiced over a few days. I know my swing CV and swing interviews work.
* FIX?
* C# — I blogged about many topics. I passed many first-round interviews. I know my c# CV (and c# interviews) works!
* c++ — similar and even more interviews passed.

This is one of the most powerful, deep and indelible personal experiences to shape my perceptions, priorities, motivations,

mutable-shared data – source of all concurrency hazard

Consistency, liveness, deadlock, mutex, synchronization … and all the concurrency complexities have a necessary conditions — mutable shared data. Without this single condition, all these headaches disappear or become unnecessary.

If all mutable data are non-shared and all shared data are immutable, then you can create a great many threads and they will run faster than under synchronization

Note “data” doesn’t imply classes. In java/c#, such data is an instance of a class or struct. In non-OO languages like C, “data” can be global variable or any heapy thingy, manipulated by a pointer.

securities with negative value

Some securities can have positive/negative values for the holder. I don’t know many asset classes in this category. (If indeed there are only a few, then they must be important). What I know — Fwd/futures contracts. These contracts are fundamental to almost all derivatives.
Most traditional securities (assets?) have a purely positive value – stocks including ETF, (junk) bonds, options, commodities, real estate, MBS…
How about swaps? I think like the fwd contract, the execution price is chosen to give both holders $0 value on trade date. After trade date, either holder can have a “position” with a positive  or negative value.
How about structured products? I guess often falling into this category. Usually between 2 holders – a dealer and an institution.

mutex – serialize access to… data or code?

These are 2 ways to look at the basic mutex use case —
DD) you can use a mutex to serialize access to a piece of shared mutable data
CC) you can use a mutex to serialize passage through a code path — the so-called critical section

Many people intuitively cling on to the intuitive DD view and visualize the runtime using a mutex to regulate multiple threads’ access to shared Data, as if the mutex is a locker. However, I find CC a more realistic/accurate view.

DD is the “logical” view whereas CC is the physical view.

DD is achieved through CC.

The more low-level you go, the more you take the physical view and speak in terms of critical sections.

DD is more object oriented. For non-OO languages like C, DD is not as natural as in OO language. If a data item is passed by pointer, then any function running on any thread can modify it. It’s not enough to use a mutex on 99% of those functions and have one function access it directly.

In OO languages, that data item would Always be a private field, automatically restricting access to very few methods of the class. Now we understand why encapsulation is essential to threading.

In non-OO languages, it’s less natural to associate a mutex with a piece of data. If the shared data is passed by pointer, then the mutex must also be passed by pointer. The mutex-pointer must accompany the data-pointer.

method hiding – c# ^ java

C# hiding rules Based on [[c# primer]] P148.

— virtual methods —
Rule – for virtual methods, there’s no hiding — base version is completely “replaced”. If Account and CheckingAccount both define
v1(), and we have a CheckingAccount instance, then we can’t invoke the base v1(), even if we upcast.

Virtual method rule is fairly simple compared to non-virtual methods —

— non-static non-virtual —
Rule – If Account and CheckingAccount both define non-virtual, non-static method m1() with identical signature, then hiding occurs.
We are strongly advised (not required) to mark the hiding explicit using “new”.

Hiding affects only static, compile-time method binding. Suppose we have a CheckingAccount instance myAccount. Both m1() versions
are available at runtime, unlike the virtual method v1().

myAccount.m1();// is the derived version

( (Account)myAccount ).m1(); // is the base version. Static binding

Inside CheckingAccount source code, we can also use base.m1(). This is same as java.

— static methods —
Rule – java method hiding applies to static method only. Simple and clean. How about c#? I guess same.

———–
I feel c++ hiding rules are less comparable. I feel c# borrowed more from Java than from c++.

dll hell (LoadLibrary) – solved in dotnet

Very common IV question…

[[Beginning vc++ 2010 ]] P 1178 has a good half-pager. Some pointers in my own language:
* a DLL is loaded (into mem) separately from the host app
* when you overwrite a dll on disk, the host app can load fine, which will automatically get the new dll to load. Happy scenario
* however, the same dll might be needed by another host app, which may now fail.

—-dll hell defined
http://msdn.microsoft.com/en-us/library/ms973843.aspx
http://www.desaware.com/tech/dllhell.aspx (circa 1998) covered pre-DLL era, good-DLL era, bad-DLL era

The reason for this issue was that the version information about the different components of an application was not recorded by the system.

The Re-install scenario: It would not be unusual to own several installers each embedding a different version of the same DLL. It is not unusual for users to reinstall software. In many cases users would install software that included an older version of commdlg.dll on a system that already contained a newer version. This would cause the more recently installed version to replace the newer version. As soon the user attempted to run the program that required the newer version, problems would occur ranging from operational difficulties to general protection faults.

Dotnet solved the problem. GAC can hold 2 versions of the same DLL. Earlier app would still use the version she likes.

I guess late binding via LoadLibrary() can also reduce dll hell.

capital control, exchange rate regulation #basics

Treat the currency of a country (say JPY) as paper money or electronic money. Capital control attempts to monitor and regulate the outflow of every JPY.

Electronic flow is probably easier to regulate. I think all the electronic systems are operated by heavily regulated agencies.

Outflow can be conversion of JPY 1mil into EUR.
Outflow can also be fund transfer into a JPY account in Brazil, without currency conversion.

Case in point — FDI often brings in foreign currency, invest, profit (in local currency)… then they want to bring the profit out of the country — outflow. They need to convert or transfer.

To be effective, the system also needs capabilities to stop a particular outflow when necessary.

Q: how effective is capital control?
A: quite effective

Q: what are the capital outflow avenues for corporations (foreign or domestic)?
A: usually through banks. It’s easier and more effective to regulate banks than corporates.

Q: what are the capital outflow avenues for individuals?
A: for small amounts they can change with friends, but large amounts often go through regulated agencies.

Q: what are the capital outflow avenues for crime gangs?
A: murky underground tunnels.

Q: how many loopholes?

%%c# log wrapper – homegrown

namespace LogWrapper
{
    ///

    /// strictly stateless  
    ///

    public class Log
    {
        private static readonly log4net.ILog log4n = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public const string FORMAT_BEGIN = ” vvv   {0}   vvv”;
        public const string FORMAT_CLOSE = ” ^^^   {0}   ^^^”;

        static Log()
        {
            //var fileInfo = new System.IO.FileInfo(PathFinder.APP_CONFIG);
            //if (!fileInfo.Exists) throw new Exception(“app.config not found”);
            //log4net.Config.XmlConfigurator.Configure(fileInfo);
        }
        ///

        /// This method conveniently logs to console, IDE output window and log4net
        ///

        ///
        public static void Info(Object o, int levels = 2, bool isStackTraceNeeded = false)
        {
            /*The thread id seen by log4net. According to MSDN:
             * An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host
             * can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host
             * can use the CLR Hosting API to schedule many managed threads against the same operating system thread,
             * or to move a managed thread between different operating system threads.
             */
            var winThrId = AppDomain.GetCurrentThreadId();
            var thId = “/T” + Thread.CurrentThread.ManagedThreadId + “/” + winThrId + “/ “;

            #region locator
            StackTrace stackTrace = new StackTrace(fNeedFileInfo: true);
            var frames = stackTrace.GetFrames();

            StringBuilder locator = new StringBuilder(StackFrameToStr(frames[1]));

            for (int i = 2; i <= levels; ++i)
            {
                if (frames.Length > i)
                {
                    locator.Append(“<-" + StackFrameToStr(frames[i]));
                }
            }

            //if (frames.Length > 2)locator.Append(“<-" + StackFrameToStr(frames[2]));

            var locator2 = “{” + locator.ToString().Trim() + “} “;
            var msg1 = o ?? “_nul_”;
            var msg2 = thId + locator2 + msg1;
            #endregion

            if (isStackTraceNeeded) msg2 += “n” + stackTrace;

            log4n.Info(msg2);
            //System.Diagnostics.Trace.Write(“Trace is possible as well.”);
            System.Diagnostics.Debug.WriteLine(msg2);
            Console.WriteLine(msg2);
            return;
        }
        public static void Error(Object o, int levels = 3)
        {
            Info(o, levels, isStackTraceNeeded: true);
        }

        public static void Crash(string msg, Exception fatalException = null)
        {
            string a = “System ought to crash. Unwise to catch this error : ” + msg;
            a += (fatalException == null) ? “” : “n–> Root Cause –> ” + fatalException.ToString();
            Log.Error(a);
            Environment.Exit(-99);
        }

        public static void LogClose(string name)
        {
            Info(string.Format(FORMAT_CLOSE, name));
        }

        public static void LogBegin(string name)
        {
            Info(string.Format(FORMAT_BEGIN, name));
        }
        ///

        /// http://stackoverflow.com/questions/2045935/is-there-anyway-to-programmably-flush-the-buffer-in-log4net
        ///

        public static void FlushBuffers()
        {
            foreach (IAppender appender in LogManager.GetRepository().GetAppenders())
            {
                var buffered = appender as BufferingAppenderSkeleton;
                if (buffered != null) buffered.Flush();
            }
        }
        #region assertion tools
        public static void A(bool condition, string msg = “Programmer Error”, bool isFatal = false)
        {
            Assert(condition, msg, isFatal);
        }
        public static void Assert(bool condition, string msg = “Programmer Error”, bool isFatal = false)
        {
            if (!condition) Error(msg);
            Debug.Assert(condition, msg);
            if (!condition && isFatal) throw new Exception(“Fatal assertion failure.”);
        }
        #endregion

        #region private
        private static string StackFrameToStr(StackFrame f)
        {
            return Path.GetFileNameWithoutExtension(f.GetFileName()) + “.” + f.GetMethod().Name + “:” + f.GetFileLineNumber();
        }
        #endregion
    }
}