Help Simple Code (If xxx Then show green arrow)

javashot

New member
I have had decent luck finding codes and tweaking them to meet my needs but having trouble on what seems to be the simplest one so far.

I am looking to show red/green arrows above or below each candle for each condition met. Basically each candle will have multiple arrows on it.

If candle is above 9ema show green arrow, if not then red arrow
If candle is above 20ema show green arrow, if not then red arrow
If 2+ of last 3 candles are bull, show green arrow, if not then red arrow
If bull engulfing candle show green arrow
If bear engulfing show red arrow

also if possible add this part.

If bull candle closed high price = start of bear candle at exact price show 2 red arrows (indicating a strong resistance similar to engulfing)

Me struggling here= Can I define a candle as "hollow candle or Filled" or do I need to define it by open and close price?

Thanks,

Im happy to send a SBUX gc your way for help.

- Java
 
Last edited:
Can I ask why you're trying to use so many arrows? I don't think you can plot multiple arrows on 1 candle unless it's 1 up arrow and 1 down arrow. What I think you can do is have color coordinated arrows for different conditions. So if it's above the 9ema you get one color, if it's above the 20ema you get another color, if it's above both the 20ema and 9ema you get a third color, etc. etc. Would that be of any use to you? I imagine it'd be pretty mind boggling to try to sort that out with how many signals you're looking for and I hate to say it but that wouldn't be a simple thing to code. You'd need to define many conditions where multiple signals overlap.
 
Last edited:
there can be many non boolean arrows on a candle, each placed at it's own price level.

https://tlc.thinkorswim.com/center/.../PaintingStrategy/PaintingStrategy-ARROW-DOWN

you can use open and close to define how the candle is moving.
Code:
def up = (close > open);
def down = (close < open);

an idea, if you only care about the last candle, you could use labels to display some words and colors.

another idea is to make a lower study and have rows of points. a row for each condition, that changes colors.

i will try to put together an example arrow study tomorrow.
 
So many arrows will be confusing and eventually one will be lost behind another. I agree w/ @halconguy. What you need is called a dashboard.
Now you have @halcyonguy script to define how the candle is moving and given that the rest of your filters can be found easily by searching this forum, you have everything you need to build a dashboard. The dashboard will show a plotted line for each of your filters making it simple to see when they line up. When you are done, it will look something like this:
n2pPlMC.png

Search this forum for dashboard to find scripting examples.
HTH
 
here is a sample of my dot row template.
copy it for as many true/false conditions you wish to display.
change the sig1 formula to your condition
change the price level with sig1row

Ruby:
def na = double.nan;

# change this to be a true false formula
def sig1 = 1;

def sig1row = -1;
plot sig1grn = if sig1 then sig1row else na;
sig1grn.SetPaintingStrategy(PaintingStrategy.POINTS);
sig1grn.SetDefaultColor(Color.green);
sig1grn.setlineweight(4);

plot sig1red = if !sig1 then sig1row else na;
sig1red.SetPaintingStrategy(PaintingStrategy.POINTS);
sig1red.SetDefaultColor(Color.red);
sig1red.setlineweight(4);

there are other ways to do it, to use 1 plot and define colors. but i made this a long time ago.
 
I have had decent luck finding codes and tweaking them to meet my needs but having trouble on what seems to be the simplest one so far.
I am looking to show red/green arrows above or below each candle for each condition met. Basically each candle will have multiple arrows on it.

1. If candle is above 9ema show green arrow, if not then red arrow
2. If candle is above 20ema show green arrow, if not then red arrow
3. If 2+ of last 3 candles are bull, show green arrow, if not then red arrow
4. If bull engulfing candle show green arrow , If bear engulfing show red arrow

here is a study that draws arrows from 4 conditions.
it places the arrows on different price levels , 4 rows above the candles, 4 rows below the candles.
as others have mentioned, it is messy.

# draw arrows when
# closest row of arrows:
# If candle is above 9ema show green up arrow, if not then red down arrow
# next row of arrows:
# If candle is above 20ema show green up arrow, if not then red down arrow
# if the 9ema and 20ema crossings are different , then draw cyan arrows
# next row of arrows:
# If 2+ of last 3 candles are bull, show yellow up arrow, if not then blue down arrow
# next row of arrows:
# If bull engulfing candle show white up arrow. If bear engulfing show magenta down arrow

