Dual Crossover with Exponential ATR Trailing Stop

#1
I was wondering if someone can help me with this. This is an Amibroker Formula Language Indicator. This formula plots two exponential moving averages along with an auto-switching exponential ATR trailing stop. I want the indicator to show a buy signal when the two exponential moving averages cross and a sell signal when the two exponentials cross back over or if the stop is hit. However, the stop does not reset on a buy order. I need that corrected. And I want this to run with short and cover positions as well. Please Help!


per = Param("per",30, 5, 150, 1);
mult = Param("mult",7, 1, 4, 0.05);
tr = Ref(EMA(ATR(1),per),0)*mult;
mov = Param("Fast EMA",5,1,200,1);
mov2 = Param("Slow EMA",205,200,400,1);

trailArray[ 0 ] = C[ 0 ]; // initialize
for( i = 1; i < BarCount; i++ )
{
prev = trailArray[ i - 1 ];

if (C[ i ] > prev AND C[ i - 1 ] > prev)
{
trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
}
else if (C[ i ] < prev AND C[ i - 1 ] < prev)
{
trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
}
else if (C[ i ] > prev)
{
trailArray[ i ] = C[ i ] - tr[ i ];
}
else
{
trailArray[ i ] = C[ i ] + tr[ i ];
}
}
trailArray = Ref(trailArray,-1);

GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(EMA(Close,mov),"\nFastEMA",colorBlue,styleLine);
Plot(EMA(Close,mov2),"\nSlowEMA",colorGreen,styleLine);
Plot(trailArray,"\ntrailArray",styleStaircase);
Plot( C, "\nCandle",colorRed, styleCandle );