Thursday, July 25, 2013

Options code updated - 25 Jul 2013

The earlier mentioned code has been updated. Now the files can be directly copied from a  dropbox link. A single compact example is shown for reference. Also now we can see probability that a strategy will return profit for the volatility.

Dropbox link: https://www.dropbox.com/sh/7yuzb20grpgg5de/fBUZBauRfx



Tuesday, July 23, 2013

First Naked Put

With current rise in TCS, it seems highly probable that the stock does not fall anymore. The rise might be subdued because of the 18% rise in the stock in the month of July 2013. This points coupled with me wanting to make my first thought out safe trade, brought me to selling deep OTM puts. I have sold one lot of 1640 TCS PE at 17.00. The strike is 1 sd away from the stock. The theta gain is 0.48 per day and delta loss is 0.2. By month end I expect to have collected the full premium of 4250.


Friday, July 19, 2013

Option strategy plotter


This code snippet displays the profit vs underlying diagram for a combination of  option strategies. The chart shows the amount of profit earned for the price movement at expiration and also the break even price.

Before we jump into the code, let us look at the python notebook example.



As we can see all the inputs need is the strategy themselves. We can also see that break even is at 107.33.

The strategies are defined are strictly typed. For Stock the syntax is as follows.
<Long/Short> <Volume> Stock <Price>

and for option...
<Long/Short> <Volume> <Strike> <Call/Put> <Price>


The graph is plotted using matplotlib's pyplot. This is the only externally used library.



import matplotlib.pyplot as plt

Next we need floating point version of range, and the best implementation happens to be the following



def drange(start, stop, step):
 r = start
 while r < stop:
  yield r
  r += step

Followed by processStrategy which processes individual strategy and generates profit of loss value for a particular price point. As we can see the the flow is split depending on whether we have Call, Put or Stock  and a Long or Short strategy.


def processOptionStrategy(strat, price_point):
 strategy = strat.split(' ') 
 if (strategy[2] == 'Stock'):
  volume = float(strategy[1])
  price = float(strategy[3])
 else:
            try:
                volume = float(strategy[1])
                strike = float(strategy[2])
                price = float(strategy[4])
            except:
                print "dumbass...numbers"
 if strategy[0] == 'Long':
  if strategy[3] == 'Call':
   if price_point < strike:
    return -(price * volume)
   else:
    return (((price_point - strike) * volume) -(price * volume))
  elif strategy[3] == 'Put':
   if price_point > strike:
    return -(price * volume)
   else:
    return (((strike - price_point) * volume) -(price * volume))
  elif strategy[2] == 'Stock':
                    return (price_point - price) * volume
  else:
   print "...Call or put"
 elif strategy[0] == 'Short':
  if strategy[3] == 'Call':
   if price_point < strike:
    return (price * volume)
   else:
    return ((price * volume)) - ((price_point - strike) * volume)
  elif strategy[3] == 'Put':
   if price_point > strike:
    return (price * volume)
   else:
    return ((price * volume)) - ((strike - price_point) * volume)
  elif strategy[2] == 'Stock':
                    return (price - price_point) * volume
  else:
   print "...Call or put"
 else:
  print "...Long or short"

and finally we plot a graph for all the possible price points in range. X axis values for all values present in strategies and breakeven points are also added.


def plotStrategy(strats, min1=0.9, max1=1.1):
 stratvals = []
 min = 100000
 max = 0
 xticks = []
 for row in strats:
     valstr = row.split(' ')[2]
     if valstr == 'Stock':
         val = float(row.split(' ')[3])
         xticks.append(val)
     else:
         val = float(valstr)
            if val < min:
                min = val
            if val > max:
             max = val
            stratvals.append(val)
            xticks.append(val)
 yvals = []
 xvals = []
 max = max * max1  
 min = min * min1
 for i in drange(min, max, (max - min) / 200.0 ):
  xvals.append(i)
  sum = 0
  for strat in strats:
   sum = sum + processOptionStrategy(strat, i)
  yvals.append(sum)
  if i in stratvals:
   xticks.append(i)
  if len(yvals) >3:
   if (abs(yvals[-1] + yvals[-2]) < abs(yvals[-1] - yvals[-2])):
    xticks.append(i-(max - min) / 200.0)
 plt.plot(xvals, yvals)
 plt.axhline(0, color='black')
 plt.xticks(xticks)
 plt.setp(plt.xticks()[1], rotation=60)
 plt.grid(b=True, which='major', color='g', linestyle='--')

and that's it. That is all required to plot Option strategies.

Downloading Yahoo IEOD charts

The following python script downloads yahoo 1 day charts and saves them for you.  I personally use this as IEOD data is available only at a price. The scripts are stored with a directory for each day. This might not provide continuity for most users but I found it useful to see the correlation between the stocks on a particular day. This script has been tested on Python 2.7 and requires no extra libraries. The code of getNIFTY.py is given below.
import urllib
import csv
import os
from datetime import date
fi = open("symbolList.csv", "rb")
symList = csv.reader(fi)
dt = date.today()
if not os.path.exists("./Intraday_YAHOO"):
    os.makedirs("./Intraday_YAHOO")
direc = "./Intraday_YAHOO/"+str(dt.year)+str(dt.month).zfill(2)+ \
str(dt.day).zfill(2)
if not os.path.exists(direc):
    os.makedirs(direc)

for row in symList:
 print row[0],row[1]
 url_chart = "http://chart.finance.yahoo.com/z?s=" + row[0] + "&t=1d&q=&l=off&z=m&a=v&p=s&lang=en-US&region=US"
 file_chart = direc+"/"+row[1]+".jpg"

 urllib.urlretrieve(url_chart, file_chart)
Create symbolList.csv with the format (<Yahoo symbol>, <image_finename>). An example is given below.
^NSEI,NSEI
ACC.NS,ACC
AMBUJACEM-EQ.NS,AMBUJACEM-EQ
AXISBANK.NS,AXISBANK
The folder structure that is generated should look somewhat like this.
~\
|   getNifty.py
\---Intraday_YAHOO
\---20130716
    |   ACC.jpg
    |   AMBUJACEM-EQ.jpg
    |   AXISBANK.jpg
\---20130717
\---20130718
\---20130719