简体   繁体   中英

Pinescript security function for colour boolean with mutable variable

I'm having issues with plotting the Coral Trend indicator colour code, into my 15min 21EMA security function. Since the Coral Trend indicator colour code has a mutable variable, I cannot resolve it. This is next level coding for me, haha.

I will post

  • the code
  • a screenshot
  • the problem
  • the solution I tried

The code:

//@version=5

indicator(title='CT Indi', shorttitle='', overlay=true)


sm = input(21, title='Smoothing Period')
cd = input(0.4, title='Constant D')

di = (sm - 1.0) / 2.0 + 1.0
c1 = 2 / (di + 1.0)
c2 = 1 - c1
c3 = 3.0 * (cd * cd + cd * cd * cd)
c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd)
c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd
i1 = 0.0
i1 := c1 * request.security(syminfo.tickerid, "15", close) + c2 * nz(i1[1])
i2 = 0.0
i2 := c1 * i1 + c2 * nz(i2[1])
i3 = 0.0
i3 := c1 * i2 + c2 * nz(i3[1])
i4 = 0.0
i4 := c1 * i3 + c2 * nz(i4[1])
i5 = 0.0
i5 := c1 * i4 + c2 * nz(i5[1])
i6 = 0.0
i6 := c1 * i5 + c2 * nz(i6[1])

bfr = -cd * cd * cd * i6 + c3 * i5 + c4 * i4 + c5 * i3

bfrC = bfr > nz(bfr[1]) ? color.white : bfr < nz(bfr[1]) ? #056656 : na

plot(request.security(syminfo.tickerid, "1", ta.ema(close, 315)), title='15m 21 EMA', color=bfrC, linewidth=2)

The screenshot:

在此处输入图像描述

The problem:

As can be seen in the screenshot on the 15min timeframe (right bottom): the 21 EMA is plotted and changes colour according to the rules in the code. This is great. Now I want to take it to other timeframes (especially lower ones). On the top and left bottom we find the 5min and 10min chart, The 15min 21 EMA is plotted there. but the colour changes is not the same as on the 15min timeframe. It takes over the information of the current timeframe (thus here 5min and 10min)? How do I get to cycle a mutable variable in to a security function in to a colour boolean?

The solution I tried:

So I thought if I would take the line that creates the colour switch, and put it into a security function as well, things would work:

plot(request.security(syminfo.tickerid, "1", ta.ema(close, 315)), title='15m 21 EMA', color=request.security(syminfo.tickerid, "15", bfrC), linewidth=2)

But then I get the 'Cannot use a mutable variable as an argument.....' comment. Seems like a function() => should be used. But how do I use this for that colour boolean?

UPDATE

When I copy this code from the answers into Pinescript I get a regular 21 EMA for the specific time frame, and the colouring is not as wanted (15min).

I made some progress on my own. But I'm stuck on spaces in between. Here is the code:

//@version=5

indicator(title='CT Indi', shorttitle='', overlay=true)


sm = input(21, title='Smoothing Period')
cd = input(0.4, title='Constant D')

di = (sm - 1.0) / 2.0 + 1.0
c1 = 2 / (di + 1.0)
c2 = 1 - c1
c3 = 3.0 * (cd * cd + cd * cd * cd)
c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd)
c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd
i1_func() =>
    i1 = 0.0
    i1 := c1 * request.security(syminfo.tickerid, "15", close) + c2 * nz(i1[1])
i2_func() =>
    i2 = 0.0
    i2 := c1 * i1_func() + c2 * nz(i2[1])
i3_func() =>
    i3 = 0.0
    i3 := c1 * i2_func() + c2 * nz(i3[1])
i4_func() =>
    i4 = 0.0
    i4 := c1 * i3_func() + c2 * nz(i4[1])
i5_func() =>
    i5 = 0.0
    i5 := c1 * i4_func() + c2 * nz(i5[1])
i6_func() =>
    i6 = 0.0
    i6 := c1 * i5_func() + c2 * nz(i6[1])

bfr = -cd * cd * cd * request.security(syminfo.tickerid, "15", i6_func()) + c3 * request.security(syminfo.tickerid, "15", i5_func()) + c4 * request.security(syminfo.tickerid, "15", i4_func()) + c5 * request.security(syminfo.tickerid, "15", i3_func())

b1 = bfr > nz( bfr[1] )
b2 = bfr < nz( bfr[1] )

bfrC = b1 ? color.white : b2 ? #056656 : na

plot(request.security(syminfo.tickerid, "1", ta.ema(close, 315)), title='15m 21 EMA', color=bfrC, linewidth=2)

I am looking for a solution where the colouring (bfrC) is the same as the previous one. So, to me it seems, where : na is something like : bfrC[1] . This line: bfrC = b1? color.white: b2? #056656: na bfrC = b1? color.white: b2? #056656: na

在此处输入图像描述

Anyone solution? Thanks!!

You have to use the request.security() function in global scope, and only then use it.

You can do something like this:

//@version=5

indicator(title='CT Indi', shorttitle='', overlay=true)

myTickerClose = request.security(syminfo.tickerid, "15", close)

sm = input(21, title='Smoothing Period')
cd = input(0.4, title='Constant D')

di = (sm - 1.0) / 2.0 + 1.0
c1 = 2 / (di + 1.0)
c2 = 1 - c1
c3 = 3.0 * (cd * cd + cd * cd * cd)
c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd)
c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd
i1 = 0.0
i1 := c1 * myTickerClose + c2 * nz(i1[1])
i2 = 0.0
i2 := c1 * i1 + c2 * nz(i2[1])
i3 = 0.0
i3 := c1 * i2 + c2 * nz(i3[1])
i4 = 0.0
i4 := c1 * i3 + c2 * nz(i4[1])
i5 = 0.0
i5 := c1 * i4 + c2 * nz(i5[1])
i6 = 0.0
i6 := c1 * i5 + c2 * nz(i6[1])

bfr = -cd * cd * cd * i6 + c3 * i5 + c4 * i4 + c5 * i3

bfrC = bfr > nz(bfr[1]) ? color.white : bfr < nz(bfr[1]) ? #056656 : na

ema21 = ta.ema(myTickerClose, 21)

plot(ema21, title='15m 21 EMA', color=bfrC, linewidth=2)

Solved, To be able to reference the previous value of a variable while declaring it: initialize the variable first and use the re-assign.= operator.

Changing

bfrC = b1 ? color.white : b2 ? #056656 : na

to

bfrC = color(na)
bfrC := b1 ? color.white : b2 ? #056656 : nz(bfrC[1])

did the trick!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM