交易腳本中print不出結果

  •   315 
  • 最後發表   小富  2024 八月 30
小富 發文於   2024/08/27

親愛的小編或高手們,

我寫了一交易腳本如下,當中利用了print的功能,但在lowestprice 與stopprice方面,卻在過程中都無法跑出結果(請見附圖),事實上,我另外對照寫了做多的腳一腳本,此一腳本卻可列釋出highestprice,不知道哪裡出錯,還請幫忙解決,謝謝!!

 小陳

 

//期指MACD做空當沖交易

 

// 宣告參數

input: FastLength(12,"DIF短天數"), SlowLength(26,"DIF長天數"), MACDLength(9,"MACD天數");

input: riskpoint(35,"停利停損點數");

var: intrabarpersist lowestprice(0);

var: intrabarpersist limit_control(0);

 

// if date<>date[1] then lowestprice =0; //自訂義變數歸零

if barfreq<>"Min"or barinterval<>5  then return;

 var: price(0),dif_s(0),macd_s(0),osc_s(0);

settotalbar((maxlist(FastLength,SlowLength,MACDLength)+1)*3.5);

 price = WeightedClose();

 

//macd25計算式

dif_s = XAverage(price, FastLength) - XAverage(price, SlowLength);

macd_s = XAverage(dif_s, MACDLength) ;

osc_s = dif_s - macd_s ;

value1 = ema(close,13); // ma13以下才能空

 

condition1 = close< value1 and osc_s cross below 0;

condition2 = limit_control <> currentbar;

 

// 空方進場策略:MACD由正轉負。

if position = 0 and condition1 and condition2 then begin

   setposition(-1);

   limit_control = currentbar;

   lowestprice = FilledAvgPrice;

end;

 

if close < lowestprice then lowestprice = close; //採最低價位做為停利(損)起算點

stopprice = lowestprice + riskpoint;

 

//監控

print("lowestprice=",numtostr(lowestprice,0),"riskpoint=",numtostr(riskpoint,0),"FilledAvgPrice=",numtostr(FilledAvgPrice,0),"stopprice=",numtostr(stopprice,0));

  

//出場策略:同一K線時,以點數控制。不同K線時,三條件擇一出場

condition3 = osc_s cross above 0 or close > stopprice; 

 if limit_control<>currentbar then if

  condition3 then setposition(0)    //不同根bar時的停損模式

else if

  close>stopprice then setposition(0) ; //同一根bar的停損模式

 

//尾盤當沖平倉策略

if Position <> 0 AND  currenttime >= 134300 then begin

SetPosition(0, market);

if position =0 then RaiseRunTimeError("尾盤沖銷,程式結束");

end;

 

附加文件

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

Hello 小富,

 

if position = 0 and condition1 and condition2 then begin

   setposition(-1);

   limit_control = currentbar;

   lowestprice = FilledAvgPrice;  //這時候的FilledAvgPrice是0

   end;

 

請注意交易指令在該次洗價計算完畢後才會執行,所以此時的 FilledAvgPrice 還是為0。

而 lowestprice 為0 的話 if close < lowestprice then lowestprice = close; 這也不可能成立。

小富 發文於   2024/08/29

親愛的管理員您好,

       我後來聽從許教授的提醒,將程式改寫如下,但似乎還是一樣無法得到執行。或者請教我該如何修正程式? 謝謝! 

另外,今天早上這樣修改後,發現連limit_control都似乎沒執行?(或是我用法用錯),我是想寫成一根K棒中最多只執行一次交易。結果卻跑出一秒之內連續進出五次。

 

if Position = 0 and condition1 and condition2 then begin
   var: intrabarpersist lowestprice(0);
   SetPosition(-1);
   if position =-1 and filled = -1 then begin
      limit_control = currentbar;
      lowestprice = FilledAvgPrice;
   end;
end;

虎科大許教授 發文於   2024/08/30

修改的程式在你另外一則發文。

小富 發文於   2024/08/30

已收到,謝謝!!

發表回覆
Close