Change this VWAP so it can be anchored

dsvitale

New member
VIP
Can anyone please improve this VWAP so the beginning date can be changed
Ruby:
input StDev1 = 1.0;
input StDev2 = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
input VWAP_Color = {default SlopeBasic, SlopeSignal, Single};

input Background_Color_Signal = {default Cloud, Fast_Lane, Both, None};
def show_cloud = if background_color_signal == background_color_signal."Cloud" or background_color_signal == background_color_signal."Both" then 1 else 0;
def show_fast_lane = if background_color_signal == background_color_signal."Fast_Lane" or background_color_signal == background_color_signal."Both" then 1 else 0;

input Show_Dev_Signal = no;
input Tolerance_Band_Width = 0.0;
def ToleranceTrue = Tolerance_Band_Width > 0.0;

input High_or_Low_of_x_bars = 0;
def high_low_threshold = 0.25;
def HLBarLength = if High_or_Low_of_x_bars == 0 then Double.NaN else High_or_Low_of_x_bars;

def Show_Day_Divider = Yes;
def Market_Open = RegularTradingStart(GetYYYYMMDD());
def Equities_Futures = if ticksize() * tickvalue() == 0.0001 then 1 else 0;
def tooearly = 0;
#if Equities_Futures and GetTime() <= Market_Open + 1200000 then 1 else 0;
def cap = GetAggregationPeriod();
def errorInAggregation = timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
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 NewDay = CompoundValue(1, periodIndx != periodIndx[1], yes);
def highestDays = TotalSum(NewDay);

rec VolumeSum;
rec MoneySum;
rec volumeVwap2Sum;

if (NewDay) {
VolumeSum = volume;
MoneySum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
VolumeSum = CompoundValue(1, VolumeSum[1] + volume, volume);
MoneySum = CompoundValue(1, MoneySum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}

def VW = MoneySum / VolumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / VolumeSum - Sqr(VW), 0));

def slopelength = 5;
def slopelookback = 8;
def slope = ((VW - VW[slopelength]) / slopelength) / close * 100000;

def slopeup = if slope[slopelookback] > 1 then 1 else 0;
def slopedn = if slope[slopelookback] < -1 then 1 else 0;
def nonslope = if !slopeup and !slopedn then 1 else 0;

def v_counter_low = fold index = 1 to 90 with v_tempvar = 0 while v_tempvar == 0 do if low[index] < low then index else 0;
def v_counter_high = fold index2 = 1 to 90 with v_tempvar2 = 0 while v_tempvar2 == 0 do if high[index2] > high then index2 else 0;


plot VWAP;
plot UpperBand_2;
plot LowerBand_2;
plot UpperBand_1;
plot LowerBand_1;
plot OuterBand_H;
plot InnerBand_H;
plot InnerBand_L;
plot OuterBand_L;

if (!errorInAggregation) {
VWAP = VW;
UpperBand_2 = VW + StDev2 * deviation;
LowerBand_2 = VW - StDev2 * deviation;
UpperBand_1 = VW + StDev1 * deviation;
LowerBand_1 = VW - StDev1 * deviation;
OuterBand_H = VW + ((1 + Tolerance_Band_Width) * deviation);
InnerBand_H = VW + ((1 - Tolerance_Band_Width) * deviation);
InnerBand_L = VW - ((1 - Tolerance_Band_Width) * deviation);
OuterBand_L = VW - ((1 + Tolerance_Band_Width) * deviation);
} else {
VWAP = Double.NaN;
UpperBand_2 = Double.NaN;
LowerBand_2 = Double.NaN;
UpperBand_1 = Double.NaN;
LowerBand_1 = Double.NaN;
OuterBand_H = Double.NaN;
InnerBand_H = Double.NaN;
InnerBand_L = Double.NaN;
OuterBand_L = Double.NaN;
}

UpperBand_2.AssignValueColor(if tooearly then Color.DARK_GRAY else if Show_Dev_Signal and slope >= 0 and slopeup then Color.GREEN else Color.GRAY);
UpperBand_2.SetDefaultColor(Color.GRAY);
UpperBand_2.HideBubble();
UpperBand_2.HideTitle();

