Logic Issue in ATR AFL.

#1
Hi friends,

I have developed a twin ATR system based on simple logic of discarding the trades that are anti trend and taking the ones which are in the direction of the trend.

I do that by deciding about the trend in bigger ATR and take trades basis smaller ATR.

This system has a small BUG (a logical reasoning issue in my code) and I am not a great programmer. In fact I m not even a naïve programmer (I just copy pasted, hit and trial my way so far).

It takes all the trades while the trend is up or down but at a time of reversal when both short and long term atr change, IT SKIPS THE FIRST TRADE.

because of this, I m not able to automate my system and unable to back test its credibility.

Please plot this indicator on any graph to see wat I am saying. I need some senior to help me rectify this flaw.

Your help will be really appreciated.

Thanks in advance

Manish Dhawan
Mystic Funds
 
#2
_SECTION_BEGIN("1ATR");
StopMode = ParamList("Stop Mode", "Chandelier|Modified ATR" );

//StopLevel = Param("Fixed perc %", 1, 0.1, 5, 0.1)/100;

StopATRFactor = Param("ATR multiple", 4, 0.5, 10, 0.1 );
StopATRPeriod = Param("ATR period", 14, 3, 50,1 );



StopATRFactor1 = Param("ATR multiple1", 5);
StopATRPeriod1 = Param("ATR period1", 5);

/* calculate support AND resistance levels
if( StopMode == "Fixed" ) // fixed percent trailing stop
{
sup = C * ( 1 - stoplevel );
res = C * ( 1 + stoplevel );
}
else // Chandelier ATR-based stop */


if( StopMode == "Chandelier" )
{
sup = C - StopATRFactor * ATR( StopATRPeriod );
res = C + StopATRFactor * ATR( StopATRPeriod );
}
else
{
HL = H - L;
MAHL = 1.5 * MA( HL, StopATRPeriod );
HiLo = IIf( HL < MAHL, HL, MAHL );
H1 = Ref( H, -1 );
L1 = Ref( L, -1 );
C1 = Ref( C, -1 );
Href = IIf( L <= H1, H - C1, ( H - C1 ) - ( L - H1 ) / 2 );
Lref = IIf( H >= L1, C1 - L, ( C1 - L ) - ( L1 - H ) / 2 );

diff1 = Max( HiLo, HRef );
diff2 = Max( diff1, LRef );

ATRmod = Wilders( diff2, StopATRPeriod );

sup = C - StopATRFactor * ATRmod ;
res = C + StopATRFactor * ATRmod ;
}

// calculate trailing stop line
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ )
{
//if( Started[ i ] == 0 ) continue;

if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
trailstop = Max( trailstop, sup[ i ] );
else
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
trailstop = Min( trailstop, res[ i ] );
else
trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );

trailARRAY[ i ] = trailstop;
}


if( StopMode == "Chandelier" )
{
sup = C - StopATRFactor1 * ATR( StopATRPeriod1 );
res = C + StopATRFactor1 * ATR( StopATRPeriod1 );
}
else
{
HL = H - L;
MAHL = 1.5 * MA( HL, StopATRPeriod1 );
HiLo = IIf( HL < MAHL, HL, MAHL );
H1 = Ref( H, -1 );
L1 = Ref( L, -1 );
C1 = Ref( C, -1 );
Href = IIf( L <= H1, H - C1, ( H - C1 ) - ( L - H1 ) / 2 );
Lref = IIf( H >= L1, C1 - L, ( C1 - L ) - ( L1 - H ) / 2 );

diff1 = Max( HiLo, HRef );
diff2 = Max( diff1, LRef );

ATRmod = Wilders( diff2, StopATRPeriod1 );

sup = C - StopATRFactor1 * ATRmod ;
res = C + StopATRFactor1 * ATRmod ;
}

// calculate trailing stop line
trailARRAY1 = Null;
trailstop1 = 0;
for( i = 1; i < BarCount; i++ )
{
//if( Started[ i ] == 0 ) continue;

if( C[ i ] > trailstop1 AND C[ i - 1 ] > trailstop1 )
trailstop1 = Max( trailstop1, sup[ i ] );
else
if( C[ i ] < trailstop1 AND C[ i - 1 ] < trailstop1 )
trailstop1 = Min( trailstop1, res[ i ] );
else
trailstop1 = IIf( C[ i ] > trailstop1, sup[ i ], res[ i ] );

trailARRAY1[ i ] = trailstop1;
}


// generate buy/sell signals based on crossover with trail stop line


Bull= C > trailArray1;
Bear = C < trailArray1;

Buy = Cross( C, trailArray) AND Bull ;
Short = Cross( trailArray, C )AND Bear;
Cover= Cross(C, trailArray) OR Bull;
Sell= Cross (trailarray,C) OR Bear;

Sell = ExRem(Sell, Buy);
Buy = ExRem(Buy, Sell);
Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);

barcomplete = BarIndex() < LastValue(BarIndex());


AlertIf( barcomplete AND Buy, "SOUND C:\\Windows\\Media\\harleyre.wav", "Audio alert", 2 );

AlertIf( barcomplete AND Short, "SOUND C:\\Windows\\Media\\crshbrn.wav", "Audio alert", 2 );

AlertIf( Sell, "SOUND C:\\Windows\\Media\\crshbrn.wav", "Audio alert", 2 );

AlertIf( Cover, "SOUND C:\\Windows\\Media\\harleyre.wav", "Audio alert", 2 );


//SetPositionSize( 10, spsShares );
SetPositionSize( 0.8, 2);


PlotShapes(Buy*shapeUpArrow,colorGreen,0,trailarray);
PlotShapes(Short*shapeDownArrow,colorRed,0,trailarray);

PlotShapes(Cover*shapeSmallUpTriangle,colorGreen,0,trailarray);
PlotShapes(Sell*shapeSmallDownTriangle,colorRed,0,trailarray);

Plot( Close,"Price",colorBlack,styleBar);
//SetBarFillColor( colorYellow );
TRAILCOLOR =IIf(C>trailARRAY,colorBlue,colorRed);
Plot( trailARRAY,"trailing stop level", TRAILCOLOR, styleLine );



Plot( Close,"Price",colorBlack,styleBar);
//SetBarFillColor( colorYellow );
TRAILCOLOR =IIf(C>trailARRAY1,colorBlue,colorRed);
Plot( trailARRAY1,"trailing stop level1", TRAILCOLOR, styleLine );







_SECTION_END();
 

Similar threads