Monday, September 24, 2012

Option data captured into database

I managed to capture stock option data into database. I made two mistakes and found the tricks to get it working. The first one was to set expiration Friday instead of the technical expiration Saturday, e.g. stockOption.setExpiration(friday). The second one was to add ib.genericTickList=101,100 setting into conf-base.properties. Below is the IB documentation on Generic Tick Types:

http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/api/generic_tick_types.htm

Integer ID Value
Tick Type
Resulting Tick Value (see list below)
100
Option Volume (currently for stocks)
29, 30
101
Option Open Interest (currently for stocks)
27, 28
104
Historical Volatility (currently for stocks)
23
106
Option Implied Volatility (currently for stocks)
24
162
Index Future Premium
31
165
Miscellaneous Stats
15, 16, 17, 18, 19, 20, 21
221
Mark Price (used in TWS P&L computations)
37
225
Auction values (volume, price and imbalance)
34, 35, 36
236
Shortable (see “Tick Values” for more information)
46

Wednesday, August 29, 2012

OHLC Bar

I think I have missed one important element in my EPL, the OHLC Bar. That will most probably useful to mimic candle stick chart with some intervals.
  • https://code.google.com/p/algo-trader/wiki/AlgoTraderDocumentation#Market_Data
  • https://groups.google.com/forum/?fromgroups#!searchin/algo-trader/ohlc
  • https://groups.google.com/forum/?fromgroups=#!searchin/algo-trader/ohlc/algo-trader/vZ5UBeLMkYE/enccRWIrJ2IJ
  • https://groups.google.com/forum/?fromgroups=#!searchin/algo-trader/ohlc/algo-trader/JwLFM69vdGU/2vSmbNiiFIAJ
  • https://groups.google.com/forum/?fromgroups=#!searchin/algo-trader/ohlc/algo-trader/8NvhNQzh9x0/uuz-H6Eq5KsJ
select * from Tick.std:groupwin(security.id).custom:ohlcbar(dateTime, last); 
select
        first(open) as open,
        max(high) as high,
        min(low) as low,
        last(close) as close
from
        Bar.win:length_batch(2);
 select
        first(price) as open,
        max(price) as high,
        min(price) as low,
        last(price) as close
from
        OHLCTick.win:time_batch(1 min)
group by
        ticker;
insert into Bar
select first as open, last as last, min as low, max as high,
* from Tick.std:groupwin(security.id).custom:ohlcbar(dateTime, last);


Smooth RSI

I was able to get RSI values from below EPL.
insert into RSI
select talib("rsi", currentValueDouble, 14) as value
from Tick(security.symbol = "GOOG");
But the series of values were not smooth. I wanna try to get smooth RSI line chart. Perhaps I can take the SMA as input for RSI, like this:
insert into SMA
select talib("sma", currentValueDouble, 5) as value
from Tick(security.symbol = "GOOG");
insert into RSI
select talib("rsi", sma.value, 14) as value
from pattern [every timer:interval(2 minutes) -> sma=SMA];
Or the other way? Making a smooth SMA line chart from RSI values.
insert into RSI
select talib("rsi", currentValueDouble, 14) as value
from Tick(security.symbol = "GOOG");
insert into SMA
select talib("sma", rsi.value, 5) as value
from pattern [every timer:interval(2 minutes) -> rsi=RSI];

Monday, August 27, 2012

Hacking TA-Lib

TA-Lib source code is available from http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-msvc.zip

I opened the com.tictactec.ta.lib.Core class. The RSI method signature looks like this:
   public RetCode rsi( int startIdx,
      int endIdx,
      double inReal[],
      int optInTimePeriod,
      MInteger outBegIdx,
      MInteger outNBElement,
      double outReal[] )
I think I made a mistake in double inReal[]. I was supplying single latest stock quote. Instead, I think I'd better supply an array of current and past stock quote in specific interval.

But when I supplied the array as a parameter, AlgoTrader can't compile the EPL. I asked this in AlgoTrader forum https://groups.google.com/forum/?fromgroups=#!topic/algo-trader/PtaMF04ay6Q

Below is the reply from Andy:
You should only provide the scalar value (tick.currentValueDouble). The GenericTALibFunction will collect all values and provide them as an Array to the talib function. Have a look at the JavaDoc of GenericTALibFunction.

Open Source Trading Platforms List

http://www.ta-lib.org/hdr_dev.html
A few of the products using the open source TA-Lib. If there we charting features in these products, I could probably hack into the code to see how TA-Lib get called.

http://www.ta-lib.org/hdr_lnk.html

http://www.traderslaboratory.com/forums/tools-trade/11086-open-source-trading-platforms-master-list.html

http://algotradingindia.blogspot.in/2012/05/open-source-trading-platforms-list.html


TA-LIB sucks

Well, I started writing my first strategy in Esper EPL. Not so difficult to reach a "Hello World" happy flow.

But too little documentation for TA-LIB http://www.ta-lib.org . No idea how to customize the parameters of MACD, RSI, STOCH etc to represent 2-minutes candle stick chart.

I think I better source other ways to generate Technical Analysis indicators. Let me hack into AIOTrade's http://aiotrade.com/ code to find out if I can do full customization on those Technical Analysis indicators.

Oh ya, that also bring the hints that I am going to use AIOTrade as my front end for AlgoTrader. Cool?

Monday, August 20, 2012

Esper Complex Event Processing

I have skimmed through Esper CEP official documentation quickly http://esper.codehaus.org/esper-4.6.0/doc/reference/en-US/pdf/esper_reference.pdf .

The EPL syntax is quite similar to SQL, with plenty extension. I think I'm ready to use EPL. Now I can understand the existing EPL used in AlgoTrader.

To develop strategies using EPL, I need to know TA-LIB http://ta-lib.org/ . I couldn't find many documentation about TA-LIB. Below are some simple documents:

http://www.ta-lib.org/function.html
http://qtstalker.sourceforge.net/talib.html
http://tadoc.org/
http://www.heatonresearch.com/content/technical-analysis-library-ta-lib-tutorial-java