2019年7月9日 星期二

[量化狼]順勢交易吧!心理線PSY

  傳統的日線PSY(心理線)邏輯非常簡單,就是一段時間內上漲K棒的比例:

PSY = n日內上漲的天數/ n*100%

        大部分網路上的心理線都是做日線且逆勢,在此我示範怎麼將PSY應用在分K,並且改為順勢。在日線上很少有不漲不跌的日子,因此用二分法(or)就可以了;但在分K中由於時間較短,常常會有行情沒動的情形發生,因此要將它分為三類(漲、平、跌)
                 改良PSY = (上漲的K棒數*1 + 平盤的K棒數*0.5 + 下跌的K棒數*0) / K棒數
        很明顯地,改良PSY還是跟原始的一樣介於0~1之間,而中值為0.5;在Multichart裡我用array運算,大家也可以用countif試試。

策略基本設定:
策略名稱
PSY策略
交易層級
5
交易成本
來回600
回測期間
2001-2019/6



程式碼Input :

  L_in(0.65),L_Out(0.3) ,
  S_in(0.3),S_Out(0.55);
Var :
  x(0),PSY(0.5) ;
Array : 
  Array_UorD[30](0) ;
 
for x = array_getmaxindex(Array_UorD) downto 2 begin
  Array_UorD[x] = Array_UorD[x-1] ;
end ;
if c>c[1] then
  Array_UorD[1] = 1
else if c<c[1] then
  Array_UorD[1] = 0
else
  Array_UorD[1] = 0.5 ;
PSY = AverageArray(Array_UorD,array_getmaxindex(Array_UorD)) ;

if marketposition<=0 and PSY>L_in then
  buy 1 share at next bar market ;
if marketposition>=0 and PSY<S_in then
  sellshort 1 share at next bar market ;
if marketposition>0 and PSY<L_out then
  sell all share at next bar market ;
if marketposition<0 and PSY>S_out then
  buytocover all share at next bar market ;

if dayofmonth(date)>14 and dayofmonth(date)<22 and dayofweek(date)=3 then
  setexitonclose ;
 邏輯中文翻譯:

將漲跌值記入array
array平均獲得PSY
PSY>0.65時,多單進場
PSY<0.3時,空單進場
PSY<0.3時,多單出場
PSY>0.55時,空單出場
結算出場
權益曲線圖:
回測績效檢視:
歷年績效分析: