epete
Member
I'm trying to save the bid price after entering a trade (on a 1 minute chart) and try to save the maximum of this value to establish a trailing stop value. I'm using the following code so far. It calculates a TS value, but that value goes up and DOWN in the current bar (I assume the script gets evaluated for every tick in the 1 minute bar). I'm puzzled why it isn't retaining a MAXimum value, but instead rises and lowers during the bar.
Code:
input TrailStop = 2.5;#hint TrailStop: Trailing stop
input offsetType = {default value, percent, tick};
def qty = GetQuantity();
def entry = GetAveragePrice();
def bid = if qty==0 then 0 else close(GetSymbol(), AggregationPeriod.MIN, PriceType.BID);
#---------------------------------
def MaxBid = if qty<>0 then
if qty>0 then
if IsNan(entry[1]) then entry else Max(bid, MaxBid[1])
else
if IsNan(entry[1]) then entry else Min(bid, MaxBid[1])
else 0;
def mult;
switch (offsetType) {
case percent:
mult = MaxBid / 100;
case value:
mult = 1;
case tick:
mult = TickSize();
}
def TSP = if qty>0 then MaxBid - TrailStop * mult else if qty<0 then MaxBid + TrailStop * mult else 0;