Highest High between Entry and Exit

nonameblitz1

New member
Good Evening!
Have been browsing these forums for a while now. I've tried this several times and this community seems tip-top and extremely helpful with answering thinkscript questions. I am having a bit of trouble, however. I am ultimately trying to code MAE and MFE of a strategy (most recent trade) or even a lower study that tells me the number. I can figure out the rest once I figure out the following:

Code:
def entry = close crosses below md1;
def exit = close[50];
def EntryPrice = EntryPrice();
def HH =if entry then high else if high > HH[1] then high else HH[1];
Plot hh1 = hh;

###def MFE = HH - EntryPrice; (only during condition will i want to do this, now that i think about it)
###def MFE1 = MFE;
Basically, what I need help with is how to find the highest high that happens between my entry and exit criteria. Am I missing something D:

My goal here is to get the MAE and MFE of several strategies so that I can do a little data science. If one can find those metrics out for each hypothetical trade then in theory we can see how far our trades go in our favor or against us on any lookback (trader preference). This can be helpful in optimizing stop losses and take profit levels, therefore, optimizing our trading!
Really just trying to figure out how to find the highest high that occurs between the entry and exit conditions, each time. From there we can find the average MFE (of a buy signal here) at a certain lookback (traders choice), plot 1st 2nd 3rd, and 4th quartile, and maybe have makeshift confidence intervals.
 
Last edited by a moderator:
Solution
Good Evening!
Have been browsing these forums for a while now. I've tried this several times and this community seems tip-top and extremely helpful with answering thinkscript questions. I am having a bit of trouble, however. I am ultimately trying to code MAE and MFE of a strategy (most recent trade) or even a lower study that tells me the number. I can figure out the rest once I figure out the following:

Code:
def entry = close crosses below md1;
def exit = close[50];
def EntryPrice = EntryPrice();
def HH =if entry then high else if high > HH[1] then high else HH[1];
Plot hh1 = hh;

###def MFE = HH - EntryPrice; (only during condition will i want to do this, now that i think about it)
###def MFE1 = MFE;
Basically, what I need help...
@JoeDV
I read your thread, Potential P/L From Study https://usethinkscript.com/threads/potential-p-l-from-study-for-thinkorswim.8884/

This may be a big ask and you may not be able to help me explicitly, however, I have a question for you and you can maybe give me insight on maybe the logic or point me in the right direction.
All i want to do is get the entry price for each signal, take the difference between that and the highest high and highest low during a trade (signal) and plot that difference separately.
Maximum adverse excursion(MAE) is the maximum a trade goes against you
Maximum favorable excursion(MFE) is the maximum a trade goes for you
I'd like to find the MAE and MFE and plot it for each iteration. (lower study) I can then find some statistics with creative coding on those numbers and it will give me more insight on the trade performance and how maybe i can approach risk mangement.
 
Last edited by a moderator:
Good Evening!
Have been browsing these forums for a while now. I've tried this several times and this community seems tip-top and extremely helpful with answering thinkscript questions. I am having a bit of trouble, however. I am ultimately trying to code MAE and MFE of a strategy (most recent trade) or even a lower study that tells me the number. I can figure out the rest once I figure out the following:

Code:
def entry = close crosses below md1;
def exit = close[50];
def EntryPrice = EntryPrice();
def HH =if entry then high else if high > HH[1] then high else HH[1];
Plot hh1 = hh;

###def MFE = HH - EntryPrice; (only during condition will i want to do this, now that i think about it)
###def MFE1 = MFE;
Basically, what I need help with is how to find the highest high that happens between my entry and exit criteria. Am I missing something D:

My goal here is to get the MAE and MFE of several strategies so that I can do a little data science. If one can find those metrics out for each hypothetical trade then in theory we can see how far our trades go in our favor or against us on any lookback (trader preference). This can be helpful in optimizing stop losses and take profit levels, therefore, optimizing our trading!
Really just trying to figure out how to find the highest high that occurs between the entry and exit conditions, each time. From there we can find the average MFE (of a buy signal here) at a certain lookback (traders choice), plot 1st 2nd 3rd, and 4th quartile, and maybe have makeshift confidence intervals.

hello and welcome
there are several things which make it hard to answer your questions.

in your first post, you mention 2 things by
abbreviation, but didn't explain what they are. you did in post2.

your code is incomplete, so we can't trace back the logic to better understand it.
md1 is undefined.

the exit formula is a price , not a condition. exit is not used. what is the exit condition?

if you want to use formulas in a strategy,
why are you using entryprice()?

