input:n(9,1,100,10),p1(3,2,40,4),p2(3,2,40,4);//参数设置
RSV:=(CLOSE-LLV(LOW,N))/(HHV(HIGH,N)-LLV(LOW,N))*100;
K:SMA(RSV,P1,1);
D:SMA(K,P2,1);
J:3*K-2*D;
kdjjc:cross(k,d);//kdj金叉
kdjsc:cross(d,k);
LastjcPrice:ref(c,BARSLAST(kdjjc=1));
LastscPrice:ref(c,BARSLAST(kdjsc=1));
if c> LastjcPrice+20*MINDIFF then
begin
sellshort(holding<0,holding,MARKET);
buy(holding=0,1,MARKET);
end
if c< LastscPrice-20*MINDIFF then
begin
sell(holding>0,holding,MARKET);
buyshort(holding<0,1,MARKET);
end
这里我取得是最近一次金叉或者死叉的价格。
上面代码后面加上这部分:
if c-ENTERPRICE>50*MINDIFF and holding<0 then sellshort(holding<0,holding,market);//空方向时止损
if ENTERPRICE-c >100*MINDIFF and holding<0 then sellshort(holding<0,holding,market);//空方向时止盈
if c-ENTERPRICE>100*MINDIFF and holding>0 then sell(holding>0,holding,market);//多方向时止盈
if ENTERPRICE-c >50*MINDIFF and holding>0 then sell(holding>0,holding,market);//多方向时止损
不等到K线走完 在交易的时候选择固定轮询模式就行了。