Plotting Box around candle

Learnbot

Active member
Hello,
I have simple strategy MACD value crosses above average, when this happens arrow gets plotted on the chart as shown below. What i was wondering if someone can help me with is automatically drawing a box around 3 candles after the MACD cross occurs (refer to the hand drawn box in the image below)

Thank you, hopefully it is something simple and doable :)

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


YQRdOZ4.png
 
Solution
@SleepyZ I tried to modify your code to add it to my MTF indicator and it is not working, can you help me with what am I doing wrong?

Code:
#Jay_MTF_Stoch_MOMO

def agg = AggregationPeriod.THREE_MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low3 = Lowest(low(period = agg), percentKLength);
def max_high3 = Highest(high(period = agg), percentKLength);
def rel_diff3 = close(period = agg) - (max_high3 + min_low3) / 2;
def diff3 = max_high3 - min_low3;

def avgrel3 = ExpAverage(ExpAverage(rel_diff3, percentDLength), percentDLength);
def avgdiff3 = ExpAverage(ExpAverage(diff3, percentDLength), percentDLength);

def SMI3 = if avgdiff3 != 0 then avgrel3 / (avgdiff3 /...
Hello,
I have simple strategy MACD value crosses above average, when this happens arrow gets plotted on the chart as shown below. What i was wondering if someone can help me with is automatically drawing a box around 3 candles after the MACD cross occurs (refer to the hand drawn box in the image below)

Thank you, hopefully it is something simple and doable :)

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


YQRdOZ4.png

This seems to work for upsignals and or downsignals

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

#declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then low else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then high else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#Mark 3 bars at Up or Down Signal-----------------------------------------
input show_downsignal_cloud = no;
def dbn=(if !isnan(downsignal) then 1 else 0);
def drange = if dbn==1 then 1 else drange[1]+1;
def dhr = if dbn==1 then high else if drange<=3 and high>dhr[1] then high else dhr[1];
def dlr = if dbn==1 then low else if drange<=3 and low<dlr[1] then low else dlr[1];
def dh=if between(drange,1,3) then dhr[-3] else double.nan;
def dl=if between(drange,1,3) then dlr[-3] else double.nan;
addcloud(if !show_downsignal_cloud then double.nan else dh,dl,color.white,color.white);

input show_upsignal_cloud=yes;
def ubn=(if !isnan(upsignal) then 1 else 0);
def urange = if ubn==1 then 1 else urange[1]+1;
def uhr = if ubn==1 then high else if urange<=3 and high>uhr[1] then high else uhr[1];
def ulr = if ubn==1 then low else if urange<=3 and low<ulr[1] then low else ulr[1];
def uh=if between(urange,1,3) then uhr[-3] else double.nan;
def ul=if between(urange,1,3) then ulr[-3] else double.nan;
addcloud(if !show_upsignal_cloud then double.nan else uh,ul,color.white,color.white);
 

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

@SleepyZ I tried to modify your code to add it to my MTF indicator and it is not working, can you help me with what am I doing wrong?

Code:
#Jay_MTF_Stoch_MOMO

def agg = AggregationPeriod.THREE_MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low3 = Lowest(low(period = agg), percentKLength);
def max_high3 = Highest(high(period = agg), percentKLength);
def rel_diff3 = close(period = agg) - (max_high3 + min_low3) / 2;
def diff3 = max_high3 - min_low3;

def avgrel3 = ExpAverage(ExpAverage(rel_diff3, percentDLength), percentDLength);
def avgdiff3 = ExpAverage(ExpAverage(diff3, percentDLength), percentDLength);

def SMI3 = if avgdiff3 != 0 then avgrel3 / (avgdiff3 / 2) * 100 else 0;

def AvgSMI3 = ExpAverage(SMI3, percentDLength);
###################################################


def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

DEF SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
DEF AvgSMI = expaverage(smi, percentDLength);
#######################################################



def l3 =  smi3>avgsmi3 and smi3 and avgsmi3 is less than or equal to over_sold;
def l1 =   smi>avgsmi and smi and avgsmi is less than or equal to over_sold;

