Up/Dn arrows...how to paint on bar[1]?

DaysOff

New member
I have coded a strategy in thinkorswim that places up or down arrows on the chart once a condition has been met. For discussion sake, let's say the condition is if two bars form with the entire range of each bar completely above the upper Bollinger band, a red arrow is placed below the second bar. When I try to code this, it will paint on the next bar after the condition has been met. Is there any way to get the arrow to paint on the prior bar?

Thanks in advance for any assistance!

In the image is an example with corresponding code below. In the example, the condition is met when two bars are entirely above or below the BB's. Instead of the arrow painting on the next bar, I'd like it to paint on the one previous...the second bar that forms entirely outside.
5Mzx056.png


Code:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;

def sDev = stdev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

#2 Bar buy
def b1 = (low > max(LowerBand, UpperBand));
def buy1Count = if b1 then buy1count[1] + 1 else 0;
#plot test = buy1count;
#test.setpaintingStrategy(paintingStrategy.VALUES_ABOVE);
plot buy1 = buy1Count[1] == 2;
buy1.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
buy1.setDefaultColor(color.uptick);
Alert(buy1, "Buy 1", alert.bar, sound.ding);


#2 Bar sell
def s1 = (high < min(LowerBand, UpperBand));
def sell1Count = if s1 then sell1count[1] + 1 else 0;
#plot test2 = sell1count;
#test2.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
plot sell1 = sell1Count[1] == 2;
sell1.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
sell1.setDefaultColor(color.downtick);
Alert(sell1, "Sell 1", alert.bar, sound.ding);
 
it should be showing the arrow as soon as 2 valid candles finish, so not much delay.

it is looking at a previous bar to determine a buy. because then it only looks at completed bars.
plot buy1 = buy1Count[1] == 2;

try changing the [1] to [0] , to make it look at the current bar.
the price fluctuations may cause multiple triggers?
plot buy1 = buy1Count[0] == 2;
 
@DaysOff If you don't wait for the two prior candles to close before painting the arrows, the indicator will repaint, giving false signals... There is good reason for the lag of some indicators because we want to know that something happened rather than something may or may not be happening... Acting prematurely can get you stopped out of a higher percentage of trades than desirable...
 
Thanks for the replies. I wasn’t suggesting the logic be changed…as it works the way it is. I was more wondering if it was possible to indicate on which bar to have the arrow painted. I don’t think it is possible…but thought I would ask.
 
Thanks for the replies. I wasn’t suggesting the logic be changed…as it works the way it is. I was more wondering if it was possible to indicate on which bar to have the arrow painted. I don’t think it is possible…but thought I would ask.

Yes, the chart bar/candle can be painted to match the arrow... The issue would be that the candle/bar would repaint along with the arrow... The code would be something like the following but it hasn't been tested... I'm assuming this is what you are looking for...

Ruby:
AssignPriceColor(if buy1 then Color.GREEN else if sell1 then Color.RED else Color.CURRENT);
 
Thanks for the replies. I wasn’t suggesting the logic be changed…as it works the way it is. I was more wondering if it was possible to indicate on which bar to have the arrow painted. I don’t think it is possible…but thought I would ask.

See if this works for you. By increasing/decreasing the arrowmover you can move arrow right/left. It will plot the arrow after the bar closes meeting your condition. The counts for buy/sell are plotted for testing and the circled areas are highlighting the arrows.

Capture.jpg
Ruby:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.SIMPLE;

def sDev = StDev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

input arrowmover = -1;#Hint arrowmover: increase/decrease arrowmover to move arrowmover right/left
def n  = arrowmover;
def n1 = n + 1;

#2 Bar buy
def b1 = (low > Max(LowerBand, UpperBand));
def buy1Count = if b1 then buy1Count[1] + 1 else 0;
plot test = buy1Count;
test.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot buy1 = if !IsNaN(close[n]) and buy1Count[n1] == 2 then 1 else 0;
buy1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy1.SetDefaultColor(Color.UPTICK);
Alert(buy1, "Buy 1", Alert.BAR, Sound.Ding);


