there are two files attached to this email.

The file "volatility.ijs' containing the code that impliments the xxxx
algorithm found at:

The file "sybd.ijs' which has open/high/low/close data for the stock
with the symbol sybd.

To run the code, put the two files in any directory then do a jconsole.

After that all you have to do is "load 'volatility.ijs'"

If the line that loads 'sybd.ijs' found on line X of the
'volatility.ijs' file is commented out, the small sample of bogus
open/close/high/low data will be used to calculate the volatility value
calculated.  Otherwise, the 3 months of SYBD's data will be used.

Any comments on the code would be appreciated.

My direction is to be able to plot a moving average of volatility.  The
idea is that if the volatility of a stock changes you should look at
that stock as a potential buy/short.

Anyhow, enjoy.

If you have any trouble, with loading and running the code, let me know.

:-)
NB. requires 'jfiles'

ArrayMaker =: ". ;. _2


sybd =: ArrayMaker 0 : 0
2006 10 02    0.105    0.109    0.102    0.109
2006 10 03    0.109    0.109    0.100    0.100
2006 10 04    0.100    0.100    0.090    0.097
2006 10 05    0.097    0.097    0.090    0.095
2006 10 06    0.095    0.098    0.094    0.097
2006 10 09    0.097    0.097    0.094    0.097
2006 10 10    0.095    0.095    0.080    0.080
2006 10 11    0.080    0.080    0.041    0.064
2006 10 12    0.064    0.070    0.050    0.064
2006 10 13    0.070    0.080    0.067    0.080
2006 10 16    0.085    0.085    0.070    0.085
2006 10 17    0.080    0.090    0.070    0.089
2006 10 18    0.095    0.113    0.088    0.105
2006 10 19    0.110    0.140    0.105    0.125
2006 10 20    0.125    0.130    0.110    0.110
2006 10 23    0.115    0.115    0.100    0.100
2006 10 24    0.100    0.105    0.095    0.105
2006 10 25    0.120    0.120    0.102    0.110
2006 10 26    0.110    0.120    0.110    0.120
2006 10 27    0.120    0.120    0.105    0.120
2006 10 30    0.120    0.120    0.108    0.120
2006 10 31    0.115    0.120    0.110    0.120
2006 11 01    0.120    0.140    0.120    0.135
2006 11 02    0.135    0.140    0.120    0.125
2006 11 03    0.125    0.129    0.120    0.129
2006 11 06    0.129    0.130    0.115    0.115
2006 11 07    0.120    0.130    0.115    0.130
2006 11 08    0.130    0.130    0.120    0.130
2006 11 09    0.130    0.130    0.120    0.125
2006 11 10    0.120    0.130    0.115    0.125
2006 11 13    0.125    0.125    0.115    0.120
2006 11 14    0.115    0.120    0.115    0.115
2006 11 15    0.115    0.125    0.115    0.120
2006 11 16    0.125    0.130    0.121    0.127
2006 11 17    0.125    0.130    0.122    0.130
2006 11 20    0.125    0.130    0.120    0.130
2006 11 21    0.125    0.130    0.125    0.130
2006 11 22    0.139    0.145    0.130    0.130
2006 11 24    0.140    0.144    0.130    0.144
2006 11 27    0.144    0.144    0.135    0.135
2006 11 28    0.149    0.152    0.140    0.150
2006 11 29    0.151    0.178    0.150    0.167
2006 11 30    0.178    0.200    0.170    0.200
2006 12 01    0.210    0.222    0.150    0.170
2006 12 04    0.155    0.170    0.147    0.150
2006 12 05    0.155    0.190    0.150    0.180
2006 12 06    0.180    0.195    0.170    0.170
2006 12 07    0.170    0.170    0.160    0.165
2006 12 08    0.170    0.180    0.160    0.180
2006 12 11    0.170    0.180    0.165    0.170
2006 12 12    0.165    0.180    0.165    0.165
2006 12 13    0.180    0.180    0.175    0.175
2006 12 14    0.175    0.180    0.165    0.172
2006 12 15    0.180    0.181    0.170    0.177
2006 12 18    0.180    0.190    0.167    0.167
2006 12 19    0.175    0.180    0.165    0.165
2006 12 20    0.168    0.180    0.165    0.170
2006 12 21    0.170    0.180    0.165    0.165
2006 12 22    0.200    0.170    0.200    0.202
2006 12 26    0.206    0.210    0.175    0.185
2006 12 27    0.175    0.195    0.175    0.180
2006 12 28    0.180    0.190    0.170    0.175
2006 12 29    0.178    0.180    0.165    0.170
2007 01 03    0.165    0.180    0.150    0.160
2007 01 04    0.150    0.160    0.140    0.145
2007 01 05    0.145    0.148    0.135    0.148
2007 01 06    0.148    0.148    0.148    0.148
2007 01 07    0.148    0.148    0.148    0.148
2007 01 08    0.148    0.150    0.145    0.150
2007 01 09    0.150    0.169    0.150    0.169
2007 01 10    0.170    0.184    0.165    0.180
2007 01 11    0.180    0.185    0.175    0.180
2007 01 12    0.185    0.185    0.165    0.170
)