your hh formula should contain the highest price during a trade. (it's missing the exit part, so once triggered, it will continue to compare highs for the remainder of the chart)
the hh formula will have values that step up to the highest high, as it compares the highs.
do you want to compare every close price during a trade, even the entry bar, to the overall highest high? or just compared to the highest high up to the current bar, like you have in your formula?

you will have to elaborate on these topics,
average MFE
lookback
plot 1st 2nd 3rd, and 4th quartile


one method that might work,
wait for entry signal,
( do all of this on entry bar)
.. save buy price(close) at entry signal
.. run a loop , to look at future bars to find the exit signal
.. get barnumbers of entry and exit
.. run a loop to find highest, lowest prices , between entry and exit barnumbers
 
Solution
Hey, thanks for the reply @halcyonguy, and most of all thanks for the questions.
Sorry for being brief. In this post, I'll describe what MAE and MFE are, post the code I'm working with directly, why I included 'entryprice()', include entry and conditions, and include all relevant data so that one could fully understand my madness.

I know the first post was brief as I try not to be a nuisance lol didn't want to waste anyone's time.
MAE stands for Maximum Adverse Excursion and MFE stands for Maximum Favorable Excursion. They are used to measure the performance of a trading strategy.

MAE is a measure of the maximum loss that a trade incurs before it is closed. It is calculated by taking the difference between the entry price and the lowest price reached before the trade is closed. MFE, on the other hand, is a measure of the maximum profit that a trade earns before it is closed. It is calculated by taking the difference between the entry price and the highest price reached before the trade is closed.

Both MAE and MFE can be used to evaluate the risk and reward of a trading strategy and to optimize the size of the trade. They can also be used to identify potential problems with an already incorporated trading strategy, such as excessive risk-taking or a lack of discipline in following the strategy.

Here is the original code I was applying this to; a simple pivot point study using standard deviation.
Code:
input length = 21;
input ShortenByAFactorOf = 1.00;
def cl = close (period = aggregationperiod.month);

def sdev = stdev(cl, length);
def stdev = sdev/Shortenbyafactorof;


def PLI = stdev[1];
input showMidpoints = yes;


DefineGlobalColor("PLI", Color.WHITE);
DefineGlobalColor("MP", Color.LIGHT_GRAY);
def op = open (period = aggregationperiod.month);
 def ST = op;
def MP = PLI;

#Plot ST
plot PLS = op;
PLS.SetDefaultColor(Color.YELLOW);
PLS.HideTitle();
PLS.SetPaintingStrategy(PaintingStrategy.DASHES);

#ST Up
plot MU1 = if showMidpoints then ST + MP  else Double.NaN ;
MU1.SetDefaultColor(GlobalColor("MP"));
MU1.HideTitle();
MU1.SetPaintingStrategy(PaintingStrategy.DASHES);
MU1.HideBubble();


plot PU2 = if ST > 0 then ST + (2 * PLI) else Double.NaN;
PU2.SetDefaultColor(GlobalColor("PLI"));
PU2.HideTitle();
PU2.SetPaintingStrategy(PaintingStrategy.DASHES);
plot PU3 = if ST > 0 then ST + (3 * PLI) else Double.NaN;
PU3.SetDefaultColor(GlobalColor("PLI"));
PU3.HideTitle();
PU3.SetPaintingStrategy(PaintingStrategy.DASHES);

plot PU4 = if ST > 0 then ST + (4 * PLI) else Double.NaN;
PU2.SetDefaultColor(GlobalColor("PLI"));
PU2.HideTitle();
PU2.SetPaintingStrategy(PaintingStrategy.DASHES);
plot PU5 = if ST > 0 then ST + (5 * PLI) else Double.NaN;
PU3.SetDefaultColor(GlobalColor("PLI"));
PU3.HideTitle();
PU3.SetPaintingStrategy(PaintingStrategy.DASHES);




#STDown
plot MD1 = if showMidpoints then ST - MP else Double.NaN;
MD1.SetDefaultColor(GlobalColor("MP"));
MD1.HideTitle();
MD1.SetPaintingStrategy(PaintingStrategy.DASHES);
MD1.HideBubble();

plot PD2 = if ST > 0 then ST - (2 * PLI) else Double.NaN;
PD2 .SetDefaultColor(GlobalColor("PLI"));
PD2 .HideTitle();
PD2 .SetPaintingStrategy(PaintingStrategy.DASHES);

plot PD3 = if ST > 0 then ST - (3 * PLI) else Double.NaN;
PD3 .SetDefaultColor(GlobalColor("PLI"));
PD3 .HideTitle();
PD3 .SetPaintingStrategy(PaintingStrategy.DASHES);

plot PD4 = if ST > 0 then ST - (4 * PLI) else Double.NaN;
PD2 .SetDefaultColor(GlobalColor("PLI"));
PD2 .HideTitle();
PD2 .SetPaintingStrategy(PaintingStrategy.DASHES);

plot PD5 = if ST > 0 then ST - (5 * PLI) else Double.NaN;
PD3 .SetDefaultColor(GlobalColor("PLI"));
PD3 .HideTitle();
PD3 .SetPaintingStrategy(PaintingStrategy.DASHES);

#def EntryPrice = EntryPrice();
#def entry = close crosses below md1;
#def exit = close[50];
#def HH =if entry then high else if high > HH[1] then high else HH[1];
#Plot hh1 = hh;

I defined the variable 'EntryPrice' as 'Entryprice()' because I wanted to originally add this to the great FPL dashboard I found here but quickly found it out the coding was more complicated. I then moved to a normal study because I wanted to begin trying to code piece by piece, hence plotting the variable 'HH1'.
EntryConditions change over time, which is the fun in trading (lol not)-- however, for this particular reason of data collection we would use exact indicator prices to simulate limit orders, I used to be able to do it but I've been trading and not coding for so long I have forgotten and for sake of speed I simply coded "close below MD1", the rest would have came when I figured out the most crucial parts of this code.

All buy entries are at the MD1, PD2, PD3, PD4, PD5 variables
All sell entries are at the MU1, PU2, PU3,PU4, PU5 variables
To close the trade, because we are using this for data collection to MAKE a strategy it would be simply closing 50 bars later or Closing let's say 1 tick or point or cent above or below the next line down or up, respectively. The latter is what is in my head but closing x amount bars in the future is good enough I'd say.

To reiterate we buy the first line below PLS, which is MD1, and close it 1 tick (point or cent) above PD2. Continue that for each line below PLS. It is the same but reverses for Sells.

To calculate MAE and MFE you will need the following information:
  • Entry price
  • Exit price (if thats how we code exit condition)
  • Lowest price: the lowest price reached by the trade before it was closed
  • Highest price: the highest price reached by the trade before it was closed
To calculate MAE, you will need to determine the difference between the entry price and the lowest price reached for each trade, I'd like to chart these or hold these numbers in a list so we can access them later, hence 'lookback' i was asking for lol. (man I'm a dummy sometimes) The Minimum of ALL the differences is the MAE of the strategy, however, I'd like to do my own statistical analysis on these numbers so we would like the MAE for each individual trade.
To calculate MFE, you will need to determine the difference between the entry price and the highest price reached for each trade. The maximum of these differences is the MFE of the strategy, and just like MAE I think there's some risk management gold in finding the MFE on an individual trade basis.