def s3 =  smi3<avgsmi3 and smi3 and avgsmi3 is greater than or equal to over_bought;
def s1 =   smi<avgsmi and smi and avgsmi is greater than or equal to over_bought;


plot long = l3 and l1 is true;
plot short = s3 and s1 is true;


AssignPriceColor ( if long then color.yellow else if short then color.white else color.current);

I changed:
Code:
def ubn=(if !isnan(upsignal) then 1 else 0);
to
Code:
def ubn=(if !isnan(long) then 1 else 0);

but now it shows cloud on all the bars, what am i doing wrong?

ty
 
@SleepyZ I tried to modify your code to add it to my MTF indicator and it is not working, can you help me with what am I doing wrong?

Code:
#Jay_MTF_Stoch_MOMO

def agg = AggregationPeriod.THREE_MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low3 = Lowest(low(period = agg), percentKLength);
def max_high3 = Highest(high(period = agg), percentKLength);
def rel_diff3 = close(period = agg) - (max_high3 + min_low3) / 2;
def diff3 = max_high3 - min_low3;

def avgrel3 = ExpAverage(ExpAverage(rel_diff3, percentDLength), percentDLength);
def avgdiff3 = ExpAverage(ExpAverage(diff3, percentDLength), percentDLength);

def SMI3 = if avgdiff3 != 0 then avgrel3 / (avgdiff3 / 2) * 100 else 0;

def AvgSMI3 = ExpAverage(SMI3, percentDLength);
###################################################


def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

DEF SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
DEF AvgSMI = expaverage(smi, percentDLength);
#######################################################



def l3 =  smi3>avgsmi3 and smi3 and avgsmi3 is less than or equal to over_sold;
def l1 =   smi>avgsmi and smi and avgsmi is less than or equal to over_sold;

def s3 =  smi3<avgsmi3 and smi3 and avgsmi3 is greater than or equal to over_bought;
def s1 =   smi<avgsmi and smi and avgsmi is greater than or equal to over_bought;


plot long = l3 and l1 is true;
plot short = s3 and s1 is true;


AssignPriceColor ( if long then color.yellow else if short then color.white else color.current);

I changed:
Code:
def ubn=(if !isnan(upsignal) then 1 else 0);
to
Code:
def ubn=(if !isnan(long) then 1 else 0);

but now it shows cloud on all the bars, what am i doing wrong?

ty
This seems to work

Ruby:
#Jay_MTF_Stoch_MOMO

def agg = AggregationPeriod.THREE_MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low3 = Lowest(low(period = agg), percentKLength);
def max_high3 = Highest(high(period = agg), percentKLength);
def rel_diff3 = close(period = agg) - (max_high3 + min_low3) / 2;
def diff3 = max_high3 - min_low3;

def avgrel3 = ExpAverage(ExpAverage(rel_diff3, percentDLength), percentDLength);
def avgdiff3 = ExpAverage(ExpAverage(diff3, percentDLength), percentDLength);

def SMI3 = if avgdiff3 != 0 then avgrel3 / (avgdiff3 / 2) * 100 else 0;

def AvgSMI3 = ExpAverage(SMI3, percentDLength);
###################################################


def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

DEF SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
DEF AvgSMI = expaverage(smi, percentDLength);
#######################################################



def l3 =  smi3>avgsmi3 and smi3 and avgsmi3 is less than or equal to over_sold;
def l1 =   smi>avgsmi and smi and avgsmi is less than or equal to over_sold;

def s3 =  smi3<avgsmi3 and smi3 and avgsmi3 is greater than or equal to over_bought;
def s1 =   smi<avgsmi and smi and avgsmi is greater than or equal to over_bought;


plot long = l3 and l1 is true;
plot short = s3 and s1 is true;


AssignPriceColor ( if long then color.yellow else if short then color.white else color.current);

