-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathDifference % between PRICE and VWAP
76 lines (59 loc) · 1.99 KB
/
Difference % between PRICE and VWAP
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Difference % between PRICE and VWAP
# Assembled by BenTen at useThinkScript.com
# Converted from https://www.tradingview.com/script/o0mHbrYo-Difference-between-PRICE-and-VWAP-V2/
declare lower;
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = { default DAY, WEEK, MONTH };
def cap = getAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;
if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume* vwap;
volumeVwap2Sum = volume* Sqr(vwap);
} else {
volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume* vwap, volume* vwap);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume* Sqr(vwap), volume* Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
def VWAP = price;
def UpperBand = price + numDevUp * deviation;
def LowerBand = price + numDevDn * deviation;
input src = close;
input len = 36;
def xSMA = vwap;
def nRes = absValue(src - xSMA) * 100 / src;
def nRes3 = simpleMovingAvg(nRes, len);
plot avg = nRes3;
input level1 = 1.28;
input level2 = 2.1;
input level3 = 2.5;
# input level4 = 3.09;
# input level5 = 4.1;
plot ZeroLine = 0;
plot histogram = nRes;
plot l1 = level1;
plot l2 = level2;
plot l3 = level3;
# plot l4 = level4;
# plot l5 = level5;
histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);