Saturday, April 28, 2012

thinkscript included: hiv delta

hiv delta is the difference between historical and implied volatility. implied volatility is a calculation of the underlying equity volatility based on the pricing of options. historical volatility is a calcuation of volatility based on the actual equity pricing over a recent period (20 bars by default.) if you are trying to decide if options are richly priced then you might want to compare the iv with hv. so the idea of my hivdelta study is to give you that comparison as a single histogram study instead of two separate line graphs. here's a picture of sdi_hivdelta in action:

SPY Weekly Chart With sdi_hivdelta.
bright green bars represent where implied minus historical was greater than zero and increasing. dark green bars represent where implied minus historical was greater than zero and decreasing. vice-versa for the red/dark-red bars.

as an option trader i want to be selling options nearly most of the time. this is because i perceive theta-burn on the obligee side of the options contract to be a trading edge. occasionally i want to be a put buyer near a complacent market top. it looks to me like the red/green transitions are the times to be buying a put. what do you think?


here's the code:

#############
#sdi_hivdelta - difference between historical and implied volatility
#hint: plots the histogram of the difference between historical and implied volatility. the idea is that options are richly priced when iv exceeds hv and selling options is the more advantageous strategy. source:
http://www.smalldoginvestor.com rev: 1.0

declare lower;
input mode = { "histMinusImp", default "impMinusHist"};
#hint mode: select which way to perform the difference calculation.
input length = 20;
#hint length: number of bars to perform historicalVolatility calculation

def ihDelta = historicalVolatility(length)- impVolatility();

plot hid = if mode == mode.impMinusHist then -ihDelta else ihDelta;
hid.setPaintingStrategy(paintingStrategy.HISTOGRAM);
hid.setlineWeight(5);
hid.assignValueColor(
  if hid < 0 and hid <= hid[1] then color.RED
  else if hid < 0 and hid > hid[1] then color.DARK_RED
  else if hid >= 0 and hid >= hid[1] then color.GREEN
  else color.DARK_GREEN
);
#############

Monday, April 16, 2012

prodigio wiz file included: altucher's strategy

i hope this doesn't seem too obsessive but I have implemented altucher's simple strategy in prodigio. this strategy can be simply stated:
buy on 4 consecutive lower closes, sell on two consecutive higher closes
here's what this strategy looks like in prodigio:

sdi_alt4down entry strategy


sdi_alt2up exit strategy
the multitude of boxes on the entry strategy are due to a mismatch in the capabilities of prodigio to implement this simple idea in a concise manner. in prodigio you basically have to tell it to compare today's close to yesterday's close and yesterday's close to the day before and so on til there are four comparison sequences. there does not seem to be a way to implement a counter for consecutive lower closes as one can do in thinkscript. but no matter, in thinkscript one cannot backtest a strategy like this against the entire s&p 100 for an entire year:


sdi_altucher equity curve when traded against s&p 100 for one year
these trades are each about 10,000 dollars of equity. the 150/81 win/loss ratio is excellent in my experience.

here's the prodigio statistics:



sdi_altucher statistics for trading s&p100 for 1 year


in prodigio i am only able to run the backtest 1 year back. in his presentation at the 2012 trader's expo in new york, altucher presented statistics on this strategy generated in fidelity's wealthlab-pro showing profits in every year going back to the late 1990's.

lastly i present the wiz file. this is an encrypted plain-text file that will implement the sdi_altucher strategy when imported into your prodigio platform. copy the text below and paste into a notepad file. then rename the extension of the file from .txt to .wiz and import into the wizard lab's strategy tab.