Ruby:
# arrowsfrom_emalevels_01

# ---------------
# halcyonguy
# 21-06-08
# draw arrows when conditions are triggered
# ---------------

# draw arrows when
# closest row of arrows:
#   If candle is above 9ema show green up arrow, if not then red down arrow
# next row of arrows:
#   If candle is above 20ema show green up arrow, if not then red down arrow
#      if the 9ema and 20ema crossings are different , then draw cyan arrows
# next row of arrows:
#   If 2+ of last 3 candles are bull, show yellow up arrow, if not then blue down arrow
# next row of arrows:
#  If bull engulfing candle show white up arrow. If bear engulfing show magenta down arrow


#  didn't do this
# ?? if bull candle closed high price = start of bear candle at exact price show 2 red arrows (indicating a strong resistance similar to engulfing)


input show_average_lines = yes;

input price = close;

def na = double.nan;

# spacing factor between rows of arrows
input arrow_spacing = 0.005;

def up = (close > open);
def down = (close < open);

input length1 = 9;
input avg_type1 = AverageType.EXPONENTIAL;
def avg1 = MovingAverage(avg_type1, price, length1);

input length2 = 20;
input avg_type2 = AverageType.EXPONENTIAL;
def avg2 = MovingAverage(avg_type2, price, length2);

# show average lines
plot av1 = if show_average_lines then avg1 else na;
av1.SetDefaultColor(Color.cyan);
plot av2 = if show_average_lines then avg2 else na;
av2.SetDefaultColor(Color.yellow);

def aboveavg1 = (price > avg1);
def belowavg1 = (price < avg1);
def aboveavg2 = (price > avg2);
def belowavg2 = (price < avg2);

# are the 2 average conditions opposite ?
def avg1avg2opp = (aboveavg1 and belowavg2) or (belowavg1 and aboveavg2);

# ------------------------

#  If candle is above 9ema show green arrow, if not then red arrow
def incr1 = 1;
plot aboveav1 = if aboveavg1 then (low * (1 - (incr1 * arrow_spacing))) else na;
aboveav1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
aboveav1.DefineColor("Up", color.green);
aboveav1.DefineColor("opp", color.cyan);
aboveav1.AssignValueColor(if avg1avg2opp then aboveav1.color("opp") else aboveav1.color("up"));
#aboveav1.SetDefaultColor(Color.green);
aboveav1.setlineweight(1);
aboveav1.hidebubble();

plot belowav1 = if belowavg1 then (high * (1 + (incr1 * arrow_spacing))) else na;
belowav1.SetPaintingStrategy(PaintingStrategy.ARROW_down);
belowav1.DefineColor("Down", color.red);
belowav1.DefineColor("opp", color.cyan);
belowav1.AssignValueColor(if avg1avg2opp then belowav1.color("opp") else belowav1.color("down"));
#belowav1.SetDefaultColor(Color.red);
belowav1.setlineweight(1);
belowav1.hidebubble();

#  If candle is above 20ema show green arrow, if not then red arrow
def incr2 = 2;
plot aboveav2 = if aboveavg2 then (low * (1 - (incr2 * arrow_spacing))) else na;
aboveav2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
aboveav2.DefineColor("Up", color.green);
aboveav2.DefineColor("opp", color.cyan);
aboveav2.AssignValueColor(if avg1avg2opp then aboveav2.color("opp") else aboveav2.color("up"));
#aboveav2.SetDefaultColor(Color.green);
aboveav2.setlineweight(1);
aboveav2.hidebubble();

plot belowav2 = if belowavg2 then (high * (1 + (incr2 * arrow_spacing))) else na;
belowav2.SetPaintingStrategy(PaintingStrategy.ARROW_down);
belowav2.DefineColor("Down", color.red);
belowav2.DefineColor("opp", color.cyan);
belowav2.AssignValueColor(if avg1avg2opp then belowav2.color("opp") else belowav2.color("down"));
#belowav2.SetDefaultColor(Color.red);
belowav2.setlineweight(1);
belowav2.hidebubble();

# ------------------------------------------

# 2 out of last 3 bars are bull/bear
def bullbearlen = 3;
def bull3bars = (sum(up, bullbearlen) >= 2);
def bear3bars = (sum(down, bullbearlen) >= 2);

