/*...continued...*/ /*
Stop Loss: strict stop loss percent until cross buy price, then at buy price
*/
stopperc = Param("Stop Loss %", 10, 0, 20, 0.1);
stopamount = Close * (stopperc/100); // calculate ATR
initial = Close - (stopamount);
stop[ 0 ] = Close[ 0 ];
for( i = 1 ; i < BarCount; i++)
{
if( Close[ i ] > stop[ i - 1])
{
temp = Close[ i ] - stopamount[ i ];
if ( ( temp > stop[ i - 1 ] ) AND (temp <= PriceBought[ i ])) stop [ i ] = temp;
else if (temp > PriceBought[ i ]) stop[ i ] = PriceBought[ i ];
else stop[ i ] = stop[ i - 1 ];
}
else stop[ i ] = initial[ i ];
}
/*
Trailing "last Low" stop. If we are above purchase price, and we break get a lower low, then time to sell.
*/
lowerlowstop[ 0 ] = 0 ;
directiondown[ 0 ] = 0 ;
directionup[ 0 ] = 0 ;
turnup = 0;
turndown = 0;
for( i = 1 ; i < BarCount; i++)
{
if (Close[i] == Close[i-1])
{
directiondown[i] = directiondown[i-1];
directionup[i] = directionup[i-1];
} else
{
directiondown[i] = (Close[i] < Close[i-1]);
directionup[i] = (Close[i] > Close[i-1]);
}
}
inthemoney = (Close > EMA(Close,EMAPer));
for( i = 1 ; i < BarCount; i++)
{
if (Buy[i]) lowerlowstop[i-1] = Close[i-1] * 0.85;
if ( directiondown[i-1] AND directionup[i]) turnup = 1; else turnup = 0;
if ( directionup[i-1] AND directiondown[i]) turndown = 1; else turndown = 0;
if ((turnup) AND (Close[i-1] > lowerlowstop[i-1]) AND inthemoney[i-1])
{
lowerlowstop[i-1] = Close[i-1];
turnup = 0;
}
lowerlowstop[i] = lowerlowstop[i-1];
}
/*
We Sell if we Cross the lowerlow rule OR if our trailing stop loss is activated
*/
Sell = Cross(lowerlowstop , Close) OR (Close < Ref( stop, -1));
/* exrem is one method to remove surplus strade signals */
Buy = ExRem(Buy, Sell);
BuyAlert = ExRem(BuyAlert, Sell);
FalseBuy = ExRem(FalseBuy, Buy);
Sell = ExRem(Sell, Buy);
/*
Set variables for use in the colour of the price graph
*/
EntrySignal = (Close > (EMA(Close,EMAPer))) AND PrimaryUp;
ExitSignal = Sell;
/*-------------------------------------
Automatic Analysis (AA) Settings
This formula can be used for the following purposes:
1 - Indicator, 2 - Commentary, 3 - Scan, 4 - Exploration, 5 - Backtest, Optimize
-------------------------------------*/
AASettings = Status("action");
if (AASettings == 1) {
/* [Indicator] */
GraphXSpace = 5; /* create empty space of 10% top and bottom of chart */
Color = IIf( EntrySignal, colorBlue, IIf( ExitSignal, colorOrange, colorGrey50 ));
Plot(C, " Close Price", Color, styleLine);
Plot(EMA(C,EMAper), " EMA("+EMAper+")", colorGreen, styleLine);
Plot( stop, "Stop Loss", colorRed, styleDashed);
Plot( lowerlowstop, "Last Low", colorGrey50, styleDashed);
SellColor = IIf( stop < Ref( stop,-1), colorOrange, colorRed);
/* styleNoRescale in the plots stops the added plots from compressing the original bar chart to the middle of the pane */
PlotShapes(shapeUpArrow * Buy, colorGreen, 0, L, - 10);
PlotShapes(shapeHollowUpArrow * BuyAlert, colorBlue, 0, L, - 20);
PlotShapes(shapeHollowUpArrow * FalseBuy, colorGreen, 0, L, - 30);
PlotShapes(shapeDownArrow * Sell, SellColor, 0, H, - 10);
} else if (AASettings == 2) {
// [Commentary]
"Closing price = " + WriteVal(Close);
"Change since yesterday = " + WriteVal(Close - Ref(Close, -1));
"Percent chg. since yesterday = " + WriteVal(ROC(Close, 1)) + " %";
} else if (AASettings == 3) {
// [Scan]
AlertIf(Buy, "SOUND C:\\PROGRAM FILES\\AMIBROKER\\BUY.WAV", "Buy", 3);
AlertIf(Sell, "SOUND C:\\PROGRAM FILES\\AMIBROKER\\SELL.WAV", "Sell", 3);
} else if (AASettings == 4) {
// [Exploration]
/* We restrict results of exploration to when the Buy AND Sell signals occur */
/* You can use Filter = 1; to display every bar result */
Filter = Buy OR Sell;
AddTextColumn(FullName(), "Company Name");
AddColumn(Buy, "Buy", 1);
AddColumn(Sell, "Sell", 1);
AddColumn(C, "Close", 1.3);
AddColumn(H, "High", 1.3);
AddColumn(directionup, "UP", 1);
AddColumn(directiondown, "DOWN", 1);
} else if (AASettings == 5) {
// [Backtest]
SetTradeDelays(0, 0, 0, 0);
}
_SECTION_END();