highest indicator value coding help

Status
Not open for further replies.

MLlalala

Member
I want to def the lowest price value looking back 3 bars, but this value is not allowed to retrace. In other words, if the value is lower than previous one, then I need to plot the previous one.

Basically I want to plot both highest and lowest value looking back N bars. This works like a trail stop, let's say I am long, then the trail stop will be the lowest value of last 3 bars or any other length. But this trail stop is only allowed to move up, if the current value is lower than the previous one, then i need to plot the previous one

I have tried many times but not work, appreciate if any one could help! many thanks in advance
 
Last edited by a moderator:
I want to def the lowest price value looking back 3 bars, but this value is not allowed to retrace. In other words, if the value is lower than previous one, then I need to plot the previous one.

I have tried many times but not work, appreciate if any one could help! many thanks in advance

hello , Sorry you will have to reword your question before I can understand it.
Your title wants the highest,
your words want the lowest,
except if the previous is higher then you want the highest???

I'll start with the basics maybe this will help

To find the lowest of 3 bars including the current one. As the current bar changes this could change the lowest value found.
def low1 = lowest(low, 3);



This one looks back 3 bars and finds the lowest but starts at the previous bar so the current bar's actions don't affect it.
def low2 = lowest(low[1], 3);
 

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

It will have to be allowed to lower or terminate at some point, or else you end up in a situation like where it only works at all time highs. In this case, I believe it will be allowed to lower after you've been stopped out, and resume when things start moving up again. Its situational though, depending on how you enter, and where you initial stop is.

def Fetch = Lowest(Low,3);
def Check = If Fetch > Fetch[1] then Fetch else Check[1];
plot Result = Check;
 
It will have to be allowed to lower or terminate at some point, or else you end up in a situation like where it only works at all time highs. In this case, I believe it will be allowed to lower after you've been stopped out, and resume when things start moving up again. Its situational though, depending on how you enter, and where you initial stop is.

def Fetch = Lowest(Low,3);
def Check = If Fetch > Fetch[1] then Fetch else Check[1];
plot Result = Check;
Thank you. I amended my code and things looks better now, but still i find the long trail stop sometimes move LOWER which is not allowed. here is my code, really hope someone could help me out

input length = 3;
input price = close;

def maHigh = highest(high, length);
def maLow = lowest(low, length);
def bp = if maHigh < maHigh[1] then maHigh else bp[1];
def sp = if maLow > maLow[1] then maLow else sp[1];

def state = {default init, short, long};
if (price > bp) {
state = state.long;
} else if (price < sp) {
state = state.short;
} else {
state = state[1];
}


plot BuyStop = if state == state.short or state != state[1] then bp else Double.NaN;


plot SellStop = if state == state.long or state != state[1] then sp else Double.NaN;

BuyStop.SetDefaultColor(GetColor(0));
SellStop.SetDefaultColor(GetColor(1));
 
Thank you. I amended my code and things looks better now, but still i find the long trail stop sometimes move LOWER which is not allowed. here is my code, really hope someone could help me out
...

Halyconguy && Joshua sort of answered the question already:

As the current bar changes this could change the lowest value found.

It will have to be allowed to lower or terminate at some point, or else you end up in a situation like where it only works at all time highs....Its situational though, depending on how you enter, and where you initial stop is.


I present Two pictures (since I'm a little hungry, I chose Chipotle stock):
Chipotle, your original code:
QmVvtlT.png


Chipotle, revised code:
iE6jXBt.png


my assumption: you want to use the price action of recent lows or highs as your trailing stop, and you choose whether or not to enter the trade based on your state.long/state.short code

If my assumptions OK, then you can try the rest of this post. If my assumptions are not OK ..

hello , Sorry you will have to reword your question before I can understand it.

If my assumption is OK, then I think I can provide what you're looking for, using a couple small revisions to your existing code:

1. In your else, use state.init instead of state[1]
2. Change your BuyStop and SellStop to use one state criteria, and leverage maHigh and maLow INSTEAD OF bp and sp.
[Based on my analysis of your code, the bp and sp were fantastic criteria for getting a long or short state, but were less than ideal for the trailing stop, as they'd sometimes appear inside candles.]


These are the steps I followed to revise your code [You can jump to the bottom for the completed code.]

1. Comment out the BuyStop and SellStop
Code:
#plot BuyStop = if state == state.short or state != state[1] then bp else Double.NaN;
#plot SellStop = if state == state.long or state != state[1] then sp else Double.NaN;
#BuyStop.SetDefaultColor(GetColor(0));
#SellStop.SetDefaultColor(GetColor(1));

2. Leverage plots (and the Data Box) to check maHigh, maLow, bp, sp, state.short, and state.Long:
Code:
plot maHighVal = maHigh;
plot maLowVal = maLow;
plot bpVal = bp;
plot spVal = sp;
maHighVal.SetStyle(CURVE.SHORT_DASH);
maLowVal.SetStyle(CURVE.SHORT_DASH);
plot stateL = state == state.long;
plot stateS = state == state.short;

3. I generally liked your state.long and state.short as they appeared in the plot, so I only slightly tweaked the "else" condition, to not let them carry over a value from the prior candle, as I think this is what caused them to stay in trades too long and adjust the stop in the wrong direction when the trade should have been stopped out:
Code:
def state = {default init, short, long};
if (price > bp) {
state = state.long;
} else if (price < sp) {
state = state.short;
} else {
state = state.init;
}

4. Update the code for BuyStop and SellStop to only consider one state, and leverage maHigh and maLow:
Code:
plot BuyStop = if state == state.short && maHigh <= maHigh[1] then maHigh else Double.NaN;
plot SellStop = if state == state.long && maLow >= maLow[1] then maLow else Double.NaN;
BuyStop.SetDefaultColor(GetColor(0));
SellStop.SetDefaultColor(GetColor(1));

5. remove the temporary maHighVal, maLowVal, bpVal, and spVal
Code:
#plot maHighVal = maHigh;
#plot maLowVal = maLow;
#plot bpVal = bp;
#plot spVal = sp;
#maHighVal.SetStyle(CURVE.SHORT_DASH);
#maLowVal.SetStyle(CURVE.SHORT_DASH);
#plot stateL = state == state.long;
#plot stateS = state == state.short;

6. Here's the complete revised code:
https://usethinkscript.com/threads/trailing-stop-based-on-highs-or-lows-for-thinkorswim.10979/
 
Last edited by a moderator:
Status
Not open for further replies.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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