最终版
[code]Input:N1(120),N2(3),N3(5),inicap(1000000),riskratio(0.2),atrlength(14);var:top(0),bot(0),mp(0),totalamt(0),lots(1),stoploss(0),stopearn(0),exetripoint(0),entrATR(0),trailatr(0),trail(False),closehl(0),initrail(0);
totalamt =inicap + (netprofit + openpositionprofit);
if AvgTrueRange(atrlength) <> 0 then lots = ((totalamt/100)*riskratio)/(AvgTrueRange(atrlength)*bigpointvalue);
mp = marketposition;
top = Highest(high,N1);
bot = Lowest(low,N1);
if mp = 0 then begin //entry model
if Close > top[1] then buy("buylong") lots shares next bar at market;
if Close < bot[1] then sellshort("sellshort") lots shares next bar at market;
end;
if marketposition <> 0 and barssinceentry = 0 then begin
entrATR = AvgTrueRange(atrlength);
exetripoint = Close;
trail = False;
if marketposition =1 then
value1 = text_new(date,time,low - 100,"L:"+numtostr(entryprice,0)+ "Atr:"+numtostr(entrATR,0) + "STP:" + NumToStr(entryprice - 3*entratr,0))
else if marketposition =-1 then
value1 = text_new(date,time,high + 100,"S:"+numtostr(entryprice,0)+ "Atr:"+numtostr(entrATR,0) + "STP:" + NumToStr(entryprice + 3*entratr,0));
end;
//long order
if marketposition = 1 and barssinceentry > 0 then begin
if Close > exetripoint then begin
exetripoint = Close;
trailatr = AvgTrueRange(atrlength);
end;
if exetripoint > entryprice + 2*entrATR then begin //tril start
value2 = text_new(date,time,high + 30,"T:"+numtostr(exetripoint,0)+"Atr:"+numtostr(trailatr,0)+"TRAIL:"+NumToStr(exetripoint - N3*trailatr,0));
value3 = tl_new(date[1],time[1],exetripoint[1],date,time,exetripoint);
sell("L-stopearn") all shares next bar at exetripoint - N3*trailatr stop;
end;
sell("L-stoploss") all shares next bar at entryprice - N2*entratr stop;
end;
//short order
if marketposition = -1 and barssinceentry> 0 then begin
if Close < exetripoint then begin
exetripoint = Close;
trailatr = AvgTrueRange(atrlength);
end;
if exetripoint < entryprice - 2*entrATR then begin
value2 = text_new(date,time,low - 30,"B:"+numtostr(exetripoint,0)+"Atr:"+numtostr(trailatr,0)+"TRAIL:"+NumToStr(exetripoint + N3*trailatr,0));
value3 = tl_new(date[1],time[1],exetripoint[1],date,time,exetripoint);
buytocover("S-stopearn") all shares next bar at exetripoint + N3*trailatr stop;
end;
buytocover("S-stoploss") all shares next bar at entryprice + N2*entratr stop;
end;
[/code]
页:
[1]