請選擇 進入手機版 | 繼續訪問電腦版

COCO研究院

 找回密碼
 註冊
搜索
查看: 8387|回復: 2

Portfolio Trader最佳化的Robustness

[複製鏈接]
發表於 23-11-4 12:09 | 顯示全部樓層 |閱讀模式
如題,使用圖表時最佳化可以設定Robustness決定穿透IS跟OSS的參數
不知道在portfolio trader中是否也能這樣設定?

評分

參與人數 1金錢 +1 收起 理由
TrendRover + 1 按一個讚

查看全部評分

 樓主| 發表於 23-11-11 19:52 | 顯示全部樓層
本帖最後由 abula 於 23-11-11 19:53 編輯

自己來回應,程式碼如下。這個程式是用官方!optimiza by sharpe ratio改的,主要邏輯是用array定義我預設的四個樣本週期,進行迴圈以計算樣本內外的netprofit,最後計算各個IS與OOS的年化報酬率來計算WFE。未通過的給一個很大的負值(-9999)。

好處是可以用WFE當作濾網來過濾樣本外表現差的參數,壞處是自己需要手動輸入Inputs(我現在做法是先跑很少參數的WFO看日期,然後手動輸入)。我一開始有想要讓程式自己算例如 currentdate-X days的方式,但台指期交易日不能完全對的上,如果你像我一樣跑的期間都至少六七年,那四個去跑十年資料應該夠,也不會太麻煩。


  1. Inputs:
  2.   InitialCapital(10000),  // Initial capital
  3.   AnnualRiskFreeRate(0.02), // Annual risk-free interest rate
  4.   Period(0), // 0=monthly, 1=daily
  5.   IS_StartDate1(0), IS_EndDate1(0), OOS_StartDate1(0), OOS_EndDate1(0),
  6.   IS_StartDate2(0), IS_EndDate2(0), OOS_StartDate2(0), OOS_EndDate2(0),
  7.   IS_StartDate3(0), IS_EndDate3(0), OOS_StartDate3(0), OOS_EndDate3(0),
  8.   IS_StartDate4(0), IS_EndDate4(0), OOS_StartDate4(0), OOS_EndDate4(0);

  9. Arrays:
  10.   IS_StartDates[4](0), // Array to store start dates of In-Sample periods
  11.   IS_EndDates[4](0), // Array to store end dates of In-Sample periods
  12.   OOS_StartDates[4](0), // Array to store start dates of Out-of-Sample periods
  13.   OOS_EndDates[4](0); // Array to store end dates of Out-of-Sample periods

  14. Vars:
  15.   PeriodIndex(0),
  16.   IS_Profit(0),
  17.   OOS_Profit(0),
  18.   IS_AnnualReturn(0),
  19.   OOS_AnnualReturn(0),
  20.   IS_CurrentProfit(0),
  21.   OOS_CurrentProfit(0),
  22.   SharpeValue(0),
  23.   FitnessValue(0),
  24.   PrevISNetProfit(0), // Previous net profit at the start of the In-Sample period
  25.   PrevOOSNetProfit(0); // Previous net profit at the start of the Out-of-Sample period

  26. // Initialize arrays only on the first bar of data
  27. If CurrentBar = 1 Then Begin
  28.   IS_StartDates[1] = IS_StartDate1; IS_EndDates[1] = IS_EndDate1; OOS_StartDates[1] = OOS_StartDate1; OOS_EndDates[1] = OOS_EndDate1;
  29.   IS_StartDates[2] = IS_StartDate2; IS_EndDates[2] = IS_EndDate2; OOS_StartDates[2] = OOS_StartDate2; OOS_EndDates[2] = OOS_EndDate2;
  30.   IS_StartDates[3] = IS_StartDate3; IS_EndDates[3] = IS_EndDate3; OOS_StartDates[3] = OOS_StartDate3; OOS_EndDates[3] = OOS_EndDate3;
  31.   IS_StartDates[4] = IS_StartDate4; IS_EndDates[4] = IS_EndDate4; OOS_StartDates[4] = OOS_StartDate4; OOS_EndDates[4] = OOS_EndDate4;
  32. End;

  33. // Calculation and strategy logic
  34. For PeriodIndex = 1 to 4 Begin
  35.   // Check if PeriodIndex is within the bounds of the arrays
  36.   If (PeriodIndex >= 1) and (PeriodIndex <= 4) Then Begin

  37.     // Existing logic for In-Sample and Out-of-Sample Periods
  38.     // ...

  39.   End; // End of bounds check
  40. End;

  41. For PeriodIndex = 1 to 4 Begin
  42.   // In-Sample Period Logic
  43.   // Check if the current date is within the In-Sample period
  44.   If Date >= IS_StartDates[PeriodIndex] and Date <= IS_EndDates[PeriodIndex] Then Begin
  45.     // On the start date, store the current net profit as the previous In-Sample net profit
  46.     If Date = IS_StartDates[PeriodIndex] Then PrevISNetProfit = NetProfit;
  47.     // On the end date, calculate the profit for the In-Sample period
  48.     If Date = IS_EndDates[PeriodIndex] Then Begin
  49.       IS_CurrentProfit = NetProfit - PrevISNetProfit;
  50.       IS_Profit = IS_Profit + IS_CurrentProfit;
  51.       // Calculate annualized return for the In-Sample period
  52.       IS_AnnualReturn = (IS_CurrentProfit / InitialCapital) / (IS_EndDates[PeriodIndex] - IS_StartDates[PeriodIndex] + 1) * 365;
  53.     End;
  54.   End;

  55.   // Out-of-Sample Period Logic
  56.   // Check if the current date is within the Out-of-Sample period
  57.   If Date >= OOS_StartDates[PeriodIndex] and Date <= OOS_EndDates[PeriodIndex] Then Begin
  58.     // On the start date, store the current net profit as the previous Out-of-Sample net profit
  59.     If Date = OOS_StartDates[PeriodIndex] Then PrevOOSNetProfit = NetProfit;
  60.     // On the end date, calculate the profit for the Out-of-Sample period
  61.     If Date = OOS_EndDates[PeriodIndex] Then Begin
  62.       OOS_CurrentProfit = NetProfit - PrevOOSNetProfit;
  63.       OOS_Profit = OOS_Profit + OOS_CurrentProfit;
  64.       // Calculate annualized return for the Out-of-Sample period
  65.       OOS_AnnualReturn = (OOS_CurrentProfit / InitialCapital) / (OOS_EndDates[PeriodIndex] - OOS_StartDates[PeriodIndex] + 1) * 365;
  66.     End;
  67.   End;
  68. End;

  69. // Calculate the Sharpe Ratio and custom fitness value on the last bar of the chart
  70. if LastBarOnChart_s then begin
  71.   // Calculate the Sharpe Ratio using the built-in function
  72.   SharpeValue = SharpeRatio(Period, AnnualRiskFreeRate, LastBarOnChart_s, InitialCapital);

  73.   // Check if the Out-of-Sample annualized return is greater than 50% of the In-Sample annualized return
  74.   If OOS_AnnualReturn > 0.5 * IS_AnnualReturn then begin
  75.     FitnessValue = SharpeValue; // If true, set the fitness value to the Sharpe Ratio
  76.   end else begin
  77.     FitnessValue = -9999 + SharpeValue; // If false, penalize the fitness value
  78.   end;
  79.   
  80.   // Set the custom fitness value for the strategy
  81.   SetCustomFitnessValue(FitnessValue);
  82. end;

複製代碼





發表於 23-11-16 12:53 | 顯示全部樓層
PT的WFA好像沒有MC的WFA多功能?

請問這個code是放在PT的MM信號內還是?
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-6-23 00:10

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 |