Moving Averages With Unwanted Signals.

renydiver

New member
Hello guys, I am working on a custom indicator based on moving averages and I need a little help getting rid of unwanted signals. Basically what I need is a way for the indicator to print only the first arrow(signal) when a trend is changing direction. Thank you so much for any help in advanced.
Ruby:
#Upper Line

input numDevUp = 0.10;
input numDevDn = -0.10;
input priceup = close;
input lengthup = 3;
input AverageType = AverageType.EXPONENTIAL;

def uptmp1 = if priceup > priceup[1] then priceup - priceup[1] else 0;
def uptmp2 = if priceup[1] > priceup then priceup[1] - priceup else 0;
def upd2 = sum(uptmp1, lengthup);
def upd4 = sum(uptmp2, lengthup);
def upcond = upd2 + upd4 == 0;
def upad3 = if upcond then 0 else (upd2 - upd4) / (upd2 + upd4) * 100;
def upcoeff = 2 / (lengthup + 1) * AbsValue(upad3) / 100;
def upasd = compoundValue("visible data" = upcoeff * priceup + (if IsNaN(upasd[1]) then 0 else upasd[1]) * (1 - upcoeff), "historical data" = priceup
);
plot VMAUP = MovingAverage (AverageType, upasd + numDevUp, LengthUP);
VMAUP.setDefaultColor(GetColor(5));

#Lower Line

input pricedn = close;
input lengthdn = 3;

def dntmp1 = if pricedn > pricedn[1] then pricedn - pricedn[1] else 0;
def dntmp2 = if pricedn[1] > pricedn then pricedn[1] - pricedn else 0;
def dnd2 = sum(dntmp1, lengthdn);
def dnd4 = sum(dntmp2, lengthdn);
def dncond = dnd2 + dnd4 == 0;
def dnad3 = if dncond then 0 else (dnd2 - dnd4) / (dnd2 + dnd4) * 100;
def dncoeff = 2 / (lengthdn + 1) * AbsValue(dnad3) / 100;
def dnasd = compoundValue("visible data" = dncoeff * pricedn + (if IsNaN(dnasd[1]) then 0 else dnasd[1]) * (1 - dncoeff), "historical data" = pricedn
);
plot VMADN = MovingAverage (AverageType, dnasd + NumDevDn, LengthDN);
VMADN.setDefaultColor(GetColor(6));

#Center Signal Trigger

input priceT = close;
input lengthT = 1;

def Ttmp1 = if priceT > priceT[1] then priceT - priceT[1] else 0;
def Ttmp2 = if priceT[1] > priceT then priceT[1] - priceT else 0;
def Td2 = sum(Ttmp1, lengthT);
def Td4 = sum(Ttmp2, lengthT);
def Tcond = Td2 + Td4 == 0;
def Tad3 = if Tcond then 0 else (Td2 - Td4) / (Td2 + Td4) * 100;
def Tcoeff = 2 / (lengthT + 1) * AbsValue(Tad3) / 100;
def Tasd = compoundValue("visible data" = Tcoeff * priceT + (if IsNaN(Tasd[1]) then 0 else Tasd[1]) * (1 - Tcoeff), "historical data" = priceT
);
plot VMAT = Tasd;
VMAT.setDefaultColor(GetColor(3));

# Signal

input showBreakoutSignals = no;

plot UpSignal = VMAT crosses above VMAUP;
plot DownSignal = VMAT crosses below VMADN;

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);

# Alerts

input Alert = yes;
Alert(UpSignal and alert, "Buy", Alert.Bar, Sound.Ding);
Alert (DownSignal and alert, "Sell", Alert.Bar, Sound.Ding);



Screenshot (57).png
 
Last edited by a moderator:
Solution
@renydiver
Code:
#Upper Line

input numDevUp = 0.10;
input numDevDn = -0.10;
input priceup = close;
input lengthup = 3;
input AverageType = AverageType.EXPONENTIAL;

