源代碼:
?
variable:bj=0; ???????????????????????????????????//全局變量
?
//做多部分
?
tr1 : max(max((high-low),abs(ref(close,1)-high)),abs(ref(close,1)-low));
atr : ma(tr1,20); ??????????????????????????????//atr公式
?
ma20:ma(close,20); ??????????????????????????????//20日均線
?
a1:=0.01*valuewhen(barpos=1,asset); ?????????????//初始資金的1%
?
a2:=floor(a1/(2*atr)); ????????????????????????//開倉手數
?
a3:=ref(hhv(high,20),1); ??????????????????????//20日高點
?
if holding=0 and close>a3 and close>ma20 and bj=0
then begin
buy(1,a2,marketr);
bj:=1;
end ??????????????????????????????????//第一次開倉
?
a4:=enterprice+2*atr;
if holding>0 and close>a4 and bj=1
then begin
buy(1,a2,marketr);
end ?????????????????????????????????//第n次開倉
?
a5:=enterprice-2*ref(atr,enterbars=1);
if holding=1 and low<=a5
then begin
sell(1,0,limitr,a4);
end ??????????????????????????????????//第一次開倉的離場 ?
?
?
e1:=ref(enterprice,enterbars);
e2:=ref(e1,enterbars);
e3:=(e1+e2)/2;
if holding>1 and close<e3
then begin
sell(1,0,marketr);
end ????????????????????????????????????//第n次開倉離場,即:收盤<("holding=n"與“holding=n-1”)/2的位置 ???????
?
?
?
//做空部分
?
a1:=0.01*valuewhen(barpos=1,asset); ?????????????//初始資金的1%
?
a2:=floor(a1/(2*atr)); ????????????????????????//開倉手數
?
a6:=ref(llv(low,20),1); ??????????????????????//20日低點
?
if holding=0 and close<a6 and close<ma20 and bj=0
then begin
buyshort(1,a2,marketr);
bj:=1;
end ??????????????????????????????????//第一次開倉
?
a7:=enterprice-2*atr;
if holding<0 and close<a7 and bj=1
then begin
buyshort(1,a2,marketr);
end ?????????????????????????????????//第n次開倉
?
a8:=enterprice+2*ref(atr,enterbars=1);
if holding=1 and high>=a8
then begin
sellshort(1,0,limitr,a8);
end ??????????????????????????????????//第一次開倉的離場 ?
?
?
e1:=ref(enterprice,enterbars);
e2:=ref(e1,enterbars);
e3:=(e1+e2)/2;
if holding<-1 and close>e3
then begin
sellshort(1,0,marketr);
end ????????????????????????????????????//第n次開倉離場,即:收盤 > ("holding=n"與“holding=n-1”)/2的位置
?
測試結果:
此主題相關圖片如下:圖片1.jpg
此主題相關圖片如下:圖片2.png
問題:
1.從2009年測試至今,為什么只交易了一次,其他條件都沒成立?
2.第二次開倉與第一次開倉點位相差根本不多,為什么開倉手數相差了一倍?
3.隨后的平倉都還沒有到達平倉條件怎么就被平掉了?
?
1、自己在圖上輸出下交易條件,然后進行下分析。很多問題不要一看到問題自己不做調試,圖標的調試非常容易的比如cond1:c>0;這種就是一個條件了
2、自己在圖上輸出下手數看是數字幾,然后分析下,為何兩次的數值差那么多