Need help to manage stop loss in HOLP/LOHP indicator

BETrader

New member
Plus
Dear ThinkScript community,
I am new with ThinkScript, I need your help.
I have modified the Thinkscript developped by @halcyonguy in order to plot an arrow on the chart when a buy signal or a sell signal is triggered. The code is given below, it works very well:

I would like to add an arrow when a stopsellsignal or a stopbuysignal is triggered.
The stopsellsignal is defined as follows:
- if a sell signal is in progress, stopsellsignal is trigerred either if low <= LowerBand_PC or if close >= high[2]. In that case an arrow up is plotted at the low of the current candle. Only the first arrow is plotted based on the first stop signal triggered.
The stopbuysignal is defined as follows:
- if a buy signal is in progress, stopbuysignal is trigerred either if high >= UpperBand_PC or if close <= low[2]. In that case an arrow down is plotted at the high of the current candle. Only the first arrow is plotted based on the first stop signal triggered.

- when a stop signal is triggered, the bur / sell sigal is no more active

In the attached chart, the green and red arrows are buy and sell signals. This is ok, it works well.
The dark green and dark red arrows are the stopsellsignal and the stopbuysignal. This is my problem, I would like some help to modified my script in order to plot theses stop arrows.

Many thanks in advance for your help.
Bruno

Code:
declare upper;

#INPUTS
input PriceChannel_Period = 21; #Length for the Price Chanbbel calculation
input Long_Short_SignalSize = 2; # Size of the Sell and Buy arrows
input Stop_SignalSize = 1; # Size of the arrows for stops
input Plot_Price_Chanel = yes; #plots the Price Channel
input Show_Stop_Signals = yes; #plots the stop signals

#Price Channel calculation
def LowerBand_PC = Lowest(low[1], PriceChannel_Period);
def UpperBand_PC = Highest(high[1], PriceChannel_Period);

#Price Channel High Breakout - Sell Signal

def highBar = high >= UpperBand_PC;

def LOHB = if highBar then low else LOHB[1];
def closedBelowLOHB = if highBar then 0 else if close < LOHB then closedBelowLOHB[1] + 1 else closedBelowLOHB[1];

def SellSignal = closedBelowLOHB == 1 and closedBelowLOHB[1] == 0;

#Price Channel Low Breakout - Buy Signal
def lowBar = low <= LowerBand_PC;

def HOLB = if lowBar then high else HOLB[1];
def closedAboveHOLB = if lowBar then 0 else if close > HOLB then closedAboveHOLB[1] + 1 else closedAboveHOLB[1];

def BuySignal = closedAboveHOLB == 1 and closedAboveHOLB[1] == 0;

#Short arrow signal - HOLB_LOHB Breakout
plot SellArrow = if SellSignal then high + 2 * TickSize() else Double.NaN;

SellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellArrow.SetDefaultColor(Color.RED);
SellArrow.SetLineWeight(Long_Short_SignalSize);

#Long arrow signal - HOLB_LOHB Breakout
plot BuyArrow = if BuySignal then low - 2 * TickSize() else Double.NaN;

BuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyArrow.SetDefaultColor(Color.GREEN);
BuyArrow.SetLineWeight(Long_Short_SignalSize);

#Plots Price Channel bands
plot PriceChannelUpperBand = if Plot_Price_Chanel then UpperBand_PC else Double.NaN;

PriceChannelUpperBand.SetPaintingStrategy(PaintingStrategy.LINE);
PriceChannelUpperBand.SetDefaultColor(Color.DARK_RED);
PriceChannelUpperBand.SetLineWeight(Long_Short_SignalSize);
PriceChannelUpperBand.SetStyle(Curve.FIRM);

plot PriceChannelLowerBand = if Plot_Price_Chanel then LowerBand_PC else Double.NaN;

PriceChannelLowerBand.SetPaintingStrategy(PaintingStrategy.LINE);
PriceChannelLowerBand.SetDefaultColor(Color.DARK_GREEN);
PriceChannelLowerBand.SetLineWeight(Long_Short_SignalSize);
PriceChannelLowerBand.SetStyle(Curve.FIRM);

# END
 

Attachments

  • Chart.png
    Chart.png
    41.6 KB · Views: 35
Last edited by a moderator:
Solution
If you make this into a strategy, be sure to write the Order lines for the open after the signal bars as your current conditions will allow the signal bar to repaint until the close of the bar.

Untitled.png


Ruby:
declare upper;

#INPUTS
input PriceChannel_Period = 21; #Length for the Price Chanbbel calculation
input Long_Short_SignalSize = 2; # Size of the Sell and Buy arrows
input Stop_SignalSize = 1; # Size of the arrows for stops
input Plot_Price_Chanel = yes; #plots the Price Channel
input Show_Stop_Signals = yes; #plots the stop signals

