• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Referring to MFE in automated trading startegy

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

    Referring to MFE in automated trading startegy

    Hello,
    Is there a way to refer to MFE via Ninjascript? Eg. I'd like to create a stop loss criteria based on MFE of an individual trade. It may look something like this:

    if (MFE > 200)
    {
    SetStopLoss (XXX);
    }

    Anyway of accomplishing this in NT?

    Very much appreciate your help in advance.

    #2
    Hello pandyav,

    Yes, you may get the average MFE (Max Favorable Excursion) by using "Performance.AllTrades.TradesPerformance.Currency. AvgMfe".

    http://www.ninjatrader.com/support/h...tml?avgmfe.htm
    JCNinjaTrader Customer Service

    Comment


      #3
      Hi JC,
      Thank you for your reply. I looked at the link you provided below and came up with the code below. Very simple, nothing fancy. All I am doing is re-setting the stoploss to default stoploss, if there is no position open and setting it to avg price if MFE is of certain value.

      //PROFIT PROTECTION CRITERIA BASED ON MFE
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss(CalculationMode.Ticks, stoplossticks);
      }

      //LONG STOP LOSS BASED ON MFE
      else if (Position.MarketPosition == MarketPosition.Long)
      {
      if ((Performance.AllTrades.TradesPerformance.Currency .AvgMfe)>=200
      && (Performance.AllTrades.TradesPerformance.Currency. AvgMfe)<=250)
      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
      }
      }

      The challenge that I run into is that the above code looks at the MFE of 'all' the trades, whereas I want to change the stop loss based on the MFE of the current trade. Now I can use Performance.RealtimeTrades instead, but then I am not sure if it will work with back testing or on historical data as it will only change based on realtime trades.

      I am stuck, any ideas on how can I change the stoploss based on the MFE of the current trade?

      Again, very much appreciate your help and support.

      Comment


        #4
        Originally posted by pandyav View Post
        Hi JC,
        Thank you for your reply. I looked at the link you provided below and came up with the code below. Very simple, nothing fancy. All I am doing is re-setting the stoploss to default stoploss, if there is no position open and setting it to avg price if MFE is of certain value.

        //PROFIT PROTECTION CRITERIA BASED ON MFE
        if (Position.MarketPosition == MarketPosition.Flat)
        {
        SetStopLoss(CalculationMode.Ticks, stoplossticks);
        }

        //LONG STOP LOSS BASED ON MFE
        else if (Position.MarketPosition == MarketPosition.Long)
        {
        if ((Performance.AllTrades.TradesPerformance.Currency .AvgMfe)>=200
        && (Performance.AllTrades.TradesPerformance.Currency. AvgMfe)<=250)
        {
        SetStopLoss(CalculationMode.Price, Position.AvgPrice);
        }
        }

        The challenge that I run into is that the above code looks at the MFE of 'all' the trades, whereas I want to change the stop loss based on the MFE of the current trade. Now I can use Performance.RealtimeTrades instead, but then I am not sure if it will work with back testing or on historical data as it will only change based on realtime trades.

        I am stuck, any ideas on how can I change the stoploss based on the MFE of the current trade?

        Again, very much appreciate your help and support.
        Is that not the same question that you asked in this thread? http://www.ninjatrader.com/support/f...832#post354832.

        To which I gave you an answer as code?

        Comment


          #5
          Hi Koganam,
          Thanks for jumping in. The thread that you referred to is helpful, the challenge is that the maximum gain is calculated for every bar. So, the maximum gain or MFE keeps changing from bar to bar. I do not think it is a true reflection of MFE. A true MFE will only go up only if there is an increase in gain.

          I looked at the link that JC provided for AvgMFE, but it calculated MFE for all the trades whereas I want to change my stoploss condition based on the MFE of the current trade.

          Please let me know if I was not clear, all I want to do is to obtain MFE of the current trade and it will go up as the trade keeps going in your favor, but it will not come down as the trade starts going against you. Is there a way to achieve this?

          Many Thanks

          Comment


            #6
            Originally posted by pandyav View Post
            Hi Koganam,
            Thanks for jumping in. The thread that you referred to is helpful, the challenge is that the maximum gain is calculated for every bar. So, the maximum gain or MFE keeps changing from bar to bar. I do not think it is a true reflection of MFE. A true MFE will only go up only if there is an increase in gain.

            I looked at the link that JC provided for AvgMFE, but it calculated MFE for all the trades whereas I want to change my stoploss condition based on the MFE of the current trade.

            Please let me know if I was not clear, all I want to do is to obtain MFE of the current trade and it will go up as the trade keeps going in your favor, but it will not come down as the trade starts going against you. Is there a way to achieve this?

            Many Thanks
            False. The code that I gave you calculates the maximum and only updates if that maximum is exceeded by the current bar.

            You, of course, do need to use a filter to be sure that you are currently in a position; and to reset your MFE to zero when you first enter the position, so that you are reflecting the reality of the MFE.

            Comment


              #7
              Hi Koganam,
              Here is the code you provided:
              double CurrentBarHighestGain = High[0] - Position.AvgPrice;
              HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);

              Let's say I enter the position at 1700 on ES. The price touches high of 1705 at the end of next 10 bars, so my MFE at the end of 10th bar will be:
              1705-1700 = 5 Points

              On the next bar, the high is 1704 and since MFE is calculated based on 'High[0]',now for the current bar it will be 1704-1700=4 points, which I don't think is the correct reflection of MFE.

              That is why I started looking at a code that remembers MFE, something like this:
              if ((Performance.AllTrades.TradesPerformance.Currency .AvgMfe)>=200
              && (Performance.AllTrades.TradesPerformance.Currency. AvgMfe)<=250)
              {
              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
              }

              The challenge is this calculates AvgMFE of 'all the trades' taken by the system and not of an individual trade.

              Any thoughts on how to capture the MFE of an individual trade?
              Again, very much appreciate your help and support in advance.

              Comment


                #8
                Originally posted by pandyav View Post
                Hi Koganam,
                Here is the code you provided:
                double CurrentBarHighestGain = High[0] - Position.AvgPrice;
                HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);

                Let's say I enter the position at 1700 on ES. The price touches high of 1705 at the end of next 10 bars, so my MFE at the end of 10th bar will be:
                1705-1700 = 5 Points

                On the next bar, the high is 1704 and since MFE is calculated based on 'High[0]',now for the current bar it will be 1704-1700=4 points, which I don't think is the correct reflection of MFE.
                If you have 2 lines of code, why do you stop your argument after the first line? Are 5 points not higher than 4 points? What does Math.Max() do? ref: http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

                Comment


                  #9
                  Not sure what do you mean by 'why do you stop after first line'?

                  Yes, 5 ticks is more than 4 ticks, and 5 ticks will be the MFE for the trade. And if I look at the code you had provided it calculates max profit/loss per bar and not the MFE. Hence I posed the question, if there is a way to refer to MFE for an individual trade?

                  Math.Max was included in the code you had provided in the other thread.
                  http://www.ninjatrader.com/support/f...832#post354832

                  Comment


                    #10
                    Originally posted by pandyav View Post
                    Not sure what do you mean by 'why do you stop after first line'?
                    You have provided an analysis of what the first line is doing. Where have you provided your analysis of what the second line is doing? It seems to me obvious then that your analysis stopped after the first line.
                    Yes, 5 ticks is more than 4 ticks, and 5 ticks will be the MFE for the trade. And if I look at the code you had provided it calculates max profit/loss per bar and not the MFE. Hence I posed the question, if there is a way to refer to MFE for an individual trade?

                    Math.Max was included in the code you had provided in the other thread.
                    http://www.ninjatrader.com/support/f...832#post354832
                    THAT is the question that I was asking.

                    Where have you provided your analysis of what the Math.Max() line is doing? Why have you chosen to ignore parts of the code? Do you really believe that they have no purpose? I wrote you code that works together to a purpose. If you choose to ignore parts of it, that I cannot help. Did you even bother to check the Microsoft reference that I provided?
                    Last edited by koganam; 01-02-2014, 01:30 PM.

                    Comment


                      #11
                      Sorry for the confusion.

                      I am not ignoring the second part of the code. All I am saying is that the code does not reflect MFE since the calculation is based on High[0].

                      Again, going back to my previous example, as prices move from 1700 to 1705, the second line of the code will say that MFE is 5. After that in the very next bar, when high is 1704, the Math.Max line will give a result of 4.

                      Whereas I want MFE to be at 5 since that is the maximum amount of points that trade has moved in my favor. So I am wondering instead of calculating maximum 'Profit/Loss' at the end of the 'every' bar, is there a way to obtain 'MFE' for an 'individual trade?'

                      Thanks!

                      Comment


                        #12
                        Originally posted by pandyav View Post
                        Sorry for the confusion.

                        I am not ignoring the second part of the code. All I am saying is that the code does not reflect MFE since the calculation is based on High[0].

                        Again, going back to my previous example, as prices move from 1700 to 1705, the second line of the code will say that MFE is 5. After that in the very next bar, when high is 1704, the Math.Max line will give a result of 4.
                        HUH!? The maximum of 5 and 4 is 4?!

                        Not in my math.

                        Comment


                          #13
                          I think we're there, hang in with me.

                          Let's look at your code one more time:

                          private double HighestGain = 0;

                          calculate double CurrentBarHighestGain = High[0] - Position.AvgPrice;
                          HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);

                          So, what you're saying is that once the price moves from 1700 to 1705, HighestGain becomes 5. And I agree with that. Now as for the next bar High is equal to 1704 and CurrentBarHighestGain will be 4.

                          Looking at the second line of the code, you're comparing existing HighestGain 5 and CurrentBarHighestGain 4. I was comparing 0 and 4 assuming that HighestGain will reset to 0 at every bar.

                          So that brings up the question that if 5 will be HighestGain for the trade, I'll have to reset HighestGain to 0 at the END of every trade, correct?

                          Comment


                            #14
                            Originally posted by pandyav View Post
                            I think we're there, hang in with me.

                            Let's look at your code one more time:

                            private double HighestGain = 0;

                            calculate double CurrentBarHighestGain = High[0] - Position.AvgPrice;
                            HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);

                            So, what you're saying is that once the price moves from 1700 to 1705, HighestGain becomes 5. And I agree with that. Now as for the next bar High is equal to 1704 and CurrentBarHighestGain will be 4.

                            Looking at the second line of the code, you're comparing existing HighestGain 5 and CurrentBarHighestGain 4. I was comparing 0 and 4 assuming that HighestGain will reset to 0 at every bar.

                            So that brings up the question that if 5 will be HighestGain for the trade, I'll have to reset HighestGain to 0 at the END of every trade, correct?
                            Yes! I said as much in my post at 1202EST.

                            Comment


                              #15
                              You're awesome. Thank you.

                              Now the last thing that I want to do is to display the HighestGain in form of a bar below the chart. Do you know what kind of code I can use to display simple bar chart for HighestGain calculated per your code?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GARZONJ, Today, 11:22 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by KonalGondal, Today, 10:18 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Kate  
                              Started by Jorge.andres.o, Today, 09:24 AM
                              2 responses
                              9 views
                              0 likes
                              Last Post Gland1
                              by Gland1
                               
                              Started by Oracletrades, Today, 09:02 AM
                              5 responses
                              9 views
                              0 likes
                              Last Post Oracletrades  
                              Started by JTizz, Today, 08:17 AM
                              7 responses
                              25 views
                              0 likes
                              Last Post JTizz
                              by JTizz
                               
                              Working...
                              X