Creator Message:
Normal VWAP = (Number of Shares Bought x Typical Price) / Total Volume
In VWOP Calculation, typical price is replaced by selected moving average type or "matype" and then multiplied by the volume .
Then a total value is calculated using math.sum with a length value that changes according to a selected oscillator's value. The total is then divided by
the sum of just volume using the same oscillating length value. Result is then passed through the selected"matype" once more to give the final result.
Indicator designed for use as a entry/exit indicator in conjunction with more traditional moving averages and/or signal filters. Useful for taking volume + an oscillator into account along with price, instead of just the price as with a simple moving average .
CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#https://www.tradingview.com/script/GsMz7gSF-VWOP-Volume-Weighted-Oscillated-Price/
#// © EsIstTurnt
#indicator("Volume Weighted & Oscillated Price",shorttitle = "VWOP",overlay=true)
# converted and mod by Sam4Cok@Samer800 - 03/2023
input BarColor = yes;
input trendStrength = 0.1;
input src = close;
input Length1 = 16;
input Length2 = 24;
input Length3 = 32;
input Length4 = 40;
input oscillatorLength = 16;
input oscillatorType = {RSI, CCI, COG, MFI, TSI, CMO, default COPPOCK, All};
input MovAvgType = {VWAP, SWMA, VWMA, SMA, EMA, RMA, WMA, default LRC};
input curve = {"Algo-1", "Algo-2",default "Algo-3", "Algo-4"};
def na = Double.NaN;
script nz {
input data = close;
input repl = 0;
def ret_val = if !IsNaN(data) then data else repl;
plot return = ret_val;
}
#export f_Vwap(simple string tf, float src, float src_v) =>
script f_Vwap {
input src = close;
def src_v = Volume;
def tf = GetDay();
def start0 = tf - tf[1];
def sumSrc0 = src * src_v;
def sumVol0 = src_v;
def sumSrc1 = CompoundValue(1, if start0 then sumSrc0 else sumSrc0 + sumSrc1[1], sumSrc0);
def sumVol1 = CompoundValue(1, if start0 then sumVol0 else sumVol0 + sumVol1[1], sumVol0);
def Vwap = sumSrc1 / sumVol1;
plot wap = Vwap;
}
#f_cmo(src, length) =>
script cmo {
input src = close;
input length = 16;
def mom = (src-src[1]);
def sm1 = sum(if (mom >= 0) then mom else 0.0, length);
def sm2 = sum(if (mom >= 0) then 0.0 else -mom, length);
def cmo = 100 * (sm1 - sm2) / (sm1 + sm2);
plot out = cmo;
}
# roc(src, length)
script roc {
input src = close;
input length = 14;
def roc = if isNaN(src[length]) then 0 else
if src[length] !=0 then 100 * ((src - src[length])/ src[length]) else 0;
plot return = roc;
}
#vwma(source, length)
script vwma {
input x = close;
input y = 15;
def v = volume;
def VWMA = SimpleMovingAvg(x * nz(v,1), y) / SimpleMovingAvg(nz(v,1), y);
plot result = VWMA;
}
#pine_swma(x) =>
script swma {
input x = close;
def swma = nz(x[3],X[2]) * 1 / 6 + NZ(x[2],X[1]) * 2 / 6 + x[1] * 2 / 6 + x[0] * 1 / 6;
plot return = swma;
}
#cog(source, length) =>
script cog {
input source = close;
input length = 14;
def Msum = Sum(source, length);
def num;
num = fold i = 0 to length with p do
p + source[i] * (i + 1);
plot return = -num / Msum;
}
#vwosc(src,len1=16,len2=24,len3=32,len4=40,osclen=16,string osctype="RSI",matype="LRC",curve="<>")=>
script vwosc {
input src = close;
input len1 = 16;
input len2 = 24;
input len3 = 32;
input len4 = 40;
input osclen = 16;
input osctype = "RSI";
input matype = "LRC";
input curve = "Algo-1";
def nRSI = RSI(Price = src, Length = osclen);
def nCCI = CCI(LENGTH = osclen);
def nCOG = cog(src, osclen);
def nMFI = MoneyFlowIndex(Length = osclen);
def nCMO = CMO(src, osclen);
def nTSI = TrueStrengthIndex(shortLength=osclen,longLength=Round(osclen * 1.5, 0));
def ROC1 = ROC(src, Round(1.1 * osclen, 0));
def ROC2 = ROC(src, Round(1.4 * osclen, 0));
def COPPOCK = WMA(ROC1 + ROC2, osclen);
def all = AbsValue(nRSI[1]-nrsi[len1]) + AbsValue(nCCI[1]-nCCI[len1]) +
AbsValue(nCOG[1]-nCOG[len1]) + AbsValue(nMFI[1]-nMFI[len1]) +
AbsValue(nCMO[1]-nCMO[len1]) + AbsValue(COPPOCK[1]-COPPOCK[len1]);
def osc = if osctype == "All" then all else
if osctype == "RSI" then nRSI else
if osctype == "CCI" then nCCI else
if osctype == "COG" then nCOG else
if osctype == "MFI" then nMFI else
if osctype == "CMO" then nCMO else
if osctype == "TSI" then nTSI else
if osctype == "COPPOCK" then COPPOCK else nRSI;
def max = Highest(osc, 500);
def min = Lowest(osc, 500);
def dif = max - min;
def lev1 =
if curve == "Algo-3" then
if osc - min < 0.25 * dif then len1 else
if osc - min < 0.35 * dif then Round((len1 + len2) / 2, 0) else
if osc - min < 0.45 * dif then len2 else
if osc - min < 0.55 * dif then Round((len2 + len3) / 2, 0) else
if osc - min < 0.65 * dif then len3 else
if osc - min < 0.75 * dif then Round((len3 + len4) / 2, 0) else len4 else
if curve == "Algo-1" then
if osc - min < 0.25 * dif or max - osc < 0.25 * dif then len1 else
if osc - min < 0.35 * dif or max - osc < 0.35 * dif then len2 else
if osc - min < 0.45 * dif or max - osc < 0.45 * dif then len3 else len4 else
if curve == "Algo-2" then
if osc - min < 0.25 * dif or max - osc < 0.25 * dif then len4 else
if osc - min < 0.35 * dif or max - osc < 0.35 * dif then len3 else
if osc - min < 0.45 * dif or max - osc < 0.45 * dif then len2 else len1 else
if curve == "Algo-4" then
if max - osc < 0.25 * dif then len1 else
if max - osc < 0.35 * dif then Round((len1 + len2) / 2, 0) else
if max - osc < 0.45 * dif then len2 else
if max - osc < 0.55 * dif then Round((len2 + len3) / 2, 0) else
if max - osc < 0.65 * dif then len3 else
if max - osc < 0.75 * dif then Round((len3 + len4) / 2) else len4 else lev1[1];
def lev = if IsNaN(lev1) then if lev[1] == 0 then len2 else lev[1] else lev1;
def v = Volume * vwap;
def swma = swma(src) * V;
def fwap = f_Vwap(src) * V;
def vwma = VWMA(src, len1) * V;
def LRC = Inertia(src, len1) * V;
def SMA = Average(src, len1) * V;
def EMA = ExpAverage(src, len1) * V;
def RMA = WildersAverage(src, len1) * V;
def WMA = WMA(src, len1) * V;
def Volsum = fold i = 0 to lev with p do
p + GetValue(V, i);
def WAPsum = fold i1 =0 to lev with p1 do
p1 + GetValue(fwap, i1);
def SWMsum = fold i2 = 0 to lev with p2 do
p2 + GetValue(swma, i2);
def VWMsum = fold i3 = 0 to lev with p3 do
p3 + GetValue(vwma, i3);
def SMAsum = fold i4 = 0 to lev with p4 do
p4 + GetValue(SMA, i4);
def EMAsum = fold i5 = 0 to lev with p5 do
p5 + GetValue(EMA, i5);
def RMAsum = fold i6 = 0 to lev with p6 do
p6 + GetValue(RMA, i6);
def WMAsum = fold i7 = 0 to lev with p7 do
p7 + GetValue(WMA, i7);
def LRCsum = fold i8 = 0 to lev with p8 do
p8 + GetValue(LRC, i8);
def WAPsrc = WAPsum / Volsum;
def SWMsrc = SWMsum / Volsum;
def VWMsrc = VWMsum / Volsum;
def SMAsrc = SMAsum / Volsum;
def EMAsrc = EMAsum / Volsum;
def RMAsrc = RMAsum / Volsum;
def WMAsrc = WMAsum / Volsum;
def LRCsrc = LRCsum / Volsum;
def ma =
if matype == "VWAP" then f_Vwap(WAPsrc) else
if matype == "SWMA" then SWMA(SWMsrc) else
if matype == "VWMA" then vwma(VWMsrc, len2) else
if matype == "SMA" then Average(SMAsrc, len2) else
if matype == "EMA" then ExpAverage(EMAsrc, len2) else
if matype == "RMA" then WildersAverage(RMAsrc, len2) else
if matype == "WMA" then WMA (WMAsrc, len2) else
if matype == "LRC" then Inertia(LRCsrc, len2) else Average(SMAsrc, len2);
plot return = ma;
plot trend = all;
}
def line = vwosc(src, Length1, Length2, Length3, Length4, oscillatorLength, oscillatorType, MovAvgType, curve);
def VWOPvalue = line;
def ExtUp = src>VWOPvalue and VWOPvalue>Average(VWOPvalue, Length2);
def Up = src>VWOPvalue and !ExtUp;
def ExtDn = src<VWOPvalue and VWOPvalue<Average(VWOPvalue, Length2);
def Dn = src<VWOPvalue and !ExtDn;
plot VWOP = VWOPvalue;
VWOP.SetLineWeight(2);
VWOP.AssignValueColor(if src>VWOPvalue then
if VWOPvalue>Average(VWOPvalue, Length2) then Color.GREEN else Color.DARK_GREEN else
if VWOPvalue<Average(VWOPvalue, Length2) then Color.RED else Color.DARK_RED);
AssignPriceColor(if !BarColor then Color.CURRENT else if src>VWOPvalue then
if VWOPvalue>Average(VWOPvalue, Length2) then Color.GREEN else Color.DARK_GREEN else
if VWOPvalue<Average(VWOPvalue, Length2) then Color.RED else Color.DARK_RED);
plot trendUp = if ExtUp and Dn[1] then low else na;#, "Buy", Color.GREEN, no);
plot trendDn = if ExtDn and Up[1] then high else na;#, "Sell", Color.RED, yes);
trendUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
trendDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
trendUp.SetDefaultColor(Color.GREEN);
trendDn.SetDefaultColor(Color.RED);
#--- END CODE
Last edited by a moderator: