Stop Loss with a loop

I know that you can use the ApplyStop function for a stop loss, but I'm interested to see if I can do it with a loop. I searched and I could find examples of a profit stop and a trailing stop using a loop, but I can't find an example of a stop loss. I tried modifying a profit stop loop in the example below but it wouldn't work for me. Does anyone know what I'm doing wrong?:

BuyPrice = Open;
SellPrice = Open;

Buy = RSI(14) < 40;
Sell = RSI(14) > 60;

StopPercent = 1;
EntryPrice = 0;

for (i=0; i<BarCount; i++)
{
	if (EntryPrice == 0 AND Buy[i] == 1)
	{
		EntryPrice = BuyPrice[i];
	}
	else 

		if (EntryPrice > 0 AND 
			Low[i] < EntryPrice * (1.0 - 0.01 * StopPercent))
		{
			Sell[i] = 1;
			SellPrice[i] = EntryPrice * (1.0 - 0.01 * StopPercent);
			EntryPrice = 0;
		}
}

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

Too vague!

@Marcel have you looked this over, it seems like it can help you.

http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

Another loop example but not using your plan of a fixed percentage,
http://www.amibroker.com/members/traders/05-2009.html

And a follow up by the same author though it gets more complicated
http://www.amibroker.com/members/traders/07-2009.html

1 Like

You have to reset stop also if your RSI sell signal gets triggered.

EDIT: Removed future leak in addition because of trade prices at Open.

/// @link https://forum.amibroker.com/t/stop-loss-with-a-loop/10755/4
/// based on
/// @link http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
BuyPrice = Open;
SellPrice = Open;

trade_delay = 1;

Buy = Ref(RSI(14) < 40, -trade_delay );
Sellsig = Ref(RSI(14) > 60, -trade_delay );

StopPercent = 1;
long_stop = 0;
ls_arr = Null;

for( i = 0; i < BarCount; i++ )
{
	if (long_stop == 0 AND Buy[i])
		long_stop = BuyPrice[i] * (1.0 - 0.01 * StopPercent);
	else 
		Buy[i] = 0; 

	if ((long_stop > 0 AND Low[i] < long_stop) OR Sellsig[i])
	{
		Sell[i] = 1;
		SellPrice[i] = Max(O[ i ], long_stop);
		long_stop = 0;
	}
	
	if( long_stop > 0 ) 
		ls_arr[i] = long_stop;	
}

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

Plot( C, "Price", colorDefault, styleCandle );
Plot( ls_arr, "ls_arr", colorRed, styleNoRescale );
PlotShapes( Buy * shapeUpArrow, colorGreen, layer = 0, L, -12 );
PlotShapes( Sell * shapeDownArrow, colorRed, layer, H, -12 );

706

1 Like

@Marcel

There was still small mistake in upper code related to SellPrice.
Stop's SellPrice should use Min() instead of Max() since at gap down (and bar price being lower than long_stop) it should take Open price as stop's exit price.

Also divided SellPrice into Sellsig trade price and stop's exit price.

if ( Sellsig[i] )
	SellPrice[i] = trade_price[i];
else 
	SellPrice[i] = Min(O[i], long_stop);

And auto choosing trade_price based on trade_delay...

if ( trade_delay > 0 )
   trade_price = Open;
else
   trade_price = Close;

So here is code update:

/// @link https://forum.amibroker.com/t/stop-loss-with-a-loop/10755/5
/// based on
/// @link http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
SetTradeDelays(0, 0, 0, 0);
trade_delay = 1;
StopPercent = 1;// set percent stop amount

Buy = Ref(RSI(14) < 40, -trade_delay);
Sellsig = Ref(RSI(14) > 60, -trade_delay);

Short = Cover = 0;

if ( trade_delay > 0 )
	trade_price = Open;
else
	trade_price = Close;

BuyPrice = trade_price;

long_stop = 0;
ls_arr = Null;
StopLevel = (1 - 0.01 * StopPercent);
for ( i = 0; i < BarCount; i++ )
{
	if (long_stop == 0 AND Buy[i])
		long_stop = BuyPrice[i] * StopLevel;
	else 
		Buy[i] = 0; 

	if ((long_stop > 0 AND Low[i] < long_stop) OR Sellsig[i])
	{
		Sell[i] = 1;
		//
		if ( Sellsig[i] )
			SellPrice[i] = trade_price[i];
		else 
			SellPrice[i] = Min(O[i], long_stop);
		//	
		long_stop = 0;
	}
	
	if( long_stop > 0 ) 
		ls_arr[i] = long_stop;	
}

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

Plot( C, "Price", colorDefault, styleCandle );
Plot( ls_arr, "ls_arr", colorRed, styleNoRescale );
PlotShapes( Buy * shapeUpArrow, colorGreen, layer = 0, L, -12 );
PlotShapes( Sell * shapeDownArrow, colorRed, layer, H, -12 );
8 Likes

Thank you so much @fxshrat for all your help!

No, it is not missing.
Entire Sell routine is handled inside loop. Sell inside loop is set to true (1) if either stop is triggered or Sellsig is true.

But instead Sell may be initialized to zero before loop. Update below.

/// @link https://forum.amibroker.com/t/stop-loss-with-a-loop/10755/8
/// based on
/// @link http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
SetTradeDelays(0, 0, 0, 0);
trade_delay = 1;
StopPercent = 1;// set percent stop amount

Buy = Ref(RSI(14) < 40, -trade_delay);
Sellsig = Ref(RSI(14) > 60, -trade_delay);

Sell = Short = Cover = 0;

if ( trade_delay > 0 )
	trade_price = Open;
else
	trade_price = Close;

BuyPrice = trade_price;

ls_arr = Null;
long_stop = 0;
StopLevel = (1 - 0.01 * StopPercent);
for ( i = 0; i < BarCount; i++ )
{
	if (long_stop == 0 AND Buy[i])
		long_stop = BuyPrice[i] * StopLevel;
	else 
		Buy[i] = 0; 

	if ((long_stop > 0 AND Low[i] < long_stop) OR Sellsig[i])
	{
		Sell[i] = 1;
		//
		if ( Sellsig[i] )
			SellPrice[i] = trade_price[i];
		else 
			SellPrice[i] = Min(O[i], long_stop);
		//	
		long_stop = 0;
	}
	
	if( long_stop > 0 ) 
		ls_arr[i] = long_stop;	
}

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

Plot( C, "Price", colorDefault, styleCandle );
Plot( ls_arr, "ls_arr", colorRed, styleNoRescale );
PlotShapes( Buy * shapeUpArrow, colorGreen, layer = 0, L, -12 );
PlotShapes( Sell * shapeDownArrow, colorRed, layer, H, -12 );

Thanks, now I understand - the sell reference was missing.