That is missing a lot of variable definitions, and other issues. Post the complete code if you have it. Otherwise, describe what you're trying to do in plain English. It would probably just be easier for me to write it from scratch.
input length = 10;
input price = hl2;
input firstTrade = {default long, short};
def maHigh = highest(high, length);
def maLow = lowest(low, length);
#def bp = if maHigh < maHigh[1] then maHigh else bp[1];
#def sp = if maLow > maLow[1] then maLow else sp[1];
def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
if (!IsNaN(price)) {
switch (firstTrade) {
case long:
state = state.long;
trail = malow ;
case short:
state = state.short;
trail = mahigh;
}
} else {
state = state.init;
trail = Double.NaN;
}
case long:
if (price > trail[1]) {
state = state.long;
trail = Max(trail[1], malow);
} else {
state = state.short;
trail = mahigh;
}
case short:
if (price < trail[1]) {
state = state.short;
trail = Min(trail[1], mahigh);
} else {
state = state.long;
trail = malow;
}
}
plot TrailingStop = trail;
#plot BuyStop = if state == state.short or state != state[1] then trail else Double.NaN;
#plot SellStop = if state == state.long or state != state[1] then trail else Double.NaN;
TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Buy", GetColor(0));
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")
else TrailingStop.Color("Buy"));