OuterBand_H.SetDefaultColor(Color.GRAY);
OuterBand_H.HideBubble();
OuterBand_H.HideTitle();
OuterBand_H.SetHiding(!ToleranceTrue);

UpperBand_1.AssignValueColor(if tooearly then Color.DARK_GRAY else Color.WHITE);
UpperBand_1.SetDefaultColor(Color.WHITE);
UpperBand_1.SetStyle(Curve.SHORT_DASH);
UpperBand_1.HideBubble();
UpperBand_1.HideTitle();

InnerBand_H.SetDefaultColor(Color.GRAY);
InnerBand_H.HideBubble();
InnerBand_H.HideTitle();
InnerBand_H.SetHiding(!ToleranceTrue);

VWAP.SetDefaultColor(Color.CYAN);
VWAP.DefineColor("VWAP Ascending", Color.ORANGE);
VWAP.DefineColor("VWAP Descending", Color.MAGENTA);
VWAP.DefineColor("Above VWAP",CreateColor(0,199,145));
VWAP.DefineColor("Below VWAP",Color.DARK_RED);
VWAP.DefineColor("Fast Lane +", Color.CYAN);
VWAP.DefineColor("Fast Lane -",CreateColor(150,0,50));

VWAP.AssignValueColor(if VWAP_Color == VWAP_Color.SlopeSignal and slopeup and slopeup[1] and slopeup[2] and slopeup[3] and slopeup[4] then Color.CYAN
else if VWAP_Color == VWAP_Color.SlopeSignal and slopedn and slopedn[1] and slopedn[2] and slopedn[3] and slopedn[4] then Color.MAGENTA
else if VWAP_Color == VWAP_Color.SlopeBasic and slope > 0 then VWAP.Color("VWAP Ascending")
else if VWAP_Color == VWAP_Color.SlopeBasic and slope <= 0 then VWAP.Color("VWAP Descending")
else if VWAP_Color == VWAP_Color.Single then color.CYAN
else Color.WHITE);

InnerBand_L.SetDefaultColor(Color.GRAY);
InnerBand_L.HideBubble();
InnerBand_L.HideTitle();
InnerBand_L.SetHiding(!ToleranceTrue);

LowerBand_1.AssignValueColor(if tooearly then Color.DARK_GRAY else Color.WHITE);
LowerBand_1.SetDefaultColor(Color.WHITE);
LowerBand_1.SetStyle(Curve.SHORT_DASH);
LowerBand_1.HideBubble();
LowerBand_1.HideTitle();

OuterBand_L.SetDefaultColor(Color.GRAY);
OuterBand_L.HideBubble();
OuterBand_L.HideTitle();
OuterBand_L.SetHiding(!ToleranceTrue);

LowerBand_2.AssignValueColor(if tooearly then Color.DARK_GRAY else if Show_Dev_Signal and slope < 0 and slopedn then Color.RED else Color.GRAY);
LowerBand_2.SetDefaultColor(Color.GRAY);
LowerBand_2.HideBubble();
LowerBand_2.HideTitle();

plot counter_low = if !isNaN(HLBarLength) and v_counter_low > HLBarLength and (between(close,VW-high_low_threshold*deviation,VW+high_low_threshold*deviation) or between(close,VW-(1+high_low_threshold)*deviation,VW-(1-high_low_threshold)*deviation) or between(close,VW+(1-high_low_threshold)*deviation,VW+(1+high_low_threshold)*deviation)) then v_counter_low else Double.NaN;
counter_low.SetPaintingStrategy(paintingstrategy.VALUES_BELOW);
counter_low.SetDefaultColor(color.RED);
counter_low.HideBubble();
counter_low.HideTitle();