def uptmp1 = if priceup > priceup[1] then priceup - priceup[1] else 0;
def uptmp2 = if priceup[1] > priceup then priceup[1] - priceup else 0;
def upd2 = sum(uptmp1, lengthup);
def upd4 = sum(uptmp2, lengthup);
def upcond = upd2 + upd4 == 0;
def upad3 = if upcond then 0 else (upd2 - upd4) / (upd2 + upd4) * 100;
def upcoeff = 2 / (lengthup + 1) * AbsValue(upad3) / 100;
def upasd = compoundValue("visible data" = upcoeff * priceup + (if IsNaN(upasd[1]) then 0 else upasd[1]) * (1 - upcoeff), "historical data" = priceup
);
plot VMAUP = MovingAverage (AverageType, upasd +...
@renydiver It would appear that the script is doing exactly what you asked for. Given this is a trending indicator when there is sideways movement you will see a lot of arrows due to attempts to find trend.
 

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

Hello guys, I am working on a custom indicator based on moving averages and I need a little help getting rid of unwanted signals. Basically what I need is a way for the indicator to print only the first arrow(signal) when a trend is changing direction. Thank you so much for any help in advanced.
Ruby:
#Upper Line

input numDevUp = 0.10;
input numDevDn = -0.10;
input priceup = close;
input lengthup = 3;
input AverageType = AverageType.EXPONENTIAL;

def uptmp1 = if priceup > priceup[1] then priceup - priceup[1] else 0;
def uptmp2 = if priceup[1] > priceup then priceup[1] - priceup else 0;
def upd2 = sum(uptmp1, lengthup);
def upd4 = sum(uptmp2, lengthup);
def upcond = upd2 + upd4 == 0;
def upad3 = if upcond then 0 else (upd2 - upd4) / (upd2 + upd4) * 100;
def upcoeff = 2 / (lengthup + 1) * AbsValue(upad3) / 100;
def upasd = compoundValue("visible data" = upcoeff * priceup + (if IsNaN(upasd[1]) then 0 else upasd[1]) * (1 - upcoeff), "historical data" = priceup
);
plot VMAUP = MovingAverage (AverageType, upasd + numDevUp, LengthUP);
VMAUP.setDefaultColor(GetColor(5));

#Lower Line

input pricedn = close;
input lengthdn = 3;

def dntmp1 = if pricedn > pricedn[1] then pricedn - pricedn[1] else 0;
def dntmp2 = if pricedn[1] > pricedn then pricedn[1] - pricedn else 0;
def dnd2 = sum(dntmp1, lengthdn);
def dnd4 = sum(dntmp2, lengthdn);
def dncond = dnd2 + dnd4 == 0;
def dnad3 = if dncond then 0 else (dnd2 - dnd4) / (dnd2 + dnd4) * 100;
def dncoeff = 2 / (lengthdn + 1) * AbsValue(dnad3) / 100;
def dnasd = compoundValue("visible data" = dncoeff * pricedn + (if IsNaN(dnasd[1]) then 0 else dnasd[1]) * (1 - dncoeff), "historical data" = pricedn
);
plot VMADN = MovingAverage (AverageType, dnasd + NumDevDn, LengthDN);
VMADN.setDefaultColor(GetColor(6));

#Center Signal Trigger

input priceT = close;
input lengthT = 1;

def Ttmp1 = if priceT > priceT[1] then priceT - priceT[1] else 0;
def Ttmp2 = if priceT[1] > priceT then priceT[1] - priceT else 0;
def Td2 = sum(Ttmp1, lengthT);
def Td4 = sum(Ttmp2, lengthT);
def Tcond = Td2 + Td4 == 0;
def Tad3 = if Tcond then 0 else (Td2 - Td4) / (Td2 + Td4) * 100;
def Tcoeff = 2 / (lengthT + 1) * AbsValue(Tad3) / 100;
def Tasd = compoundValue("visible data" = Tcoeff * priceT + (if IsNaN(Tasd[1]) then 0 else Tasd[1]) * (1 - Tcoeff), "historical data" = priceT
);
plot VMAT = Tasd;
VMAT.setDefaultColor(GetColor(3));

# Signal

input showBreakoutSignals = no;

plot UpSignal = VMAT crosses above VMAUP;
plot DownSignal = VMAT crosses below VMADN;

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);

# Alerts

input Alert = yes;
Alert(UpSignal and alert, "Buy", Alert.Bar, Sound.Ding);
Alert (DownSignal and alert, "Sell", Alert.Bar, Sound.Ding);



View attachment 864
Hello Ruby,

