One study impact color on separate study?

apbreaux

New member
I'm utilizing the following two studies on a single time frame's chart. I'm wondering if the first study can influence the colored line of the second study? If so, how do I accomplish that? More specifically, when the cloud from the first study is green, for example, can I make it so the line of second study stays green despite a pullback (where it would normally turn red) in the chart; and only change to red when the green cloud disappears? Many thanks.

First Study:
Code:
#AAAStudyMyedits#

input ATRLength = 2;
input ATRMultiplier = 1;
input ShowLabels = yes;
input UseClose = no;
input HighlightState = yes;
input Alerts = yes;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then Highest(close, ATRLength) else Highest(high, ATRLength)) - atr;
def longStopPrev = if IsNaN(longStop[1]) then longStop else longStop[1];
def LS = if close[1] > longStopPrev then Max(longStop, longStopPrev) else longStop;

def shortStop = (if UseClose then Lowest(close, ATRLength) else Lowest(low, ATRLength)) + atr;
def shortStopPrev = if IsNaN(shortStop[1]) then shortStop else shortStop[1];
def SS = if close[1] < shortStopPrev then Min(shortStop, shortStopPrev) else shortStop;

def dir = if close > shortStopPrev then 1 else if close < longStopPrev then -1 else dir[1];
def direction = dir;

plot LongStopPlot = if direction == 1 then LS else Double.NaN;
LongStopPlot.SetDefaultColor(Color.GREEN);

plot ShortStopPlot = if direction == -1 then SS else Double.NaN;
ShortStopPlot.SetDefaultColor(Color.RED);

def midPricePlot = OHLC4;

def callentrySignal = direction == 1 and direction[1] == -1;
def putentrySignal = direction == -1 and direction[1] == 1;

AddChartBubble(callentrySignal and ShowLabels, low, "C-In", Color.GREEN, no);
AddChartBubble(putentrySignal and ShowLabels, high, "P-In", Color.RED, yes);

AddCloud(if direction == 1 and HighlightState then LongStopPlot else Double.NaN, if direction == 1 and HighlightState then midPricePlot else Double.NaN, Color.LIGHT_GREEN , Color.LIGHT_GREEN, no);

AddCloud(if direction == -1 and HighlightState then ShortStopPlot else Double.NaN, if direction == -1 and HighlightState then midPricePlot else Double.NaN, Color.LIGHT_RED, Color.LIGHT_RED, no);

Alert(direction == 1 and direction[1] == -1 and Alerts, "Call", Alert.ONCE, Sound.Ding);
Alert(direction == -1 and direction[1] == 1 and Alerts, "Put", Alert.ONCE, Sound.Ding);


Second Study:
Code:
#thinkscript.com#
#by Ramesh16#

input displace = 0;
input length = 1;
input price = ohlc4;
input movingAverageType = {default Simple, Exponential, Weighted, Hull, Variable};

rec data;

switch (movingAverageType) {
case Simple:
    data = CompoundValue(1, Average(price[-displace], length), price);
case Exponential:
    data = CompoundValue(1, ExpAverage(price[-displace], length), price);
case Weighted:
    data = CompoundValue(1, WMA(price[-displace], length), price);
case Hull:
    data = CompoundValue(1, HullMovingAvg(price[-displace], length), price);
case Variable:
    data = VariableMA(price = price, length = length);
}

plot ave = data;

ave.SetLineWeight(3);
ave.AssignValueColor(if ave > ave[1] then Color.GREEN else Color.RED);
ave.HideBubble();
def lastbar = IsNaN(close[-1]) and !IsNaN(close);
#AddChartBubble(lastbar, data, "MA" + Length, Color.DARK_ORANGE);
 
Last edited:
Solution
I'm utilizing the following two studies on a single time frame's chart. I'm wondering if the first study can influence the colored line of the second study? If so, how do I accomplish that? More specifically, when the cloud from the first study is green, for example, can I make it so the line of second study stays green despite a pullback (where it would normally turn red) in the chart; and only change to red when the green cloud disappears? Many thanks.

First Study:
Code:
#AAAStudyMyedits#

input ATRLength = 2;
input ATRMultiplier = 1;
input ShowLabels = yes;
input UseClose = no;
input HighlightState = yes;
input Alerts = yes;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then Highest(close, ATRLength) else...
I'm utilizing the following two studies on a single time frame's chart. I'm wondering if the first study can influence the colored line of the second study? If so, how do I accomplish that? More specifically, when the cloud from the first study is green, for example, can I make it so the line of second study stays green despite a pullback (where it would normally turn red) in the chart; and only change to red when the green cloud disappears? Many thanks.

First Study:
Code:
#AAAStudyMyedits#

input ATRLength = 2;
input ATRMultiplier = 1;
input ShowLabels = yes;
input UseClose = no;
input HighlightState = yes;
input Alerts = yes;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then Highest(close, ATRLength) else Highest(high, ATRLength)) - atr;
def longStopPrev = if IsNaN(longStop[1]) then longStop else longStop[1];
def LS = if close[1] > longStopPrev then Max(longStop, longStopPrev) else longStop;

def shortStop = (if UseClose then Lowest(close, ATRLength) else Lowest(low, ATRLength)) + atr;
def shortStopPrev = if IsNaN(shortStop[1]) then shortStop else shortStop[1];
def SS = if close[1] < shortStopPrev then Min(shortStop, shortStopPrev) else shortStop;

def dir = if close > shortStopPrev then 1 else if close < longStopPrev then -1 else dir[1];
def direction = dir;

plot LongStopPlot = if direction == 1 then LS else Double.NaN;
LongStopPlot.SetDefaultColor(Color.GREEN);

plot ShortStopPlot = if direction == -1 then SS else Double.NaN;
ShortStopPlot.SetDefaultColor(Color.RED);