#Price Channel calculation
def LowerBand_PC = Lowest(low[1], PriceChannel_Period);
def UpperBand_PC = Highest(high[1], PriceChannel_Period);

#Price Channel High Breakout - Sell Signal

def highBar =...
If you make this into a strategy, be sure to write the Order lines for the open after the signal bars as your current conditions will allow the signal bar to repaint until the close of the bar.

Untitled.png


Ruby:
declare upper;

#INPUTS
input PriceChannel_Period = 21; #Length for the Price Chanbbel calculation
input Long_Short_SignalSize = 2; # Size of the Sell and Buy arrows
input Stop_SignalSize = 1; # Size of the arrows for stops
input Plot_Price_Chanel = yes; #plots the Price Channel
input Show_Stop_Signals = yes; #plots the stop signals

#Price Channel calculation
def LowerBand_PC = Lowest(low[1], PriceChannel_Period);
def UpperBand_PC = Highest(high[1], PriceChannel_Period);

#Price Channel High Breakout - Sell Signal

def highBar = high >= UpperBand_PC;

def LOHB = if highBar then low else LOHB[1];
def closedBelowLOHB = if highBar then 0 else if close < LOHB then closedBelowLOHB[1] + 1 else closedBelowLOHB[1];

#Price Channel Low Breakout - Buy Signal
def lowBar = low <= LowerBand_PC;

def HOLB = if lowBar then high else HOLB[1];
def closedAboveHOLB = if lowBar then 0 else if close > HOLB then closedAboveHOLB[1] + 1 else closedAboveHOLB[1];

def BuySignal;
def SellSignal;
def StopSellSignal;
def StopBuySignal;

if (high >= UpperBand_PC or close <= low[2]) && BuySignal[1] then {
BuySignal = 0;
SellSignal = 0;
StopSellSignal = 0;
StopBuySignal = 1;
}else if (low <= LowerBand_PC or close >= high[2]) && SellSignal[1] then {
BuySignal = 0;
SellSignal = 0;
StopSellSignal = 1;
StopBuySignal = 0;
}else if closedAboveHOLB == 1 and closedAboveHOLB[1] == 0 && !SellSignal[1] then {
BuySignal = 1;
SellSignal = 0;
StopSellSignal = 0;
StopBuySignal = 0;
}else if closedBelowLOHB == 1 and closedBelowLOHB[1] == 0 && !BuySignal[1] then {
BuySignal = 0;
SellSignal = 1;
StopSellSignal = 0;
StopBuySignal = 0;
}else{
BuySignal = BuySignal[1];
SellSignal = SellSignal[1];
StopSellSignal = 0;
StopBuySignal = 0;}

#Short arrow signal - HOLB_LOHB Breakout
plot SellArrow = if SellSignal && !SellSignal[1] then high + 2 * TickSize() else Double.NaN;

SellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellArrow.SetDefaultColor(Color.RED);
SellArrow.SetLineWeight(Long_Short_SignalSize);

#Long arrow signal - HOLB_LOHB Breakout
plot BuyArrow = if BuySignal && !BuySignal[1] then low - 2 * TickSize() else Double.NaN;

BuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyArrow.SetDefaultColor(Color.GREEN);
BuyArrow.SetLineWeight(Long_Short_SignalSize);

#Short Stop arrow signal
plot StopSellArrow = if StopSellSignal && !StopSellSignal[1] then low - 2 * TickSize() else Double.NaN;

StopSellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
StopSellArrow.SetDefaultColor(Color.DARK_GREEN);
StopSellArrow.SetLineWeight(Stop_SignalSize);

#Long stop arrow signal
plot StopBuyArrow = if StopBuySignal && !StopBuySignal[1] then high + 2 * TickSize() else Double.NaN;

StopBuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
StopBuyArrow.SetDefaultColor(Color.DARK_RED);
StopBuyArrow.SetLineWeight(Stop_SignalSize);

#Plots Price Channel bands
plot PriceChannelUpperBand = if Plot_Price_Chanel then UpperBand_PC else Double.NaN;

PriceChannelUpperBand.SetPaintingStrategy(PaintingStrategy.LINE);
PriceChannelUpperBand.SetDefaultColor(Color.DARK_RED);
PriceChannelUpperBand.SetLineWeight(Long_Short_SignalSize);
PriceChannelUpperBand.SetStyle(Curve.FIRM);

plot PriceChannelLowerBand = if Plot_Price_Chanel then LowerBand_PC else Double.NaN;

PriceChannelLowerBand.SetPaintingStrategy(PaintingStrategy.LINE);
PriceChannelLowerBand.SetDefaultColor(Color.DARK_GREEN);
PriceChannelLowerBand.SetLineWeight(Long_Short_SignalSize);
PriceChannelLowerBand.SetStyle(Curve.FIRM);

# END
 
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
537 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