Thank you so much for the quick response. Yes, the only thing I would like it to do if is possible is that instead of printing a series of signal every time the average triggers the signal, to print a signal only if there is not a preceding same type of signal. For example, if there is already a bear(RED) arrow printed, do not print another RED arrow until after a Green arrow(signal) is printed. Excuse my grammar, I hope I'm explaining it in a way it can be understood(English is not my first language).
 
I installed the script and am seeing the indicator perform as expected, as @MerryDay stated... Modifying the code to perform as you suggest would negate the overall functionality... Those signals are meant to be paired with other confirmations and are not intended to be acted upon every time they fire... By your logic you might act on the wrong arrow and then miss the proper arrow that has better confirmation...
 
Last edited by a moderator:
@renydiver
Code:
#Upper Line

input numDevUp = 0.10;
input numDevDn = -0.10;
input priceup = close;
input lengthup = 3;
input AverageType = AverageType.EXPONENTIAL;

def uptmp1 = if priceup > priceup[1] then priceup - priceup[1] else 0;
def uptmp2 = if priceup[1] > priceup then priceup[1] - priceup else 0;
def upd2 = sum(uptmp1, lengthup);
def upd4 = sum(uptmp2, lengthup);
def upcond = upd2 + upd4 == 0;
def upad3 = if upcond then 0 else (upd2 - upd4) / (upd2 + upd4) * 100;
def upcoeff = 2 / (lengthup + 1) * AbsValue(upad3) / 100;
def upasd = compoundValue("visible data" = upcoeff * priceup + (if IsNaN(upasd[1]) then 0 else upasd[1]) * (1 - upcoeff), "historical data" = priceup
);
plot VMAUP = MovingAverage (AverageType, upasd + numDevUp, LengthUP);
VMAUP.setDefaultColor(GetColor(5));

#Lower Line

input pricedn = close;
input lengthdn = 3;

def dntmp1 = if pricedn > pricedn[1] then pricedn - pricedn[1] else 0;
def dntmp2 = if pricedn[1] > pricedn then pricedn[1] - pricedn else 0;
def dnd2 = sum(dntmp1, lengthdn);
def dnd4 = sum(dntmp2, lengthdn);
def dncond = dnd2 + dnd4 == 0;
def dnad3 = if dncond then 0 else (dnd2 - dnd4) / (dnd2 + dnd4) * 100;
def dncoeff = 2 / (lengthdn + 1) * AbsValue(dnad3) / 100;
def dnasd = compoundValue("visible data" = dncoeff * pricedn + (if IsNaN(dnasd[1]) then 0 else dnasd[1]) * (1 - dncoeff), "historical data" = pricedn
);
plot VMADN = MovingAverage (AverageType, dnasd + NumDevDn, LengthDN);
VMADN.setDefaultColor(GetColor(6));

#Center Signal Trigger

input priceT = close;
input lengthT = 1;

def Ttmp1 = if priceT > priceT[1] then priceT - priceT[1] else 0;
def Ttmp2 = if priceT[1] > priceT then priceT[1] - priceT else 0;
def Td2 = sum(Ttmp1, lengthT);
def Td4 = sum(Ttmp2, lengthT);
def Tcond = Td2 + Td4 == 0;
def Tad3 = if Tcond then 0 else (Td2 - Td4) / (Td2 + Td4) * 100;
def Tcoeff = 2 / (lengthT + 1) * AbsValue(Tad3) / 100;
def Tasd = compoundValue("visible data" = Tcoeff * priceT + (if IsNaN(Tasd[1]) then 0 else Tasd[1]) * (1 - Tcoeff), "historical data" = priceT
);
plot VMAT = Tasd;
VMAT.setDefaultColor(GetColor(3));

# Signal

input showBreakoutSignals = no;
def signal = if VMAT crosses above VMAUP then 1 else if VMAT crosses below VMADN then -1 else signal[1];
plot UpSignal = VMAT crosses above VMAUP and signal[1] == -1;
plot DownSignal = VMAT crosses below VMADN and signal[1] == 1;

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);

# Alerts

input Alert = yes;
Alert(UpSignal and alert, "Buy", Alert.Bar, Sound.Ding);
Alert (DownSignal and alert, "Sell", Alert.Bar, Sound.Ding);
Screenshot (27).png
 
Last edited by a moderator:
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
409 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