# How to set loss stop for multiple signals?

Hi! How can I write custom loss stop for multiple signals for trading in 24-hr or specific time schedule? My code is a pattern trading strategy, is quite cumbersome. Please comment!

``````Buy1 = Ref(Pat1,-1) AND Close >= Ref(LL,-1);
Short1 = Ref(Pat2,-1) AND Close <= Ref(HH,-1);
//Short2 = Pat15 AND Close == LL;

/* Loss Stop  */
for( i = 0; i < BarCount; i++ )
{
if ( PriceatBuy == 0 AND Buy1[i] )
{
}
if ( PriceatBuy > 0 AND SellPrice[i] < PriceatBuyStop )
{
Sell1[ i ] = 1;
//SellPrice[ i ] = Close[i];
}
else
{
Sell1[ i ] = 0;
}

if ( PriceatShort == 0 AND Short1[i] )
{
PriceatShort = ShortPrice[i];
PriceatShortStop = H1[i-1];
}
if ( PriceatShort > 0 AND CoverPrice[i] > PriceatShortStop )
{
Cover1[i] = 1;
//CoverPrice[i] = Close[i];
PriceatShort = 0;
}
else
{
Cover1[i] = 0;
PriceatShortStop = 999999;
}
}

/* trading in 24-hr or specific time schedule */
if ( TradeMode == 1 )
{
Buy =   Buy1 AND tn >= TimeEnter;
Short = Short1 AND tn >= TimeEnter;
Sell = Sell1 OR Short OR Cross( tn, TimeQuit );
Cover = Cover1 OR Buy OR Cross( tn, TimeQuit );
}
else if ( TradeMode == 2 )
{
Short = Short1;
Sell = Sell1 or Short;
Cover = Cover1 or Buy;
}
``````

See techniques described here:

and here

Tomasz, thank you very much! However, I am looking for guidance to rewrite my code because my code is cumbersome.

I have written the code based on the example from:
http://www.amibroker.com/guide/h_backtest.html

Coding your own custom stop types

ApplyStop function is intended to cover most “popular” kinds of stops. You can however code your own kind of stops and exits using looping code. For example the following re-implements profit target stop and shows how to refer to the trade entry price in your formulas:

``````/* a sample low-level implementation of Profit-target stop in AFL: */

Buy = Cross( MACD(), Signal() );

for( i = 0; i < BarCount; i++ )
{
if( priceatbuy == 0 && Buy[ i ] )