plot counter_high = if !isNaN(HLBarLength) and v_counter_high > HLBarLength and (between(close,VW-high_low_threshold*deviation,VW+high_low_threshold*deviation) or between(close,VW-(1+high_low_threshold)*deviation,VW-(1-high_low_threshold)*deviation) or between(close,VW+(1-high_low_threshold)*deviation,VW+(1+high_low_threshold)*deviation)) then v_counter_high else Double.NaN;
counter_high.SetPaintingStrategy(paintingStrategy.VALUES_ABOVE);
counter_high.SetDefaultColor(color.GREEN);
counter_high.HideBubble();
counter_high.HideTitle();

def var_close = if Show_Cloud then close else Double.NaN;
AddCloud(var_close, VW, VWAP.Color("Above VWAP"), VWAP.Color("Below VWAP"));

def var_VWoneup = if Show_Fast_Lane then UpperBand_1 else Double.NaN;
def var_VWonedn = if Show_Fast_Lane then LowerBand_1 else Double.NaN;

AddCloud(close, var_VWoneup, VWAP.Color("Fast Lane +"),color.BLACK);
AddCloud(close, var_VWonedn, Color.BLACK, VWAP.Color("Fast Lane -"));

AddVerticalLine(if GetAggregationPeriod() < AggregationPeriod.DAY and Show_Day_Divider then if GetTime() == Market_Open and !isNaN(close) then yes else if GetTime() <= Market_Open + 60000 and !isNaN(close) and periodIndx != periodIndx[1] then yes else no else no,"",CreateColor(40,40,40),curve.FIRM);
 
Last edited by a moderator:
Solution
Can anyone please improve this VWAP so the beginning date can be changed
This will start the VWAP at the:

input timeFrame = {default DATE, DAY, WEEK, MONTH};
input date_begin = 20230920;
case DATE:
periodindx = getyyyYMMDD() >= date_begin;

Screenshot 2023-09-25 141342.png
Code:
input StDev1 = 1.0;
input StDev2 = 2.0;
input timeFrame = {default DATE, DAY, WEEK, MONTH};
input date_begin = 20230920;
input VWAP_Color = {default SlopeBasic, SlopeSignal, Single};

input Background_Color_Signal = {default Cloud, Fast_Lane, Both, None};
def show_cloud = if background_color_signal == background_color_signal."Cloud" or background_color_signal == background_color_signal."Both" then 1 else 0;
def show_fast_lane = if...
Can anyone please improve this VWAP so the beginning date can be changed
This will start the VWAP at the:

input timeFrame = {default DATE, DAY, WEEK, MONTH};
input date_begin = 20230920;
case DATE:
periodindx = getyyyYMMDD() >= date_begin;

Screenshot 2023-09-25 141342.png
Code:
input StDev1 = 1.0;
input StDev2 = 2.0;
input timeFrame = {default DATE, DAY, WEEK, MONTH};
input date_begin = 20230920;
input VWAP_Color = {default SlopeBasic, SlopeSignal, Single};

input Background_Color_Signal = {default Cloud, Fast_Lane, Both, None};
def show_cloud = if background_color_signal == background_color_signal."Cloud" or background_color_signal == background_color_signal."Both" then 1 else 0;
def show_fast_lane = if background_color_signal == background_color_signal."Fast_Lane" or background_color_signal == background_color_signal."Both" then 1 else 0;

input Show_Dev_Signal = no;
input Tolerance_Band_Width = 0.0;
def ToleranceTrue = Tolerance_Band_Width > 0.0;

input High_or_Low_of_x_bars = 0;
def high_low_threshold = 0.25;
def HLBarLength = if High_or_Low_of_x_bars == 0 then Double.NaN else High_or_Low_of_x_bars;

def Show_Day_Divider = Yes;
def Market_Open = RegularTradingStart(GetYYYYMMDD());
def Equities_Futures = if ticksize() * tickvalue() == 0.0001 then 1 else 0;
def tooearly = 0;
#if Equities_Futures and GetTime() <= Market_Open + 1200000 then 1 else 0;
def cap = GetAggregationPeriod();
def errorInAggregation = timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
def yyyyMmDd = GetYYYYMMDD();

