I wanted to wrap a C# WinForms object in a Python dictionary, so I could make it look like a dictionary by implenting __getitem__ and __contains__ using hasattr() and getattr(). I’d been using generic EventHandlers to dispatch from C# to Python quite happily to date. But the __contains__ implementation needs to return a bool, which doesn’t fit with the event handler model. This post showed me the way. Here’s the code…


using IronPython.Runtime.Operations;

// DictAdapter puts a read only C# dictionary interface on any
// Python object, which may actaully be a C# object in turn.
public class CsDictAdapter : IDictionary {

#region Delegates
public delegate object  GetItem( object key);
public delegate bool    HasItem( object key);

public GetItem __getitem__;
public HasItem __contains__;
#endregion

#region Constructors
public CsDictAdapter( object gi, object hi) {
this.__getitem__ = ( GetItem)Ops.GetDelegate(gi, typeof(GetItem));
this.__contains__ = ( HasItem)Ops.GetDelegate(hi, typeof(HasItem));
}
#endregion

#region Properties
public object this[object key] {
get { return __getitem__(key); }
set { }
}

public ICollection Keys {
get { return null; }
}

public ICollection Values {
get { return null; }
}

public int Count {
get { return 0; }
}

public bool IsReadOnly {
get { return true; }
}

public bool IsFixedSize {
get { return true; }
}

public Object SyncRoot {
get { return this; }
}

public bool IsSynchronized {
get { return false; }
}
#endregion

// Delegating implementations of all other methods.

#region Interface methods
public void Remove(Object key) { }
public void Add(Object key, Object value) { }

public bool ContainsKey( Object key) {
return __contains__( key);
}

public bool Contains( Object key) {
return __contains__( key);
}

public void Clear() { }

public int Add(Object item) {
return 0;
}

public void CopyTo(Object[] array, int arrayIndex) { }

public void CopyTo( System.Array array, int arrayIndex ) { }

IDictionaryEnumerator IDictionary.GetEnumerator( ) {
return null;
}

IEnumerator IEnumerable.GetEnumerator( ) {
return null;
}
#endregion
}

Daniel Beunza posts on his NYC presentation on visualization. Visualization is one of my pet themes – I’ve posted my thoughts on why it hasn’t been widely applied in trading. I’ll add a couple more observations I’ve made while building and deploying a 3D visualization system on our trading floor, at the same time as addressing a couple of points Daniel makes…

  • Daniel: “Traders are practical and resist innovation”. Traders resist innovation for innovation’s sake. If they can see an opportunity to profit from innovation, they’ll be very keen on it.
  • Traders know markets far more intimately than developers of visualization software ever can. Consequently visualizations can end up showing traders stuff they “just know”: stuff that has already been internalised and assimilated by the trader.
  • Visualization development cycles can be slow, especially if you’re coding in C++, which can make it hard to hold the attention of traders.
  • Daniel: “I believe that innovation will happen”. I used to share this belief, but I’m not so sure now. Seeing startups fail, and trying it myself has persuaded me that something very special will be needed.

I wonder if Daniel has come across Brad Paley’s work ?

LiquidityHub tech

October 23, 2007

LiquidityHub have a new website up. Definitely an improvement on the last effort. Of interest to coders will be the tech page, detailing the various Java products used as building blocks. Notable are the Cameron FIX gateway, BEA servers and Fiorano messaging. Using Cameron to present a FIX wire protocol to dealers is a moot point since most talk to LH via the ION MarketView gateway which hides the FIX implementation. The BEA app server comes with a real time VM – which will be a relief to any dealer technologists worried about garbage collection delaying their streaming quotes. Fiorano is a JMS implementation that supplies pubsub and MQ style messaging.

Wikipedia has a great collection of cognitive biases. Of course, the field of behavioural finance is all about how cognitive bias causes real market participants to behave differently than the rational actors of the efficent market hypothesis. Overcoming cognitive bias is a big part in becoming a good trader; Nierderhoffer and Taleb have both commented at length in their books about the psychology of overcoming biases to execute their trading strategies.

Cognitive biases are a large factor in debugging exercises too. Having written some code, it can be very hard to see why it doesn’t do what’s intended. When debugging I often exhort myself to “listen to the system.” Cognitive bias can blind one to the content of error messages. Forcing oneself to read them word by word can direct attention to a cause or symptom.

Experimental Hive

October 17, 2007

The Visual Studio SDK adds IronPython support to VisualStudio 2005. But with it you need to launch VS from an alternate “Experimental Hive” menu option. My employer’s packaging system somehow zapped this setup on a recent reboot. So I reinstalled the IronPython support in my regular VisualStudio instance by following this advice.

Teaching the kids to code

October 11, 2007

I’ve had a couple of attempts at teaching our two sons to program. I’ve tried pygame and squeak, but neither has triggered that obsessional, can’t prise fingers away from keyboard quality. Tonight I had another go, using scratch from MIT. It’s a marvellous visual programming environment, with a very low barrier to entry. Both the lads were hooked instantly. Strongly recommended as a first dev env. I think I might try coding up some program trading algorithms in it ;)

has_key & ContainsKey

October 11, 2007

Python’s built in dictionary type – {} – has a has_key method. C#’s System.Collections.Hashtable has ContainsKey. I’d like the same Python code to operate on dictionaries and hashtables regardless. Fortunately IronPython adds the ContainsKey method to the built in dictionary type, so a simple replacement of has_key invocations with ContainsKey does the trick.

Niederhoffer profile

October 10, 2007

The New Yorker has a new profile on Victor Niederhoffer. What a character !

IronPython notes

October 8, 2007

I’m collecting IronPython observations here

Paul Graham on Philosophy

October 8, 2007

I’ve been a fan of Paul Graham‘s since I read “Beating the Averages” some time ago. So I was intrigued to read his thoughts on philosophy, and gratified to discover he was a philosophy undergrad himself, as I was. His essay reminded me of Freddie Ayer’s opening to Language, Truth & Logic: “The traditional pursuits of philosophers are, for the most part, as unwarranted as they are unfruitful”.

I wouldn’t take such a strong stance as Graham on the uselessness of philosophy, since I think the most basic question of metaphysics & ontology – “what kind of things are there ?” – is the same question we ask ourselves when embarking on a new design. So software design is applied ontology.

And I do like Murray Gell-Mann’s comment on philosophy: “philosopy is the discipline whose adherents kick up a load of dust, then complain they can’t see”.

I think Graham’s problem with philosophy stems from the fact that he started with it. It’s probably better to master a technical discipline, then philosophize about it – witness Taleb, Soros, Niederhoffer on markets. Or Naur on computing.