[TS懶人包] [保險服務]

2010年3月3日 星期三

變數的運用(四)

這篇主要講解上一篇的練習題目,因為程式碼稍微的長了一些。而這一篇的練習題目會再從這邊發展下去…所以,真的想要學習的就跟上進度吧。

首先是練習程式的程式碼:

vars: crossStatus(0), openStatus(0);
vars: mc(0);

mc = marketposition * currentcontracts;

if date <> date[1] then begin
crossStatus = 0;
openStatus = 0;
end;

Value1 = MACD(close, 12, 26);
Value2 = XAverage(MACD(close, 12, 26), 9);

if opend(0) > closed(1) then begin
openStatus = 1;
end;

if opend(0) < closed(1) then begin
openStatus = -1;
end;

if openStatus = 1 and Value1 cross under Value2 then begin
crossStatus = 1;
end;

if openStatus = -1 and Value1 cross over Value2 then begin
crossStatus = -1;
end;

if time < 1300 and crossStatus = 1 and Value1 cross over Value2 then begin
buy next bar at market;
end;

if time < 1300 and crossStatus = -1 and Value1 cross under Value2 then begin
sell next bar at market;
end;

if time = 1340 then begin
exitlong next bar at market;
exitshort next bar at market;
end;

if mc <> mc[1] then begin
openStatus = 0;
crossStatus = 0;
end;

setstoploss(10000);


首先我另外宣告了一個變數 mc,之前提到過,這是用來記錄倉位變化用的。當然你也可以取別的名字。

前半段程式大概沒多大的差別,首先是開高開低的判斷…再這邊我開高的情況記錄為 1,而開低的情況我記錄為 -1;因為這個開高、開低是不會產生衝突的,所以利用一個變數來記錄二個狀態。到最後也許一個變數會記錄多種狀態也說不定。

接下來則是第一次交叉的狀態記錄,在開高的時候第一次交叉的狀態記錄為 1,而在開低的時候第一次交叉記錄為 -1,同樣的,這二種情況是不會衝突的,所以也是利用一個變數來記錄就可以了。

再來就是進場條件了,進場條件沒什麼更動,所以只要狀態正確、接下來的交叉就是進場作單了。

然後是一個收盤平倉的程式碼。

再來則是當倉位改變的時候,我把狀態歸零了,這可以避免掉停損後,又產生另一次的進場。在前面的文章提到過,倉位轉變記錄的用法…這邊就用到了。

最後…則是一個停損的保留字語法…偷懶一下,在停損的部份就直接用保留字完成了。關於幾個出場好用的保留字請參考:程式交易 - TradeStation 幾個好用的保留字


接下來練習的時間到了…讓我們再搞得複雜一點,進場的時候以交叉的高低點作突破進場,並限制產生第一次交叉時的 MACD 值在 50 上(開高的死亡交叉)或 50 下(開低的黃金交叉),第二次交叉時 MACD 值在 50 下(開高的黃金交叉)或 50 上(開低的死亡交叉)。

1 則留言:

  1. \*習題*\

    vars: crossStatus(0), openStatus(0);
    vars: mc(0);

    mc = marketposition * currentcontracts;

    if date <> date[1] then begin
    crossStatus = 0;
    openStatus = 0;
    end;

    Value1 = MACD(close, 12, 26);
    Value2 = XAverage(MACD(close, 12, 26), 9);

    if opend(0) > closed(1) then begin
    openStatus = 1;
    end;

    if opend(0) < closed(1) then begin
    openStatus = -1;
    end;

    if openStatus = 1 and Value1 cross under Value2 and Value1 > 50 then begin
    crossStatus = 1;
    end;

    if openStatus = -1 and Value1 cross over Value2 and Value1 < 50 then begin
    crossStatus = -1;
    end;

    if time < 1300 and crossStatus = 1 and Value1 cross over Value2 and Value1 > 50 then begin
    price=buypoint;
    buy next bar at buypoint;
    end;

    if time < 1300 and crossStatus = -1 and Value1 cross under Value2 and Value1 < 50 then begin
    price=sellpoint;
    sell next bar at sellpoint;
    end;

    if time = 1340 then begin
    exitlong next bar at market;
    exitshort next bar at market;
    end;

    if mc <> mc[1] then begin
    openStatus = 0;
    crossStatus = 0;
    end;

    setstoploss(10000);


    煩請DK大批閱

    回覆刪除

請留下您的大名…匿名者恕不回應…

Related Posts Plugin for WordPress, Blogger...