Need some help on setting up exit signal

I have the entry signal identified clearly.
In terms of the exit signal, I would consider the following

  1. trailing stop @ close below 9 ema
  2. crosses below the low of the day before entry.
RANGE=H-L;
hh7=RANGE==HHV(RANGE,7);
whencrossover50=BarsSince(Cross(H,MA(C,50)));
takeoff=hh7 and Ref(BarsSince(hh7),-1) >Ref(whencrossover50,-1) OR (Cross(H,MA(C,50)) AND hh7);
Buy= MA(V,20)>3000 AND MA(C,20)>10 AND Ref(takeoff,-1) AND H>Ref(H,-1) AND Ref(C>O,-1) AND V>MA(V,60);
BuyPrice=Ref(H,-1);

Here is my entry signal. how do I build the exit signal?

Moderator comment: Read How to use this site and Use Code Tags!

As long as you include some way of enforcing the entry is above the 9 EMA, you can code your trailing stop as:

TrailingStop = Cross(EMA(C, 9), Close);

otherwise, simply:

TrailingStop = C < EMA(C, 9);

Your other condition requires you to know the low of the day prior to entry. The issue there is that Buy is an array that can be True every time the conditions are met, producing multiple signals, even after a trade is taken. So ValueWhen is typically no good in those situations, as it will only look for the most recent Buy signal, which may not be the original entry.

There are a number of ways around this (some discussion of which is available on this very forum), but using a loop which can hold the value you need at entry, is one of the more robust approaches. Start here for guidance: http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/comment-page-1/

2 Likes

Thanks, I understand now. I tried the logic and I have 1 small error that I cannot figure out where the syntax error is. can you help?

_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
_SECTION_END();

RANGE = H - L;
hh7 = RANGE == HHV( RANGE, 7 );
FORTYDAYHIGH = HHV( H, 40 );
FORTYDAYLOW = llv( L, 40 );

EXPANSIONLONG = H > Ref( FORTYDAYHIGH, -1 ) AND hh7;
EXPANSIONshort = L < Ref( FORTYDAYLOW, -1 ) AND hh7;

PlotShapes( hh7*shapeDigit7, colorYellow, layer = 0, L + range / 2 );
PlotShapes( hh7*shapeDigit7, colorYellow, layer = 0, L + range / 2 );
PlotShapes( IIf( Ref( EXPANSIONLONG, -1 ) AND H > Ref( H, -1 ) , shapeDigit1, shapeNone ), colorYellow, layer = 0, H + range / 2 );
PlotShapes( IIf( Ref( EXPANSIONshort, -1 ) AND L < Ref( l, -1 ) , shapeDigit1, shapeNone ), colorYellow, layer = 0, L - range / 2 );

_SECTION_BEGIN( "MA" );
P = ParamField( "Price field", -1 );
Periods = 9;
stopline = MA( P, Periods );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style" ) );
_SECTION_END();

_SECTION_BEGIN( "MA1" );
P = ParamField( "Price field", -1 );
Periods = 50;
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style" ) );
_SECTION_END();
Buy = MA( V, 20 ) > 300000 AND MA( C, 20 ) > 30 AND Ref( EXPANSIONLONG, -1 ) AND H > Ref( H, -1 );
Sell = MA( V, 20 ) > 300000 AND MA( C, 20 ) > 30 AND Ref( EXPANSIONshort, -1 ) AND L < Ref( l, -1 );
whencrossover50 = BarsSince( Cross( H, MA( C, 50 ) ) );

takeoff = hh7 and Ref( BarsSince( hh7 ), -1 ) > Ref( whencrossover50, -1 ) OR( Cross( H, MA( C, 50 ) ) AND hh7 );

Buysignal = Ref( takeoff, -1 ) AND H > Ref( H, -1 ) AND Ref( C > O, -1 ) AND V > MA( V, 60 );

Buy = MA( V, 20 ) > 300000 AND MA( C, 20 ) > 10 AND Buysignal;
BuyPrice = Ref( H, -1 );

Sell = False;

inlongposition = True;
ts = barwhenienteredlong = Null;

for( i = 1; i < BarCount; i++ )
{
    if( ts == Null AND Buy[i] == 1 )
    {
        inlongposition = True;
        ts  =  Low[i - 1];
        barwhenienteredlong = i;
    }

    if( inlongposition AND( i > barwhenienteredlong ) )  // look for trail stops.
    {
        Buy[i] = 0;

        if( Close[i] < stopline[i] )
        {
			//pull trail stop
            Sell[i] = 1;
            SellPrice[i] = Close[i];
            inlongposition = False;
            ts = Null;
        }
    }

    if( inlongposition AND i > barwhenienteredlong ) // look for stoploss
    {
        Buy[i] = 0;

        if( Low[i] < ts )
        {
			//pull trail stop
            Sell[i] = 1;
            SellPrice[i] = Close[i];
            inlongposition = False;
            ts = Null;
        }
    }

	//it either pulls stop
	//or i do trailing stop.
}

PlotShapes( takeoff * shapeCircle, colorYellow, layer = 0, L - ATR( 14 ) / 2, -6 );
PlotShapes( Sell * shapeCircle, colorPink, layer = 0, l );

You forgot to close the for loop.
Also SellPrice is an array you missed the SellPrice[i],
Did not check your logic

One more thing, after you get the SellPrice set correctly, remember to change scenario in Bactester settings so Analysis uses your sellprice

thanks, I fixed it all but it still shows just 1 error and I have racked my head on this one. I don’t see missing anyting.
screenshot of error

— REPEATED THE SAME CODE —

I have racked my brain for some time now and still cannot find where the error is.
Need some help.

— TRIMMED REPETITION OF THE SAME CODE —

I’m not seeing any error when I verify the code. Not on version 6.22 anyway.

What version of Amibroker do you have?

Pro Tip: Highlight your code in the editor and choose Edit / Prettify Selection. If there is an error that’s connected with unbalanced braces for example, that will help you spot it easier.

Just upgraded to the latest beta and I am still getting this error. I guess, I will send this to support as I cannot figure out where I am making a mistake and the message does not point to the issues correctly.

--- TRIMMED REPETITION OF THE SAME CODE---

You are using some 3rd party editors (maybe MS Word) for copy-pasting that are NOT suited for using with the code.

So you end up with extra “white” (invisible) characters that are NOT expected.

You have invalid ASCII codes 0xA0 (decimal 160) appended in places where it shows syntax errors
by this third party editor that you are using.

NEVER EVER use MS Word for copy-pasting the code!

I have fixed your spaghetti formatting and removed invalid characters from your code in this post - now you can copy paste directly to AB and it will work.

2 Likes

Tomasz, thanks for the help.
FYI, I did not use word. I am only using AFL All I did was to copy the code from AMIBROKER KB and pasted to the AFL editor. I did scan the code with Notepad ++ as I was suspicious on the characters but it only showed CR.LF.
How can I detect it in future so I don’t have to go thro this exercise. Again, I did not use word or any other external editors.

Notepad++ is an external editor. Code 160 can be generated in some editors if you press SPACE with CTRL KEY held down. If you press SPACE don’t hold other keys at once.

1 Like