View difference between Paste ID: svbp0zma and 2ShqEvS4
SHOW: | | - or go back to the newest paste.
1
### Expected Move Labels ###
2
# This script will add expected move labels, #
3
# in dollars, to charts. IT IS INTENDED TO BE#
4
# USED ONLY ON CHARTS WITH DAILY BARS. The   #
5
# math will not work one charts with other   #
6
# settings.                                  #
7
8
### Settings for coloring ###
9
10
input customWindow = 50;
11
input violationMetricHigh = high;
12
input violationMetricLow = low;
13
input centerPoint = close;
14
15
def dayCountBacktest = 3;
16
def weekCountBacktest = 5;
17
def monthCountBacktest = 22;
18
def customWindowBacktest = customWindow - (RoundDown(customWindow / 7, 0) * 2);
19
def customWindowDanger = RoundUp(customWindowBacktest * 0.32, 0);
20
def customWindowWarning = RoundDown(customWindowBacktest * 0.16, 0);
21
def dayCountDanger = RoundUp(dayCountBacktest * 0.32, 0);
22
def dayCountWarning = RoundDown(dayCountBacktest * 0.16, 0);
23
def weekCountDanger = RoundUp(weekCountBacktest * 0.32, 0);
24
def weekCountWarning = RoundDown(weekCountBacktest * 0.16, 0);
25
def monthCountDanger = RoundUp(monthCountBacktest * 0.32, 0);
26
def monthCountWarning = RoundDown(monthCountBacktest * 0.16, 0);
27
28
script ExMove {
29
    input center = 0;
30
    input window = 1;
31
32
    def IVBase = imp_volatility();
33
34
    plot ExMove = center * IVBase * Sqrt(window / 365);
35
}
36
37
### Calculate the expected move ###
38
def ExMoveOneDay = ExMove(centerPoint, 1);
39
def ExMoveOneWeek = ExMove(centerPoint, 7);
40
def ExMoveOneMonth = ExMove(centerPoint, 30);
41
def ExMoveCustom = ExMove(centerPoint, customWindow);
42
43
### Determine violations ###
44
def dayBacktestCenter = (Average(close, dayCountBacktest) + Average(close, RoundUp(dayCountBacktest/2, 0)) + Average(close, RoundUp(dayCountBacktest/4, 0)))/3;
45
def dayCount = fold i = 1 to dayCountBacktest with countA = 0 
46
do 
47
  if violationMetricHigh[i] > (dayBacktestCenter + ExMoveOneDay) or violationMetricLow[i] < (dayBacktestCenter - ExMoveOneDay) then 
48
    countA + 1
49
  else
50
    countA;
51
52
53
def weekBacktestCenter = (Average(close, weekCountBacktest) + Average(close, RoundUp(weekCountBacktest/2, 0)) + Average(close, RoundUp(weekCountBacktest/4, 0)))/3;
54
def weekCount = fold n = 1 to weekCountBacktest with countB = 0 
55
do 
56
  if violationMetricHigh[n] > (weekBacktestCenter + ExMoveOneWeek) or violationMetricLow[n] < (weekBacktestCenter - ExMoveOneWeek) then 
57
    countB + 1
58
  else
59
    countB;
60
61
def monthBacktestCenter = (Average(close, monthCountBacktest) + Average(close, RoundUp(monthCountBacktest/2, 0)) + Average(close, RoundUp(monthCountBacktest/4, 0)))/3;
62
def monthCount = fold m = 1 to monthCountBacktest with countC = 0 
63
do 
64
  if violationMetricHigh[m] > (monthBacktestCenter + ExMoveOneMonth) or violationMetricLow[m] < (monthBacktestCenter - ExMoveOneMonth) then 
65
    countC + 1
66
  else
67
    countC;
68
69
def customBacktestCenter = (Average(close, customWindowBacktest) + Average(close, RoundUp(customWindowBacktest/2, 0)) + Average(close, RoundUp(customWindowBacktest/4, 0)))/3;
70
def customCount = fold o = 1 to customWindowBacktest with countD = 0 
71
do 
72
  if violationMetricHigh[o] > (customBacktestCenter + ExMoveCustom) or violationMetricLow[o] < (customBacktestCenter - ExMoveCustom) then 
73
    countD + 1
74
  else
75
    countD;
76
77
### Add labels to chart ###
78
AddLabel(yes, "Range at 68% confidence", Color.WHITE);
79
AddLabel(yes, "1 Day: +/-" + AsDollars(ExMoveOneDay) + "(" + dayCount + "/" + dayCountBacktest + ")", if dayCount >= dayCountDanger then Color.RED else if dayCount < dayCountDanger and dayCount > dayCountWarning then Color.YELLOW else Color.GREEN);
80
AddLabel(yes, "1 Week: +/-" + AsDollars(ExMoveOneWeek) + "(" + weekCount + "/" + weekCountBacktest + ")", if weekCount >= weekCountDanger then Color.RED else if weekCount > weekCountWarning and weekCount < weekCountDanger then Color.YELLOW else Color.GREEN);
81
AddLabel(yes, "1 Month: +/-" + AsDollars(ExMoveOneMonth) + "(" + monthCount + "/" + monthCountBacktest + ")", if monthCount >= monthCountDanger then Color.RED else if monthCount > monthCountWarning and monthCount < monthCountDanger then Color.YELLOW else Color.GREEN);
82
AddLabel(yes, customWindow + " Days: +/-" + AsDollars(ExMoveCustom) + "(" + customCount + "/" + customWindowBacktest + ")", if customCount >= customWindowDanger then Color.RED else if customCount > customWindowWarning and customCount < customWindowDanger then Color.YELLOW else Color.GREEN);