Hey guys. I've been learning about trading and coding in Amibroker.
I've put together some code based on:
- 10 week moving average of index greater than previous week
- Stock closing at new 20 week high
- 20 week rate of change > 30
That part of it seemed to work ok until I started adding a trailing stop loss.
The stop loss is supposed to be based on a 40% decline from a stock's highest point unless the index is in decline (based on 10 previous 10 week's trend) in which case it moves up to a 10% decline.
All calculations are done weekly as a close of trading Friday.
When I run this as a scan, it's giving my Buy signals where the stocks don't seem to be meeting the ROC hurdle. It seemed to work ok until I added the trailing stop loss code.
Clearly I've done something(s) wrong and I'm pulling my hair out.
Any help will be much appreciated.
SetOption ( "maxopenpositions", mp = 20 );
SetPositionSize(5, spsPercentageOfEquity);
index = "INX"; // replace with your index symbol
SetForeign ( index );
{
TimeFrameSet ( inWeekly );
{
Average = MA ( C, 10 );
buysignal = average > Ref ( average, - 1 );
buysignal1 = average > Ref ( average, - 1 ); // Buy Condition #1
uptrend = close > average;
RestorePriceArrays();
periodHigh = HHV ( Close, 20 );
WeeklyClose = close;
chg = ROC ( Close, 20 ) ;
buysignal2 = Close > Ref ( periodhigh, -1 ); // Buy Condition #2
buysignal3 = chg > 30 ; // Buy Condition #3
buysignal4= uptrend;
endofweek = 1;
}
TimeFrameRestore ();
buysignal = TimeFrameExpand ( buysignal , inWeekly, expandpoint );
uptrend = TimeFrameExpand ( uptrend , inWeekly );
endofweek = TimeFrameExpand ( endofweek , inWeekly, expandpoint ) ;
firstdayofweek = Ref ( endofweek, -1 );
}
Buy= buysignal1 AND buysignal2 AND buysignal3;
//Trailing Stop Information
Buy = Sell = Short = Cover = 0;
position = ExitTrade = EnterTrade = 0;
stopArray = Null ;
TrailingDistance = IIf ( uptrend , 40 , 10 ) / 100 * Close;
//*************************************
pricecolor = colorDefault;
dow = DayOfWeek();
dt = DateTime();
dtsignal = "";
for ( i = 0 ; i < BarCount ; i ++ )
{
if ( ExitTrade )
{
if ( firstdayofweek [i] ) // exit on monday
{
Sell [i] = 1;
SellPrice [i] = Open[i] ;
position = 0;
ExitTrade = 0 ;
}
}
if ( position )
{
if ( endofweek [i] )
{
if ( WeeklyClose [i ] < stop )
{
ExitTrade = 1;
pricecolor[i] = colorred;
dtsignal = DateTimeToStr ( dt[i] );
}
}
if ( ExitTrade == 0 )
{
stop = max ( stop , Close [i] - TrailingDistance [i] ) ; // adjust stop
}
}
if ( EnterTrade )
{
if ( firstdayofweek [i] )
{
Buy [i] = 1;
BuyPrice[i] = Open[i];
stop = buyprice [i] * ( 1 - 49 / 100 ) ; // initial stop
position = 1;
EnterTrade = 0;
}
}
if ( !position )
{
if ( buysignal [i] )
{
//printf ( "\n" + dow[i] );
EnterTrade = 1;
pricecolor[i] = colorBrightGreen;
dtsignal = DateTimeToStr ( dt[i] );
}
}
if ( position )
StopArray [i] = stop ;
}
//*****************************************
Plot ( C, "", colorDefault , stylebar );
shape = Buy *shapeUpArrow;
//Plot ( stopArray, "", colorRed , stylestaircase );
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );
//PlotShapes (Buy,colorGreen, 0 , Low);
//PlotShapes ( sell shapedownArrow, colorred, 0 , high );
ribbonColor = IIf( uptrend , colorGreen, colorRed );
Plot( 2, "", ribbonColor , styleOwnScale | styleArea | styleNoLabel, -0.5, 100 );
GraphXSpace = 10 ;
SetChartOptions( 0 , chartShowDates | chartShowArrows );