Sunday, September 29, 2013

thinkscript included: sdi_seaproj - seasonal projection of price

sdi_seaproj is the latest incarnation of my seasonal projection study. i feel this is not only superior to my previous sdi_seapro5 but also to the seasonality chart mode provided by thinkorswim.  then again, every child is brilliant in the eyes of its parent. here's an image of the new seaproj:
sdi_seaproj of 10, year-long seasons anchored 20 weeks before current bar.
relative merits over tos seasonality:
  • plots on a regular chart - no need to switch to a special charting mode.
  • has choice of anchor date - tos seasonality chart mode is permanently anchored to january 1, whereas sdi_seaproj can anchor to a variable date trailing a parameter number of bars in the past or a fixed date of one's choosing. anchoring near the last bar of the chart plots the projection mostly into the right extension area leaving the main chart relatively uncluttered for technical analysis.
  • provides reliability indicator - tos seasonality provides no statistical measure of the distribution of seasonal data whereas sdi_seaproj provides a reliability indicator based on standard deviation to show when seasonality is reliable or not.
  • can skip an anomalous season - sdi_seaproj has a parameter one can specify to remove an anomalous season from the average. this was mentioned by larry williams (of williams %r fame) as being crucial to utilizing seasonality charting.
  • can explore subcycles - sdi_seapro has parameters one can specify to define the season length and number to average. this frees one to explore cycles in the chart data on any scale, whereas tos seasonality is limited to a season length of one year and season number defined by the chart duration.
relative merits over sdi_seapro5:
  • variable number of seasons -previously, sdi_seapro5 was limited to a fixed five-season average. now, in sdi_seaproj, i utilize the fold statement of thinkscript to process a user specified number of seasons. as a consequence sdi_seaproj cannot show individual season projections which require a separate, valid, plot object to be statically declared for each season. this turns out to be a minor detraction since there is a reliability indication in the sdi studies.
  • flat line color tied to projection average color. previously one had to change the color of both of these plots separately.
  • plot starts on anchor date close. previously, in sdi_seapro5, the plot of the average started on the bar after the anchor date. now, in sdi_seaproj, the plot starts on the close price of the anchor date to provide confidence in the correct date selection.
  • reliability indicator color chooser. previously, in sdi_seaproj5, the color of the reliability indicator was fixed to green/red, now, in sdi_seaproj, one can choose the up/down colors for ri.
  • simplified reliability indicator. previously, in sdi_seapro5, there were indicators that decorated the top edge of the reliability indicator that did not provide much in the way of actionable information. now, in sdi_seaproj, the reliability indicator is simplified to one job: showing where the standard deviation bands cross the flatline. there is a straightforward parameter called, ri factor, that determines the multiple of standard deviation to utilize in the reliability indicator.
here's the code: (how to install)
#########################
# sdi_seaproj: Season Seasonal Projection Of Price
#hint: Displays a seasonal projection of price based on the average percentage price move between the anchor date and same seasonal dates in the past. Also shows a reliability indicator to help discern which stocks have seasons reliable enough to trade. sdi_seaproj revision: 1.0 source:
http://www.smalldoginvestor.com
# author: allen everhart
# rev 1.0 date: 28sep2013
#
# copylefts reserved. This is free software. That means you are free
# to use or modify it for your own usage but not for resale.
# Help me get the word out about my blog by keeping this header
# in place. 

input trailAnchor = 0; #hint trailAnchor: Sets Moving anchor date to n-bars ago. FixedAnchorDate takes precedence over trailAnchor. The plot of the seasonal average always starts on the close of the anchorDate bar. sdi_seaproj revision: 1.0 source: http://www.smalldoginvestor.com
input fixedAnchorDate = 0; #hint fixedAnchorDate: Sets an anchor date that does not move. Format: YYYYMMDD. FixedAnchorDate takes precedence over trailAnchor. The plot of the seasonal average always starts on the close of the anchorDate bar.
input numSeasons = 5 ; #hint numSeasons: Number of seasons to average.<br><br>note: On the shorter chart durations (1-5 years) thinkscript can access data beyond the scope of the chart. If the projection is not displaying then select fewer seasons and/or increase the chart duration.
input lenSeason = 52 ; #hint lenSeason: Sets number of bars in a season. <br><br>note: There are about 252 trading days per year for US stocks and about 260/year for futures and forex.
input skipSeason = 0 ; #hint skipSeason: Suppress an anomalous season from the average. Skipping a season causes the average to look back an extra season so as to preserve the numSeasons in the average.
input riFactor = 0.3 ; #hint riFactor:multiple of standard deviation to use for ri (reliability indicator.) the ri shows when there might be a reliable trade based on seasonality. the up-ri shows when the lower band line (riFactor*stdev) tracks above the flat line. the down-ri shows when the upper band line tracks below the flat line. <br><br>note: 54% of the individual year projections are above the 0.1*stdev band, 62% above 0.3*stdev, 73% above 0.6*stdev and 84% above 1.0*stdev.  

