germanburrito
Active member
Hello! I haven’t been here in a while, but here’s the idea:
I have two indicators, TWAP (Time-Weighted Average Price) and VWAP (Volume-Weighted Average Price). I’m trying to create a cumulative indicator to track whether VWAP is too far away from TWAP over time.
here is the starting code
I have two indicators, TWAP (Time-Weighted Average Price) and VWAP (Volume-Weighted Average Price). I’m trying to create a cumulative indicator to track whether VWAP is too far away from TWAP over time.
- TWAP calculates the price across time.
- VWAP calculates the price with volume considered across time.
here is the starting code
Code:
input price = ohlc4;
input timeFrame = {default Day, Week, Month, Chart};
# Define the period index for the selected timeframe
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);
case Chart:
periodIndx = 0;
}
# Detect new day
def newDay = CompoundValue(1, periodIndx != periodIndx[1], yes);
def isCloseTime = SecondsTillTime(1600) == 0;
# TWAP Calculation
def cumePrice = if newDay then price else price + cumePrice[1];
def cumeBarNumber = if newDay then 1 else 1 + cumeBarNumber[1];
def TWAP_Price = Round(cumePrice / cumeBarNumber, 4);
plot TWAP = TWAP_Price;
# VWAP Calculation
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
def volumeSum;
def volumeVWAPSum;
if (isPeriodRolled) {
volumeSum = volume;
volumeVWAPSum = volume * vwap;
} else {
volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
volumeVWAPSum = CompoundValue(1, volumeVWAPSum[1] + volume * vwap, volume * vwap);
}
def VWAP = volumeVWAPSum / volumeSum;
plot VWAP_Plot = VWAP;
# Difference Calculation (VWAP - TWAP) at the close
def dailyDifference = if isCloseTime then VWAP - TWAP else Double.NaN;
def lastValidDifference = if !IsNaN(dailyDifference) then dailyDifference else lastValidDifference[1];
# Accumulated Total Difference
def totalDifference = CompoundValue(1, if newDay then totalDifference[1] + lastValidDifference else totalDifference[1], 0);
# Debugging Labels
AddLabel(yes, "Daily Difference (VWAP - TWAP): " + AsText(lastValidDifference), if lastValidDifference >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "Accumulated Total: " + AsText(totalDifference), if totalDifference >= 0 then Color.GREEN else Color.RED);
# Debugging Plots (Temporary for Verification)
plot DebugDailyDiff = if !IsNaN(dailyDifference) then dailyDifference else Double.NaN;
DebugDailyDiff.SetDefaultColor(Color.MAGENTA);
DebugDailyDiff.SetLineWeight(2);
plot DebugTotalDiff = totalDifference;
DebugTotalDiff.SetDefaultColor(Color.YELLOW);
DebugTotalDiff.SetLineWeight(2);
# Styling
VWAP_Plot.SetDefaultColor(Color.BLUE);
TWAP.SetDefaultColor(Color.ORANGE);
AddCloud(VWAP, TWAP, Color.GREEN, Color.RED);
VWAP_Plot.SetLineWeight(2);
TWAP.SetLineWeight(2);