NOTE: THIS IS FOR A BUY TRADE, A SELL TRADE IS VICE VERSA.

I couldn't even get past the first part of attempting to code this to get to adding the isLong, isShort variables so we can turn off long or short trades. My original plan was to make a long-only study and a short-only study and as I said before, add them to the FPL strategy when it was complete.

I'm not sure if thinkorswim has good statistical modeling capabilities so one way to measure MAE and MFE is to find mean, median, mode, quartiles, skewness, kurtosis, and standard deviation. We can find out where to put our stops and take profits of pretty much any strategy and quite possibly find some ways to extract ticks. It's not only us with DNA, the market has it too!!!!! We just have to do some gene editing. I mean we can be pretty deep but I prefer KISS.

I just tried to keep things short but if you guys wanna go down a journey with me I'm here for it. I've traded for a bond desk where I learned how to make markets for a year, didn't like it but nonetheless some experience. I've also been trading for myself for about 8 years (in July).

Let me know if I can provide anything else, also I will attempt your recommendations when the kiddos are asleep tonight. I'll probably end up learning a lot but not getting much accomplished...

Nice to meet you guys, apologize for bumping a 2 year old post
 
Last edited:
Hey @durdcash , a little while back you posted this thread below:

https://usethinkscript.com/threads/statistics-to-help-optimize-exits.12800/

I too asked a similar question and I think I have something coded that may help, will be updating it soon. Also @halcyonguy posted logic above to help.
Here are the various FPL calculations located on the forum.
Perhaps the syntax, found here, will forward your quest:
https://usethinkscript.com/threads/...acktesting-data-utility-for-thinkorswim.1624/
 

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