Troubleshoot Watchlist VWAP SMA crossover

MWilliamson83

New member
I have created a custom script for chart signals which works exactly as intended. It's basically a crossover event involving VWAP and some SMAs. However, when coding the watchlist columns, it will not work. I've plotted each variable one by one, including VWAP, and each matches the values on the chart exactly. But the crossover trigger that I'm using for the chart just isn't working, or it's firing at the wrong times, etc. So the code is below, am I missing something?

Note, I copied the VWAP code from the VWAP study, didn't write that part.


Code:
##########################################
#                                        #
#   Assigning & plotting VWAP            #
#                                        #
##########################################

input numDevDn = -2.0;
input numDevUp = 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;
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));

plot VWAP = price;
#plot UpperBand = price + numDevUp * deviation;
#plot LowerBand = price + numDevDn * deviation;

#VWAP.SetDefaultColor(Color.YELLOW);
#UpperBand.setDefaultColor(getColor(2));
#LowerBand.setDefaultColor(getColor(4));


##########################################
#                                        #
#       Plotting the EMAs                #
#                                        #
##########################################

input short_ema_length = 10;
input long_ema_length = 30;


def short_ema = MovAvgExponential(close, short_ema_length);
#short_ema.SetDefaultColor(Color.CYAN);

def long_ema = MovAvgExponential(close, long_ema_length);
#long_ema.SetDefaultColor(Color.RED);

input lookback = 10;

##########################################
#                                        #
#   Defining main signal                 #
#                                        #
##########################################

def bearish_trigger =

short_ema < long_ema
and
long_ema < VWAP
###use next line (delete the # signs to use) to include price over the MAs and VWAP as a condition
###and close > short_ema
;

##########################################
#                                        #
#   Plotting the trigger signal          #
#                                        #
##########################################

def bearish_signal = bearish_trigger[1] is false and bearish_trigger and bearish_trigger is false within lookback bars;

#addLabel(Yes, if bearish_signal then "Sell" else " " ,color.red);

#bearish_signal.AssignValueColor(Color.RED);
#bearish_signal.SetPaintingStrategy(paintingStrategy = PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
I have created a custom script for chart signals which works exactly as intended. It's basically a crossover event involving VWAP and some SMAs. However, when coding the watchlist columns, it will not work. I've plotted each variable one by one, including VWAP, and each matches the values on the chart exactly. But the crossover trigger that I'm using for the chart just isn't working, or it's firing at the wrong times, etc. So the code is below, am I missing something?

Note, I copied the VWAP code from the VWAP study, didn't write that part.


Code:
##########################################
#                                        #
#   Assigning & plotting VWAP            #
#                                        #
##########################################

input numDevDn = -2.0;
input numDevUp = 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;
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));

plot VWAP = price;
#plot UpperBand = price + numDevUp * deviation;
#plot LowerBand = price + numDevDn * deviation;

#VWAP.SetDefaultColor(Color.YELLOW);
#UpperBand.setDefaultColor(getColor(2));
#LowerBand.setDefaultColor(getColor(4));


##########################################
#                                        #
#       Plotting the EMAs                #
#                                        #
##########################################

input short_ema_length = 10;
input long_ema_length = 30;


def short_ema = MovAvgExponential(close, short_ema_length);
#short_ema.SetDefaultColor(Color.CYAN);

def long_ema = MovAvgExponential(close, long_ema_length);
#long_ema.SetDefaultColor(Color.RED);

input lookback = 10;

##########################################
#                                        #
#   Defining main signal                 #
#                                        #
##########################################

def bearish_trigger =

short_ema < long_ema
and
long_ema < VWAP
###use next line (delete the # signs to use) to include price over the MAs and VWAP as a condition
###and close > short_ema
;

##########################################
#                                        #
#   Plotting the trigger signal          #
#                                        #
##########################################

def bearish_signal = bearish_trigger[1] is false and bearish_trigger and bearish_trigger is false within lookback bars;

#addLabel(Yes, if bearish_signal then "Sell" else " " ,color.red);

#bearish_signal.AssignValueColor(Color.RED);
#bearish_signal.SetPaintingStrategy(paintingStrategy = PaintingStrategy.BOOLEAN_ARROW_DOWN);
I've been away from trading / coding for a while so unfortunately I don't have an answer for you. But from what I do remember, a consideration you have to take into account when writing code for columns is that they have a specific time period they are assigned to, where as a chart script usually changes its timeframe value according to chart you are viewing. So chart codes tend to be more dynamic in this sense, whereas column codes tend to be more static because the user assigns a timeframe.

My guess is that you're getting incorrect signals because of what timeframe your column is assigned to. For instance, if your column is assigned to a 5m chart and you're looking at a 1hr, the signals won't look correct because you're not on the correct time frame. You can change the time frame assignment by clicking the button next to the name of your code and selecting the time frame you want it to work on. Hope this helps.
 

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
372 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