Inputs: InitialStop(21),Profit(89),BreakEven(21),MinDist(13)
,Trade(34),car(3),StopLine(34),StopCar(3),Band(89),ref(1)
,Line(144),Turn(21),Stpadjust(4),Cnt(1),Factor(1),DayTrade(0),output(0);
Vars:BuyLimit(0),SellLimit(0),isStop(0),isBand(0),TheEnd(0)
,isBuy(0),isSell(0),ProfitHit(0),BandHit(0)
,lastB(0),lastS(0),isLong(0),isShort(0),isTrace2(0)
,Bprice(0),Sprice(0),BandH(0),BandL(0),isTrace(0)
,Blost(0),Slost(0),PP(0),HitProfit(0),isTrade2(0)
,Bcost(0),Scost(0),isTurned(0),LineH2(0),LineL2(0),LineH1(0),LineL1(0)
,Turn1(0),Turn2(0),balance(0),Blost1(0),Slost1(0),Dist(0),Hit(0)
,nColor( green ), nCurrentContracts(0);
Dist=MinDist+Stpadjust*2; //Dist=6+4*2
if Band>0 then begin
Value3 = StdDev((High+Low)/2, Band)[1]; //Band = 89
BandH = AverageFC((High+Low)/2, Band)[1] + 2.5 * Value3;
BandL = AverageFC((High+Low)/2, Band)[1] - 2.5 * Value3;
end;
if Line>0 and Turn>0 then begin
Value1=AverageFC((High+Low)/2, Turn)[0];
Value2=AverageFC((High+Low)/2, Line)[0];
Value21=AverageFC((High+Low)/2, Turn)[1];
Value22=AverageFC((High+Low)/2, Line)[1];
LineH1=AverageFC(High, Line)[0];
LineH2=AverageFC(High, Line)[1];
LineL1=AverageFC(Low, Line)[0];
LineL2=AverageFC(Low, Line)[1];
Turn1=AverageFC((High+Low)/2, Turn)[0]; // Turn = 21
Turn2=AverageFC((High+Low)/2, Turn)[1];
end;
if Turn2>=LineL2 and Turn1<LineL1 and Line<>0 then begin
isTurned=2;
end;
if Turn2<=LineH2 and Turn1>LineH1 and Line<>0 then begin
isTurned=1;
end;
if StopLine>0 then begin
Value12=AverageFC(Low, StopLine)[1]; // StopLine = 34
Value11=AverageFC(High, StopLine)[1];
end;
HitProfit=Profit;
balance=absvalue( CurrentContracts );
if CurrentContracts = 0 then begin
isLong=0;
isTrace=1;
isShort=0;
Hit=0;
Slost=0;
Scost=0;
Blost=0;
Bcost=0;
end;
if time>=84500 then begin
isTrace2=0;
end;
if StopLine>0 and StopCar=1 then isStop=Stopped(StopLine,ref) else isStop=0;
if StopLine>0 and StopCar>1 then isStop=Stopped2(StopLine,StopCar,ref) else isStop=0;
if Trade>0 and car>0 then isTrade2=Crossed(Trade,car,ref) else isTrade2=0;
if isTrace=1 and isLong=0 and isShort=0 and (time<132500 or DayTrade=0) then begin
if isTrade2=1 and (isTurned=1 or Value2=0)
and (Close[0]>Value12 or Value12=0) then begin
buy("bb") Cnt contracts next bar at market;
isLong=1;
isShort=0;
ProfitHit=0;
BandHit=0;
Sprice=0;
Bcost=Close[0];
Bprice=Close[0]+IntPortion(HitProfit/1);
Blost=Close[0] - InitialStop;
PP=Close[0];
//Return;
end;
if isTrade2=2 and (isTurned=2 or Value2=0)
and (Close[0]<Value11 or Value11=0) then begin
SellShort("ss") Cnt contracts next bar at market;
isLong=0;
isShort=1;
ProfitHit=0;
BandHit=0;
Bprice=0;
Scost=Close[0];
Sprice=Close[0]-IntPortion(HitProfit/1);
Slost=Close[0] + InitialStop;
PP=Close[0];
//Return;
end;
end;
if isLong = 1 then begin
balance=absvalue( CurrentContracts );
if InitialStop > 0 and Blost=0 and Bcost=0 then begin
Blost=EntryPrice(0) - InitialStop ;
Bcost=EntryPrice(0);
end;
if Blost<Bcost and EntryTime(0)=time then begin
//exitlong("XL0") all contracts this bar at Blost-Stpadjust*2 stop;
sell("XL0") all contracts next bar at Blost-Stpadjust*2 stop;
end;
if Blost<Bcost and EntryTime(0)<time then begin
//exitlong("XL1") all contracts this bar at Blost stop;
sell("XL1") all contracts next bar at Blost stop;
end;
if Blost<Bcost and EntryTime(0)<time and Close[0]<Blost then begin
//exitlong("XL") all contracts next bar at market;
sell("XL") all contracts next bar at market;
end;
if BreakEven > 0 and High[0]-Bcost>BreakEven and Blost<Bcost and EntryTime(0)<time then begin
Blost=Bcost+1;
Blost1=Bcost+1;
end;
if MinDist > 0 and High[0]-Blost>Dist and Blost>Bcost and EntryTime(0)<time
and (balance=Cnt or ProfitHit=1 or Hit=1) then begin
Blost=High[0]-Dist;
Blost1=High[0]-MinDist;
//exitlong("TL0") Round(Cnt/2,0) contracts this bar at Blost stop;
sell("TL0") Round(Cnt/2,0) contracts next bar at Blost stop;
end;
if MinDist > 0 and High[0]-Blost<Dist and High[0]<=Blost1 and Blost>Bcost
and EntryTime(0)<time then begin
//exitlong("TL1") Round(Cnt/2,0) contracts this bar at Blost stop;
sell("TL1") Round(Cnt/2,0) contracts next bar at Blost stop;
end;
if MinDist > 0 and High[0]-Blost<Dist and High[0]>Blost1 and Blost>Bcost
and EntryTime(0)<time then begin
//exitlong("TL2") Round(Cnt/2,0) contracts this bar at Blost1-Stpadjust stop;
sell("TL2") Round(Cnt/2,0) contracts next bar at Blost1-Stpadjust stop;
end;
if MinDist > 0 and High[0]-Blost<Dist and High[0]>Blost1 and Blost>Bcost
and EntryTime(0)<time and Close[0]<Blost1 then begin
//exitlong("TL") Round(Cnt/2,0) contracts next bar at market;
sell("TL") Round(Cnt/2,0) contracts next bar at market;
end;
if High > Bprice and HitProfit > 0 and ProfitHit = 0 then begin
//exitlong("EL") Round(Cnt/2,0) contracts total next bar at Market;
sell("EL") Round(Cnt/2,0) contracts total next bar at Market;
ProfitHit = 1;
isTrace2=1;
Bprice=Bprice+IntPortion(HitProfit*Factor);
PP=Close[0];
end;
if High > Bprice and HitProfit > 0 and ProfitHit = 1 then begin
//exitlong("EL1") Round(Cnt/2,0) contracts total next bar at Market;
sell("EL1") Round(Cnt/2,0) contracts total next bar at Market;
ProfitHit = 2;
PP=Close[0];
end;
if isStop = 2 then begin//and Close > Bcost then
//exitlong("XL2") all contracts next bar at market;
sell("XL2") all contracts next bar at market;
isLong=0; isTrace=1; isShort=0;
PP=Close[0];
end;
if High > BandH and Band > 0 and BandHit = 0 and ProfitHit = 1 then begin
//exitlong("top") Round(balance/2,0) contract total next bar at market;
sell("top") Round(balance/2,0) contract total next bar at market;
BandHit = 1;
PP=Close[0];
end;
if Turn2>=LineL2 and Turn1<LineL1 and Line<>0 then begin
//exitlong("EL3") Round(balance/2,0) contracts next bar at market;
sell("EL3") Round(balance/2,0) contracts next bar at market;
PP=Close[0];
Hit=1;
end;
end;
if isShort = 1 then begin
balance=absvalue( CurrentContracts );
if InitialStop > 0 and Slost=0 and Scost=0 then begin
Slost=EntryPrice(0) + InitialStop ;
Scost=EntryPrice(0);
end;
if Slost>Scost and EntryTime(0)=time then begin
//exitshort("XS0") all contracts this bar at Slost+Stpadjust*2 stop;
BuytoCover("XS0") all contracts next bar at Slost+Stpadjust*2 stop;
end;
if Slost>Scost and EntryTime(0)<time then begin
//exitshort("XS1") all contracts this bar at Slost stop;
BuytoCover("XS1") all contracts next bar at Slost stop;
end;
if Slost>Scost and EntryTime(0)<time and Close[0]>Slost then begin
//exitshort("XS") all contracts next bar at market;
BuytoCover("XS") all contracts next bar at market;
end;
if BreakEven > 0 and Scost-Low[0]>BreakEven and Slost>Scost and EntryTime(0)<time then begin
Slost=Scost-1 ;
Slost1=Scost-1 ;
end;
if MinDist > 0 and Slost-Low[0]>Dist and Slost<Scost and EntryTime(0)<time
and (balance=Cnt or ProfitHit=1 or Hit=1) then begin
Slost=Low[0]+Dist ;
Slost1=Low[0]+MinDist;
//exitshort("TS0") Round(Cnt/2,0) contracts this bar at Slost stop;
BuytoCover("TS0") Round(Cnt/2,0) contracts next bar at Slost stop;
end;
if MinDist > 0 and Slost-Low[0]<Dist and Low[0]>=Slost1 and Slost<Scost
and EntryTime(0)<time then begin
//exitshort("TS1") Round(Cnt/2,0) contracts this bar at Slost stop;
BuytoCover("TS1") Round(Cnt/2,0) contracts next bar at Slost stop;
end;
if MinDist > 0 and Slost-Low[0]<Dist and Low[0]<Slost1 and Slost<Scost
and EntryTime(0)<time then begin
//exitshort("TS2") Round(Cnt/2,0) contracts this bar at Slost1+Stpadjust stop;
BuytoCover("TS2") Round(Cnt/2,0) contracts next bar at Slost1+Stpadjust stop;
end;
if MinDist > 0 and Slost-Low[0]<Dist and Low[0]<Slost1 and Slost<Scost
and EntryTime(0)<time and Close[0]>Slost1 then begin
//exitshort("TS") Round(Cnt/2,0) contracts next bar at market;
BuytoCover("TS") Round(Cnt/2,0) contracts next bar at market;
end;
if Low < Sprice and HitProfit > 0 and ProfitHit = 0 then begin
//exitshort("ES") Round(Cnt/2,0) contracts total next bar at Market;
BuytoCover("ES") Round(Cnt/2,0) contracts total next bar at Market;
ProfitHit = 1;
isTrace2=1;
Sprice=Sprice-IntPortion(HitProfit*Factor);
PP=Close[0];
end;
if Low < Sprice and HitProfit > 0 and ProfitHit = 1 then begin
//exitshort("ES1") Round(Cnt/2,0) contracts total next bar at Market;
BuytoCover("ES1") Round(Cnt/2,0) contracts total next bar at Market;
ProfitHit = 2;
PP=Close[0];
end;
if isStop = 1 then begin//and Close < Scost then
//exitshort("XS2") all contracts next bar at market;
BuytoCover("XS2") all contracts next bar at market;
isShort=0; isTrace=1; isLong=0;
PP=Close[0];
end;
if Low < BandL and Band > 0 and BandHit = 0 and ProfitHit = 1 then begin
//exitshort("bot") Round(balance/2,0) contract total next bar at market;
BuytoCover("bot") Round(balance/2,0) contract total next bar at market;
BandHit = 1;
PP=Close[0];
end;
if Turn2<=LineH2 and Turn1>LineH1 and Line<>0 then begin
//exitshort("ES3") Round(balance/2,0) contracts next bar at market;
BuytoCover("ES3") Round(balance/2,0) contracts next bar at market;
PP=Close[0];
Hit=1;
end;
end;
//if Date=1090819 or Date=1090916 or Date=1091021 or Date=1091118 or Date=1091216 then //Last Trading Day
// TheEnd=132500 else TheEnd=134000
//end if
if time>=TheEnd and DayTrade=1 then begin
if CurrentContracts > 0 then begin
//exitlong("end1") all contracts next bar at market;
sell("end1") all contracts next bar at market;
PP=Close[0];
end;
if CurrentContracts < 0 then begin
//exitshort("end2") all contracts next bar at market;
buytocover("end2") all contracts next bar at market;
PP=Close[0];
end;
end;
if date=lastcalcdate and time=lastcalctime and output>0 then begin
FileDelete( "R:\CurrentH"+numtostr(output,0)+".txt" );
// if currentcontracts=0 then begin
// fileappend("R:\CurrentH"+numtostr(output,0)+".txt",cdate(date)+" "+ctime(time)+","+
// numtostr(currentcontracts,0)+","+numtostr(ExitPrice(0),0));
// end;
// if currentcontracts!=0 then begin
// fileappend("R:\CurrentH"+numtostr(output,0)+".txt",cdate(date)+" "+ctime(time)+","+
// numtostr(currentcontracts,0)+","+numtostr(PP,0));
// end;
end; |