Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimizer Type: Max. Expectancy

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

    Optimizer Type: Max. Expectancy

    Being slightly frustrated with the limited number of optimization types in the strategy analyzer, I tried to create one.

    As this is a bit different than creating a strategy or indicator, its not quite as easy to tell if things are working correctly and I wouldn't mind if a few people (especially those familiar with expectancy) could test to see if this works.

    Expectancy is based on the work of Van K. Tharp and although I found a few different formulas for it, the only one I could seem to get working was by the forum member 'anachronist'.

    Expectancy = (AverageWin × PercentWinners + AverageLoss × PercentLosers) ⁄ AbsoluteValue(AverageLoss)
    Instructions:
    -Save this file to your '\NinjaTrader 6.5\bin\Custom\Type' folder.
    -Compile any existing NinjaScript.
    -Select 'max. expectancy' for the 'Optimize On' setting in the Strategy Analyzer/Optimize window



    *(old version removed)
    Last edited by Elliott Wave; 12-17-2008, 12:36 PM.

    #2
    Hi Elliott,

    Always looking for new things. When I tried your optimizer, it altered the optimizations of other optimizations as well (not your setting, but a built-in setting like max avg profit). As soon as I removed your CS and re-compiled, things were back to normal.

    Thought you should know. If this is a problem of my local config, I'd like to know how to resolve it.

    Comment


      #3
      I think this expectancy formula is a useful measure for some things (risk/reward analysis, position sizing, etc.), but I'm not sure it is a good general way to rank/select trading strategies. The reason is that it does not take into account the profit per day (number of trades). Using only expectancy as a ranking, a single trade per year gaining $100 would be ranked better than a strategy that earns $75 per trade with hundreds of trades per year. One quick and dirty way to make the expectancy consider the number of trades is to multiply it by the square root of the number of trades. I implemented a basic expectancy optimizer type a few weeks ago, and went this route, though I had to add in some multipliers to get the weighting the way I wanted it, and still wasn't that happy with it.

      The most common formula I see quoted for expectancy is this:
      Expectancy = (Probability of Win * Average Win$) - (Probability of Loss * Average Loss$)

      Van Tharp describes expectancy here only in reference to R-multiples of a hard stop loss, so the max risk can be calculated for every trade:

      For NT, we won't always have a hard stop, so some alternative/conditional formula will likely be necessary. He also suggests a minimum of 30 trades, but recommends 100 or more before calculating a value. A NT version might have to fudge on that requirement, or it could return 0 for less than 30 trades, but that might confuse some people.
      The information here states he has a simplified expectancy formula on his DVDs, so maybe that is what people are quoting for the other formulas based on average losses, since it is easier to calculate:

      I don't have Van Tharp's books, so I don't know for sure how he describes expectancy there.

      I think Van Tharp's SQN formula is one useful way to rank general trading systems. There is a version of it here in the forums, but I have not analyzed it yet. A validated version of that would be a great addition to NT 7, but my first choice would be annual and monthly Sortino ratios.

      I'd make one minor change to your code: Make sure LosingTrades.Count isn't 0 before dividing by it.
      Last edited by Anagoge; 12-16-2008, 07:33 PM.

      Comment


        #4
        Is this code line incorrect?

        Code:
        aveWin = systemPerformance.AllTrades.TradesPerformance.Currency.AvgProfit;
        It seems like you would want the average winning trade dollars, not the average dollars gained for all trades. This seems like it would be better:

        Code:
        aveWin = systemPerformance.AllTrades.WinningTrades.TradesPerformance.Currency.AvgProfit;
        aveLose = systemPerformance.AllTrades.LosingTrades.TradesPerformance.Currency.AvgProfit;

        Comment


          #5
          Originally posted by Anagoge View Post
          Is this code line incorrect?

          Code:
          aveWin = systemPerformance.AllTrades.TradesPerformance.Currency.AvgProfit;
          It seems like you would want the average winning trade dollars, not the average dollars gained for all trades. This seems like it would be better:

          Code:
          aveWin = systemPerformance.AllTrades.WinningTrades.TradesPerformance.Currency.AvgProfit;
          aveLose = systemPerformance.AllTrades.LosingTrades.TradesPerformance.Currency.AvgProfit;
          It may not be correct. I think you are right.

          I took that calculation from @MaxAvgProfit.cs, as I was looking at some existing types to get an idea of how to create new ones.

          I've changed it and it appears to work better, and I also changed the first part to try and avoid a divide by zero error which might happen if the first trade is a winner.

          Code:
          if (systemPerformance.AllTrades.Count == 0 || systemPerformance.AllTrades.LosingTrades.Count == 0)
                      {
                          return 0;
                     }
          Here is the updated version. I also attached the SQN formula you mentioned.
          Attached Files
          Last edited by Elliott Wave; 12-17-2008, 12:33 PM.

          Comment


            #6
            That looks good. The only little issue that might warrant a change is how to handle backtests with no losing trades. Technically, this would be ideal, and deserve a very high expectancy rating, but in reality it may mean the system wasn't tested on enough data. Right now, it returns 0 in that case, and that is one reasonable solution, but another option is to return a large number (the question is which one?) or return just the numerator of the solution without dividing by the absolute value of the average loss. None of these options are ideal, so maybe someone has a better idea...

            Comment


              #7
              Luckily I doubt it will be an issue. No strategy with a sufficient number of trades will have none that are losers.

              Like you mentioned the easiest way would simply be to use the other calculation

              (Probability of Win * Average Win$) - (Probability of Loss * Average Loss$)

              But if there are no losers the probability of win is 100% so the second part of the equation wouldn't be needed as it would be 0 * 0. In that case the expectancy would just be the average win.

              Comment


                #8
                Thanks a lot for this optimizer type, it's the best one I've seen until now

                I also use Van Tharps R-multiples to evaluate a backtested strategy. The R values your optimizer calculates are the same ones I get when I calculate them in excel.

                Comment


                  #9
                  Hi,
                  Has anyone used the SQN file in NT8?

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by WaleeTheRobot, 01-07-2024, 10:07 AM
                  16 responses
                  424 views
                  2 likes
                  Last Post WaleeTheRobot  
                  Started by ceeb69, Today, 07:17 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post ceeb69
                  by ceeb69
                   
                  Started by rene69851, 02-19-2024, 06:45 PM
                  2 responses
                  15 views
                  0 likes
                  Last Post rene69851  
                  Started by algospoke, 02-19-2024, 03:25 PM
                  2 responses
                  17 views
                  0 likes
                  Last Post algospoke  
                  Started by rbeckmann05, Today, 12:19 PM
                  7 responses
                  23 views
                  0 likes
                  Last Post rbeckmann05  
                  Working...
                  X