[PEL] 复制代码 variable:firsthd:=0;//初始底仓
variable:iszy:=0;//标记是否止盈了
variable:fx:=0;//记录最近一次操作的方向 .1 是加仓,-1是减仓
if todaybar=1 and holding<>0 then firsthd:=holding;//始终在每天开始时候更新 初始底仓
if todaybar=1 then //始终在每天开始时候更新 全局变量记录
begin
iszy:=0;
fx:=0;
end
ma5:=ma(c,5);
ma120:=ma(c,120);
//底仓逻辑
if holding=0 and cross(ma5,ma120) and iszy=0 then
begin
buy(1,1000,market);
firsthd:=holding;//首次底仓的赋值
end
s1:=0.5*firsthd;//提前计算好按照初始仓位百分比的加减仓手数
s2:=0.1*firsthd;
o1:=callstock('',vtopen,6,0);//当日开盘价
ozd:100*(c-o1)/o1;//基于开盘价的涨跌幅
v1:=ozd-fracpart(ozd);
if v1>=3 and fx=0 then fx:=-1;
if v1<=-3 and fx=0 then fx:=1;
tcd:=time<145600;//时间限制
//减仓
if fx=-1 and iszy=0 and tcd then
begin
hdx:=firsthd-s1-(v1-3)*s2;//根据当前涨跌幅计算当前应该有的仓位情况
if holding>hdx and hdx>=0 then 减仓:sell(1,holding-hdx,market);
end
//加仓,加仓上限默认不超过总仓位,否则收盘无法平到初始仓位的程度
if fx=1 and iszy=0 and tcd then
begin
hdx:=firsthd+s1-(v1+3)*s2;
if holding<hdx and hdx<=2*firsthd then 加仓:buy(1,hdx-holding,market);
end
老仓:holding-dayholding,nodraw;
浮动盈亏:if(avgenterprice<>0,c/avgenterprice,0);
if c/avgenterprice>=1.05 and 老仓>0 and iszy=0 and tcd then
begin
止盈:sell(1,老仓,market);
iszy:=1;
end
if not(tcd) and holding<>firsthd then
begin
if holding-firsthd>0 and holding-dayholding>0 then 收盘减仓:sell(1,holding-firsthd,market);
if firsthd-holding>0 then 收盘补仓:buy(1,firsthd-holding,market);
end
持仓:holding,nodraw;
供参考 |