def anchorDate =
  if fixedAnchorDate > 0 then
    fixedAnchorDate
  else if trailAnchor > 0 && !IsNaN(close[-trailAnchor]) && IsNaN(close[-trailAnchor - 1]) then
    GetYYYYMMDD()
  else if trailAnchor > 0 && anchorDate[1] > 0 then # 1.5.2 added [1] to originDate
    anchorDate[1]
  else
    0
;
#plot od = originDate;


### pn tells us how many bars we are past the originDate
def pn =
  if !pn[1] && GetYYYYMMDD()[1] == anchorDate then
    1
  else if !pn[1] && IsNaN(close) && !IsNaN(close[1]) then
    1
  else if pn[1] > 0 then
    pn[1] + 1
  else
    0
;
#plot p=pn;

#number of seasons we are skipping
def skipNum = if skipSeason > 0 && skipSeason <= numSeasons then 1 else 0 ;

def avgPctMove =
  if pn > 0 then
    fold sn = 1 to numSeasons + skipNum + 1 with psum=0 do
        if sn != skipSeason then
            psum + ((GetValue(close, sn * lenSeason) / GetValue(close, sn * lenSeason + pn)) / numSeasons)
        else
            psum
  else 0
;

def flatLine =
  if pn[-1] == 1 then close
  else if pn >= 1 then flatLine[1]
  else 0;

plot pa =
    if pn[-1] == 1 then close
    else if pn > 0 then avgPctMove * flatLine
    else Double.NaN ;
pa.SetDefaultColor(Color.BLUE);

plot fl = if pn[-1] > 0 then flatLine else Double.NaN; # flat line
fl.SetStyle(Curve.SHORT_DASH) ;
fl.assignValueColor(pa.takeValueColor());

def variance =
  if pn > 0 then
    fold ssn = 1 to numSeasons + skipNum + 1 with pvar=0 do
        if ssn != skipSeason then
            pvar + (Sqr(GetValue(close, ssn * lenSeason) - pa) / numSeasons)
        else
            pvar
  else 0
;
def pstd = Sqrt(variance);

def hpul =  pa + (riFactor * pstd);
def hpll =  pa - (riFactor * pstd);
#plot ul = hpul ;
#plot ll = hpll ;
plot ri = # High Probability indicator
  if riFactor == 0.0 then
    Double.NaN
  else if pn > 0 && hpll > flatLine then
    hpll
  else if pn > 0 && hpul < flatLine then
    hpul
  else
    Double.NaN
;
ri.DefineColor("Up", Color.UPTICK);
ri.DefineColor("Down", Color.DOWNTICK);
AddCloud( ri, fl, ri.Color("Up"), ri.Color("Down") );
ri.SetHiding(1);
########################


Sunday, September 22, 2013

lessons of ai: aiSpy 6 months later ...

last spring, i unveiled a study called aiSpy that i had created (with a good deal of effort mind you) that implemented a form of artificial intelligence called a feed-forward network. this feed-forward network was static, that is it didn't modify itself but was the end result of an extensive genetic training program. ffn's are used to do complex pattern recognition for speech-to-text and handwriting recognition and many other human-interface applications you may be familiar with in your cell phone.  if there were patterns to the complicated dance of the market then they ought to have ended up encoded in the ffn, which at the time of release was outperforming buy and hold over 2, 5 and 10 year periods.

so how did aiSpy do in the real world? here's a picture of the performance over the last 6 months, post-training:
last 6 month performance of aiSpy.

the p&l for aiSpy, trading $10,000 per trade, is circled, 497.08, and contrasted to the growth of $10,000 bought 6 months ago and held (bnh, green number in square)  and the growth of $10,000 invested in 6 increments (dca, blue number in square.) bnh is the winner with a $967 gain, dca came in 2nd with a $545 gain and aiSpy third with a $497 gain. i should note that the aiSpy p&l does not include the impact of commissions, which at the standard thinkorswim rate amounts to $10 per round-trip, or $120 over the last 6 months for 12 twelve trades. the aiSpy result is an even more distant 3rd. while this is a lack-luster result, at least it is positive. what went wrong? i think i know and this knowledge is helping me improve my trading and i will share this in a series of posts called lessons of ai.




Wednesday, September 18, 2013

thinkscript included: sdi_ivp revision 1.2 - choose your color

i am thrilled that tastytrade has incorporated my sdi_ivp chart label into their on-air charting. it is wonderful to give back to this community. however, i am seeing that not everyone is thrilled with my choice of plum for the color of the label (too close to yahoo-logo color?) the 1.2 revision enables you to choose your own color without having to edit the code.

here's how the color chooser works:

goto edit studies by clicking on the beaker icon on the top bar of the chart and rightclick on the sdi_ivp study. this gives:


the labelColor plot only exists to pull in the color chooser widget that comes with plot objects, all visible manifestations of it are hidden.

click on the color box and you get the color chooser drop down:
 
click on one of the 9 standard colors then click ok/ok. when i click on dark blue the label color changes:
 

 
 
or click on the select button in the color chooser to choose a color from a broad palette of colors or input the explicit rgb values if you know them:


 
 
