我測試以下的交易腳本,是OK的,你試試看。
//15Min MACD做多當沖交易
//微型台指近月(一般)
{執行時間控制}
if barfreq <> "Min"or barinterval<>15 then raiseRunTimeError("限用15分鐘"); {交易時間頻率設定}
// 宣告參數
input: time1(084500,"程式開始時間");
input: FastLength(5,"DIF短天數"), SlowLength(25,"DIF長天數"), MACDLength(2,"MACD天數");
input: FastLength2(13,"趨勢DIF短天數"), SlowLength2(89,"趨勢DIF長天數"), MACDLength2(34,"趨勢MACD天數");
input: FastLength15(5,"DIF短天數"), SlowLength15(25,"DIF長天數"), MACDLength15(2,"MACD天數");
input: riskpoint1(35,"停利停損點數"),riskpercent(0.2,"停利百分比");
settotalbar((maxlist(FastLength2,SlowLength2,MACDLength2)+1)*4);
var: price(0),dif_s(0),macd_s(0),osc_s(0);
var: stopprice(0),dif_s2(0),macd_s2(0),osc_s2(0);
var: riskpoint(0),riskpoint2(0),riskpoint3(0),profit(0);
var: intrabarpersist controlbar(0),intrabarpersist highestprice(0);
if currenttime >= time1 then
Begin
price = WeightedClose();
//macd25計算式
dif_s = XAverage(price, FastLength) - XAverage(price, SlowLength);
macd_s = XAverage(dif_s, MACDLength) ;
osc_s = dif_s - macd_s ;
//趨勢環境 macd89計算式
dif_s2 = XAverage(price, FastLength2) - XAverage(price, SlowLength2);
macd_s2 = XAverage(dif_s2, MACDLength2) ;
osc_s2 = dif_s2 - macd_s2 ;
//進場條件
condition1 = osc_s2 > 0 and dif_s2 >0 and osc_s >=osc_s[1] and osc_s[1]>=0;
condition2 = controlbar <> currentbar;
// 多方進場策略:MACD由負轉正。
if Position = 0 and condition1 and condition2 then
begin
SetPosition(1);
end;
//停利停損處理
if Position = 1 and Filled = 1 then
begin
if Close <= FilledAvgPrice -riskpoint1 then
begin
{ 停損 }
SetPosition(0);
highestprice = 0;
controlbar = currentbar;
end
else
begin
{ 判斷是否要啟動停利 }
if highestprice = 0 and Close >= FilledAvgPrice {and controlbar <>currentbar} then
begin { -riskpoint}
highestprice = Close;
{停利點數}
profit = highestprice - FilledAvgPrice; {用highestprice才能抓住最大利潤去計算停利點}
riskpoint2 = close *(riskpercent/100);
riskpoint3 = (highestprice - FilledAvgPrice)/3;
end;
if profit < riskpoint2 then riskpoint = riskpoint1
else if profit > riskpoint2 and profit <200 then riskpoint = riskpoint2
else if profit > 200 then riskpoint = riskpoint3;
if highestprice <> 0 then
begin
if Close <= highestprice - riskpoint or osc_S cross below 0 then
begin
SetPosition(0);
highestprice = 0;
controlbar = currentbar;
end
else
if Close > highestprice then
begin
highestprice = Close;
end;
end;
end;
end;
//監控
print("highestprice=",numtostr(highestprice,0),"riskpoint=",numtostr(riskpoint,0),"FilledAvgPrice=",numtostr(FilledAvgPrice,0),"stopprice=",numtostr(stopprice,0),"close=",numtostr(close,0));
print("osc_s2=",numtostr(osc_s2,2),"dif_s2=",numtostr(dif_s2,2),"osc_s=",numtostr(osc_s,2),"osc_s[1]=",numtostr(osc_s[1],2));
print("controlbar=",numtostr(controlbar,0),"currentbar=",numtostr(currentbar,0));
//尾盤當沖平倉策略
if Position <> 0 AND currenttime >= 134300 then
begin
SetPosition(0, market);
if position =0 then RaiseRunTimeError("尾盤沖銷,程式結束");
end;
end;
print(date,time,close);
15 評論