forked from gpellis/ThinkOrSwim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXLT_IVAnalysisSTUDY.ThinkScript
59 lines (43 loc) · 1.75 KB
/
XLT_IVAnalysisSTUDY.ThinkScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
declare hide_on_intraday;
declare lower;
#Bad Data Handling
input removeBadTicks = yes;
input badTickFilter = 3.0;
#Historical Volatility Settings
input hvLongLength = 252;
input hvShortLength = 20;
#Range Bands Settings
input upperBandPercent = 20;
input midBandPercent = 20;
input lowerBandPercent = 20;
#Calculate HV
def clLog = log(close / close[1]);
plot hv = stdev(clLog, hvShortLength) * Sqrt(hvShortLength * hvLongLength / (hvShortLength - 1)) * 100;
def highHV = Highest(hv, hvLongLength);
def lowHV = Lowest(hv, hvLongLength);
#Calculate IV
def curIV = if(IsNaN(imp_volatility()) and !IsNaN(hv), -1, imp_volatility());
rec cleanIV = if((curIV between Max((cleanIV[1] - badTickFilter), 0) and (cleanIV[1] + badTickFilter)), curIV, cleanIV[1]);
plot iv = round(if(removeBadTicks, cleanIV, imp_volatility()) * 100);
def totalRange = highHV - lowHV;
#Plot Bands
plot upperBandTop = highHV;
plot upperBandBottom = upperBandTop - (totalRange * (upperBandPercent/100));
plot midBandTop = highHV - (totalRange * ((100 - midBandPercent)/100))/2;
plot midBandBottom = lowHV + (totalRange * ((100 - midBandPercent)/100))/2;
plot lowerBandBottom = lowHV;
plot lowerBandTop = lowerBandBottom + (totalRange * (lowerBandPercent/100));
#Setting default look
hv.SetDefaultColor(color.BLUE);
hv.SetLineWeight(2);
iv.SetDefaultColor(color.RED);
iv.SetLineWeight(2);
upperBandTop.SetDefaultColor(color.GRAY);
AddCloud(upperBandTop, upperBandBottom, color.GREEN);
upperBandBottom.SetDefaultColor(color.GRAY);
midBandTop.SetDefaultColor(color.GRAY);
AddCloud(midBandTop, midBandBottom, color.Yellow);
midBandBottom.SetDefaultColor(color.GRAY);
lowerBandTop.SetDefaultColor(color.GRAY);
AddCloud(lowerBandTop, lowerBandBottom, color.RED);
lowerBandBottom.SetDefaultColor(color.GRAY);