Code: Select all
Inputs:
ATRLength(4),
ATRMult(2.1),
TicksAway(30);
Vars:
vATR(0),
vAVG(0),
vUp(0),
vDn(0),
vSuperTrend(0),
vTrend(1),
vFlag(0),
vFlagh(0),
vStopLossPrice(0),
vEndTimeS(172000);
vATR=averagetruerange(ATRLength)*ATRMult;
vAVG=(high+low)/2;
vUp=vAVG+vATR;
vDn=vAVG-vATR;
if close>vUp[1] then vTrend=1
else if close<vDn[1] then vTrend=-1;
if vTrend<0 and vTrend[1]>0 then vFlag=1 else vFlag=0;
if vTrend>0 and vTrend[1]<0 then vFlagh=1 else vFlagh=0;
if vTrend>0 and vDn<vDn[1] then vDn=vDn[1];
if vTrend<0 and vUp>vUp[1] then vUp=vUp[1];
if vFlag=1 then vUp=vAVG+vATR;
if vFlagh=1 then vDn=vAVG-vATR;
if time_s<vEndTimeS then begin
if vTrend=1 and vTrend[1]>0 then begin
buy next bar Close+4 limit;
// setta stoploss iniziale
vStopLossPrice=(close+4)-TicksAway;
end;
if vTrend=-1 and vTrend[1]<0 then begin
sellshort next bar Close-4 limit;
// setta stoploss iniziale
vStopLossPrice=(close-4)+TicksAway;
end;
end;
// Manage open long position
if (MarketPosition(0) > 0) then begin
// Alla chiusura della barra aggiorna il trailingstop se long
if (BarStatus(1) = 2) then begin
// se togli // avrai un ricalcolo del trailing stop
// se lasci // avrai invece uno stoploss fisso
// vStopLossPrice = MaxList(vStopLossPrice , Close-TicksAway);
end;
Sell all contracts next bar at vStopLossPrice stop;
end;
// Manage open short position
if (MarketPosition(0) < 0) then begin
// Alla chiusura della barra aggiorna il trailing stop se short
if (BarStatus(1) = 2) then begin
// se togli // avrai un ricalcolo del trailing stop
// se lasci // avrai invece uno stoploss fisso
// vStopLossPrice = MinList(vStopLossPrice , Close+TicksAway);
end;
buytocover all contracts next bar at vStopLossPrice stop;
end;
if time_s>vEndTimeS then begin
Sell all contracts next bar at market;
buytocover all contracts next bar at market;
end;
someone could please see how it doesn t work?
grazie