Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

MFE calculation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • NinjaTrader_AlanP
    replied
    Hello calhawk01,

    Helpguide documentation in note 3,

    • When backtesting, the high and low of the bar series is used



    May I confirm you are referring to the backtest output for MFE?

    Regarding the script you posted, when I swapped the print statement for the following I was able to avoid the error you mentioned,

    Code:
    Print((t.TradeNumber + 1) + ";" + entryPrice + ";" + Time[entryBarIndex] + ";" + exitPrice + ";" + ";" + lengthOfTrade + ";" + worstPriceReached + ";" + bestPriceReached + ";      :" + 50*maeTrade + ";" + 50*mfeTrade);
    I look forward to your reply.

    Leave a comment:


  • staycool3_a
    replied
    Originally posted by NinjaTrader_AlanP View Post
    Hello calhawk01,

    I absolutely agree with the point you’re making and will submit a feature request that the highest value be stored rather than using the high/low of the bar during a backtest.

    Because its documented in the Helpguide, its expected and not considered a bug.

    I will submit a feature request and add my vote to this as well.

    Please let us know if you need further assistance.


    Thanks. I don't think this should be added to the "suggestion" box. This should be a priority as everyone who uses MFE/MAE ARE under the impression that the variable is giving them min/max from entry to exit. It blows my mind to read threads going back to NT 6.5 regarding this issue and this issue still has not been resolved! It's as simple as finding the min/max between two points... not sure why this is being treated as you have to create a rocket and land it on the moon.

    Im not sure how you can say that the current output is in-line "with the documentation" regarding this variable. How is it possible that the highest point during backtest using high/low is not being capitured? I'm still at a loss and don't understand why the highest point between entry and exit isn't being captured using "high" of the bars between entries and exits. The highest high in the bars between entry and exits would be the highest value....... why isn't that being capituted during backtest... as you said.. backtest uses high/low to calc mae/mfe

    Is there anyone out there who has a custom MFE/Mae script that does a better job finding highest point/min point??

    Code:
    foreach (Trade t in SystemPerformance.AllTrades)
    {
    	if (t.Entry.MarketPosition == MarketPosition.Long)
    	{
    		// what's the entry price?
    		entryPrice = t.Entry.Price;
    		// on which bar did that happen?
    		entryBarIndex = (Count - 1) - t.Entry.BarIndex;
    
    		// what's the exit price?
    		exitPrice = t.Exit.Price;
    		// on which bar did that happen?
    		exitBarIndex = (Count - 1) - t.Exit.BarIndex;
    
    		// so, how long did the trade lasted?
    		lengthOfTrade = (entryBarIndex - exitBarIndex) + 1;
    
    		// so, what's the best price for an long trade that happend during this time period?        excluding the entry and exit bar
    		bestPriceReached = MAX(High, lengthOfTrade - 1)[exitBarIndex + 1];
    
    		if (t.Exit.Price > bestPriceReached)
    		{
    			bestPriceReached = t.Exit.Price;
    		}
    
    		// .. and what is the worst price?
    		worstPriceReached = MIN(Low, lengthOfTrade - 1)[exitBarIndex + 1];
    
    		// compare the 'worstPriceReached' with the entry and exit bar
    		if (t.Exit.Price < worstPriceReached)
    		{
    			worstPriceReached = t.Exit.Price;
    		}
    
    		// What's the MAE for this trade?
    		// According to NT help: "MAE (max. adverse excursion) is defined as |worst price trade reached – entry price|, "
    		maeTrade = Math.Abs(worstPriceReached - entryPrice) * t.Quantity;
    
    		// What's the MFE for this trade?
    		// According to NT help: "MFE (max. favorable excursion) is defined as (best price trade reached – entry price), "
    		mfeTrade = (bestPriceReached - entryPrice) * t.Quantity;
    
    		Print((t.TradeNumber + 1) + ";" + entryPrice + ";" + Time[entryBarIndex] + ";" + exitPrice + ";" + Time[exitBarIndex] + ";" + lengthOfTrade + ";" + worstPriceReached + ";" + bestPriceReached + ";" + maeTrade + ";" + mfeTrade);
    	}
    I was able to find the above custom code for mfe/Mae calc for n7, this compiled for n8 but during backtest during, it gives me the error,

    Code:
    Strategy 'MFEcalc': Error on calling 'OnBarUpdate' method on bar 22: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    Not sure why I'm getting this... hoping I can get this to work and maybe this is better than the n8 MFE code...... I do conceptually understand what the error is but not sure the above code is referencing that's causing bars that are not available

    I do have:

    Code:
    	if (CurrentBars[0] < 1
    			|| CurrentBars[1] < 1)
    				return;
    Last edited by staycool3_a; 01-22-2018, 05:28 PM.

    Leave a comment:


  • NinjaTrader_AlanP
    replied
    Hello calhawk01,

    I absolutely agree with the point you’re making and will submit a feature request that the highest value be stored rather than using the high/low of the bar during a backtest.

    Because its documented in the Helpguide, its expected and not considered a bug.

    I will submit a feature request and add my vote to this as well.

    Please let us know if you need further assistance.

    Leave a comment:


  • staycool3_a
    replied
    Originally posted by NinjaTrader_AlanP View Post
    Hello calhawk01,

    Thank you for your note.

    The third note at the link below states that when Backtesting, the high/low of the bar series is used, which is going to be much less accurate than when in real time, which uses a bid/ask series.



    The reason you are seeing a trade that exited later with a lower MFE in a backtest, would suggest that bar had a higher low or lower high, depending on whether you were long or short, than the previous exit.

    Please let us know if this is unclear or if you need further assistance.

    I need more clarification

    By definition, for long positions, MFE is the highest point reached between entry and exit.

    Suppose the following data and times

    On bar one we enter two trades

    On bar 3 we exit trade one. From bar one to bar 3, the highest point reached, suppose, it was 100, 100-entry is MFE for trade 1

    Then we exit trade two at bar 5, the highest point from entry of this trade up until bar 5 is still 100. MFE for this trade should also be 100.

    Why is this not happening? How does n8 magically just ignore the highest point and use some other value? This should still hold true based on high/low bar data during back tests...

    Conceptually, don't you think this is a wrong output?

    Leave a comment:


  • NinjaTrader_AlanP
    replied
    Hello calhawk01,

    Thank you for your note.

    The third note at the link below states that when Backtesting, the high/low of the bar series is used, which is going to be much less accurate than when in real time, which uses a bid/ask series.



    The reason you are seeing a trade that exited later with a lower MFE in a backtest, would suggest that bar had a higher low or lower high, depending on whether you were long or short, than the previous exit.

    Please let us know if this is unclear or if you need further assistance.

    Leave a comment:


  • staycool3_a
    started a topic MFE calculation

    MFE calculation

    hi

    MFE is an extremely powerful variable that nt produces for the underlying trades for a strategy. However, I am becoming extremely skeptical behind the accuracy of this variable. I have a strategy where my entry logic is same exact and my exits are based on time.

    during strategy run 1 backtest: I exit after 1 bars
    during strategy run 2 backtest: I exit after 2 bars

    there are many scenarios where during both of my strategies runs, my entries are made on the same exact time and price. for example:

    strategy 1 run: entry time = 1am and price = .73
    strategy 2 run: entry time = 1am and price = .73

    since my exit is time based:

    strategy 1 run: exit at 1:01 am at price of X
    strategy 2 run: exit at 1:02 am at price of Y

    *note my strategy bars are actually 180 minute bars but in the example above i am using 1 minute for simplicity.

    under the above entries, I am seeing that my MFE for strategy 1 (lower time) for that trade is actually higher than strategy 2 run (higher time). That makes no sense, because strategy 2 run exits after 2 bars, which would be cumulative of previous bars. Therefore, my strategy 2 run for entries that were made at the same price and same time in compared to strategy 1 run should ALWAYS have a greater MFE.

    see attached excel file screenshot explaining the scenario. i am suspecting the same is true about mae and other variables as well. why is this happening? there are important variables and the software should not be getting these incorrectly. the formula behind mfe is fairly simple.

    The extent to which this variable is incorrect is mind-boggling; out of 3057 trades that occured at the same exact time and at the same exact price; the mfe is incorrect 92% of the time. incorrect= 1 bar mfe > 2 bar mfe. How am I suppose to build strategies when I can't even trust the variable?

    what am I missing here?

    thanks
    Attached Files
    Last edited by staycool3_a; 01-20-2018, 02:27 AM.

Latest Posts

Collapse

Topics Statistics Last Post
Started by MarianApalaghiei, Today, 10:49 PM
3 responses
9 views
0 likes
Last Post NinjaTrader_Manfred  
Started by XXtrader, Today, 11:30 PM
0 responses
3 views
0 likes
Last Post XXtrader  
Started by love2code2trade, Yesterday, 01:45 PM
4 responses
28 views
0 likes
Last Post love2code2trade  
Started by funk10101, Today, 09:43 PM
0 responses
9 views
0 likes
Last Post funk10101  
Started by pkefal, 04-11-2024, 07:39 AM
11 responses
37 views
0 likes
Last Post jeronymite  
Working...
X