Candle high & low to appear on one timeframe only - pine-script-v5

I have the following code which marks out the high and low of a the 2H candle at the same time every day.
The problem I have is that if i switch to a lower timeframe, it adds the high and low to every candle and becomes very annoying.
Im not sure how to make the lines visible only on the 2H timeframe please.
// ENTRY LOCATION FOR EUROPEAN OPEN--------------------------------------------
string tfInput = input.timeframe("120", "Timeframe")
// Initialize variables on bar zero only
var hi = float(na)
var lo = float(na)
var line hiLine = na
var line loLine = na
// Detect changes in timeframe.
bool inSession = not na(time(timeframe.period, "2300-0100"))
if inSession
// New bar in higher timeframe; reset values and create new lines and box.
hi := high
lo := low
hiLine := line.new(bar_index - 2, hi, bar_index + 5, hi, color = color.blue, width = 2)
loLine := line.new(bar_index - 2, lo, bar_index + 5, lo, color = color.blue, width = 2)

Related

plot the high low of 1 minute and 15 minutes bar during RTH 9:30 am ET (Pine script)

How do I plot horizontal lines using high and low of the first one minute bar after market opens at 9:30 (NYSE time)?
Possibly I would like to plot the open, high and close of 1st 1 minute bar.
Then when first 15 minutes is done, would like to plot the high and low of that bar as well.
I tried with OHLC indicator and others, but couldn't find something to tackle exactly this.
Also the below code that I got it from chatGPT, but it got couple of errors
type here
indicator("First Minute Bar High and Low", overlay=true)
var float firstBarHigh = na
var float firstBarLow = na
// Define the regular trading hours for the NYSE in Eastern Time
var tradingHoursStart = input("09:30-5", "Trading Hours")
var tradingHoursEnd = input("16:00-5", "Trading Hours")
var tradingHoursRange = time(tradingHoursStart + ":1234567-" + tradingHoursEnd + ":1234567")
// Define the desired time in Eastern Time
var timeInET = timenow("America/New_York")
if hour(timeInET) == 9 and minute(timeInET) == 30 and timeInET >= tradingHoursRange
if barstate.isfirst
firstBarHigh := high
firstBarLow := low
```plot(firstBarHigh, "First Minute High", color=color.green)
```plot(firstBarLow, "First Minute Low", color=color.red)

Compare bool and float to determine plot (pine script v5)