def incr3 = 3;
plot bull3 = if bull3bars then (low * (1 - (incr3 * arrow_spacing))) else na;
bull3.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bull3.SetDefaultColor(Color.yellow);
bull3.setlineweight(4);
bull3.hidebubble();

plot bear3 = if bear3bars then (high * (1 + (incr3 * arrow_spacing))) else na;
bear3.SetPaintingStrategy(PaintingStrategy.ARROW_down);
bear3.SetDefaultColor(Color.blue);
bear3.setlineweight(4);
bear3.hidebubble();


# ------------------------------------------

# If bull engulfing candle show white arrow , If bear engulfing show magenta arrow
# bullish engulfing:  after a red bar,  a green bar, opens below prev close, and closes above prev open
def bullengulfing = down[1] and (open < close[1]) and ( close > open[1]);
# bearish engulfing:  after a green bar,  a red bar, opens above prev close, and closes below prev open
def bearengulfing = up[1] and (open > close[1]) and ( close < open[1]);

def incr4 = 4;
plot bulleng = if bullengulfing then (low * (1 - (incr4 * arrow_spacing))) else na;
bulleng.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bulleng.SetDefaultColor(Color.white);
bulleng.setlineweight(4);
bulleng.hidebubble();

plot beareng = if bearengulfing then (high * (1 + (incr4 * arrow_spacing))) else na;
beareng.SetPaintingStrategy(PaintingStrategy.ARROW_down);
beareng.SetDefaultColor(Color.magenta);
beareng.setlineweight(4);
beareng.hidebubble();

#

k3SfZ6J.jpg
 
here is a study that draws arrows from 4 conditions.
it places the arrows on different price levels , 4 rows above the candles, 4 rows below the candles.
as others have mentioned, it is messy.

# draw arrows when
# closest row of arrows:
# If candle is above 9ema show green up arrow, if not then red down arrow
# next row of arrows:
# If candle is above 20ema show green up arrow, if not then red down arrow
# if the 9ema and 20ema crossings are different , then draw cyan arrows
# next row of arrows:
# If 2+ of last 3 candles are bull, show yellow up arrow, if not then blue down arrow
# next row of arrows:
# If bull engulfing candle show white up arrow. If bear engulfing show magenta down arrow

Ruby:
# arrowsfrom_emalevels_01

# ---------------
# halcyonguy
# 21-06-08
# draw arrows when conditions are triggered
# ---------------

# draw arrows when
# closest row of arrows:
#   If candle is above 9ema show green up arrow, if not then red down arrow
# next row of arrows:
#   If candle is above 20ema show green up arrow, if not then red down arrow
#      if the 9ema and 20ema crossings are different , then draw cyan arrows
# next row of arrows:
#   If 2+ of last 3 candles are bull, show yellow up arrow, if not then blue down arrow
# next row of arrows:
#  If bull engulfing candle show white up arrow. If bear engulfing show magenta down arrow


#  didn't do this
# ?? if bull candle closed high price = start of bear candle at exact price show 2 red arrows (indicating a strong resistance similar to engulfing)


input show_average_lines = yes;

input price = close;

def na = double.nan;

# spacing factor between rows of arrows
input arrow_spacing = 0.005;

def up = (close > open);
def down = (close < open);

input length1 = 9;
input avg_type1 = AverageType.EXPONENTIAL;
def avg1 = MovingAverage(avg_type1, price, length1);

input length2 = 20;
input avg_type2 = AverageType.EXPONENTIAL;
def avg2 = MovingAverage(avg_type2, price, length2);

# show average lines
plot av1 = if show_average_lines then avg1 else na;
av1.SetDefaultColor(Color.cyan);
plot av2 = if show_average_lines then avg2 else na;
av2.SetDefaultColor(Color.yellow);

def aboveavg1 = (price > avg1);
def belowavg1 = (price < avg1);
def aboveavg2 = (price > avg2);
def belowavg2 = (price < avg2);

# are the 2 average conditions opposite ?
def avg1avg2opp = (aboveavg1 and belowavg2) or (belowavg1 and aboveavg2);

# ------------------------