as always i maintain the revised source code in the original post for sdi_ivp

Friday, September 13, 2013

thinkscript included: sdi_ivp version 1.1 released.

previously sdi_ivp would print N/A on equities that contained gaps in the data feed for implied volatility. most notably this applied to aapl, which did not have implied volatility data for 8/7 and 8/8/2013. in the 1.1 version sdi_ivp ignores the data gaps in implied volatility and produces the same implied volatility percentile number that you get on the trade tab. this has been checked for the s&p 100.

here is aapl with the 1.1 version of sdi_ivp:



the code source for 1.1 is on the original release post for sdi_ivp

Sunday, September 1, 2013

follow the money ... trade bear put diagonals

 
deep throat: follow the money.
bob woodward: what do you mean? where?
deep throat: oh, i can't tell you that.
bob woodward: but you could tell me that.
deep throat: no, i have to do this my way. you tell me what you know, and i'll confirm. i'll keep you in the right direction if i can, but that's all. just... follow the money.
-all the president's men (1976)
 
spy put diagonal, time unit-cost is  .48/week (3.35/7)
i am in the calendar vending business. selling a calendar is what happens when one rolls a shorted option from one expiration period to the next. this nets a credit because, in the option world, time-is-money and the same strike option with more dte (days-to-expiration) is generally worth more money.

the problem is that markets move. the price of the underlying can move far from the strike of my short option and this reduces what i can collect on the roll when the time comes. so it behooves me to heed deep-throat's advice and follow-the-money so as to be rolling shorted options that are as close as possible to the underlying's price as expiration draws nigh.

the bear-put-diagonal helps with this problem because it is such a flexible trade. when time has run down on the shorted option, the bear put diagonal can almost always be adjusted in such a way that the shorted option ends up closer to the money yet the adjustment still nets a credit.

i initiate my bear-put-diagonals on the eve of earnings reports. this is when implied volatility will be inverted - the near term options price way higher than the remaining time warrants due to event risk. i prefer equities with weekly options because there are always soon-to-expire options available when earnings come due for reporting. often-times my bear-put diagonals are profitable the next day because of the crush in implied volatility that occurs post-event.

to initiate the trade i sell the first otm strike in the weekly options and purchase the first itm strike in an expiration that is 90 or so days away. the near-term option should price something like 50% of the far-term option. the reason for the 90 days is that that purchases time in bulk. the option market naturally discounts bulk purchases of time in proportion to the square root of remaining time. this makes it possible to sell time in smaller chunks in the form of week-to-week calendars, via roll transactions, that add up to more than the bulk purchase. one can think of this as a wholesale/retail business except the product being vended is time.

higher priced stocks are more efficient, commission-wise. i generally prefer stocks above 30 for bear-put diagonals since i am paying two commissions per diagonal and these higher priced stocks tend to be better quality companies anyway.
 
i always purchase the put diagonal in multiples of 3 or higher because, if need be, this facilitates morphing the trade into a backratio. this is how i adjust the trade for a stock that gaps-up big.

the risk of the bear-put diagonal is limited to the purchase price of the trade. the trade may post a loss initially on gapping behavior but after following through with adjustments and rolls it is hard to envision a situation that would lock-in a full loss for a 90 day duration. still, i size my trades so that the full loss is a small acceptable portion of my account.

after acquisition i rest a gtc limit order to sell at 25% gain. this is the goal. also, i make note of the unit-cost of time by dividing the cost of the diagonal by the number of roll opportunities. the next day, after the vol-crush, if the trade is profitable but the gtc order has not filled then i close the position at market. otherwise i manage the trade for the long haul and this means dealing with gapping behavior after earnings.

if the underlying has gapped up then when the shorted puts are nearly worthless i buy them back and sell the next week's options usually at a higher strike closer to the money. one can always morph the bear diagonal into a calendar, with no additional risk, by selling the same strike as the far-term put. selling strikes higher than the far-term put is possible too but increases the risk in the trade in proportion to the difference in the strike prices. when chasing the money two or more strikes above the far-term put's strike price i sell one fewer option than i bought-back. this morphs the trade into a backratio. if the market subsequently sucker-punches me with a sharp reversal then i have the extra long-put that will at least ease my pain if not drive the trade to profitability, depending on how vol expands. the rolls of two atm short puts more than pays for three long puts.

if the underlying has gapped down on earnings then there is usually some way to roll the whole position down for a net credit. re-establishing an atm bear-put diagonal is preferable but, if that fails to generate a credit, then i consider morphing into a calendar, a bull-put diagonal or a backratio. shifting the long put down more strikes than the short put is a powerful engine to generate credit especially because this works in the direction of volatility (ie lower prices in the underlying increase implied volatility.)

it has been my experience that equities tend to stabilize after gapping on earnings. so after the post-earnings adjustment the trade management typically devolves into the business of rolling options. my bear-put-diagonal-earnings trades have moved the meter on my account and have become my go-to trade. i plan to share a few of them on mytrade in the near future.

happy trading to you.