Board logo

标题: 【optimalfgeo】 [打印本页]

作者: 龙听    时间: 2022-11-2 14:30     标题: 【optimalfgeo】

  1. //OptimalFGeo by George Pruitt
  2. //My interpretation Sept. 2018
  3. //www.georgepruitt.com
  4. //georgeppruitt@gmail.com

  5. input: minNumTrades(numericSimple);
  6. vars: totalTradesCount(0),tradeCnt(0);
  7. array: tradesArray[500](0);

  8. vars: iCnt(00),jCnt(00),grandTot(0),highI(0);
  9. vars: optF(0.0),gMean(0.0),fVal(0.0),HPR(0.0),TWR(0.0),hiTWR(0.0);
  10. vars: biggestLoser(0.0),gat(0.0);

  11. totalTradesCount = totalTrades;
  12. If totalTradesCount > totalTradesCount[1] then
  13. begin
  14.         tradeCnt = tradeCnt + 1;
  15.         tradesArray[tradeCnt] = positionProfit(1);
  16. end;

  17. // Taken from my Fortran library - GPP and Vince Book PMF

  18. optF = 0.0;
  19. gMean = 1.00;
  20. gat   = 0.00;
  21. //Only calculate if new trade
  22. IF(tradeCnt>minNumTrades and totalTradesCount > totalTradesCount[1]) then
  23. Begin
  24.         biggestLoser = 0;
  25.         grandTot = 0;
  26.         For iCnt = 1 to tradeCnt //get the biggest loser
  27.         begin
  28.                    grandTot = grandTot + tradesArray[iCnt];
  29.                    IF(tradesArray[iCnt]<biggestLoser) then biggestLoser = tradesArray[iCnt];
  30.         end;
  31. //        print(grandTot," ",biggestLoser);
  32.         IF({grandTot > 0 and} biggestLoser <0) then
  33.         begin
  34. //                print("Inside TWR Calculations");
  35.                 highI = 0;
  36.                 hiTWR = 0.0;
  37.                 for iCnt = 1 to 100
  38.                 begin
  39.                         fVal = .01 * iCnt;
  40.                         TWR = 1.0;
  41.                         for jCnt = 1 to tradeCnt // calculate the Terminal Wealth Relative
  42.                         begin
  43.                             HPR = 1. + (fVal * (-1*tradesArray[jCnt]) / biggestLoser);
  44.                             TWR = TWR * HPR;
  45. //                           print(fVal," ",iCnt," " ,jCnt," Trades ",tradesArray[jCnt]," HPR ",HPR:6:4," TWR : ",TWR:6:4," hiTWR",hiTWR:6:4," bl ",biggestLoser);
  46.                         end;
  47. //                        print(iCnt," ",TWR," ",hiTWR);
  48.                         IF(TWR>hiTWR) THEN
  49.                         begin
  50.                             hiTWR = TWR;
  51.                             optF = fVal;            // assign optF to fVal in case its the correct one               
  52.                         end
  53.                         else
  54.                             break;                     //highest f found - stop looping
  55.                 end;               
  56.                 If (TWR <= hiTWR or optF >= 0.999999) then
  57.                 begin
  58.                         TWR  = hiTWR;
  59.                         OptimalFGeo = optF;  //assign optF to the name of the function
  60.                 end;      
  61.                 gmean = power(TWR,(1.0 / tradeCnt));
  62.                
  63.                 if(optF<>0) then gat   = (gMean - 1.0) * (biggestLoser / -(optF));               
  64.                 print(d," gmean ",gmean:6:4," ",gat:6:4);  // I calculate the GMEAN and GeoAvgTrade
  65.         end;
  66. end;
复制代码





欢迎光临 龙听期货论坛 (http://qhlt.club/) Powered by Discuz! 7.2