# Exit with limited points

##### New member
I am working on a strategy where I am using two indicators: the TSI (true strength index) and the SMI (stochastic momentum index). When they both cross above or below, it triggers the buy order and exits when it reaches n points or ticks.

Now that I've figured out how to enter as a buy or sell, I'm stuck on how to exit at a specific point or tick value. Could you please help me figure that out? I am not an expert in coding.

Thank you,

#
# TD Ameritrade IP Company, Inc. (c) 2007-2022
#

declare lower;

#####################################################
################## True Strength Index ##################
#####################################################

input longLength = 25;
input shortLength = 13;
input signalLength = 13;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close[1];
def doubleSmoothedAbsDiff = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diff), longLength), shortLength);

plot TSI;
plot Signal;

TSI = if doubleSmoothedAbsDiff == 0 then 0
else 100 * (MovingAverage(averageType, MovingAverage(averageType, diff, longLength), shortLength)) / doubleSmoothedAbsDiff;
Signal = MovingAverage(averageType, TSI, signalLength);

plot ZeroLine = 0;

TSI.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
Signal.hide();
ZeroLine.SetDefaultColor(GetColor(5));

#####################################################
############### Stochastic Momentum Index ##############
#####################################################

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

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);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));

#####################################################
################### Buy or Sell Order ###################
#####################################################

def Buy = TrueStrengthIndex("signal length" = 13)."TSI" crosses above TrueStrengthIndex("signal length" = 13)."Signal" within 2 bars and StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."SMI" crosses above StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."AvgSMI" within 3 bars;

dey Sell = TrueStrengthIndex("signal length" = 13)."TSI" crosses below TrueStrengthIndex("signal length" = 13)."Signal" within 2 bars and StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."SMI" crosses below StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."AvgSMI" within 3 bars;

#AddOrder(OrderType.SELL_TO_OPEN, SELL, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "SHORT");
#AddOrder(OrderType.SELL_TO_CLOSE, SELL, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "EXIT");

You can use entryprice() + you target in points only works for backrest

It’s sell_to_ close for long exit

You can use entryprice() + you target in points only works for backrest

It’s sell_to_ close for long exit

I did not get where I need to make the change and add entryprice().

