Posted by : 波動馬克 2016年11月22日 星期二

摩台相對強弱與電金相對強弱




    接續上一個文章我直接寫了一個訊號,邏輯上就非常簡單,濾網一就是摩台相對台指強,同時摩台為正價差,台指為逆價差,且以開盤為準電子相對金融強,同時用個時間當濾網(9:05-12:00),然後進場用最近16根K線的高stop buy;今天就是標準的軋空盤。
那空單進場呢?就反過來作就對了。
    出場呢?出場就比較複雜一點,剛開始用個起使停損,接著就用階梯式停損。
還有就是在結算的時候要早點出場。等下會分享一下我出場的程式碼;
    這裡要先提一個移動停損點的劃法,也就是移動是停損的指標怎麼劃在圖上,
通常我的習慣是要把指標劃出了,特別是移動式停損點,這樣好去比對程式有沒有出錯。
因為程式碼太長,我乾脆就在每行的右邊加上注釋。


var:BB(0),SS(0);
if i_MarketPosition=1 then BB=BB+1;{用BB表示多單進場後的第幾根Bar}
if i_MarketPosition=-1 then SS=SS-1;{用SS表示多單進場後的第幾根Bar}
if i_MarketPosition=0 then
begin
{i_Marketposition是內建的指令,特別用在指標上,用以判斷現在訊號是多還是空}
  BB=0;
  SS=0;
end;
{********************set   stop***************************************}
input:adj(1),multi(1),PT_threshold(20),Reverse(15);
var:Entryprice(0),longstop(0),shortstop(0),HH(0),LL(0),HC(0),LC(0),
    LongPT_threshold(0),ShortPT_threshold(0),LongPT(0),ShortPT(0);
if BB=1 then {多進場後的當下,對應在訊號就是Barssinceentry=0,要設定起始停損用}
begin
  Entryprice=open;
  if Mod(entryprice*adj,10)<>0 then
   begin
     longstop=entryprice-(10*TickSize+Mod(entryprice*adj,10)*TickSize);
   end;
   if Mod(entryprice*adj,10)=0 then
   begin
     longstop=entryprice-20*TickSize;
   end;
  LongPT_threshold=Entryprice+PT_threshold*TickSize;
  HH=high;{進場當下的高}
  HC=close;{進場當下的收}
end;

if SS=-1 then {空單進場的當下,設定起始停損}
begin
  Entryprice=open;
  if Mod(entryprice*adj,10)<>0 then
   begin
     shortstop=entryprice+(20*TickSize-Mod(entryprice*adj,10)*TickSize);
   end;
   if Mod(entryprice*adj,10)=0 then
   begin
     shortstop=entryprice+20*TickSize;
   end;
  ShortPT_threshold=Entryprice-PT_threshold*TickSize;
  LL=low;{進場當下的低}
  LC=close;{進場當下的收}
end;

if high>HH then HH=high;{進場後的新高}
if close>HC then HC=close;
if low<LL then LL=low;{進場後的新低}
if close<LC then LC=close;

if BB>1 then begin{多單進場後,對應在訊號碼就是Barssinceentry>0}
if c>HH[1] then longstop=maxlist(longstop,minlist(low,low[1])-multi*averagetruerange(14));
{這裡就要特別解釋為甚麼停損會移動了,在進場後的第一根之後,如果收盤價站上進場後的高,這裡特別說明了是站上,所以就用c>HH[1],不能用c>HH喔,這樣永遠不會站上進場後的高,然後停損怎麼移動呢?就是選擇最近兩根K選最低的那個價位再減一個彈性值,我選擇的是幾倍的ATR,然後呢再跟原來的起始停損比取大者,因為有時候盤還沒站上進場後的新高時因為上下甩動大造成ATR變大,結果移動停損就比起始停損大,這不合理,我就是要初始的停損固定在原來的價位,打掉就出掉了,除非盤勢對我有利,就是站上新高,我才位往上移動,好累,打好多字,目的就是要解釋清楚這行在操作上的意義,因為停損會移動就是靠這個邏輯。打完了}
if close<LongPT_threshold then LongPT=longstop;
{這行是我多餘要來測試的,我之前就設了一個目標獲利點(LongPT)為了要試看看停利用的,如果大家要用可以試試看,不過我測試的結果,這種停利法會很早就出場,賺不到大根的,我們做程式交易的就是要抓肥尾,賺賠比要大才划算,程式交易就類似選擇權Long Gama的概念,就是在等大根波動,我喜歡波動,也愛不確定,市場愈不確定愈好,愈亂愈好,或是市場上有90%往同一邊看的時候這樣最好,我最喜歡早上聽晨間新聞了,特別是ㄧ些廣播節目還有就是媒體的頭條,幾乎當天的盤勢都會逆著頭條走,特別是大家的方向都一致的時候,這很有趣,題外話啦!}
if close>LongPT_threshold and  (high>HH[1] and close>HC[1]) then LongPT=high-Reverse*TickSize;
end;
if SS<-1 then begin
if c<LL[1] then shortstop=minlist(shortstop,maxlist(high,high[1])+multi*averagetruerange(14));
if close>ShortPT_threshold then ShortPT=shortstop;
if close<ShortPT_threshold and (low<LL[1] and close<LC[1]) then ShortPT=low+Reverse*TickSize;

end;
if BB>1 then
begin
   plot1(LongPT_threshold,"longPT_threshold");
   plot2(Entryprice,"entryprice");
   plot3(longPT,"longPT");
   plot4(longstop,"longstop");
end;
if SS<-1 then
begin
   plot5(ShortPT_threshold,"shortPT_threshold");
   plot6(Entryprice,"entryprice");
   plot7(shortPT,"shortPT");
   plot8(shortstop,"shortstop");
end;
    以上的程式是移動式停損指標,大家可以試試看放在其它的商品,我特別用了一些通則,譬如用幾倍的ATR,這樣試用在不同的商品,同時我也用了TickSize,電子的TickSize就是0.05
金融的TickSize就是0.2,台指就是1)。

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © 波動馬克的程式交易 - Blogger Templates - Powered by Blogger - Designed by Johanes Djogan -