Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimal F Position Sizing

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

    Optimal F Position Sizing

    Hello Good Morning Everybody!
    I would like to try to code Mr Ralph Vince's Optimal F for my
    position sizing money management strategy as following:



    #region*Using*declarations
    using*System;
    using*System.ComponentModel;
    using*System.Diagnostics;
    using*System.Drawing;
    using*System.Drawing.Drawing2D;
    using*System.Xml.Serialization;
    using*NinjaTrader.Cbi;
    using*NinjaTrader.Data;
    using*NinjaTrader.Indicator;
    using*NinjaTrader.Gui.Chart;
    using*NinjaTrader.Strategy;
    #endregion
    //*This*namespace*holds*all*strategies*and*is*requir ed.*Do*not*change*it.
    namespace*NinjaTrader.Strategy
    {
    ****///*<summary>
    ****///*Enter*the*description*of*your*strategy*here
    ****///*</summary>
    ****[Description("Enter*the*description*of*your*strateg y*here")]
    ****public*class*OptimalFpositionSizing*:*Strategy
    ****{
    **
    public*enum*MMType
    {
    Optimal_F;
    }

    ********#region*Variables
    ********//*Wizard*generated*variable***
    ******private*int*longQuantity=*0;
    *** private*double*priorTradesCumProfit=*0;
    * ***private*double*accbalNew***=*0;
    **private*double*winningPercent_W*=*0;
    **private*double**riskf****=*2;
    **private*double**psize****=*0;
    **private*MMType*mmtype;
    **private*double*optimalF*=*0;*
    **
    **
    *
    ******#endregion
    ********///*<summary>
    ********///*This*method*is*used*to*configure*the*strategy*and *is*called*once*before*any*strategy*method*is*call ed.
    ********///*</summary>
    ********protected*override*void*Initialize()
    ********{
    ****AccountSize*=*50000;
    ****EntriesPerDirection*=1*;
    ***EntryHandling*=*EntryHandling.UniqueEntries;
    ***ExitOnClose*=*false;
    ***IncludeCommission*=*true;
    ***CalculateOnBarClose*=*false;
    *******
    ******}
    ********///*<summary>
    ********///*Called*on*each*bar*update*event*(incoming*tick)
    ********///*</summary>
    ********protected*override*void*OnBarUpdate()
    ********{
    ***if*(Bars.FirstBarOfSession)
    ***{
    ****//*AllTrades*includes*virtual*and*real-time*trades.*For*real-time*trades*please*use*.RealtimeTrades
    ****
    priorTradesCumProfit*=*Performance.AllTrades.Trade sPerformance.Currency.CumProfit;
    ***}


    *******//LONG*CRITERIA
    ******
    ***
    **if(***********something)
    {
    *****PositionSize();
    *****EnterLong(longQuantity,*"PositionLong");
    ***}

    #region*PositionSIzeFunction*
    **void*PositionSize()
    **{
    ********
    ****
    AccbalNew*=*AccountSize*+*priorTradesCumProfit;
    *
    **
    ***
    if (Performance.AllTrades.Count > 20
    && Performance.AllTrades.LosingTrades.Count!=0)
    {
    winningPercent_W=((double)Performance.AllTrades.Wi nningTrades.Count-1)/((double)Performance.AllTrades.Count-1);
    }
    ****if(*mmtype*==*MMType.Optimal_F)
    ****{
    double optimalF = 2*winningPercent_W - 1
    // Optimal F = 2P - 1, P=Probability of winning trades.

    ** **psize*=*AccbalNew***(optimalF/100)/stopLoss;
    ** *****longQuantity=(Convert.ToInt32(Math.Floor(psiz e)/100)*100);
    *** ****}*
    * }
    ****Print("");
    ****Print("ACC*BAlNew:*"+AccbalNew);
    ****Print("Time:*"+Time[0]);
    ****Print("CumProfit:*"+priorTradesCumProfit1);
    ****Print("Position*SIze:*"+Psize);*
    ****Print("Optimal_F:*"+optimalF);
    **}
    #endregion

    _______

    As I believed that using Winning Ratio (winningPercent_W) to calculate
    the Optimal F is not accurate enough.

    Can anybody help to code the Optimal F base on Mr Ralph Vince's
    Formula, as I found it very difficult to code from DataSeries of past hostorical
    data.
    Thanks for your help.

    Arthur
    Last edited by ArthurMFTse; 10-02-2014, 07:41 AM.

    #2
    Arthur,

    Thank you for your note.

    Do you have a reference of the formula that you should be following?

    What exactly is wrong with the ratio?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hello Cal,

      Thanks for your quick reply, I do not know the exact formula for
      Optimal F, as
      Optimal F can't be done with a simple formula. It is iterating over all trades for all potential value of optimal F until the optimum value is formulated.

      I use its simple formula (I not sure this formula is the exact
      calculation or not?)
      Optimal F = 2P - 1 where p is the probability of winning trade base on
      large data simulation.

      Thanks for your help.

      Arthur

      Comment


        #4
        Arthur,

        From what I've seen is that the Optimal F = biggest loser / Stake per contract

        Does that sound about right?
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Hello Cal,

          Thanks for your Optimal F Formula.

          I will backtest to see the performance.

          Comment


            #6
            Here is what I`ve created to print Optimal F Geometric statistics to the NT Log at the end of a strategy execution. You should only need to cut and paste vPrint_GeoStatistics() into your NT script as a stand alone method and also include a call to it in the NT OnTermination() event... Cheers

            protected override void OnTermination()
            {
            vPrint_GeoStatistics();
            }

            private void vPrint_GeoStatistics()
            {
            Print("******************************************* ************************************************") ;
            Print("GeoMetric Statistics");
            Print("******************************************* ************************************************") ;
            if (Performance.AllTrades.Count <= 10)
            {
            Print("\t .... Trade Count <= 10 ..... cannot calculate GeoMetric Statistics");
            return;
            }

            double dblHPR = 0;
            double dblTWR = 1;
            double dblTWR_Prev = 1;
            double dblTWR_Highest = 1;
            double dblOpf_Final = 0;
            double dblOptf_Dolr = 0;
            double dblMathExpectation = 0;
            double dblGeoAvgTrade = 0;
            double dblGeoMean = 0;

            double dblPRR = 0;
            double dblAvg_WinDlr = Performance.AllTrades.WinningTrades.TradesPerforma nce.Currency.AvgProfit;
            double dblAvg_LossDlr = Performance.AllTrades.LosingTrades.TradesPerforman ce.Currency.AvgProfit;
            double dblTradesCount = Performance.AllTrades.Count;
            double dblWinCount = Performance.AllTrades.WinningTrades.Count;
            double dblLossCount = Performance.AllTrades.LosingTrades.Count;

            double dblLoss_Largest = 0;
            int ijk = 1;

            // ************************************************** **
            // Calculate Pessimistic Return Ratio
            // ************************************************** **
            dblPRR = (((dblWinCount - (Math.Pow(dblWinCount, 0.5))) / dblTradesCount) * dblAvg_WinDlr) /
            (((dblLossCount + (Math.Pow(dblLossCount, 0.5))) / dblTradesCount) * Math.Abs(dblAvg_LossDlr));

            // ************************************************** **
            // Calculate Geo Statistics
            // ************************************************** **

            // Determine Largest Loss
            for (ijk = 1; ijk < Performance.AllTrades.LosingTrades.Count - 1; ijk++)
            {
            Trade Trade_Loss = Performance.AllTrades.LosingTrades[ijk];
            dblLoss_Largest = Math.Min(dblLoss_Largest, Trade_Loss.ProfitCurrency);
            }

            // Determine Optf
            for (double dblOpf_temp = 0.01; dblOpf_temp <= 1; dblOpf_temp = dblOpf_temp + 0.01)
            {
            dblTWR = 1;

            for (ijk = 1; ijk < Performance.AllTrades.Count - 1; ijk++)
            {
            Trade Trade_All = Performance.AllTrades[ijk];
            dblHPR = 1 + (dblOpf_temp * (-Math.Round(Trade_All.ProfitCurrency, 2) / dblLoss_Largest));
            dblTWR = dblTWR * dblHPR;
            }

            if (dblTWR <= dblTWR_Prev)
            {
            dblOpf_Final = dblOpf_temp;
            dblTWR_Highest = dblTWR_Prev;
            break;
            }
            else
            {
            dblTWR_Prev = dblTWR;
            }

            }

            dblGeoMean = Math.Pow(dblTWR_Highest, 1 / (double)Performance.AllTrades.Count);
            dblMathExpectation = (dblGeoMean - 1) * 100;

            // If Optf is somewhat possible... calc OptfDollar and GAT
            if (dblOpf_Final != 0.01)
            {
            dblOptf_Dolr = dblLoss_Largest / -dblOpf_Final;
            dblGeoAvgTrade = (dblGeoMean - 1) * (dblLoss_Largest / -dblOpf_Final);
            }
            else
            {
            dblOpf_Final = 0;
            dblOptf_Dolr = 0;
            dblGeoAvgTrade = 0;
            dblGeoMean = 0;
            dblMathExpectation = 0;
            }

            if (dblGeoMean == double.PositiveInfinity || dblPRR == double.PositiveInfinity || dblPRR < 0)
            {
            dblOpf_Final = 0;
            dblOptf_Dolr = 0;
            dblGeoAvgTrade = 0;
            dblGeoMean = 0;
            dblMathExpectation = 0;
            dblPRR = 0;
            }

            Print("Optf=\t\t" + dblOpf_Final.ToString("0.00"));
            Print("Optf Dollar=\t" + dblOptf_Dolr.ToString("C"));
            Print("Math Exp=\t" + dblMathExpectation.ToString("C"));
            Print("GAT=\t\t" + dblGeoAvgTrade.ToString("C"));
            Print("GEOMean=\t" + dblGeoMean.ToString("0.00"));
            Print("PRR=\t\t" + dblPRR.ToString("0.00"));
            Print("LargeLoss=\t" + dblLoss_Largest.ToString("C"));
            }

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by f.saeidi, Today, 11:02 AM
            1 response
            1 view
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by geotrades1, Today, 10:02 AM
            4 responses
            12 views
            0 likes
            Last Post geotrades1  
            Started by rajendrasubedi2023, Today, 09:50 AM
            3 responses
            15 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by lorem, Today, 09:18 AM
            2 responses
            11 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by geddyisodin, Today, 05:20 AM
            4 responses
            30 views
            0 likes
            Last Post geddyisodin  
            Working...
            X