#  If candle is above 9ema show green arrow, if not then red arrow
def incr1 = 1;
plot aboveav1 = if aboveavg1 then (low * (1 - (incr1 * arrow_spacing))) else na;
aboveav1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
aboveav1.DefineColor("Up", color.green);
aboveav1.DefineColor("opp", color.cyan);
aboveav1.AssignValueColor(if avg1avg2opp then aboveav1.color("opp") else aboveav1.color("up"));
#aboveav1.SetDefaultColor(Color.green);
aboveav1.setlineweight(1);
aboveav1.hidebubble();

plot belowav1 = if belowavg1 then (high * (1 + (incr1 * arrow_spacing))) else na;
belowav1.SetPaintingStrategy(PaintingStrategy.ARROW_down);
belowav1.DefineColor("Down", color.red);
belowav1.DefineColor("opp", color.cyan);
belowav1.AssignValueColor(if avg1avg2opp then belowav1.color("opp") else belowav1.color("down"));
#belowav1.SetDefaultColor(Color.red);
belowav1.setlineweight(1);
belowav1.hidebubble();

#  If candle is above 20ema show green arrow, if not then red arrow
def incr2 = 2;
plot aboveav2 = if aboveavg2 then (low * (1 - (incr2 * arrow_spacing))) else na;
aboveav2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
aboveav2.DefineColor("Up", color.green);
aboveav2.DefineColor("opp", color.cyan);
aboveav2.AssignValueColor(if avg1avg2opp then aboveav2.color("opp") else aboveav2.color("up"));
#aboveav2.SetDefaultColor(Color.green);
aboveav2.setlineweight(1);
aboveav2.hidebubble();

plot belowav2 = if belowavg2 then (high * (1 + (incr2 * arrow_spacing))) else na;
belowav2.SetPaintingStrategy(PaintingStrategy.ARROW_down);
belowav2.DefineColor("Down", color.red);
belowav2.DefineColor("opp", color.cyan);
belowav2.AssignValueColor(if avg1avg2opp then belowav2.color("opp") else belowav2.color("down"));
#belowav2.SetDefaultColor(Color.red);
belowav2.setlineweight(1);
belowav2.hidebubble();

# ------------------------------------------

# 2 out of last 3 bars are bull/bear
def bullbearlen = 3;
def bull3bars = (sum(up, bullbearlen) >= 2);
def bear3bars = (sum(down, bullbearlen) >= 2);

def incr3 = 3;
plot bull3 = if bull3bars then (low * (1 - (incr3 * arrow_spacing))) else na;
bull3.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bull3.SetDefaultColor(Color.yellow);
bull3.setlineweight(4);
bull3.hidebubble();

plot bear3 = if bear3bars then (high * (1 + (incr3 * arrow_spacing))) else na;
bear3.SetPaintingStrategy(PaintingStrategy.ARROW_down);
bear3.SetDefaultColor(Color.blue);
bear3.setlineweight(4);
bear3.hidebubble();


# ------------------------------------------

# If bull engulfing candle show white arrow , If bear engulfing show magenta arrow
# bullish engulfing:  after a red bar,  a green bar, opens below prev close, and closes above prev open
def bullengulfing = down[1] and (open < close[1]) and ( close > open[1]);
# bearish engulfing:  after a green bar,  a red bar, opens above prev close, and closes below prev open
def bearengulfing = up[1] and (open > close[1]) and ( close < open[1]);

def incr4 = 4;
plot bulleng = if bullengulfing then (low * (1 - (incr4 * arrow_spacing))) else na;
bulleng.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bulleng.SetDefaultColor(Color.white);
bulleng.setlineweight(4);
bulleng.hidebubble();

plot beareng = if bearengulfing then (high * (1 + (incr4 * arrow_spacing))) else na;
beareng.SetPaintingStrategy(PaintingStrategy.ARROW_down);
beareng.SetDefaultColor(Color.magenta);
beareng.setlineweight(4);
beareng.hidebubble();

#

k3SfZ6J.jpg
Is there a way to add a column to watchlist for which stock is riding the intra day 9ema above or below the last 3 candle sticks with options for 1 and 5m? Green color for above and Red color for below
 
Last edited:
How can we make this into a column in the scanner? I want to scan all my stocks and see which one is riding the intra day 9ema above or below the last 3 candle sticks

to make a scanner, need to come up with a true/false formula.
'which one' what ? ooh, above or below?
'riding' is vague and confusing. what do you really mean, in terms of numbers?

------------------
maybe this will help

this is set up to be true if,
close is within 0.7% of EMA9 , for 3 bars, either above or below the average.