def midPricePlot = OHLC4;

def callentrySignal = direction == 1 and direction[1] == -1;
def putentrySignal = direction == -1 and direction[1] == 1;

AddChartBubble(callentrySignal and ShowLabels, low, "C-In", Color.GREEN, no);
AddChartBubble(putentrySignal and ShowLabels, high, "P-In", Color.RED, yes);

AddCloud(if direction == 1 and HighlightState then LongStopPlot else Double.NaN, if direction == 1 and HighlightState then midPricePlot else Double.NaN, Color.LIGHT_GREEN , Color.LIGHT_GREEN, no);

AddCloud(if direction == -1 and HighlightState then ShortStopPlot else Double.NaN, if direction == -1 and HighlightState then midPricePlot else Double.NaN, Color.LIGHT_RED, Color.LIGHT_RED, no);

Alert(direction == 1 and direction[1] == -1 and Alerts, "Call", Alert.ONCE, Sound.Ding);
Alert(direction == -1 and direction[1] == 1 and Alerts, "Put", Alert.ONCE, Sound.Ding);


Second Study:
Code:
#thinkscript.com#
#by Ramesh16#

input displace = 0;
input length = 1;
input price = ohlc4;
input movingAverageType = {default Simple, Exponential, Weighted, Hull, Variable};

rec data;

switch (movingAverageType) {
case Simple:
    data = CompoundValue(1, Average(price[-displace], length), price);
case Exponential:
    data = CompoundValue(1, ExpAverage(price[-displace], length), price);
case Weighted:
    data = CompoundValue(1, WMA(price[-displace], length), price);
case Hull:
    data = CompoundValue(1, HullMovingAvg(price[-displace], length), price);
case Variable:
    data = VariableMA(price = price, length = length);
}

plot ave = data;

ave.SetLineWeight(3);
ave.AssignValueColor(if ave > ave[1] then Color.GREEN else Color.RED);
ave.HideBubble();
def lastbar = IsNaN(close[-1]) and !IsNaN(close);
#AddChartBubble(lastbar, data, "MA" + Length, Color.DARK_ORANGE);

This combines the 2 studies into 1 study, where the direction from your 1st study above can influence the color of the ave plot in the 2nd study above.

These scripts combined easily as there were not any duplicated data in either. If there were duplicated data, we could have used the script function.

The image shows the combined script in the upper chart and your 2nd study in the lower chart. You can see how the combined study influences the color of the plot ave.

Screenshot 2023-11-09 071256.png
Code:
#AAAStudyMyedits#

input ATRLength = 2;
input ATRMultiplier = 1;
input ShowLabels = yes;
input UseClose = no;
input HighlightState = yes;
input Alerts = yes;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then Highest(close, ATRLength) else Highest(high, ATRLength)) - atr;
def longStopPrev = if IsNaN(longStop[1]) then longStop else longStop[1];
def LS = if close[1] > longStopPrev then Max(longStop, longStopPrev) else longStop;

def shortStop = (if UseClose then Lowest(close, ATRLength) else Lowest(low, ATRLength)) + atr;
def shortStopPrev = if IsNaN(shortStop[1]) then shortStop else shortStop[1];
def SS = if close[1] < shortStopPrev then Min(shortStop, shortStopPrev) else shortStop;

def dir = if close > shortStopPrev then 1 else if close < longStopPrev then -1 else dir[1];
def direction = dir;

plot LongStopPlot = if direction == 1 then LS else Double.NaN;
LongStopPlot.SetDefaultColor(Color.GREEN);

plot ShortStopPlot = if direction == -1 then SS else Double.NaN;
ShortStopPlot.SetDefaultColor(Color.RED);

def midPricePlot = OHLC4;

def callentrySignal = direction == 1 and direction[1] == -1;
def putentrySignal = direction == -1 and direction[1] == 1;

AddChartBubble(callentrySignal and ShowLabels, low, "C-In", Color.GREEN, no);
AddChartBubble(putentrySignal and ShowLabels, high, "P-In", Color.RED, yes);

AddCloud(if direction == 1 and HighlightState then LongStopPlot else Double.NaN, if direction == 1 and HighlightState then midPricePlot else Double.NaN, Color.LIGHT_GREEN , Color.LIGHT_GREEN, no);

AddCloud(if direction == -1 and HighlightState then ShortStopPlot else Double.NaN, if direction == -1 and HighlightState then midPricePlot else Double.NaN, Color.LIGHT_RED, Color.LIGHT_RED, no);

Alert(direction == 1 and direction[1] == -1 and Alerts, "Call", Alert.ONCE, Sound.Ding);
Alert(direction == -1 and direction[1] == 1 and Alerts, "Put", Alert.ONCE, Sound.Ding);

#thinkscript.com#
#by Ramesh16#

input displace = 0;
input length = 1;
input price = ohlc4;
input movingAverageType = {default Simple, Exponential, Weighted, Hull, Variable};

rec data;

switch (movingAverageType) {
case Simple:
    data = CompoundValue(1, Average(price[-displace], length), price);
case Exponential:
    data = CompoundValue(1, ExpAverage(price[-displace], length), price);
case Weighted:
    data = CompoundValue(1, WMA(price[-displace], length), price);
case Hull:
    data = CompoundValue(1, HullMovingAvg(price[-displace], length), price);
case Variable:
    data = VariableMA(price = price, length = length);
}

plot ave = data;

ave.SetLineWeight(3);
ave.AssignValueColor(if ave > ave[1] or direction == 1 then Color.GREEN else Color.RED);
ave.HideBubble();
def lastbar = IsNaN(close[-1]) and !IsNaN(close);
#AddChartBubble(lastbar, data, "MA" + Length, Color.DARK_ORANGE);
 
Solution

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