5EMA VWAP cross indicator label

zeek

zeek

Active member
2019 Donor
Hi guys, one of my long strategies is to look for the 5EMA to reclaim/cross the VWAP to the upside on the 5min chart and was wondering if anyone could code an indicator for this. Preferably, when the cross happens, a label lights up in a strong color and when there is no cross happening, it stays in a grey color.

Can this be done?
 
horserider

horserider

Well-known member
VIP
@zeek See if this works. You can turn off the plots if confirm it is working.


Code:
# 5 EMA and VWAP cross. Modified 2 ToS studies and added labels for crosses. By Horserider 7/21/2019

# TD Ameritrade IP Company, Inc. (c) 2011-2019
#

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;


plot VWAP = price;


# TD Ameritrade IP Company, Inc. (c) 2017-2019
#

input price2 = close;
input length = 5;
input displace = 0;


plot AvgExp = ExpAverage(price2[-displace], length);


AddLabel(yes, "Buy ", if avgexp > vwap then Color.GREEN else color.GRAY);

AddLabel(yes, "Sell", if avgexp < vwap then Color.RED else color.GRAY);
 
markos

markos

Well-known member
VIP
@horserider you are a valuable help on this site. I appreciate the work you do. Thanks! Markos 🙏
 
M

mohitdas

New member
VIP
Hey @horserider thank you for writing this code, i am struggling with the code where i would like to paint an up( Buy Signal )/down( Sell Signal) arrow when the action takes place. Would you be able to guide on the same. Thank You

I used the below code to setup alerts

def Bull = avgexp > vwap;
def Bear = avgexp < vwap;

Alert(Bull, "Buy", Alert.Bar, Sound.Chimes);
Alert(Bear, "Sell", Alert.bar, Sound.Chimes);
 
horserider

horserider

Well-known member
VIP
@mohitdas

Redid original to include new label and arrows and alerts.

Code:
# EMA and VWAP cross. Modified 2 ToS studies and added  labels for crosses. By Horserider 7/21/2019
# Redid label and added cross arrows.


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 VWAP = price;

#
#
input price2 = close;
input length = 5;
input displace = 0;

def AvgExp = ExpAverage(price2[-displace], length);

AddLabel(yes, "VWAP EXP Cross", if avgexp > vwap then Color.GREEN else color.RED);

input showBreakoutSignals = no;
plot UpSignal = avgexp crosses above vwap;
plot DownSignal = avgexp crosses below vwap;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);


UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

Alert(UpSignal, "Buy", Alert.Bar, Sound.Chimes);
Alert(DownSignal, "Sell", Alert.bar, Sound.Chimes);
 
T

tmvo89

New member
Hi @horserider , is there a way to not display the indicator on any other time frame except for the 3min or 5 min timeframe?

Thanks,
Tri
 

Similar threads

Top