Demalesius
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: March 19th, 2022
Contact: TradingView Profile

Coding a certain value difference between moving averages

Hello guys

I have following problem. I want to have a code for an opening order for following criteria:

EMA8 has to be higher than EMA14 but also with at least 5% or a certain value.

This is working fine but doesn't includes the 5% or a value:



longCondition1 = ema(close, 8) > ema(close, 14)



So I tried something like this for the 5%:



longCondition1 = ema(close, 8) > (ema(close, 14) * 1.05)

longCondition1 = ema(close, 8) > ((ema(close, 14) + (ema(close,14) * 0.05))



And I tried this for the value of for example 200:



longCondition1 = ema(close, 8) > (ema(close, 14) + 200)



I really can't figure it out.

May someone can help me please..

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Coding a certain value difference between moving averages

Hey,

Issue is when you add 5% it will add a huge difference to the ema 8 and ema14 which will mean ema8 will never cross that point. Just to show this here is code to plot ema 8 , ema 14 and ema14*1.05 lines.

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds

//@version=5
indicator("My script", overlay=true)

ema8 = ta.ema(close, 8)
ema14= ta.ema(close, 14)

plot(ema8, color = color.green, linewidth = 2)
plot(ema14, color = color.purple, linewidth = 2)
plot(ema14*1.005, color = color.orange, linewidth = 2)


Return to “Pine Script Q&A”