Whilst I'm waiting for the inevitable flood of responses to the real-time topic I'll continue with where I left off on the
SignalGenerator.
I realize it might be a little difficult to see the whole picture at the moment but hopefully things will become clearer once I draft the next UML for this part of the system.
In order to do that, I need to refine the list of order management methods that I want to make available to the system developer . So, I'm jumping back to that before I cover scripting:
1. Order Management methods
I've spent a little while thinking about this one and have gone around in circles a bit. Looking at Wealthlab for example, there is a bewildering array of order management or "trading system" functions:
http://www.wealth-lab.com/cgi-bin/WealthLab.DLL/getpage?page=FnTradingSystem.htm
Neoticker has a similar range of functions.
Cursory analysis suggests that some of the proliferation is perhaps due to the lack of callback functions that Mojo Trader will have. The most important one is the onPositionOpened() handler. This method I envisage as being used to trigger stop loss or profit target orders.
If anyone has experience with other trading software e.g. Tradestation, Amibroker etc. and are familiar with their order management methods, please add your input here!
I'll start off with the basics and some design options.
Terminology
1) Method names could reflect: long or short
2) Method names could reflect: buy or sell
3) Method names could reflect: buyToOpen, buyToClose, sellToOpen, sellToClose.
4) Method names could reflect buy, sell, buyToCover, sellShort
So effectively, there are two issues there: whether to use Long/Short or Buy/Sell nomenclature and whether to differentiate between operations if there is already a position.
I have seen some software give their order management methods the following semantics:
Buy - will close existing short position and open a new long position.
Sell - will close existing long position and open a new short position.
In effect, the order will be double the size if there was an existing position in the opposite direction.
Order Types
It seems to be the norm to provide separate methods for the different order types e.g.:
buyMarket (or just buy)
buyLimit
buyStop
etc.
Ditto for Sell methods.
Annotations
It seems sensible and is widely practiced to have annotations or notes metadata for an order for logging purposes. How best to implement this? From an end user point of view, simply having two different methods seems the most obvious choice e.g.
buyMarket(arg1,arg2);
or
buyMarket(arg1,arg2,"Yabadabadoo!");
i.e. provide an overloaded method for each order management method there is. This doubles the number of methods which bothers me a little. I wonder if Java's varargs might be of use here? Thanks to dcraig for bringing that to my attention.
Position Size
It makes a lot of sense and again is not uncommon to separate out position sizing or money management e.g.
http://www.geniustrader.org/
The question is how best to do this or rather at this point, how best to present the options to the end-user developer via the order managment methods.
The conclusion I have come to is to again offer two versions of the same method. One where the position size is explicitly specified and the other where it is not and thus deferred to the position size strategy to figure out e.g.
buyMarket(arg1,100,"I have explicitly set the position size of this order!);
or
buyMarket(arg1,"The position size strategy is going to figure out the size of this order for me!");
I'll leave the details of position sizing or money management to a later discussion as there are a few options for dealing with that.
What I think is important is that a reference to the order created is returned from all of these order management methods so that the SignalGenerator can keep a hold of them and interrogate their properties later on. So, however the position sizing is done as long as this requirement is met there shouldn't be a problem.
Specifying the Instrument for the Order
This is yet another issue that could result in the proliferation of methods.
Multi-instrument strategies are crucial for my purposes and I'm not convinced this issue is supported properly by some software though I see the latest RightEdge beta now seems to have support for this.
It seems as if Multi-instrument support was a bit of an after-thought for NeoTicker as all of the order management methods are replicated with "Ex" versions which allow the specification of the instrument.
The question is how best to deal with specifying which instrument to trade. There are various options:
1) If you want to trade the Instrument that is applicable to the current event e.g. onBar() then either the Instrument is specified in the handler e.g. onBar(Instrument i,Bar b) and there fore when it comes to order management you do:
buyMarket(i,100,"100% up room to go!");
2) Alternatively, the instrument applicable to the current event is set by the Strategy so all you have to do is:
buyMarket(100,"No fear!");
This mandates an extra method on the SignalGenerator interface: setCurrentInstrument(Instrument i) which gets called just before the event handler. I'm trying to think if there are possible synchronization issues with this approach.
3) However, if you want to trade a separate instrument which is distinct to those for which you are receiving market data then you can explicitly create the appropriate instrument (perhaps in a initialization method and saved to a member variable) and then reference that Instrument in the order method e.g.
buyMarket(myOtherInstrument,100,"Arb city!");
Conclusion
Your thoughts an opinions much appreciated! Ideally, I'd like to keep the order management methods as clean and simple as possible but it's difficult to see how the number can be kept down whilst at the same time keeping the burden on the programmer to a minimum in terms of remembering which arguments to specify.
To summarize the optionality of arguments we have:
1) Annotation/Note
2) Position Size
3) Instrument
And then different methods for each order type and direction combination.
Remember these order management methods are not only creating an
Order object but they are also submitting them to the
OrderManager or perhaps being passed through some filters before it reaches the
OrderManager.
If you're still awake after reading this lengthy post then let me know any ideas you have.