def periodIndx;
switch (timeFrame) {
case DATE:
periodindx = getyyyYMMDD() >= date_begin;
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
def NewDay = CompoundValue(1, periodIndx != periodIndx[1], yes);
def highestDays = TotalSum(NewDay);

rec VolumeSum;
rec MoneySum;
rec volumeVwap2Sum;

if (NewDay) {
VolumeSum = volume;
MoneySum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
VolumeSum = CompoundValue(1, VolumeSum[1] + volume, volume);
MoneySum = CompoundValue(1, MoneySum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}

def VW = MoneySum / VolumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / VolumeSum - Sqr(VW), 0));

def slopelength = 5;
def slopelookback = 8;
def slope = ((VW - VW[slopelength]) / slopelength) / close * 100000;

def slopeup = if slope[slopelookback] > 1 then 1 else 0;
def slopedn = if slope[slopelookback] < -1 then 1 else 0;
def nonslope = if !slopeup and !slopedn then 1 else 0;

def v_counter_low = fold index = 1 to 90 with v_tempvar = 0 while v_tempvar == 0 do if low[index] < low then index else 0;
def v_counter_high = fold index2 = 1 to 90 with v_tempvar2 = 0 while v_tempvar2 == 0 do if high[index2] > high then index2 else 0;


plot VWAP;
plot UpperBand_2;
plot LowerBand_2;
plot UpperBand_1;
plot LowerBand_1;
plot OuterBand_H;
plot InnerBand_H;
plot InnerBand_L;
plot OuterBand_L;

if (!errorInAggregation) {
VWAP = VW;
UpperBand_2 = VW + StDev2 * deviation;
LowerBand_2 = VW - StDev2 * deviation;
UpperBand_1 = VW + StDev1 * deviation;
LowerBand_1 = VW - StDev1 * deviation;
OuterBand_H = VW + ((1 + Tolerance_Band_Width) * deviation);
InnerBand_H = VW + ((1 - Tolerance_Band_Width) * deviation);
InnerBand_L = VW - ((1 - Tolerance_Band_Width) * deviation);
OuterBand_L = VW - ((1 + Tolerance_Band_Width) * deviation);
} else {
VWAP = Double.NaN;
UpperBand_2 = Double.NaN;
LowerBand_2 = Double.NaN;
UpperBand_1 = Double.NaN;
LowerBand_1 = Double.NaN;
OuterBand_H = Double.NaN;
InnerBand_H = Double.NaN;
InnerBand_L = Double.NaN;
OuterBand_L = Double.NaN;
}

UpperBand_2.AssignValueColor(if tooearly then Color.DARK_GRAY else if Show_Dev_Signal and slope >= 0 and slopeup then Color.GREEN else Color.GRAY);
UpperBand_2.SetDefaultColor(Color.GRAY);
UpperBand_2.HideBubble();
UpperBand_2.HideTitle();

OuterBand_H.SetDefaultColor(Color.GRAY);
OuterBand_H.HideBubble();
OuterBand_H.HideTitle();
OuterBand_H.SetHiding(!ToleranceTrue);

UpperBand_1.AssignValueColor(if tooearly then Color.DARK_GRAY else Color.WHITE);
UpperBand_1.SetDefaultColor(Color.WHITE);
UpperBand_1.SetStyle(Curve.SHORT_DASH);
UpperBand_1.HideBubble();
UpperBand_1.HideTitle();

InnerBand_H.SetDefaultColor(Color.GRAY);
InnerBand_H.HideBubble();
InnerBand_H.HideTitle();
InnerBand_H.SetHiding(!ToleranceTrue);

VWAP.SetDefaultColor(Color.CYAN);
VWAP.DefineColor("VWAP Ascending", Color.ORANGE);
VWAP.DefineColor("VWAP Descending", Color.MAGENTA);
VWAP.DefineColor("Above VWAP",CreateColor(0,199,145));
VWAP.DefineColor("Below VWAP",Color.DARK_RED);
VWAP.DefineColor("Fast Lane +", Color.CYAN);
VWAP.DefineColor("Fast Lane -",CreateColor(150,0,50));

