I'd like to reuse trailing stop (TS) mechanism in my code as demonstrated in the following pseudo-code. The code shows only the short part. TS.S refers to Trailing Stop (price) for Short. However, the trailing stop works just fine only for the first time it gets hit from below. As market keeps falling, new trailing stop price is calculated, however, even after this newly calculated trailing stop gets hit again and again from below, it never triggers the "buytocover" line. However, "KKKK" does get printed each time when price crosses above the trailing stop price. it's just that "buytocover" line doesn't work.
Just to test the code, I replace the "buytocover" line with "sellshort 1 contract next bar at market;", nothing happens even when the TS line gets hit for the first time, however, I do get "KKKK" printed after this line.
Any inputs? Thanks!
Code: Select all
[IntrabarOrderGeneration = TRUE]
if marketposition=-1 then begin
//calculate Trailing Stop price line
..............
TS.S= XXXXX;
..............
if C>TS.S and TS.S.triggered=false then begin
buytocover 1 contracts next bar at market;
TS.S.triggered=true;
print("KKKK");
end;
//after TS gets hit
if TS.S.triggered then begin
//do some processing right after TS gets hit
TS.S.triggered=false;
end;
end;
//when flat, clear all variables
if marketposition=0 then begin
TS.S=999999;
TS.S.triggered=false;
.......
end;
-kernel