--
input:ma_length(12,5,60,1),tp_length(30,3,40,2);//均线参数
INPUT:RiskRatio(1,0,10,1);
INPUT:p(0.05,0.01,1,0.01);
INPUT:SDLen(100,80,120,1);
variable:long_ss=1,short_ss=1,dzt=0,kzt=0;
variable:平多bar=1,平空bar=1;
variable:dzyss=0,kzyss=0;
my_ma:ref(ma(c,ma_length),3);
my_ma_1:=ref(ma(c,ma_length),1);
zf:=(c-ref(o,5))/ref(o,5);
zf_1:=ref(zf,1);
n_high:=ref(hhv(h,tp_length),1);
n_low:=ref(llv(l,tp_length),1);
m :=floor(SDLen*p);
s1 :=large(zf,SDLen,m);
s2 :=small(zf,SDLen,m);
//进场
long_entry_inst:=ref(c,1)>ref(my_ma,1) and h>=n_high;
short_entry_inst:=ref(c,1)<ref(my_ma,1) and l<=n_low;
//出场
long_final_leave:=l<=my_ma;
short_final_leave:=h>=my_ma;
多状态:dzt,NODRAW;
空状态:kzt,NODRAW;
//////////////////////////////////////////////////////////////////
if long_final_leave and holding>0 then begin//多头
LONG_EXIT_PRICE1:=my_ma;
sell(1,holding,limitr,LONG_EXIT_PRICE1);
平多bar:=barpos;dzt:=0;
end
if zf_1>s1 and holding>0 and dzt=2 then begin
LONG_EXIT_PRICE2:=open;
dzyss:=floor(long_ss/2);
sell(1,dzyss,limitr,LONG_EXIT_PRICE2);
dzt:=1;
end
if short_final_leave and holding<0 then begin//空头
SHORT_EXIT_PRICE1:=open;
sellshort(1,abs(holding),limitr,SHORT_EXIT_PRICE1);
平空bar:=barpos;kzt:=0;
end
if zf_1<s2 and holding<0 and kzt=2 then begin
SHORT_EXIT_PRICE2:=open;
kzyss:=floor(abs(holding)/2);
sell(1,dzyss,limitr,SHORT_EXIT_PRICE2);
kzt:=1;
end
/////////////////////////////////////////////////////////////////
if long_entry_inst and holding=0 and 平多bar<>barpos and dzt=0 then begin
long_entry_price:=n_high;
long_ss:=floor((ASSET*RiskRatio)/(100*multiplier*(n_high-my_ma)));
buy(1,long_ss,limitr,long_entry_price);
dzt:=2;
end
if short_entry_inst and holding=0 and 平空bar<>barpos and kzt=0 then begin
short_entry_price:=n_low;
short_ss:=floor((ASSET*RiskRatio)/(100*multiplier*(my_ma-n_low)));
buyshort(1,short_ss,limitr,short_entry_price);
kzt:=2;
end