三重IF 第三層不跑

  •   464 
  • 最後發表   東.  2022 八月 11
東. 發文於   2022/08/02

策略:做空1口 停損20 停利40,移動停損停利3次,但是第3層一直平不了倉,所以我加了print,發現1~12除了6和12皆可以正常平倉,為什麼6和12沒辦法平呢?  已苦惱2星期!

input: profit_point(40, "停利(點)");
input: loss_point(20, "停損(點)");
input: profit_point60(60, "停利(點)");
input: profit_point80(80, "停利(點)");
input: profit_point120(120, "停利(點)");
value1=1;
value2=2;
value3=3;
value4=4;
value5=5;
value6=6;
value7=7;
value8=8;
value9=9;
value10=10;
value11=11;
value12=12;

SetPosition(-1, MARKET);        { 以市價買進 }
if Position = -1 then 
    begin           print("",value1);
        { 依照成本價格設定停損/停利 }
        if Close <= FilledAvgPrice - profit_point then  //賺40點                       
            begin       print("",value2);           
                if Close <= FilledAvgPrice - profit_point80 then //第一筆賺40點後又賺40點                     
                    begin       print("",value3);   
                        if Close <= FilledAvgPrice - profit_point60 or Close <= FilledAvgPrice - profit_point120 then//再賺停利60、120。 
                            begin   print("",value4);
                                SetPosition(0); 
                            end;
                    end 
                else if Close >= FilledAvgPrice - loss_point then //第一筆賺40點後虧20點
                    begin           
                    print("",value5);       
                                if Close <= FilledAvgPrice - profit_point60 or Close >= FilledAvgPrice then //停利60停損0 
                                begin
                                SetPosition(0); print("",value6);
                                end;
                    end;
            end
        else 
            begin   print("",value7);
                if Close >= FilledAvgPrice + loss_point then //虧20點
                    begin   print("",value8);
                        if Close >= FilledAvgPrice + profit_point then //第一筆虧20點再虧20點                        
                            begin   print("",value9);
                                if  Close >= FilledAvgPrice + profit_point60 or Close <= FilledAvgPrice then //停損-60停損0                               
                                    begin print("",value10);
                                        SetPosition(0); 
                                    end;
                            end

                        else if Close <= FilledAvgPrice - loss_point then//虧20點後賺40點 
                            begin   print("",value11);
                                        if Close <= FilledAvgPrice - profit_point60 or Close >= FilledAvgPrice then //停利60停損0
                                            begin
                                            SetPosition(0); print("",value12);
                                            end;
                            end;
                    end;
            end;
    end;
    

排序方式: 標準 | 最新
musashi 發文於   2022/08/02

1.如果要測試腳本,那麼第19行的SetPosition(-1, MARKET);  要用once跑一次就好,否則每次跑腳本都會執行(會讓後面的SetPosition無法正確執行)。

2.四層巢狀式的IF寫法太複雜了,其中還包含內層IF價格判斷比外層先成立的錯誤,單純判斷價格的話要考慮先後順序。

3.再來是你要寫出【第一筆賺40點後虧20點】但卻是寫成【如果賺了40點就...,沒賺40點且虧了20點就...】,建議先用condition把每個條件獨立判斷會比較容易上手。

3.別直接複製文章語法貼上,不然會出現多餘的網頁語法在腳本中喔。

東. 發文於   2022/08/05

 

3.再來是你要寫出【第一筆賺40點後虧20點】但卻是寫成【如果賺了40點就...,沒賺40點且虧了20點就...】,建議先用condition把每個條件獨立判斷會比較容易上手

請問M大 這句話我想了很久 不太理解是什麼意思

因為【第一筆賺40點後虧20點】代表一定要先賺40點後虧20點才執行以下動作,所以我else是放在第10行對應第三行的if

 

還是大大可以給我建議這種應該怎麼寫會比較好呢

 

 

if Close <= FilledAvgPrice - profit_point then //賺40點
            begin 
                if Close <= FilledAvgPrice - profit_point80 then //第一筆賺40點後又賺40點 
                begin 
                    if Close <= FilledAvgPrice - profit_point60 or Close <= FilledAvgPrice - profit_point120 then//再賺停利60、120。 
                    begin
                    SetPosition(0); print("",VALUE4);
                    end;
                end 
                else 
                begin 
                    if Close >= FilledAvgPrice - loss_point then //第一筆賺40點後虧20點 
                    begin
                        if Close <= FilledAvgPrice - profit_point60 or Close >= FilledAvgPrice then //停利60停損0 
                        SetPosition(0); print("",VALUE6);
                    end;
                end;
            end
        else 