#2 Bar sell
def s1 = (high < Min(LowerBand, UpperBand));
def sell1Count = if s1 then sell1Count[1] + 1 else 0;
plot test2 = sell1count;
test2.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
plot sell1 = if !IsNaN(close[n]) and sell1Count[n1] == 2 then 1 else 0;
sell1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell1.SetDefaultColor(Color.DOWNTICK);
Alert(sell1, "Sell 1", Alert.BAR, Sound.Ding);
 
Last edited:
See if this works for you. By increasing/decreasing the arrowmover you can move arrow right/left. It will plot the arrow after the bar closes meeting your condition. The counts for buy/sell are plotted for testing and the circled areas are highlighting the arrows.
Hi, How do I incorporate this ArrowMover feature into the following? Thanks in advance for your help.

plot ArrowUp = if EMA9 crosses above SMA20
then low
else double.nan;
ArrowUP.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
ArrowUP.SetLineWeight(2);
ArrowUP.SetDefaultColor(Color.GREEN);

plot ArrowDN = if EMA9 crosses below SMA20
then high
else double.nan;
ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
ArrowDN.SetLineWeight(2);
ArrowDN.SetDefaultColor(Color.RED);
Alert(ArrowUp, " ", Alert.Bar, Sound.Ding);
Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);
 
Hi, How do I incorporate this ArrowMover feature into the following? Thanks in advance for your help.

plot ArrowUp = if EMA9 crosses above SMA20
then low
else double.nan;
ArrowUP.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
ArrowUP.SetLineWeight(2);
ArrowUP.SetDefaultColor(Color.GREEN);

plot ArrowDN = if EMA9 crosses below SMA20
then high
else double.nan;
ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
ArrowDN.SetLineWeight(2);
ArrowDN.SetDefaultColor(Color.RED);
Alert(ArrowUp, " ", Alert.Bar, Sound.Ding);
Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);

This has the arrowmover included, which was modified to fit with your code. When 'n' is set to zero, then both arrowmover is at the normal arrow plot. A negative number moves it n spaces to the left and a postive number to the right of the normal arrow.

Your arrowup and arrowdn was changed from low/high to 1/0 boolean to facilitate the arrowmover code adaption.

Screenshot-2022-12-01-094024.png
Ruby:
def ema9 = ExpAverage(close, 9);
def sma20 = SimpleMovingAvg(close, 20);

def Arrow_Up = if ema9 crosses above sma20 then 1 else 0;
def Arrow_DN = if ema9 crosses below sma20 then 1 else 0;


input arrowmover = -1;#Hint arrowmover: increase/decrease arrowmover to move arrowmover right/left
def n  = arrowmover;


#2 Bar up
def upCount  = if Arrow_Up then upCount[1] + 1 else 0;

plot arrowup = if !IsNaN(close[n]) and upCount[n] == 1 then 1 else 0;
arrowup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrowup.SetDefaultColor(Color.UPTICK);


#2 Bar dn
def dnCount  = if Arrow_DN then dnCount[1] + 1 else 0;

plot arrowdn = if !IsNaN(close[n]) and dnCount[n] == 1 then 1 else 0;
arrowdn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
arrowdn.SetDefaultColor(Color.DOWNTICK);

Alert(Arrow_Up, " ", Alert.BAR, Sound.Ding);
Alert(Arrow_DN, " ", Alert.BAR, Sound.Bell);

#Test Bubbles
input test = yes;
AddChartBubble(test and arrowup == 1, low * .9995, "Off\nSet: \n" + n, Color.GREEN, no);
AddChartBubble(test and arrowdn == 1, high * 1.0005, "Off\nSet: \n" + n, Color.RED);
 
This has the arrowmover included, which was modified to fit with your code. When 'n' is set to zero, then both arrowmover is at the normal arrow plot. A negative number moves it n spaces to the left and a postive number to the right of the normal arrow.

Your arrowup and arrowdn was changed from low/high to 1/0 boolean to facilitate the arrowmover code adaption.
Thank you so much!
 

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
345 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