Hi, Your code is incomplete. Where have you initialized Buy, Sell, Short, Cover, longstop, shortstop?
Mike --- In [email protected], "bertbulaba" <bertbul...@...> wrote: > > Hello everyone, > > I hope that there is a few kind individuals out there that could have a > look at this code I am using as an replacement for the ApplyStop() > function, which I cannot use in my real time strategy. It is a Trailing > Stop and a Stop-Loss built into one loop. The source of the main code is > from the AmiBroker site, which I have then modified to suit my needs > (added ATR() etc). There is something wrong with it since the results > compared to ApplyStop() is very different (much worse that is). Also the > Sell signal doesn't plot for some reason. I have been staring myself > blind on this one, so any help is appreciated. > > Cheers! > > /************ Code starts here ***************/ > trailstop = 0; > priceatbuy = 0; > priceatshort = 0; > Sum1 = Sum2 = 0; > for( i = 1; i < BarCount; i++ ) > { > // Calculate ATR inline using EMA (instead of Wilder's MA) > tr[i] = Max(H[i] - L[i], Max(H[i] - C[i-1], C[i-1] - L[i])); > > // 34 bar EMA of True Range > fac = 2.0 / (1.0 + 34); > sum1 += (fac * (tr[i] - sum1)); > sum2 += (fac * (1.0 - sum2)); > etr[i] = Nz(sum1 / sum2); > > // Trailing Stop > if( trailstop == 0 AND Buy[ i ] ) > { > trailstop = High[ i ] - longstop * etr[i]; > } > else Buy[ i ] = 0; // remove excess buy signals > > if( trailstop == 0 AND Short[ i ] ) > { > trailstop = Low[ i ] + shortstop * etr[i]; > } > else Short[ i ] = 0; // remove excess short signals > > if( trailstop > 0 AND Low[ i ] < trailstop ) > { > Sell[ i ] = 1; > SellPrice[ i ] = trailstop; > trailstop = 0; > } > > if( trailstop > 0 AND High[ i ] > trailstop ) > { > Cover[ i ] = 1; > CoverPrice[ i ] = trailstop; > trailstop = 0; > } > > // Stoploss > if( priceatbuy == 0 AND Buy[ i ] ) priceatbuy = BuyPrice[ i ]; > if( priceatshort == 0 AND Short[ i ] ) priceatshort = ShortPrice[ i > ]; > > if( priceatbuy > 0 AND SellPrice[ i ] < (priceatbuy - stopLevel * > etr[i])) > { > Sell[ i ] = 1; > SellPrice[ i ] = priceatbuy - stopLevel * etr[i]; > priceatbuy = 0; > } > else Sell[ i ] = 0; > > if( priceatshort > 0 AND CoverPrice[ i ] > (priceatbuy + stopLevel * > etr[i])) > { > Cover[ i ] = 1; > CoverPrice[ i ] = priceatshort + stopLevel * etr[i]; > priceatshort = 0; > } > else Cover[ i ] = 0; > > } > > // Plot Entries and Exits > Plot(C, "Price", 1, styleCandle); > PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low); > PlotShapes(Sell*shapeHollowDownArrow,colorRed,0,High); > PlotShapes(Cover*shapeHollowUpArrow,colorGreen,0,Low); > PlotShapes(Short*shapeDownArrow,colorRed,0,High); >