row =: {
col =: {"1

open    =:    3 col sybd
high    =:    4 col sybd
low     =:    5 col sybd
close   =:    6 col sybd
NB.  Historical Open-High-Low-Close Volitility: Garman and Klass (Yang Zhang)

NB.  List of symbols
NB.  rho  Volatility
NB.  Z    Number of closing prices in a year
NB.  n    Number of historical prices used for the volatility estimate
NB.  Oi   The opening price
NB.  Hi   The High
NB.  Li   The Low
NB.  Ci   The Close

NB.  Simulated open, close, high and low data

open  =: 1 2 3 4 5 6 7 8 9
close =: 10 20 30 40 50 60 70 80 90
high  =: 15 25 35 45 55 65 75 85 95
low   =: 1 2 3 4 5 6 7 8 9

NB. load './sybd.ijs'

openData  =: open
closeData =: close
highData  =: high
lowData   =: low

Z  =: 256
n  =: _1 + # closeData
Ci =: close
Co =: open
Hi =: high
Lo =: low

NB. square the natural logrithm of the left argument devided by the right 
argument
lnSquare =: 4 : 0
  *: ^. (  x % y )
)

vol =: %: (( Z % n ) * ( +/ ( 0 ) + (0.5 * ( Hi lnSquare Lo )) - ((( 2 * ^. 2) 
-1 ) * ( Ci lnSquare Co )) ))


diffLn =: 4 : 0
   ( ^. x ) - ^. y
)

u =: high diffLn low
d =: low diffLn open 
c =: ^. close

NB. Yang Zhang

Oi        =: ( }. openData )
Ci        =: ( }. closeData )
Ci_1      =: ( }: closeData )
Hi        =: ( }. highData )
Li        =: ( }. lowData )

ln         =: ^.      NB. produce natural log of a number

k =: 0.34 % ( 1 + ( ( n + 1 ) % ( n - 1 )))  NB. OK

rho2_rs =: ( Z % n ) * ( +/ ((ln( Hi % Ci)) * (ln( Hi % Oi ))) + ( ln (Li % 
Ci)) * ( ln ( Li % Oi)))  

u_close =: ( 1 % n) * ( +/ ln ( Ci % Oi))

rho2_close =: (Z %( n-1)) * ( +/ *: ( ln ( Ci % Oi) - u_close ))

u_open =: (1 % n ) * ( +/ (ln Oi % Ci_1))

rho2_open_square =: (Z % ( n - 1 )) * ( +/ *: ( ln ((  Oi  ) % ( Ci_1 ))))

rho_squared =: rho2_open_square + ( k * rho2_close ) + (( 1 - k) * rho2_rs) 

echo rho_squared
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to