Stoploss Question

#1
Dear friends,

I am new to AFL and developing a trading system/strategy using MACD and RSI. It generates signals and they are plotted in the chart too. For formula, refere one of my thread question in this board itself.

I want to implement a Stoploss and Profit target (if possible) for the buy and sell signals. I read the AFL help, but still couldnt understand the correct usage.

I tried the following code:

ApplyStop(0,2,15,2);
//param1 = 0 = type, MaximumStoploss
//param2 = 2 = mode, amount in points
//param3 = 15 = amount, in this case 15 pts
//param4 = 1 = exitatstop, exit at next bar

Plot(ShapeStar * Applystop(0,2,152) , ColorYellow);

I assumed this would show 1 stoploss signal after a buy/sell arrow. But for each bar in the price chart, the yellow stars show. Can anyone suggest me how to code this scenerio:

1. After a buy, the maximum SL is 15 pts and the Profit target is 20 pts
2. After a sell, the maximum SL is 15 pts and the profit target is 20 pts.

The SL signal and the profit signal should appear in the chart and should stay there. Not to be a trailing stop.

Thanks for any reply and help.

kesk
 

marked9

New Member
#2
Is this what you are looking for ?


StopLevel = 1 - Param("trailing stop %", 3, 0.1, 10, 0.1)/100;

Buy = Cross( MACD(), Signal() );
Sell = 0;
trailARRAY = Null;
trailstop = 0;

for( i = 1; i < BarCount; i++ )
{

if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] * stoplevel;
}
else Buy[ i ] = 0; // remove excess buy signals

if( trailstop > 0 AND Low[ i ] < trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}

if( trailstop > 0 )
{
trailstop = Max( High[ i ] * stoplevel, trailstop );
trailARRAY[ i ] = trailstop;
}

}

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( Close,"Price",colorBlack,styleBar);
Plot( trailARRAY,"trailing stop level", colorRed );


This can plot trailing stoploss from a buy to its end signal.
Its fairly easy, it moves the stoploss as the price moves up. All you do is to define percentage profit margin.
I am using this code, I modified it so I get max profit after a buy signal, till it reaches a sell signal and the price comes down to the percentage margin I set, following any type of indicator I am using.
There are some very nice examples on AmiBroker’s site for using AFL.


http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
 
#3
Thanks for your reply, but this is not what i wanted.

Suppose there is a Long at 4300. My SL should be at 4285, represented by an arrow/star. when the CMP moves to 4310, now the SL should move to 4295. This is the behaviour I want. I want to keep my SL at 15 pts always.

The profit target cannot be marked visually in the chart, as this might occur in the future. This is always 20 pts.

If it cannot be represented graphically, atleast it should appear in a box, words, some where in the chart.

Help will be appreciated.

kesk
 

marked9

New Member
#4
Try this, this will plot an arrow at the last bar with the last close price and with the 15% stoploss from the last buy signal.



PriceAtBuy = ValueWhen (Buy,BuyPrice);
StopLoss = ((PriceAtBuy/100)*15);


for( i = 0; i < BarCount; i++ )
{
GraphXSpace = 5;
dist = 2.5*ATR(5);

LastBar = (BarIndex () == BarCount -1)* shapeDownArrow ;
PlotShapes ( LastBar , ((IIf ( LastBar , colorOrange, 0) )), 0);
if( LastBar ) PlotText( "Last Price\n" + C + "\nStopLoss " + StopLoss, i, L[ i ]+dist, colorOrange );

}



If you have the buy value set within a function, then put below lines in that function.

global PriceAtBuy;
global StopLoss;
PriceAtBuy = ValueWhen (Buy,BuyPrice);
StopLoss = ((PriceAtBuy/100)*15);
 
#5
Dear Marked9,

The modified code seems to hog the system resource and RAM and Amibroker and computer hangs.

Removing the code ensures, Amibroker works normally.

Is there some kind of endless loop going on? I couldnt really figure out.

kesk
 
#6
Ok, I am not sure if you were able find to right place to paste it.

I changed it so you can use directly, copy paste the whole code in a new file and plot it -

/////////
SetBarsRequired( sbrAll, sbrAll );

_N(Title = StrFormat("15% StopLoss- {{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
MYcolor = IIf( EMA(C,13)>EMA(EMA(C,13),9) AND C>Peak(C,2,1), colorLime, IIf(EMA(C,13)>EMA(EMA(C,13),9) AND C<Peak(C,2,1),colorBlue, colorBlack ));

Duration = Interval();
if ( Duration > 59)
{
PlotOHLC( Open, High, Low, Close, "", Mycolor, styleBar |styleThick );

}
else
if ( Duration = 1)
{
Plot( Close, "", colorBlack,styleLine);
}
else
PlotOHLC( Open, High, Low, Close, "", Mycolor, styleBar |styleThick );

Buy = Cross( EMA( Close, 9 ), EMA( Close, 15 ) );
Sell = Cross( EMA( Close, 15 ), EMA( Close, 9 ) );

PriceAtBuy = ValueWhen (Buy,BuyPrice);
StopLoss = ((PriceAtBuy/100)*15);

LastBar = (BarIndex () == BarCount -1)* shapeDownArrow ;

GraphXSpace = 5;
dist = 40*ATR(5);

for( i = 0; i < BarCount; i++ )
{
if( LastBar) PlotText( "Last Price\n" + C + "\nStopLoss\n " + StopLoss, i, L[ i ]+dist, colorOrange );
}

space = 1;
spacebuy=1;
up = Buy * shapeUpArrow;
PlotShapes(up,colorLime,0,Graph0,spacebuy-38);
spacesell=1;
down = Sell * shapeDownArrow;
PlotShapes(down ,colorRed,0,Graph0,spacesell-38);

PlotShapes ( LastBar , ((IIf ( LastBar , colorOrange, 0) )),0, space+4);
///////


Regards