親愛的小編或高手們,
我寫了一交易腳本如下,當中利用了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;

4 評論