Page 1 of 1

Making Label Optional

Posted: Tue Feb 16, 2021 7:50 pm
by Fxxtrader
Hello -
I have this label script and would like to make it optional to display. Where would I place the condition (? and : na)?

Code: Select all

f_print(_text) => var _label = label.new(time, y=na, text=_text, xloc=xloc.bar_time, yloc=yloc.price, color=color.white, style=label.style_label_center,
 textcolor=color.black, size=size.large, textalign=text.align_left),
 label.set_xy(_label, time + left_rightOffset, highestHigh),
 label.set_text(_label, _text),
 label.set_color(_label, labelCol),
 label.set_textcolor(_label, textCol)
f_print(syminfo.ticker + ", " + timeframe.period + "\n" + atrText + "\n" + kijunText  + "\n" + candleSizeTxt + "\n" + perText)

This label (dashboard ) is a work in progress, here is the full script

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/
// © ForexTradingIdeas
//@version=4

study("Watermark Label", shorttitle="Watermark", overlay=true, precision=5)

// Kijun-Sen Line
show_kijun      = input(false, title="Show Kijun-Sen Line", type=input.bool)
donchian(len)   => avg(lowest(len), highest(len))
kijun_Sen_Line  = donchian(26)
plot            (show_kijun ? kijun_Sen_Line : na, color= close > kijun_Sen_Line ? color.lime : color.blue, title="Kijun", linewidth=1)


// Watermark Input
// showWaterMrk     = input(title="Show WaterMark Details", type=input.bool, defval=true)
title               = input(false, title="Determine Watermark Location", type=input.bool)
hhLookback          = input(title="Highest High Lookback", type=input.integer, defval=21, minval=1)
up_down_YLoc        = input(title="Y Location = HH + ATR Multiple", type=input.float, defval=3.0, step=0.5)
left_right_XLoc     = input(title="X Location = # of bars from live bar", type=input.integer, defval=1)
divider             = input(false, title=" Dashboard Inputs ", type=input.bool)

// Dashboard input and calculations
atrMult         = input(title="ATR Multiplier Factor", type=input.float, defval=1.25, step=0.25, minval=0)
atr             = atr(14)
atrMultValue    = round(((atr * atrMult) / syminfo.mintick) / 10)
atrText         = "ATR * " + tostring(atrMult) + " = " + tostring(atrMultValue)
candleSize      = abs((high-low) / syminfo.mintick) / 10
avgCndlSize     = (avg(abs(high-low), abs(high[1]-low[1]), abs(high[2]-low[2]), abs(high[3]-low[3]), abs(high[4]-low[4]))) / syminfo.mintick / 10
candleSizeTxt   = "Avg[5] Candle Size " + tostring(avgCndlSize) + "\n" + "Current Candle Size " + tostring(candleSize)
bullKijun       = close > kijun_Sen_Line 
bearKijun       = close < kijun_Sen_Line 
kijunSignal     = iff(bullKijun, "Bullish", "Bearish")
kijunText       = "Kijun is " + kijunSignal
perText         = "Trade edges, ONLY"

//  Watermark Label Calculations
bar_Duration        = time - time[1]
left_rightOffset    = left_right_XLoc * bar_Duration
highestHigh         = highest(high, hhLookback) + (atr(14)) * up_down_YLoc
labelCol            = close[1]>open[1] ? color.green : color.red
textCol             = close[1]>open[1] ?color.white : color.yellow

// Label - label.new(x, y, text, xloc, yloc, color, style, textcolor, size, textalign, tooltip)
// Label.new variables
f_print(_text) => var _label = label.new(time, y=na, text=_text, xloc=xloc.bar_time, yloc=yloc.price, color=color.white, style=label.style_label_center,
 textcolor=color.black, size=size.large, textalign=text.align_left),
 label.set_xy(_label, time + left_rightOffset, highestHigh),
 label.set_text(_label, _text),
 label.set_color(_label, labelCol),
 label.set_textcolor(_label, textCol)
f_print(syminfo.ticker + ", " + timeframe.period + "\n" + atrText + "\n" + kijunText  + "\n" + candleSizeTxt + "\n" + perText)

Also is there a better way to do averages?

Code: Select all

avgCndlSize     = (avg(abs(high-low), abs(high[1]-low[1]), abs(high[2]-low[2]), abs(high[3]-low[3]), abs(high[4]-low[4]))) / syminfo.mintick / 10
Thank you and I hope your finding winning trades.

Re: Making Label Optional

Posted: Thu Feb 18, 2021 10:55 pm
by kmarryat
Conditional label.

Create a boolean input to turn the label display on an off, then place the function call in an if statement

Code: Select all

ShowLabel = input(defval=true, title="Show Label", type=input.bool)

f_print(_text) => var _label = label.new(time, y=na, text=_text, xloc=xloc.bar_time, yloc=yloc.price, color=color.white, style=label.style_label_center,
 textcolor=color.black, size=size.large, textalign=text.align_left),
 label.set_xy(_label, time + left_rightOffset, highestHigh),
 label.set_text(_label, _text),
 label.set_color(_label, labelCol),
 label.set_textcolor(_label, textCol)

if ShowLabel
	f_print(syminfo.ticker + ", " + timeframe.period + "\n" + atrText + "\n" + kijunText  + "\n" + candleSizeTxt + "\n" + perText)


I'm not aware of a more efficient way to do averages. You could simplify it a little like this:

Code: Select all

cSize = abs(high-low)
avgCndlSize     = avg(cSize, cSize[1], cSize[2], cSize[3], cSize[4]) / syminfo.mintick / 10
Hope that helps.

Re: Making Label Optional

Posted: Thu Feb 18, 2021 11:03 pm
by Fxxtrader
@kmarryat You're awesome, man!
This will now allow me to combine some indicators and labels, even differentiate between label dashboards within one script.

I'd like to learn more about using labels, I can see a lot of value in them.

Thanks.