0

I would like to apply the below to several securities.

The end product would display 3 lines showing percentage change from 1700 to 2100 using a baseline of midpoint from 1500 to 1700 using lowest and highest candle from that period.

For example I would like to monitor these 3 securities:

BINANCE:NEBLUSD
BINANCE:RSRUSD
BINANCE:TRXUSD
study("Baseline", overlay=false)

Coin01 = input(title = "Coin Selection", defval="BINANCE:NEBLUSD")
Coin02 = input(title = "Coin Selection", defval="BINANCE:RSRUSD")
Coin03 = input(title = "Coin Selection", defval="BINANCE:TRXUSD")

baselinetime = input('1500-1700:1234567', title="Baseline")
blt = time(timeframe.period, baselinetime)

analysisrange = input('1700-2100:1234567', title="analysisrange")
ar = time(timeframe.period, analysisrange)

var highe_01 = 0.0
var lowe_01  = 10e10
if blt
    if not blt[1]
        highe_01 := high
        lowe_01  := low
    else
        highe_01 := max(high, highe_01)
        lowe_01  := min(low, lowe_01)

midpoint = (highe_01+lowe_01)/2
inc = (close - midpoint)//change(close, length)
p = (inc/close)*100

//This enables me to view the chosen coin on the chart
plot(ar ? p : na, title="Percentage Change", color=color.blue, linewidth=2, style=plot.style_linebr)

//How do I apply the above formula to several coins? The below doesn't work, but can't get my head around how to apply it
plot(Coin01 : p : na, title="Coin01", color=color.red, linewidth=2, style=plot.stle_linebr)
plot(Coin02 : p : na, title="Coin01", color=color.green, linewidth=2, style=plot.stle_linebr)
plot(Coin03 : p : na, title="Coin01", color=color.blue, linewidth=2, style=plot.stle_linebr)
frien_dd
  • 154
  • 2
  • 12
  • Take out the block for calculating the value of `p` as a function. Perform `security` to calculate the function `p` for each securities. – AnyDozer Jan 24 '21 at 11:09
  • @AnyDozer thanks for the reply have you any examples you could point me too. I have only configured to what security is on the chart. – frien_dd Jan 24 '21 at 16:35
  • Look [here](https://stackoverflow.com/questions/65863553/alertcondition-fails-when-the-condition-comes-from-a-security-function/65868159?noredirect=1#comment116467929_65868159) – AnyDozer Jan 24 '21 at 16:45
  • Did you manage to do it yourself or is the question still relevant? – AnyDozer Jan 26 '21 at 12:54
  • @AnyDozer I am still reading into it (can't seem to get it to work). If/when I succeed I will post an update. If you have any further tips throw them my way. – frien_dd Jan 27 '21 at 22:47

1 Answers1

1
//@version=4

study("Baseline", overlay=false)

Coin01 = input(title = "Coin Selection", defval="BINANCE:NEBLUSD")
Coin02 = input(title = "Coin Selection", defval="BINANCE:RSRUSD")
Coin03 = input(title = "Coin Selection", defval="BINANCE:TRXUSD")

baselinetime = input("0930-1130", "Baseline", input.session)
blt = time(timeframe.period, baselinetime+":1234567")

analysisrange = input("1130-1730", "analysisrange", input.session)
ar = time(timeframe.period, analysisrange+":1234567")

p (_blt, _ar) =>
    highe_01 = 0.0
    lowe_01  = 10e10
    p=0.
    var midpoint=0.
    if _blt
        if not _blt[1]
            highe_01 := high
            lowe_01  := low
        else
            highe_01 := max(high, highe_01)
            lowe_01  := min(low, lowe_01)
            midpoint := (highe_01+lowe_01)/2
    if _ar 
        p:=((close - midpoint)/midpoint)*100
    else 
        p:=na
    p

//This enables me to view the chosen coin on the chart
//plot(ar ? p : na, title="Percentage Change", color=color.blue, linewidth=2, style=plot.style_linebr)

//How do I apply the above formula to several coins? The below doesn't work, but can't get my head around how to apply it
 
//p0 = p(blt, ar) // for the current ticker
p1 = security(Coin01, timeframe.period, p(blt, ar))
p2 = security(Coin02, timeframe.period, p(blt, ar))
p3 = security(Coin03, timeframe.period, p(blt, ar))

plot(p1, title="Coin01", color=color.red,   linewidth=2, style=plot.style_linebr)
plot(p2, title="Coin02", color=color.green, linewidth=2, style=plot.style_linebr)
plot(p3, title="Coin03", color=color.blue,  linewidth=2, style=plot.style_linebr)
AnyDozer
  • 2,236
  • 2
  • 5
  • 22