replace this with yours
AddOrder(OrderType.SELL_TO_CLOSE, entryPrice1()+5, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LONGEXIT");
AddOrder(OrderType.SELL_TO_CLOSE, entryPrice1()-3, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LONGSTOP");
AddOrder(OrderType.SELL_TO_OPEN, SELL, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "SHORT");
AddOrder(OrderType.BUY_TO_CLOSE, entryPrice1()- 5, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "SHORTEXIT");

Last edited:
replace this with yours
AddOrder(OrderType.SELL_TO_CLOSE, entryPrice1()+5, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LONGEXIT");
AddOrder(OrderType.SELL_TO_CLOSE, entryPrice1()-3, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LONGSTOP");
AddOrder(OrderType.SELL_TO_OPEN, SELL, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "SHORT");
AddOrder(OrderType.BUY_TO_CLOSE, entryPrice1()- 5, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "SHORTEXIT");

You are awesome! It works great.
The only issue is that my TSI and SMI indicators show up on the chart which makes it very difficult to read the candles. I tried to change the plot to def but it threw an error. Any suggestions?

see if this what you looking for
Code:
``````#
# TD Ameritrade IP Company, Inc. (c) 2007-2022
#

#####################################################
################## True Strength Index ##################
#####################################################

input longLength = 25;
input shortLength = 13;
input signalLength = 13;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close[1];
def doubleSmoothedAbsDiff = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diff), longLength), shortLength);

plot TSI;
plot Signal;

TSI = if doubleSmoothedAbsDiff == 0 then 0
else 100 * (MovingAverage(averageType, MovingAverage(averageType, diff, longLength), shortLength)) / doubleSmoothedAbsDiff;
Signal = MovingAverage(averageType, TSI, signalLength);

plot ZeroLine = 0;

TSI.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
Signal.hide();
ZeroLine.SetDefaultColor(GetColor(5));

#####################################################
############### Stochastic Momentum Index ##############
#####################################################

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

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff1 = 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;
#smi.setDefaultColor(getColor(1));

def AvgSMI = expaverage(smi, percentDLength);
#avgsmi.setDefaultColor(getcolor(5));

def overbought = over_bought;
#overbought.setDefaultColor(getcolor(5));

def oversold = over_sold;
#oversold.setDefaultColor(getcolor(5));

#####################################################
################### Buy or Sell Order ###################
#####################################################

def Buy = TrueStrengthIndex("signal length" = 13)."TSI" crosses above TrueStrengthIndex("signal length" = 13)."Signal" within 2 bars and StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."SMI" crosses above StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."AvgSMI" within 3 bars;

def Sell = TrueStrengthIndex("signal length" = 13)."TSI" crosses below TrueStrengthIndex("signal length" = 13)."Signal" within 2 bars and StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."SMI" crosses below StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."AvgSMI" within 3 bars;
AddOrder(OrderType.SELL_TO_CLOSE, entryPrice()+5, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LONGEXIT");
AddOrder(OrderType.SELL_TO_CLOSE, entryPrice()-3, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LONGSTOP");
AddOrder(OrderType.SELL_TO_OPEN, SELL, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "SHORT");
AddOrder(OrderType.BUY_TO_CLOSE, entryPrice()- 5, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "SHORTEXIT");

Last edited:
see if this what you looking for
Code:
``````#
# TD Ameritrade IP Company, Inc. (c) 2007-2022
#

#####################################################
################## True Strength Index ##################
#####################################################

input longLength = 25;
input shortLength = 13;
input signalLength = 13;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close[1];
def doubleSmoothedAbsDiff = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diff), longLength), shortLength);

plot TSI;
plot Signal;

TSI = if doubleSmoothedAbsDiff == 0 then 0
else 100 * (MovingAverage(averageType, MovingAverage(averageType, diff, longLength), shortLength)) / doubleSmoothedAbsDiff;
Signal = MovingAverage(averageType, TSI, signalLength);

plot ZeroLine = 0;

TSI.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
Signal.hide();
ZeroLine.SetDefaultColor(GetColor(5));

#####################################################
############### Stochastic Momentum Index ##############
#####################################################

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

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff1 = 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;
#smi.setDefaultColor(getColor(1));

def AvgSMI = expaverage(smi, percentDLength);
#avgsmi.setDefaultColor(getcolor(5));

def overbought = over_bought;
#overbought.setDefaultColor(getcolor(5));

def oversold = over_sold;
#oversold.setDefaultColor(getcolor(5));

#####################################################
################### Buy or Sell Order ###################
#####################################################

def Buy = TrueStrengthIndex("signal length" = 13)."TSI" crosses above TrueStrengthIndex("signal length" = 13)."Signal" within 2 bars and StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."SMI" crosses above StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."AvgSMI" within 3 bars;

def Sell = TrueStrengthIndex("signal length" = 13)."TSI" crosses below TrueStrengthIndex("signal length" = 13)."Signal" within 2 bars and StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."SMI" crosses below StochasticMomentumIndex("over bought" = 50.0, "over sold" = -50.0)."AvgSMI" within 3 bars;
AddOrder(OrderType.SELL_TO_CLOSE, entryPrice()+5, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LONGEXIT");
AddOrder(OrderType.SELL_TO_CLOSE, entryPrice()-3, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LONGSTOP");
AddOrder(OrderType.SELL_TO_OPEN, SELL, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "SHORT");
AddOrder(OrderType.BUY_TO_CLOSE, entryPrice()- 5, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "SHORTEXIT");

Everything is aligned as I wanted. I really appreciate your help

87k+ Posts
444 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?