Arnaud Deblander
Arnaud Deblander

Reputation: 23

How to use an input in a security function (Multi TF indicator) on pinescript?

I've tried to modify an indicator already available on Tradingview.

The idea was to transform a WaveTrends in a "line" indicators which change color when an oversold or overbought condition is met. Those indicators are wavetrends from another timeframe.

I have two problems with that, I want to use an input function to be able to change the timeframe without have to do it by changing the code.

Second issue, You can see 4 line on the image

  1. mid TF short condition
  2. longTF short condition
  3. mid TF long condition
  4. longTF long condition.

I bypassed my difficulties by creating 2 lines for long and 2 lines for short but I would like to combined them in only 2 lines with 3 different color : Green for oversold condition, red for overbought condition and finally gray in between.

study("WTcheckMTF", shorttitle="WTCMTF")

n1 = input(10, "Channel Length")
n2 = input(21, "Average Length")
obLevel1 = input(60, "Over Bought Level 1")
obLevel2 = input(53, "Over Bought Level 2")
osLevel1 = input(-60, "Over Sold Level 1")
osLevel2 = input(-53, "Over Sold Level 2")

ap = hlc3 
esa = ema(ap, n1)
d = ema(abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ema(ci, n2)

wt1 = tci
wt2 = sma(wt1,4)

//Mid and longer TF defining

long = input("D", "Interval used for long TF", type = resolution)
mid = input("360", "Interval used for mid TF", type = resolution)

**MidWT = security(tickerid, "360",wt2)
LongWT = security(tickerid, "720", wt2)**

//treshold MT LT

LWTLT = input(-53, "OS Threshold L")
LWTMT = input(-53, "OS Threshold M")
SWTLT = input(53, "OB Threshold L")
SWTMT = input(53, "OB Threshold M")

L1=1
M1=2
L2=3
M2=4

// Color for MT LT indicators

**lcolor1 = LongWT <= LWTLT ? lime : gray
mcolor1 = MidWT <= LWTMT ? lime : gray
lcolor2 = LongWT >= SWTLT ? red : gray
mcolor2 = MidWT >= SWTMT ? red : gray**

// plot

plot(L1, style=line,color=lcolor1,linewidth=25)
plot(M1, style=line,color=mcolor1,linewidth=25)
plot(L2, style=line,color=lcolor2,linewidth=25)
plot(M2, style=line,color=mcolor2,linewidth=25)

Upvotes: 2

Views: 1311

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8779

Enabled your input calls for resolution. The default should be a value that appears in the dropdown, so changed "360" for "240".

For colors, your conditions for one line are now together in one statement. Note how you can have multiple ternary conditions embedded in each other. They are more difficult to follow when you are not used to them, but are very handy.

Remember to include the compiler directive at the beginning of your script when posting code, so we can know which version of Pine your script is written for.

//@version=3
study("WTcheckMTF", shorttitle="WTCMTF")

n1 = input(10, "Channel Length")
n2 = input(21, "Average Length")
obLevel1 = input(60, "Over Bought Level 1")
obLevel2 = input(53, "Over Bought Level 2")
osLevel1 = input(-60, "Over Sold Level 1")
osLevel2 = input(-53, "Over Sold Level 2")

ap = hlc3 
esa = ema(ap, n1)
d = ema(abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ema(ci, n2)

wt1 = tci
wt2 = sma(wt1,4)

//Mid and longer TF defining

long = input("D", "Interval used for long TF", type = resolution)
mid = input("240", "Interval used for mid TF", type = resolution)

MidWT = security(tickerid, mid, wt2)
LongWT = security(tickerid, long, wt2)

//treshold MT LT

LWTLT = input(-53, "OS Threshold L")
LWTMT = input(-53, "OS Threshold M")
SWTLT = input(53, "OB Threshold L")
SWTMT = input(53, "OB Threshold M")

L1=1
M1=2
L2=3
M2=4

// Color for MT LT indicators

lcolor = LongWT <= LWTLT ? lime : LongWT >= SWTLT ? red : gray
mcolor = MidWT  <= LWTMT ? lime : MidWT  >= SWTMT ? red : gray

// plot

plot(L1, "L1", lcolor, 25)
plot(M1, "M1", mcolor, 25)

Upvotes: 2

Related Questions