#Mark 3 bars at Up or Down Signal-----------------------------------------
input show_downsignal_cloud = no;
def dbn=(if short then 1 else 0);
def drange = if dbn==1 then 1 else drange[1]+1;
def dhr = if dbn==1 then high else if drange<=3 and high>dhr[1] then high else dhr[1];
def dlr = if dbn==1 then low else if drange<=3 and low<dlr[1] then low else dlr[1];
def dh=if between(drange,1,3) then dhr[-3] else double.nan;
def dl=if between(drange,1,3) then dlr[-3] else double.nan;
addcloud(if !show_downsignal_cloud then double.nan else dh,dl,color.white,color.white);

input show_upsignal_cloud=yes;
def ubn=(if long then 1 else 0);
def urange = if ubn==1 then 1 else urange[1]+1;
def uhr = if ubn==1 then high else if urange<=3 and high>uhr[1] then high else uhr[1];
def ulr = if ubn==1 then low else if urange<=3 and low<ulr[1] then low else ulr[1];
def uh=if between(urange,1,3) then uhr[-3] else double.nan;
def ul=if between(urange,1,3) then ulr[-3] else double.nan;
addcloud(if !show_upsignal_cloud then double.nan else uh,ul,color.white,color.white);
 
Solution
This seems to work
#Jay_MTF_Stoch_MOMO

input aggregationPeriod = AggregationPeriod.DAY;
def open = open(period = aggregationPeriod);
def high = high(period = aggregationPeriod);
def low = low(period = aggregationPeriod);
def close = close(period = aggregationPeriod);

input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low3 = Lowest(low(period = aggregationPeriod), percentKLength);
def max_high3 = Highest(high(period = aggregationPeriod), percentKLength);
def rel_diff3 = close(period = aggregationPeriod) - (max_high3 + min_low3) / 2;
def diff3 = max_high3 - min_low3;

def avgrel3 = ExpAverage(ExpAverage(rel_diff3, percentDLength), percentDLength);
def avgdiff3 = ExpAverage(ExpAverage(diff3, percentDLength), percentDLength);

def SMI3 = if avgdiff3 != 0 then avgrel3 / (avgdiff3 / 2) * 100 else 0;

def AvgSMI3 = ExpAverage(SMI3, percentDLength);
###################################################


def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

DEF SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
DEF AvgSMI = expaverage(smi, percentDLength);
#######################################################



def l3 = smi3>avgsmi3 and smi3 and avgsmi3 is less than or equal to over_sold;
def l1 = smi>avgsmi and smi and avgsmi is less than or equal to over_sold;

def s3 = smi3<avgsmi3 and smi3 and avgsmi3 is greater than or equal to over_bought;
def s1 = smi<avgsmi and smi and avgsmi is greater than or equal to over_bought;


plot long = l3 and l1 is true;
plot short = s3 and s1 is true;


AssignPriceColor ( if long then color.yellow else if short then color.white else color.current);

#Mark 3 bars at Up or Down Signal-----------------------------------------
input show_downsignal_cloud = no;
def dbn=(if short then 1 else 0);
def drange = if dbn==1 then 1 else drange[1]+1;
def dhr = if dbn==1 then high else if drange<=3 and high>dhr[1] then high else dhr[1];
def dlr = if dbn==1 then low else if drange<=3 and low<dlr[1] then low else dlr[1];
def dh=if between(drange,1,3) then dhr[-3] else double.nan;
def dl=if between(drange,1,3) then dlr[-3] else double.nan;
addcloud(if !show_downsignal_cloud then double.nan else dh,dl,color.white,color.white);

input show_upsignal_cloud=yes;
def ubn=(if long then 1 else 0);
def urange = if ubn==1 then 1 else urange[1]+1;
def uhr = if ubn==1 then high else if urange<=3 and high>uhr[1] then high else uhr[1];
def ulr = if ubn==1 then low else if urange<=3 and low<ulr[1] then low else ulr[1];
def uh=if between(urange,1,3) then uhr[-3] else double.nan;
def ul=if between(urange,1,3) then ulr[-3] else double.nan;
addcloud(if !show_upsignal_cloud then double.nan else uh,ul,color.white,color.white);

 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
269 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