Plot one arrow n bars back from today

keithmj

New member
That title probably sounds confusing. This seems simple, but I can't seem to get it.

This code is set to the SMA. It simply counts how many consecutive bars have passes where the current SMA is greater than the previous SMA. If the current SMA is less than the previous, it resets to 0. I then create a label to see the value.

def trend = if SMA > SMA[1] then trend[1] + 1 else 0;

AddLabel(showTrendLabel, length + " SMA trend: " + trend, if trend >= minimumTrendLength then Color.GREEN else Color.RED);

With that I get what I want. Trend is the number of consecutive bars the current SMA was greater than the previous. Now, I simply want to plot one arrow at the beginning of the latest trend.

I thought it would be as easy as plotting an arrow at Close[trend]. But that would only work if it plotted the arrow after the SMA finished plotting. Also, it appears you can't use a variable in this context as the index number. I keep getting an error saying it expects a constant.

So, basically, I know how many bars I want to go back from the last bar and plot an arrow, but I don't know how to make that work.
 
Solution
That title probably sounds confusing. This seems simple, but I can't seem to get it.

This code is set to the SMA. It simply counts how many consecutive bars have passes where the current SMA is greater than the previous SMA. If the current SMA is less than the previous, it resets to 0. I then create a label to see the value.

def trend = if SMA > SMA[1] then trend[1] + 1 else 0;

AddLabel(showTrendLabel, length + " SMA trend: " + trend, if trend >= minimumTrendLength then Color.GREEN else Color.RED);

With that I get what I want. Trend is the number of consecutive bars the current SMA was greater than the previous. Now, I simply want to plot one arrow at the beginning of the latest trend.

I thought it would be as easy as plotting...
That title probably sounds confusing. This seems simple, but I can't seem to get it.

This code is set to the SMA. It simply counts how many consecutive bars have passes where the current SMA is greater than the previous SMA. If the current SMA is less than the previous, it resets to 0. I then create a label to see the value.

def trend = if SMA > SMA[1] then trend[1] + 1 else 0;

AddLabel(showTrendLabel, length + " SMA trend: " + trend, if trend >= minimumTrendLength then Color.GREEN else Color.RED);

With that I get what I want. Trend is the number of consecutive bars the current SMA was greater than the previous. Now, I simply want to plot one arrow at the beginning of the latest trend.

I thought it would be as easy as plotting an arrow at Close[trend]. But that would only work if it plotted the arrow after the SMA finished plotting. Also, it appears you can't use a variable in this context as the index number. I keep getting an error saying it expects a constant.

So, basically, I know how many bars I want to go back from the last bar and plot an arrow, but I don't know how to make that work.

you can't offset to another bar and plot something.
plots have to happen with logic that is true on that bar.


to plot an arrow every time the counter resets,
plot z = if trend == 0 then 1 else 0;
z.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);


if you want just 1 arrow, on just the most recent counter reset, then the study will have to look at future bars and see if any of them have trend = 0. if future bars do = 0, then don't plot an arrow on current bar.

a fold loop, using a negative offset, can read data from future bars.
this line says, as long as close has a valid price, keep checking. if the price is an error (after the last bar), stop the loop.
while !isnan( getvalue(close, -i))

Code:
# enter sma code
def sma = ....

def trend = if SMA > SMA[1] then trend[1] + 1 else 0;

AddLabel(showTrendLabel, length + " SMA trend: " + trend, if trend >= minimumTrendLength then Color.GREEN else Color.RED);

# i used a big number, instead of using highestall() to find the last bar.
# if you have a 5 day 1 minute chart, will need a bigger number, 390 x 5
def big = 1000;
def c = fold i = 1 to big
with p
while !isnan( getvalue(close, -i))
do p + (if getvalue(trend, -i) == 0 then 1 else 0);

# if no future bars have been reset, c=0
# and a reset happened on the current bar,
# then plot an arrow
plot z = if trend == 0 and c == 0 then 1 else 0;
z.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
#


a side note,
Close[trend] doesn't work,

but this might work, to READ DATA from another bar, but not to plot.
def x = getvalue(low, trend);

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetValue
 
Solution
Awesome sauce! That was a lot more work than I had envisioned. It does almost exactly what I want, but I'll figure out the rest. Basically, I want to arrow to point to the SMA line, not the price. If that's even possible. If not, it's no big deal.

I'm curious, why did you use 1000 instead of using something more dynamic like highestall() for big?
 
Awesome sauce! That was a lot more work than I had envisioned. It does almost exactly what I want, but I'll figure out the rest. Basically, I want to arrow to point to the SMA line, not the price. If that's even possible. If not, it's no big deal.

I'm curious, why did you use 1000 instead of using something more dynamic like highestall() for big?

diff arrow,
replace the plot lines with these,

plot z = if trend == 0 and c == 0 then sma else double.nan;
z.setpaintingStrategy(paintingStrategy.ARROW_UP);
z.setdefaultcolor(color.cyan);
#


using highestall will change the study to be complex. there will be a yellow triangle at the top of script window. a complex study may run slower.
most charts will have less than 1000 bars, and the while line will cause the loop to look at only available bars, not a 1000 on each iteration.

i could have done something like this,

def lastbn = highestall(if !isnan(close) then barnumber() else 0);
def big = lastbn - bn + 1;
 
diff arrow,
replace the plot lines with these,

plot z = if trend == 0 and c == 0 then sma else double.nan;
z.setpaintingStrategy(paintingStrategy.ARROW_UP);
z.setdefaultcolor(color.cyan);
#


using highestall will change the study to be complex. there will be a yellow triangle at the top of script window. a complex study may run slower.
most charts will have less than 1000 bars, and the while line will cause the loop to look at only available bars, not a 1000 on each iteration.

i could have done something like this,

def lastbn = highestall(if !isnan(close) then barnumber() else 0);
def big = lastbn - bn + 1;
Now that I've been looking at it, I actually like it on the price chart better. I do have one more question, but it's not very crucial. Is there a programmable way to set the arrow color to the same color of the SMA? I don't see a way to access the properties of the SMA and get its color property to set the color property of the arrow. It's no biggy if that's not possible either.
 
Now that I've been looking at it, I actually like it on the price chart better. I do have one more question, but it's not very crucial. Is there a programmable way to set the arrow color to the same color of the SMA? I don't see a way to access the properties of the SMA and get its color property to set the color property of the arrow. It's no biggy if that's not possible either.

you can use this to copy the color of another plotted shape

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/TakeValueColor
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
436 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top