Page 1 of 1
Plot shape and multiple timeframes
Posted: Mon May 08, 2023 1:18 am
by Ponderosa
Is it possible to plot a shape on say, a 10 minute chart, while drawing data from a higher timeframe for that shape? If so, could you give an example of how this is done? I'm having trouble.
Thank you!!
Re: Plot shape and multiple timeframes
Posted: Wed May 10, 2023 10:59 pm
by Steve Burman
Ponderosa,
I need more information. What data do you want to draw from the higher timeframe and given it is a 10 minute chart, where on the 10 minute chart do you want to draw it given for example if you want to plot data from the 1hr timeframe, but which 10 minute bar(s) would that be plotted over (all 10m bars for the hour or the last one or the next one after the hour) ? I'm not saying this is possible but wanting clarity first.
Re: Plot shape and multiple timeframes
Posted: Thu May 18, 2023 1:17 am
by Ponderosa
Steve,
Say I want to plot a shape on the 18 minute chart and trigger an alert when the 18 minute RSI crosses below 30, but only if the 240 minute 100 period EMA is higher than one bar ago. I use these timeframes because they do not divide to an integer.
I want to plot the first instance in a cycle. I normally filter this with:
bs1 = ta.barssince(condition)
trigger = condition and bs1[1] > 20
Re: Plot shape and multiple timeframes
Posted: Wed May 24, 2023 12:15 am
by Steve Burman
Hi Ponderosa,
Sorry for the delay in replying. Are you a PineScript Mastery student? If so there is a video on how to get data from higher timeframes. If not the I highly recommend signing up as you will learn so much cool and useful stuff. Here's a link just incase.
https://courses.theartoftrading.com/cou ... ref=de21bf
Anyway this should help in the short term.
@version=5
indicator(title="HTF EMA", overlay=true)
// Get user input
res = input.string(title="EMA Timeframe", defval="240")
len = input.int(title="EMA Length", defval=100)
// Calculate EMA from higher timeframe
ema = ta.ema(close, len)
ema240 = request.security(syminfo.tickerid, res, ema[barstate.isrealtime ? 1 : 0])
rsi = ta.rsi(close,14)
condition = false
// If rsi > 30 and ema240 > previous bar
if rsi > 30
if ema240 > ema240[1]
condition = true
// If condition true do something here
// Draw EMA
plot(ema240, linewidth=2, title="HTF EMA")
Re: Plot shape and multiple timeframes
Posted: Sun Jun 04, 2023 7:51 pm
by Ponderosa
Steve thanks,
I am a PS Mastery student and am using the security function to draw htf data. But when I add my indicator to the chart, any plots that are drawing htf data do not plot. If I change the chart to that higher timeframe, I see some of the plots there, but not as many as there should be. I have written several different plot functions using htf data and this always seems to be the case for me. I really hope I am doing something wrong. Would be very disappointing if PS doesn't allow for this. The last one I made for a 3 minute chart and I used some 30 minute data, thinking that because it's an even 10x it should work but it doesn't.
Here is a simple example of what I'm trying to do:
//@version=5
indicator("No HTF Example", overlay=true)
tf30 = input.timeframe('30')
ema = ta.ema(close, 50)
ema30 = request.security(syminfo.tickerid, tf30, ema[barstate.isconfirmed ? 0 : 1], gaps=barmerge.gaps_on)
//Reversal up on 30 minute 50ema
reversal30 = ema30 > ema30[1] and ema30[1] < ema30[2]
bs1 = ta.barssince(reversal30)
//Reversal up on 3 minute 50ema
reversal3 = ema > ema[1] and ema[1] < ema[2]
//3 minute 50ema reversal happens within 250 bars of 30 minute 50ema reversal
attn = reversal3 and bs1 < 250
plotshape(reversal30, title='30m 50EMA', style=shape.diamond, location=location.bottom,
color=color.yellow, size=size.small, text='30 EMA', textcolor=color.white)
plotshape(attn, title='Attn', style=shape.diamond, location=location.belowbar,
color=color.fuchsia, size=size.small, text='Heads Up', textcolor=color.white)
If you add this to a 3 minute chart, you will see there are no plots. If you zoom out to the 30 minute chart, there are many. Zoom out more to the daily chart, and there are very few "heads up" plots.
If you are able to tell me or can tell me where Matthew explains why I am getting this behavior I would be very thankful. If you could explain how to get this example to plot the "heads up" symbol to the 3 minute chart I would be extremely thankful.
Re: Plot shape and multiple timeframes
Posted: Tue Jun 06, 2023 11:49 pm
by Steve Burman
Hey Ponderosa,
I debugged the script on small timeframe increments between 3 and 30 and found that the plotshapes displayed at 21 minute chart but not a 20 minute chart. It definitely seems like a timing issue. I also found that gaps=barmerge.gaps_on for your security.request statement is displaying na for the ema30 for all bars except the bars every 30 minutes irrespective of the chart timeframe. Given all this I asked ChatGPT4 why this might be happening and its response was as follows:
ChatGPT4 ...Yes, the issue here is likely due to the way the script calculates the bs1 variable and uses it in the attn condition.
Specifically, bs1 is set as the number of bars since the last reversal on the 30-minute EMA. When you run the script on a 3-minute chart, each bar represents 3 minutes of data. However, when running it on a 30-minute chart, each bar represents 30 minutes of data. So, a condition like bs1 < 250 represents a different amount of time in these two scenarios.
On a 3-minute chart, bs1 < 250 represents less than 750 minutes (or 12.5 hours), while on a 30-minute chart, bs1 < 250 represents less than 7500 minutes (or about 5.2 days).
If the 30-minute EMA reversals don't occur frequently enough (at least once every 750 minutes), the attn condition will never be true on a 3-minute chart, and therefore no plots will be displayed.
You could adjust the code to make the number of bars in the bs1 < 250 condition relative to the timeframe. For example, if you want it to represent 5 days, you could change it to bs1 < 250 * ta.timeframe.multiplier, which will be 250 on a 30-minute chart and 2500 on a 3-minute chart. However, keep in mind that this will change the behavior of the script, and you'll need to decide if this adjustment makes sense for the trading strategy that the script represents.
Back to me... that is incorrect because there is no ta.timeframe.multiplier, just timeframe.multiplier as it's not a technical analysis function. Anyway, I tried this and it didn't work so I don't have an answer for you. Hopefully this reply may trigger something for you but if not, I suggest you post a TV ticket to ask the experts
Good fortune and please let me know if you get a positive result