You folks helped me with another part of this script and I'm making good progress. However, I'm stuck again.
I added a code to produce a dynamic overbought/oversold line on the indicator. That part works great. Now, I'm wanting to add a plotshape when conditions are met to signal long or short. Unfortunately, I can't figure out how to compare a bool variable and a float variable. The dynamic OB/OS lines are floats. The crossover lines are bool.
What I want is for the (green/long) shape to plot when a crossover happens below the dynamic oversold line and the (red/short) shape to plot when the crossunder happens above the dynamic overbought line.
I've tried all kinds of things (var, loop with "while" using sb value as a trigger). I either break the code or I get every crossover plotted. Here is what I have that is stable. I took all my junk that didn't work out:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HammerGeek
//#version=5
indicator('Impulse MACD [HammerGeek - LazyBear]', shorttitle='IMACD_HG_LB', overlay=false)
lengthMA = input(34)
lengthSignal = input(9)
//OverBought = input.float(0.1)
//OverSold = input.float(-0.1)
calc_smma(src, len) =>
smma = 0.0
sma_1 = ta.sma(src, len)
smma := na(smma[1]) ? sma_1 : (smma[1] * (len - 1) + src) / len
smma
calc_zlema(src, length) =>
ema1 = ta.ema(src, length)
ema2 = ta.ema(ema1, length)
d = ema1 - ema2
ema1 + d
src = hlc3
hi = calc_smma(high, lengthMA)
lo = calc_smma(low, lengthMA)
mi = calc_zlema(src, lengthMA)
md = mi > hi ? mi - hi : mi < lo ? mi - lo : 0
sb = ta.sma(md, lengthSignal)
sh = md - sb
mda = ta.sma(md, lengthSignal)
OverBoughtLine = ta.highest(sb * 0.5, lengthMA*100)
OverSoldLine = ta.lowest(sb * 0.5, lengthSignal*100)
shsbCrossOver = ta.crossover(md, sb)
shsbCrossUnder = ta.crossunder(md, sb)
//mdc = src > mi ? src > hi ? color.lime : color.green : src < lo ? color.red : color.orange
mdc = color.green
plot(0, color=color.new(color.gray, 0), linewidth=1, title='MidLine')
plot(md, color=mdc, linewidth=2, title='ImpulseMACD', style=plot.style_line)
plot(sh, color=color.rgb(122, 5, 168, 40), linewidth=2, title='ImpulseHisto', style=plot.style_area)
plot(sb, color=color.rgb(255, 255, 255, transp = 40), linewidth=2, title='ImpulseMACDCDSignal')
plot(OverBoughtLine, color=color.new(#f3e032, 0), linewidth=1, title = 'Overbought Line')
plot(OverSoldLine, color=color.new(color.yellow, 0), linewidth=1, title = 'Oversold Line')
plotshape(shsbCrossOver, "LONG", shape.triangleup, location.bottom, color.new(color.green, 0))
plotshape(shsbCrossUnder, "SHORT", shape.triangledown, location.top, color.new(#cd0808, 0))
//ebc = input(false, title='Enable bar colors')
//barcolor(ebc ? mdc : na)
You cannot compare a bool with a float. It doesn't make any sense and it is not legal in pinescript.
What I want is for the (green/long) shape to plot when a crossover
happens below the dynamic oversold line and the (red/short) shape to
plot when the crossunder happens above the dynamic overbought line.
You want to check if the values you use in that crossover is under your dynamic oversold line at the time of crossover.
OverSoldLine = ta.lowest(sb * 0.5, lengthSignal*100)
shsbCrossOver = ta.crossover(md, sb)
is_good = shsbCrossOver and (md < OverSoldLine) and (sb < OverSoldLine)

Looking for current price > previous day high . Works fine but plotting for all the 5 minutes candle

Here is my usecase :
if currentprice of 5 minutes candle is greater than previous day high , plot the chart with the 'x' mark in green .
If currentprice of 5 minutes candle is lesser than previous day low , plot the chart with 'x' mark in green
This script prints the 'x' mark for every 5 minutes but I want to plot only first for the first time when condition is met
I used a counter to check if it is a new day but seems that is not working as expected
//#version=5
indicator("HLPrice", max_lines_count = 11, overlay=true)
emaValue8 = ta.ema(close,8)
emaValue21 = ta.ema(close,21)
vwapValue = ta.vwap(hlc3)
// to highlight the session
timeframe = "1D"
isNewDay = timeframe.change(timeframe)
bgcolor(isNewDay ? color.new(color.green, 80) : na)
[dh,dl,dc] = request.security(syminfo.ticker, "D", [high[1],low[1], close[1]], lookahead=barmerge.lookahead_on)
isNewDayy = time("D") != time("D")[1]
var plotcond = false
greaterThanPreviousDayHigh = false
lesserThanPreviousDayLow = false
if isNewDayy
plotcond := true
if close[0] > dh and plotcond
greaterThanPreviousDayHigh:=true
if close[0] < dl and plotcond
lesserThanPreviousDayLow:=true
plotshape(series=greaterThanPreviousDayHigh , title="Candle on All EMAs" ,color = color.green, size=size.tiny)
plotshape(series=lesserThanPreviousDayLow , title="Candle on All EMAs" ,color = color.red, size=size.tiny)

Script for indicating order history - Tradeview Pine

I want to indicate historical trades in tradingview charts via a script based on information on time and price for entry and close.
My best idea is to search through "time" to find matches for entry and close, and then change the background color according to short or long position or draw a horizontal line. However, this seems not optimal. Any suggestions?
I'd implement that in the next way:
//#version=3
strategy("Background changing", overlay=true)
NONE = 0
LONG = 1000
SHORT = -1000
position = NONE
position := nz(position[1], NONE)
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("LongEntryId", strategy.long)
position := LONG
if (close < high[1])
strategy.close("LongEntryId")
position := NONE
getColor(state) =>
state == LONG ? green :
state == SHORT ? red :
white
bgcolor(color=getColor(position))
Or you can put arrows to the chart:
//#version=3
study("My Script", overlay=true)
order = 0
if time >= timestamp(2018, 1, 10, 0, 0)
order := 1
if time == timestamp(2018, 1, 17, 0, 0)
order := -1
plotarrow(order)

getting a 100% between two numbers?

I happen to have a timer which keeps track of two different points.
When I start, both are at zero.
when the button is pressed, one of them, let's call it A, keeps on incrementing by 1 every second.
If I press the button again, the state will switch and B variable will now start adding a point every second.
Let's say I have A at 10 and B at 30.
I would like to calculate the % between them.
For this specific case, I'd like a print out that A = 25% and B = 75%.
This is my code so far:
var total = A + B
var BPercentage = total - B / 100
var AestPercentage = A/B*100
var damo = 00.00
damo = 100/Double(total/B)
print(damo)
None of these seem to work.
Perhaps something like the following?
var total = A + B
var bPercentage = (B / total) * 100
var aPercentage = (A / total) * 100
var percentageDiff = abs(aPercentage - bPercentage)