"sdi_altucher",VALID,STRATEGY,04/16/2012 18:32
-----BEGIN----
AAABAA3zKxoF36Pn9pFdzKwlko8nv9kA35LC0fgOSJQ9rMXRMlB/sK5MCQWnGfnO64vgZ+h8EcEb
iwYvCxra52BOJgtauMnyQY96XhWU/4G7mGHpRIIrEfxd2IRw3eQ8AYsRsh9In+8ljAVk+app20++
WtMIe7ZoMml12ZpMM9PybLlmCzUr2AK0TZ1DoVeQREJk7PAykZbtUvlp2/Pc3pwuTbW6SY/2MccX
VMWLqo1LlJo7zEi4jxbRzx5cKjqcpsCsEMm58knT6+BMrdtBIYylhqev0fD5z4xQIhmyYGzFtYyH
vOU0fWii88Jd7BdGfG2BqUjTMpfeTMIhSHtVNMOEmFw62CaaV21kwVPFQdClayJq6ftO6mMWBcZ5
cCxsSdv0vrCTiQdIjcxyu+ZEOpNa0ikveKfchTgkt4rhQPWMjQ0xFcSkqcZjW88MruD7ivybgGCL
KH/hBkhAhC5qkNsQbueC4uj9XeoYgDSSLDQezkCCBdgYtBrEG8k+q4AQBvpb3hc7IxY9GmwfJeWY
K+7CoviG2EL4cQ8CUT5Y+cVs9EaXMh2ATENbxfVDTDxjmEoCF8CTby71kJ6PNfcQDEZf4hNo6oxr
B37QO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END----






Saturday, April 7, 2012

thinkscript included: altucher's strategy

during the trader's expo this year james altucher mentioned a simple strategy for trading the markets that he says has produced positive results almost every year for the last decade. in a nutshell, his strategy is:

buy on four consecutive lower closes, sell on two consecutive higher closes
this is a long-only strategy and it uses only market orders - really quite remarkable. so i thought i'd use altucher's strategy to explore the thinkorswim strategies facility.

thinkorswim strategies are the equivalent of what have come to be known as expert advisors. an expert advisor will help you map out how a strategy has performed in the past. the thinkorswim ea's will notate entries and exits on tos charts and keep a running tally of the hypothetical (they are only automated advice, not live trading) profit and loss. they are programmed in thinkscript.  here's what my ea for the altucher strategy looks like:
spy with altucher's strategy, the p&l for the year is circled
sdi_altLE is where the strategy bought-to-open, and sdi_altLX is where the strategy sold-to-close.  this produced loss on spy but a small one considering that each trade is 100 shares - you didn't really think this would work all-the-time on any equity!? there's the rub, altucher's strategy works really well on a handful of equities and not so well on others. however, an ea can tell you in a flash which ones look rosey in the strategy's rearview mirror.

here's a more successful picture of the poster child, aapl:
aapl with altucher's strategy
a nice gain but it completely missed the run-up this spring. the ~15% return on aapl is a bit underwhelming when compared to the 50% run-up. this underscores the boundaries of utility in the altucher strategy. it seems to do well with equities that are in favor but not trending. it will buy on weakness and sell into the rise, generally a good thing as most equities spend far more time treading water than making a big splash.

altucher gets crushed if the equity falls out of favor and starts trending down without a breather but not always. take a look how altucher fared on fslr:
fslr with altucher strategy
fslr dropped from the 150's down to 20 and yet, this altucher strategy, trading only to the long side, produced a $1800 gain, an unlikely feat of scalping the bounces in a down trend.

there are two scripts for my implementation of the altucher expert advisor, one for entry and the other for exit. also, you will need to create strategies, not studies. if you cut 'n paste the code be sure to click the strategies tab in the edit studies dialogue to create a new one.

############
# sdi_altLE - altucher strategy long entry.
#hint: buy to open at market after four consecutive lower closes. source: smalldoginvestor.com rev:1.0
def condition = close<close[1] && close[1]<close[2] && close[2]<close[3] && close[3]<close[4] ;
addOrder(OrderType.BUY_AUTO, condition);
############


#############
#sdi_altLX - altucher strategy long exit.
#hint: sell to close at market after two consecutive higher closes. source: smalldoginvestor.com rev:1.0
def condition = close>close[1] && close[1]>close[2] ;
addOrder(OrderType.SELl_TO_CLOSE, condition);
##############