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.
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;
input showMidpoints = yes;
def op = open (period = aggregationperiod.month);
def ST = op;
def MP = PLI;
plot PLS = op;
plot MU1 = if showMidpoints then ST + MP else Double.NaN ;
plot PU2 = if ST > 0 then ST + (2 * PLI) else Double.NaN;
plot PU3 = if ST > 0 then ST + (3 * PLI) else Double.NaN;
plot PU4 = if ST > 0 then ST + (4 * PLI) else Double.NaN;
plot PU5 = if ST > 0 then ST + (5 * PLI) else Double.NaN;
plot MD1 = if showMidpoints then ST - MP else Double.NaN;
plot PD2 = if ST > 0 then ST - (2 * PLI) else Double.NaN;
plot PD3 = if ST > 0 then ST - (3 * PLI) else Double.NaN;
plot PD4 = if ST > 0 then ST - (4 * PLI) else Double.NaN;
plot PD5 = if ST > 0 then ST - (5 * PLI) else Double.NaN;
#def EntryPrice = EntryPrice();
#def entry = close crosses below md1;
#def exit = close;
#def HH =if entry then high else if high > HH then high else HH;
#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
All sell entries are at the MU1, PU2, PU3,PU4, PU5
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