Exit with limited points

shamrozkadiwal

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.BUY_TO_OPEN, BUY, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "BUY");
AddOrder(OrderType.BUY_TO_CLOSE, BUY, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "EXIT");
#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");
 
replace this with yours
AddOrder(OrderType.BUY_TO_OPEN, BUY, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "BUY");
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");
AddOrder(OrderType.BUY_TO_CLOSE, entryPrice1()+3,, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "SHORTSTOP");
 
Last edited:
replace this with yours
AddOrder(OrderType.BUY_TO_OPEN, BUY, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "BUY");
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");
AddOrder(OrderType.BUY_TO_CLOSE, entryPrice1()+3,, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "SHORTSTOP");

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?

"C:\Users\charo\Downloads\Screenshot 2023-01-04 114140.jpg"
 
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.BUY_TO_OPEN, BUY, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "BUY");
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");
AddOrder(OrderType.BUY_TO_CLOSE, entryPrice()+3, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "SHORTSTOP");
 
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.BUY_TO_OPEN, BUY, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "BUY");
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");
AddOrder(OrderType.BUY_TO_CLOSE, entryPrice()+3, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "SHORTSTOP");

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

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