Hi,
is there a simpler way to go back to redefine a variable base on the condition? I have to create all the variable twice just to be able to use the specific variable.
Thanks!
is there a simpler way to go back to redefine a variable base on the condition? I have to create all the variable twice just to be able to use the specific variable.
Thanks!
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2011-2023
#
def numDevDn2 = -2.0;
def numDevUp2 = 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 or
timeFrame == timeFrame.MONTH and cap >= AggregationPeriod.QUARTER;
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 UpperBand2 = price + numDevUp2 * deviation;
def LowerBand2 = price + numDevDn2 * deviation;
def LU2 = LowerBand2 == UpperBand2; ############### REASON 1
input numDevDn2A = -2.0;
input numDevUp2A = 2.0;
input timeFrameA = {default DAY, WEEK, MONTH, CUSTOM};
input CTFA = 62; ############### REASON 2
def capA = GetAggregationPeriod();
def errorInAggregationA =
timeFrameA == timeFrameA.DAY and capA >= AggregationPeriod.WEEK or
timeFrameA == timeFrameA.WEEK and capA >= AggregationPeriod.MONTH or
timeFrameA == timeFrameA.MONTH and capA >= AggregationPeriod.QUARTER;
Assert(!errorInAggregationA, "timeFrame should be not less than current chart aggregation period");
def yyyyMmDdA = GetYYYYMMDD();
def periodIndxA;
switch (timeFrameA) {
case DAY:
periodIndxA = yyyyMmDdA;
case WEEK:
periodIndxA = Floor((DaysFromDate(First(yyyyMmDdA)) + GetDayOfWeek(First(yyyyMmDdA))) / 7);
case MONTH:
periodIndxA = roundDown(yyyyMmDdA / 100, 0);
case CUSTOM:
periodIndxA = if LU2 then Floor((DaysFromDate(First(yyyyMmDdA)) + GetDayOfWeek(First(yyyyMmDdA))) / CTFA + 1) else Floor((DaysFromDate(First(yyyyMmDdA)) + GetDayOfWeek(First(yyyyMmDdA))) / CTFA); ############### REASON 3
}
def isPeriodRolledA = CompoundValue(1, periodIndxA != periodIndxA[1], yes);
def volumeSumA;
def volumeVwapSumA;
def volumeVwap2SumA;
if (isPeriodRolledA) {
volumeSumA = volume;
volumeVwapSumA = volume * vwap;
volumeVwap2SumA = volume * Sqr(vwap);
} else {
volumeSumA = CompoundValue(1, volumeSumA[1] + volume, volume);
volumeVwapSumA = CompoundValue(1, volumeVwapSumA[1] + volume * vwap, volume * vwap);
volumeVwap2SumA = CompoundValue(1, volumeVwap2SumA[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def priceA = volumeVwapSumA / volumeSumA;
def deviationA = Sqrt(Max(volumeVwap2SumA / volumeSumA - Sqr(priceA), 0));
plot VWAPA = priceA;
plot UpperBand2A = priceA + numDevUp2A * deviationA;
plot LowerBand2A = priceA + numDevDn2A * deviationA;