VWAP.AssignValueColor(if VWAP_Color == VWAP_Color.SlopeSignal and slopeup and slopeup[1] and slopeup[2] and slopeup[3] and slopeup[4] then Color.CYAN
else if VWAP_Color == VWAP_Color.SlopeSignal and slopedn and slopedn[1] and slopedn[2] and slopedn[3] and slopedn[4] then Color.MAGENTA
else if VWAP_Color == VWAP_Color.SlopeBasic and slope > 0 then VWAP.Color("VWAP Ascending")
else if VWAP_Color == VWAP_Color.SlopeBasic and slope <= 0 then VWAP.Color("VWAP Descending")
else if VWAP_Color == VWAP_Color.Single then color.CYAN
else Color.WHITE);

InnerBand_L.SetDefaultColor(Color.GRAY);
InnerBand_L.HideBubble();
InnerBand_L.HideTitle();
InnerBand_L.SetHiding(!ToleranceTrue);

LowerBand_1.AssignValueColor(if tooearly then Color.DARK_GRAY else Color.WHITE);
LowerBand_1.SetDefaultColor(Color.WHITE);
LowerBand_1.SetStyle(Curve.SHORT_DASH);
LowerBand_1.HideBubble();
LowerBand_1.HideTitle();

OuterBand_L.SetDefaultColor(Color.GRAY);
OuterBand_L.HideBubble();
OuterBand_L.HideTitle();
OuterBand_L.SetHiding(!ToleranceTrue);

LowerBand_2.AssignValueColor(if tooearly then Color.DARK_GRAY else if Show_Dev_Signal and slope < 0 and slopedn then Color.RED else Color.GRAY);
LowerBand_2.SetDefaultColor(Color.GRAY);
LowerBand_2.HideBubble();
LowerBand_2.HideTitle();

plot counter_low = if !isNaN(HLBarLength) and v_counter_low > HLBarLength and (between(close,VW-high_low_threshold*deviation,VW+high_low_threshold*deviation) or between(close,VW-(1+high_low_threshold)*deviation,VW-(1-high_low_threshold)*deviation) or between(close,VW+(1-high_low_threshold)*deviation,VW+(1+high_low_threshold)*deviation)) then v_counter_low else Double.NaN;
counter_low.SetPaintingStrategy(paintingstrategy.VALUES_BELOW);
counter_low.SetDefaultColor(color.RED);
counter_low.HideBubble();
counter_low.HideTitle();

plot counter_high = if !isNaN(HLBarLength) and v_counter_high > HLBarLength and (between(close,VW-high_low_threshold*deviation,VW+high_low_threshold*deviation) or between(close,VW-(1+high_low_threshold)*deviation,VW-(1-high_low_threshold)*deviation) or between(close,VW+(1-high_low_threshold)*deviation,VW+(1+high_low_threshold)*deviation)) then v_counter_high else Double.NaN;
counter_high.SetPaintingStrategy(paintingStrategy.VALUES_ABOVE);
counter_high.SetDefaultColor(color.GREEN);
counter_high.HideBubble();
counter_high.HideTitle();

def var_close = if Show_Cloud then close else Double.NaN;
AddCloud(var_close, VW, VWAP.Color("Above VWAP"), VWAP.Color("Below VWAP"));

def var_VWoneup = if Show_Fast_Lane then UpperBand_1 else Double.NaN;
def var_VWonedn = if Show_Fast_Lane then LowerBand_1 else Double.NaN;

AddCloud(close, var_VWoneup, VWAP.Color("Fast Lane +"),color.BLACK);
AddCloud(close, var_VWonedn, Color.BLACK, VWAP.Color("Fast Lane -"));

AddVerticalLine(if GetAggregationPeriod() < AggregationPeriod.DAY and Show_Day_Divider then if GetTime() == Market_Open and !isNaN(close) then yes else if GetTime() <= Market_Open + 60000 and !isNaN(close) and periodIndx != periodIndx[1] then yes else no else no,"",CreateColor(40,40,40),curve.FIRM);
 
Last edited:
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
498 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top