Code:
# near_ma_by_x_percent_01

def na = Double.NaN;
def bn = BarNumber();
def price = close;

input ma1_len = 9;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input percent = 0.7;

def per = (100*absvalue(close - ma1)/ma1);
def near = per < percent;
def xup = close > ma1 and near;
def xdwn = close < ma1 and near;

input sequential_bars = 3;
def isupseq = sum(xup, sequential_bars) == sequential_bars;
def isdwnseq = sum(xdwn, sequential_bars) == sequential_bars;


# scan code. this is true if close is withing a ma for x bars
plot z = isupseq or isdwnseq;


---------------------------

this is a study for testing

Code:
# near_ma_by_x_percent_01

def na = Double.NaN;
def bn = BarNumber();
def price = close;

input ma1_len = 9;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input percent = 0.7;

def per = (100*absvalue(close - ma1)/ma1);
def near = per < percent;
def xup = close > ma1 and near;
def xdwn = close < ma1 and near;

input sequential_bars = 3;
def isupseq = sum(xup, sequential_bars) == sequential_bars;
def isdwnseq = sum(xdwn, sequential_bars) == sequential_bars;


# scan code. this is true if close is withing a ma for x bars
# plot z = isupseq or isdwnseq;


#=======================================

input show_lines = yes;
plot z1 = if show_lines then ma1 else na;
z1.SetDefaultColor(GetColor(1));
#z1.setlineweight(1);
z1.HideBubble();
#

input arrows = yes;
plot zu = if arrows then isupseq else na;
zu.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
zu.SetDefaultColor(color.green);

plot zd = if arrows then isdwnseq else na;
zd.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
zd.SetDefaultColor(color.red);


#addverticalline(isupseq, "-", color.green);
#addverticalline(isdwnseq, "-", color.red);

input test1 = no;
addchartbubble(test1, low,
per + " %\n" +
near + " near\n" +
isupseq + " isup\n" +
isdwnseq  + " isdwn"
, color.yellow, no);
#
 
to make a scanner, need to come up with a true/false formula.
'which one' what ? ooh, above or below?
'riding' is vague and confusing. what do you really mean, in terms of numbers?

------------------
maybe this will help

this is set up to be true if,
close is within 0.7% of EMA9 , for 3 bars, either above or below the average.


Code:
# near_ma_by_x_percent_01

def na = Double.NaN;
def bn = BarNumber();
def price = close;

input ma1_len = 9;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input percent = 0.7;

def per = (100*absvalue(close - ma1)/ma1);
def near = per < percent;
def xup = close > ma1 and near;
def xdwn = close < ma1 and near;

input sequential_bars = 3;
def isupseq = sum(xup, sequential_bars) == sequential_bars;
def isdwnseq = sum(xdwn, sequential_bars) == sequential_bars;


# scan code. this is true if close is withing a ma for x bars
plot z = isupseq or isdwnseq;


---------------------------

this is a study for testing

Code:
# near_ma_by_x_percent_01

def na = Double.NaN;
def bn = BarNumber();
def price = close;

input ma1_len = 9;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input percent = 0.7;

def per = (100*absvalue(close - ma1)/ma1);
def near = per < percent;
def xup = close > ma1 and near;
def xdwn = close < ma1 and near;

input sequential_bars = 3;
def isupseq = sum(xup, sequential_bars) == sequential_bars;
def isdwnseq = sum(xdwn, sequential_bars) == sequential_bars;


# scan code. this is true if close is withing a ma for x bars
# plot z = isupseq or isdwnseq;


#=======================================

input show_lines = yes;
plot z1 = if show_lines then ma1 else na;
z1.SetDefaultColor(GetColor(1));
#z1.setlineweight(1);
z1.HideBubble();
#

input arrows = yes;
plot zu = if arrows then isupseq else na;
zu.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
zu.SetDefaultColor(color.green);

plot zd = if arrows then isdwnseq else na;
zd.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
zd.SetDefaultColor(color.red);


#addverticalline(isupseq, "-", color.green);
#addverticalline(isdwnseq, "-", color.red);

input test1 = no;
addchartbubble(test1, low,
per + " %\n" +
near + " near\n" +
isupseq + " isup\n" +
isdwnseq  + " isdwn"
, color.yellow, no);
#
Thanks! I'll give this a try.
 

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