自動交易print出來條件都對 但是沒有執行賣出動作

  •   208 
  • 最後發表   bigjohnsonlai  2024 五月 26
bigjohnsonlai 發文於   2024/05/09

小幫手好

以下程式碼為自動交易腳本

if barcount=1 and _condition1=1

then begin

setposition(-2);

end;

我設定條件看print出來的數值都對 可是回測沒有執行setposition=-2,想請教問題出在哪

看起來 raw data都是對的

回測條件:60k 商品6189 日期2024/02/23 附件為print的raw data

 

 

 

 

if GetSymbolInfo("買賣現沖")=false or GetSymbolInfo("處置股")=true then raiseruntimeerror("不可當沖");
input:
  Length(20,"MA的天數"),
  UpperBand(2,"上通道標準差倍數"),
  LowerBand(2,"下通道標準差倍數"),
  day(5,"前幾根k棒平均量");

variable: 
  mid(0),up(0),down(0),C5(0);  

up=bollingerband(close,Length,UpperBand);
mid=average(close,Length);
down=bollingerband(close,Length,-1*LowerBand);
C5=average(volume,day);



///變數定義
variable:barcount(0);

///barcount 定義///

if date <> date[1] then ///參數定義
begin
    barcount=1;

end
else
    barcount+=1;


input: openhigh(1.02, "開盤衝高(%)");
input: kill(5, "下殺(%)");
input: highpercent(1.3, "一段時間內衝高幾(%)");
input: period(40, "多長時間內衝高幾趴");
variable: 
  _condition1(0);  

if barcount=1 and c>fastLowest(c,period)*highpercent and o>c[1]*openhigh and (h-c)/h*100>kill
then begin
  _condition1=1;
end;

if barcount=1 and _condition1=1
then begin
setposition(-2);
end;



print(file("C:\print\"),position,barcount,o,c[1]*openhigh,(h-c)/c*100,c,fastLowest(c,period)*highpercent,_condition1);

 

附加文件

排序方式: 標準 | 最新
XS小編 發文於   2024/05/14

Hello bigjohnsonlai,

 

您的問題出在於資料引用筆數不夠,導致壓縮到回測區間,造成沒有交易的情況。

可以加上 setbackbar(40); 就可以正常回測。

 

另外腳本中有使用到 FastLowest 函數,此為節省效能的函數,但有些限制。

if barcount=1 and c>fastLowest(c,period)*highpercent ...

這種寫法會讓當 barcount 不是1的時候就不繼續運算接下來的條件,而 FastLowest 需要每根Bar都運算,故這種寫法計算出的數值可能會出錯。

建議您可以改為:

value1 = fastLowest(c,period)*highpercent;

if barcount=1 and c>value1 ...

 

bigjohnsonlai 發文於   2024/05/19

 hi 小幫手

感謝幫忙!

我回測當天沒問題了

可是我回測區間跑6189這支從2024/1/1~2024/5/19反而不行

 程式碼也改過了如下

if GetSymbolInfo("買賣現沖")=false or GetSymbolInfo("處置股")=true then raiseruntimeerror("不可當沖");
input:
  Length(20,"MA的天數"),
  UpperBand(2,"上通道標準差倍數"),
  LowerBand(2,"下通道標準差倍數"),
  day(5,"前幾根k棒平均量");

variable: 
  mid(0),up(0),down(0),C5(0);  

up=bollingerband(close,Length,UpperBand);
mid=average(close,Length);
down=bollingerband(close,Length,-1*LowerBand);
C5=average(volume,day);

setbackbar(400);

///變數定義
variable:barcount(0);

///barcount 定義///

if date <> date[1] then ///參數定義
begin
    barcount=1;

end
else
    barcount+=1;


input: openhigh(1.02, "開盤衝高(%)");
input: kill(5, "下殺(%)");
input: highpercent(1.3, "一段時間內衝高幾(%)");
input: period(40, "多長時間內衝高幾趴");
variable: 
  _condition1(0);  

value1 = fastLowest(c,period)*highpercent;

if barcount=1 and c>value1 and o>c[1]*openhigh and (h-c)/h*100>kill
then begin
  _condition1=1;
end;

if barcount=1 and _condition1=1
then begin
setposition(-2);
end;

{
input: profit_percent(30, "停利(%)");
input: loss_percent(4, "停損(%)");

if Position = -1 and Filled = -1 then begin
    { 依照成本價格設定停損/停利 }

    if profit_percent > 0 and Close <= FilledAvgPrice*(1-0.01*profit_percent)   
                                                                              then begin        { 停利 }
        SetPosition(0);
    end 
        else if loss_percent > 0 and Close >= FilledAvgPrice*(1+0.01*loss_percent)      //////停損幾趴

                                                                                          then begin    
        { 停損 }
        SetPosition(0);
    end;
end;}

print(file("C:\print\"),position,barcount,o,c[1]*openhigh,(h-c)/c*100,c,fastLowest(c,period)*highpercent,_condition1);

 

回測單日可以 回測區間不行 請問是什麼導致的

 

bigjohnsonlai 發文於   2024/05/19

回測設定如下

 

XS小編 發文於   2024/05/23

Hello bigjohnsonlai,

 

因為您在腳本中撰寫

if GetSymbolInfo("買賣現沖")=false or GetSymbolInfo("處置股")=true then raiseruntimeerror("不可當沖");

而回測期間有發生對應的情況導致回測失敗 (參考附圖)。

 

建議可以將此判斷放入進場條件中會比較適合,或是改為 return

附加文件

bigjohnsonlai 發文於   2024/05/26

原來是這樣!感謝小幫手!

發表回覆
Close