0
votes

Im trying to write an indicator with Pine Script for Trading View.

I want to write a EMA based on the 1-hour-chart and use it in the 5-minute-chart. Including a 8 and 34 tradingperiod (2 EMA). Is that possible?

Usefull to see the higher trend when I'm trading in the 5-minute-chart.

Its my first time i'm trying to write a code at all.

Unfortunately i dont have time to learn it.

It would be really nice, if there is someone who could give me some foray on how to solve that problem.

I have tried to find some guides with google, but there are only some on how to program a EMA.

Everything i found is:

//@version=3

// At the very start of the pine script we always declare the version we are going to use, // You can see, above we are using the version 3 of the pine script // The @version word in pine script must be commented at the begning of the script

study("Coding ema in pinescript", overlay=true)

// This is where we are defining the study, // A study function in the pinescipt is used to tell the pine script that we will be building an indicator // the use of " overlay=true ", lets the pine script know that you want to overlay the plot in the charts on to the // candlestick chart.

// now we define an EMA function, named pine_ema with 2 arguments x and y // the sole aim of this pine_ema function is to return the curent ema of the current candle closing price pine_ema(src, time_period) =>

alpha = 2 / (time_period + 1)
// we have defined the alpha function above
ema = 0.0
// this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
ema := alpha * src + (1 - alpha) * nz(ema[1])
// this returns the computed ema at the current time
// notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
// since the ema was previously declared to 0
// this is called mutable variale declaration in pine script
ema
// return ema from the function

_10_period_ema = pine_ema(close, 10) // here we just called our function with a src of close and time_period of value 10

plot(_10_period_ema, color=red, transp=30, linewidth=2) // now we plot the _10_period_ema

1

1 Answers

0
votes

You can use this on any chart lower than 1 hour:

//@version=4
study("", "Emas 1H", true)
fastLength = input(8)
slowLength = input(34)
ema1hFast = security(syminfo.tickerid, "60", ema(close, fastLength))
ema1hSlow = security(syminfo.tickerid, "60", ema(close, slowLength))
plot(ema1hFast)
plot(ema1hSlow, color = color.fuchsia)

[EDIT 2019.09.07 08:55 — LucF]

//@version=4
study("", "Emas 1H", true)
fastLength  = input(8)
slowLength  = input(34)
smooth      = input(false, "Smooth")
smoothLen   = input(4, "Smoothing length", minval = 2)

ema1hFastRaw    = security(syminfo.tickerid, "60", ema(close, fastLength))
ema1hSlowRaw    = security(syminfo.tickerid, "60", ema(close, slowLength))
ema1hFastSm     = ema(ema(ema(ema1hFastRaw, smoothLen), smoothLen), smoothLen)
ema1hSlowSm     = ema(ema(ema(ema1hSlowRaw, smoothLen), smoothLen), smoothLen)
ema1hFast       = smooth ? ema1hFastSm : ema1hFastRaw
ema1hSlow       = smooth ? ema1hSlowSm : ema1hSlowRaw

plot(ema1hFast)
plot(ema1hSlow, color = color.fuchsia)