Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
Stdev = Sample Standard Deviation
Ln = Natural Logarithm
Sqrt = Square Root

volatility Index = Stdev(Ln(P1/P0), Ln(P2/P1), ..., Ln(P30/P29)) *Sqrt(365)

i want to write this code on a data frame like this

	Date	Twap
0	2013-10-01	123.8187
1	2013-10-02	124.62557999999999
2	2013-10-03	110.758455
3	2013-10-04	113.2481625
4	2013-10-05	119.98882750000001
5	2013-10-06	121.10016250000001
6	2013-10-07	121.218495
7	2013-10-08	122.454705
8	2013-10-09	123.61454
9	2013-10-10	125.46166249999999
10	2013-10-11	125.4478275
11	2013-10-12	125.09333
12	2013-10-13	126.1209575
13	2013-10-14	130.7805775
14	2013-10-15	133.6325575
15	2013-10-16	138.130205
16	2013-10-17	140.57558
17	2013-10-18	140.68141250000002
18	2013-10-19	147.66287
19	2013-10-20	158.1210375
20	2013-10-21	162.220495
21	2013-10-22	170.8828675
22	2013-10-23	182.96707750000002
23	2013-10-24	194.289745
24	2013-10-25	185.277995
25	2013-10-26	175.5104125
26	2013-10-27	176.9478725
27	2013-10-28	182.0395825
28	2013-10-29	189.65937
29	2013-10-30	195.04937
30	2013-10-31	196.262995
31	2013-11-01	196.918245
32	2013-11-02	197.89224750000002
33	2013-11-03	199.8566625
34	2013-11-04	205.5794575
35	2013-11-05	217.89729
36	2013-11-06	235.860705
37	2013-11-07	254.05082749999997
38	2013-11-08	277.9382
39	2013-11-09	315.68937
40	2013-11-10	312.36775
41	2013-11-11	293.71074500000003
42	2013-11-12	321.64056
43	2013-11-13	347.718685
44	2013-11-14	380.0384975
45	2013-11-15	402.89374999999995
46	2013-11-16	410.94737250000003
47	2013-11-17	427.670185
48	2013-11-18	474.050745
49	2013-11-19	619.415935
50	2013-11-20	584.2309325
51	2013-11-21	528.8384325
52	2013-11-22	633.0808724999999
53	2013-11-23	720.7646225
54	2013-11-24	758.8703725
55	2013-11-25	752.5100575
56	2013-11-26	772.9409975
57	2013-11-27	843.5803125
58	2013-11-28	915.7935625
59	2013-11-29	1000.1489
60	2013-11-30	1102.253675
61	2013-12-01	1127.42
62	2013-12-02	1000.675655
63	2013-12-03	1007.0671624999999
64	2013-12-04	1053.5350274999998
65	2013-12-05	1107.1964725
66	2013-12-06	1039.6750299999999
67	2013-12-07	864.8679325
68	2013-12-08	708.4128499999999
69	2013-12-09	765.0535
70	2013-12-10	879.3037400000001
71	2013-12-11	947.4553675
72	2013-12-12	910.3923375
73	2013-12-13	885.056975
74	2013-12-14	898.4179125
75	2013-12-15	871.095485
76	2013-12-16	855.9061925
77	2013-12-17	758.7809275
78	2013-12-18	678.12831
79	2013-12-19	571.078495
80	2013-12-20	625.6539124999999
81	2013-12-21	668.4366125
82	2013-12-22	619.480035
83	2013-12-23	627.3999
84	2013-12-24	659.2937299999999
85	2013-12-25	663.205455
86	2013-12-26	683.3997850000001
87	2013-12-27	734.7544125
88	2013-12-28	747.228785
89	2013-12-29	727.71843
90	2013-12-30	736.0334125
91	2013-12-31	751.1306775
92	2014-01-01	754.6834125
93	2014-01-02	765.5737025000001
94	2014-01-03	791.9052525000001
95	2014-01-04	813.8303324999999
96	2014-01-05	841.7824925
97	2014-01-06	918.7571275
98	2014-01-07	967.7621549999999
99	2014-01-08	894.2876549999999
100	2014-01-09	856.2382775
101	2014-01-10	850.7965899999999
102	2014-01-11	869.8508075
103	2014-01-12	906.5073674999999
104	2014-01-13	892.106
105	2014-01-14	857.8113925000001
106	2014-01-15	856.7985974999999
107	2014-01-16	867.3681675
108	2014-01-17	859.29376
109	2014-01-18	835.4024975
110	2014-01-19	839.4864524999999
111	2014-01-20	860.06041
112	2014-01-21	873.8656225
113	2014-01-22	871.3352
114	2014-01-23	866.7214099999999
115	2014-01-24	851.0422475
116	2014-01-25	830.4061375
117	2014-01-26	850.66715
118	2014-01-27	874.0466025000001

What I have tried:

<pre>p = data[['Date','Twap']] 

a=np.array([])

def volatility(x):
    for i in range(30-1):
        np.append(a,np.log(x['Twap'][i+1]/x['Twap'][i]))

p.rolling(window = 30).apply(volatility)


but I get an error what should I do to be able to continue my calculations sorry if my question is naive I'm not a programmer
Posted
Comments
Richard MacCutchan 12-Feb-21 3:47am    
"but I get an error"
Then please show the full details of the error and explain where it occurs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900