if +40後 

         if+40

        else -20

else

musashi 發文於   2022/08/05

第3行不成立才會執行第10行else,所以第10行之後都是指【第一筆沒賺40點】才會執行。

先瞭解if else的用法才能把自己的想法寫出來喔。

如果要寫else,then和else之間不可放";",除非加入begin和end
在沒有begin和end情況下,then和else的後面只會執行到第一個";"
else 前面的end不加";"

    if ... then aaa; bbb; ccc; ddd; 
    ▲     符合時:  執行aaa
    ▲   不符合時:不執行aaa
    ▲ 不受if判斷:bbb ccc ddd

    if ... then aaa else bbb; ccc; ddd;         
    ▲     符合時:執行aaa、不執行bbb
    ▲   不符合時:執行bbb、不執行aaa    
    ▲ 不受if判斷:ccc ddd

    if ... then aaa else begin bbb; ccc; end; ddd;  
    ▲     符合時:執行aaa    、不執行bbb ccc
    ▲   不符合時:執行bbb ccc、不執行aaa
    ▲ 不受if判斷:ddd

    if ... then begin aaa; bbb; end; ccc; ddd;  
    ▲     符合時:  執行aaa bbb
    ▲   不符合時:不執行aaa bbb
    ▲ 不受if判斷:ccc ddd


    if ... then begin aaa; bbb; end else begin ccc; ddd; end;eee;   
    ▲     符合時:執行aaa bbb、不執行ccc ddd
    ▲   不符合時:執行ccc ddd、不執行aaa bbb
    ▲ 不受if判斷:eee

  • 按讚來自於
  • johnlintw
XQ小幫手 發文於   2022/08/11

Hello 東.,

 

小幫手補充,如果腳本同一次執行中有複數個 SetPosition 成立的話,只會執行第一個。

 

另外關於您的腳本:

if Close <= FilledAvgPrice - profit_point then begin  //賺40點

    if Close <= FilledAvgPrice - profit_point80 then begin  //第一筆賺40點後又賺40點 

        if Close <= FilledAvgPrice - profit_point60 or Close <= FilledAvgPrice - profit_point120 then begin//再賺停利60、120。 

            SetPosition(0); print("",VALUE4);

            end;

        end 

    else begin 

        if Close >= FilledAvgPrice - loss_point then begin //第一筆賺40點後虧20點                     

            if Close <= FilledAvgPrice - profit_point60 or Close >= FilledAvgPrice then SetPosition(0); print("",VALUE6); //停利60停損0 

            end;

        end;

    end

else

 

小幫手認為您應該是覺得價格先達成條件 第一筆賺40點後又賺40點 後就會一直成立,但這是錯誤的。

您當下的 close 和 FilledAvgPrice 是固定的,所以當 FilledAvgPrice - profit_point80 條件成立時,Close <= FilledAvgPrice - profit_point60這個條件也一定會成立。

並不是先達成 第一筆賺40點後又賺40點 接著在回檔到賺40點的時候停利。

 

如果您是要有先後順序的,應該用變數來處理,舉例來說:

var: intrabarpersist _cond1(0), intrabarpersist _cond2(0)

condition1 = Close <= FilledAvgPrice - profit_point;  //賺40點

condition2 = Close <= FilledAvgPrice - profit_point80;  //第一筆賺40點後又賺40點

condition3 = Close <= FilledAvgPrice - profit_point60 or Close <= FilledAvgPrice - profit_point120; //再賺停利60、120

if condition1 then _cond1 = 1;

if _cond1 = 1 and condition2 then _cond2 = 1;

if _cond2 = 1 and condition3 then begin

    setposition(0, market);

    value1 = 0;

    value2 = 0;

    end;

這樣就會變成 condition1 達成過  ==> condition2 達成過  ==> condition3 的模式,而不是同時 condition1, condition2, condition3 都要符合。

 

感謝 musashi 的熱心回覆。

發表回覆
Close