
runmode:0;
variable:longtrade=0;
variable:shorttrade=0;
variable:stopline=0;
predayhigh:=callstock(stklabel,vthigh,6,-1);
predaylow:=callstock(stklabel,vtlow,6,-1);
predayrange:=(predayhigh-predaylow)*0.5;
predayrange:=round(predayrange/mindiff)*mindiff;
dayopen:=callstock(stklabel,vtopen,6,0);
trailingstop:=predayrange;
dist:=barslast(date>ref(date,1))+1;
myentrytime:=time<=145500;
myexittime:=time>=150000;
upperband:=ref(hhv(high,dist),1);
lowerband:=ref(llv(low,dist),1);
hh:=ref(high,1);
ll:=ref(low,1);
if holding=0 then begin
if myentrytime and dayopen
predaylow then begin
if high>=predayhigh and longtrade=0 then begin
buy(1,1,limitr,max(open,predayhigh));
longtrade:=1;
end
if low<=predaylow and shorttrade=0 then begin
buyshort(1,1,limitr,min(open,predaylow));
shorttrade:=1;
end
end
if myentrytime and dayopen>=predayhigh then begin
if dist>=4 and high>=upperband and longtrade=0 then begin
buy(1,1,limitr,max(open,upperband));
longtrade:=1;
end
end
if myentrytime and dayopen<=predaylow then begin
if dist>=4 and low<=lowerband and shorttrade=0 then begin
buyshort(1,1,limitr,min(open,lowerband));
shorttrade:=1;
end
end
end
if holding>0 and enterbars>=1 then begin
myexitprice:=0;
if stopline=0 then
stopline:=enterprice-trailingstop;
if hh-trailingstop>stopline then
stopline:=hh-trailingstop;
if low<=stopline then
myexitprice:=min(open,stopline);
if myexittime then
myexitprice:=close;
if myexitprice>0 then begin
sell(1,holding,limitr,myexitprice);
stopline:=0;
end
end
if holding<0 and enterbars>=1 then begin
myexitprice:=0;
if stopline=0 then
stopline:=enterprice+1000;
if ll+trailingstop stopline:=ll+trailingstop;
if high>=stopline then
myexitprice:=max(open,stopline);
if myexittime then
myexitprice:=close;
if myexitprice>0 then begin
sellshort(1,holding,limitr,myexitprice);
stopline:=0;
end
end
if myexittime then begin
longtrade:=0;
shorttrade:=0;
end
盈亏:asset-500000,noaxis,coloryellow,linethick2;
非常感谢楼主的提供,学到了很多东西,我对源代码的一些编绎不过去的也进行了修整。有问题的话请指正O(∩_∩)O~。
以下内容为程序代码:
1 runmode:0;
2
3 variable:longtrade=0;
4 variable:shorttrade=0;
5 variable:stopline=0;
6
7 predayhigh:=callstock(stklabel,vthigh,6,-1); //昨天最高价
8 predaylow:=callstock(stklabel,vtlow,6,-1); //昨天最低价
9 predayrange:=(predayhigh-predaylow)*0.5; //
10 predayrange:=round(predayrange/mindiff)*mindiff;
11
12 dayopen:=callstock(stklabel,vtopen,6,0); //当天开盘价
13
14 trailingstop:=predayrange; //止损是(昨天最高价-昨天最低价)/2
15
16 dist:=barslast(date>ref(date,1))+1;
17
18 myentrytime:=time<=145500;
19 myexittime:=time>=150000;
20
21 upperband:=ref(hhv(high,dist),1);
22 lowerband:=ref(llv(low,dist),1);
23
24 hh:=ref(high,1);
25 ll:=ref(low,1);
26
27 if holding=0 then //没有持仓时,进行条件判断,开单,交易时间段为开盘到14:55分
28 begin
29 if myentrytime and dayopen<=predaylow then
30 begin
31 if high>=predayhigh and longtrade=0 then //今日开盘<昨日最低,最高价突破昨日最高价,开多单
32 begin
33 buy(1,1,limitr,max(open,predayhigh));
34 longtrade:=1;
35 end
36
37 if low<=predaylow and shorttrade=0 then //今日开盘<昨日最低,最低价突破昨日最低价,开空单
38 begin
39 buyshort(1,1,limitr,min(open,predaylow));
40 shorttrade:=1;
41 end
42 end
43
44 if myentrytime and dayopen>=predayhigh then
45 begin
46 if dist>=4 and high>=upperband and longtrade=0 then //今日开盘>昨日最高,当日K线的第四个周期之后,价格创当日新高,开多单
47 begin
48 buy(1,1,limitr,max(open,upperband));
49 longtrade:=1;
50 end
51 end
52
53 if myentrytime and dayopen<=predaylow then //今日开盘<=昨日最低,当日K线的第四个周期之后,价格创当日新低,开空单
54 begin
55 if dist>=4 and low<=lowerband and shorttrade=0 then
56 begin
57 buyshort(1,1,limitr,min(open,lowerband));
58 shorttrade:=1;
59 end
60 end
61 end
62
63 if holding>0 and enterbars>=1 then //持多单进行平仓设置
64 begin
65 myexitprice:=0;
66
67 if stopline=0 then
68 stopline:=enterprice-trailingstop; //初始止损价格为: 开仓价-(昨日最高-昨日最低)/2
69
70 if hh-trailingstop>stopline then //当(前一根K线的最高价-trailingstop)>之前的stopline,则重新设置止损线
71 stopline:=hh-trailingstop;
72
73 if low<=stopline then
74 myexitprice:=min(open,stopline); //跌破止损线,记录平仓价格
75
76 if myexittime then
77 myexitprice:=close;
78
79 if myexitprice>0 then //平多单
80 begin
81 sell(1,holding,limitr,myexitprice);
82 stopline:=0; //设置止损线为0
83 end
84 end
85
86 if holding<0 and enterbars>=1 then //持空单,进行平仓设置
87 begin
88 myexitprice:=0;
89
90 if stopline=0 then
91 stopline:=enterprice+trailingstop; //初始止损价格为: 开仓价+(昨日最高-昨日最低)/2
92
93 if ll+trailingstop<stopline then stopline:=ll+trailingstop; //当(前一根K线的最低价+trailingstop)<之前的stopline,则重新设置止损线
94
95 if high>=stopline then
96 myexitprice:=max(open,stopline); //上穿止损线,记录平仓价格
97
98 if myexittime then
99 myexitprice:=close;
100
101 if myexitprice>0 then
102 begin
103 sellshort(1,holding,limitr,myexitprice);
104 stopline:=0;
105 end
106 end
107
108 if myexittime then
109 begin
110 longtrade:=0;
111 shorttrade:=0;
112 end
113
114