Target & Stoploss Levels in %

Hi Dear Members.

Can someone please write or share an AFL which plots upto 10 levels of Target (%) in horizontal lines & atleast 1 level of Stoploss (%). 1%, 2%, 3%.....distance is from current price on a 1 min chart.

I have done extensive research for something similar in this forum & in google before asking for help here.

I trade crypto (spot) & stocks.

Thank you very much, fellows.

Best Regards,
RK

Hi,

you have to share your efforts also here and show your attempt.

I tried like this from -3% to +10%

for(i=-3; i<=10; i++)
{
	PlotGrid( LastValue(C) * (1 + (0.01*i)), 35 + i, 1, 1, 0 );
}

You have written current price, so line will keep moving. If you want fixed, you can change price to entry value. If it is previous close, then you get previous close from Daily etc

There are many examples here in help
Using graph styles, colors and titles in Indicator Builder (amibroker.com)

1 Like

Hello nsm51

Thank you so much for the formula. I love it.

Yes, I read the link to learn further but I was unable to get next steps done.

  1. YES, I would love to have the Grid FIXED from the ENTRY PRICE of my trade.
    So that I know how many % price has moved from my entry price once in real trade.

I could not understand how to change price to Entry value in the above formula.

  1. I want to learn how to apply the text above the horizontal line or change colors of default lines put up by the formula. I attached an image to show you what I have in my mind. % # (Price at the level)

I appreciate your time & effort.

Best
RK

Text On LInes

  1. instead of LastValue(), use entry price.
  2. It is possible, use PlotText()
1 Like

Hi

for(i=-1; i<=5; i++)
{
PlotGrid( Entry Price(C) * (1 + (0.01*i)), 35 + i, 1, 1, 0 );
}
PlotText( "%" + "Price" + C[ i ], i, L[ i ]-dist[i], colorGreen );

Its giving syntax errors :frowning:

Errors

You have to learn AFL, such simple things cannot be passed.

Entry price means, put your hardcoded entry value of price as number in its place.

1 Like
// Plotting values
// Parameters
entry = 40000;	 	    // Value of entry. 40000 etc,  LastValue(C)
percStep = 1;			// % like 0.5 1, 1.5, 2 etc 
from = -3;				// STOP starting like -3% etc
to = 10;				// Target End like 10%

colarr = "[30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50;30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]";
mx = MxFromString( colarr );

j = 0;
 
for(i=from; i<=to; i+=percStep)
{
	prc = entry * (1 + (0.01*i));
	col = mx[0][j++];
	
	PlotGrid( prc, col, 1, 1, 0 );
	if( i == 0) {
		txt = StrFormat("Entry   %0.0f", entry);
	}
	else if ( i < 0) {
		txt = StrFormat("STOP %0.1f%%   %0.0f", i,prc);
	}
	else {
		txt = StrFormat("%0.1f%%   %0.0f", i,prc);
	}
	PlotText( txt, BarCount - 20 , prc, col);
}

3 Likes

Hey there

I know exactly what you mean...!!!

By the time I discovered 'AFL', I had come to a realization that technical analysis do not work..!!

So I never learnt AFL as I do not use a single indicator and except changing colors & few numbers inside the AFL, I did not feel the need to learn it like full on.

However, YOU have changed my perspective & I will put in more efforts to learn it now as I do need to work with risk management on a constant basis & if I understand AFL programming, I can further my risk management principles.

I thank you for inspiring me to learn AFL, I will put in more efforts, slowly & gradually, as I do not wish to get discouraged like the first time I started feeding data into Amibroker.

Best Regards,
RK

1 Like

Aww, thank you so very much, nsm51. I really appreciate it a million.....!!!!!

Sorry, you had to spoon fed me :frowning:

It looks B R I L L I A N T on first sight, the moment I entered entry price, manually, wallah..!!

I am going through the AFL to see / learn what have you written so I can learn a thing or two :slight_smile:

Best Regards
RK

I am also using the help file and putting together code. Still far from proficient but one has to learn AFL. Its very easy to use.

2 Likes

Hi

It seems the Price text on the % levels is incorrect (Picture attached) &
can we add upto 4 decimals, it will be PERFECT then.

Cheers hai.

Best Regards
RK

Correction

Price after 2% & after -1% stop is incorrect.

It will be wonderful if text showed correct price level at which the horizontal line is & addition of upto 4 decimals.

Thank you very very much.....!!!!!

This would be a good exercise for you to examine the code and see what to change to get the decimals required.

1 Like

I will try harder now

I DID IT...!!!

0.3f

f seems to be float & 3f signifies I want to view 3 decimals

By doing 0.3f, the price levels corrected too & i understood why the error occurred in the first place, it was rounding off earlier, now it's accurate.

THANK YOU SO MUCH, NSM51. YOU ARE AWESOME......!!!!!!

My next & ultimate task is to work on colors of the horizontal lines.

Best Regards
RK

I am extremely thankful to you, Buddy

There is zero matrix required.
It is repeated sequence (of same 21 colors).
So why wasting energy for MxFromString and wasting time for writing lengthy string?
(Besides you will get subscript out of range error soon in upper code.)
This happens if copying from elsewhere without thinking.

//////// ZERO USE OF MATRIX ////////////
// Plotting values
// Parameters
entry = LastValue(C);	 	    // Value of entry. 40000 etc,  LastValue(C)
percStep = 0.5;			// % like 0.5 1, 1.5, 2 etc 
from = -3;				// STOP starting like -3% etc
to = 10;				// Target End like 10%
cnt = (to-from)/percStep;
x_txt = BarCount - 20; 

Plot( C, "Price", colorDefault, styleBar );
fmt1 = "Entry   %0.3f";
fmt2 = "STOP %0.1f%%   %0.3f";
fmt3 = "TGT %0.1f%%   %0.3f";
for(i=0; i<=cnt; i++)
{
	step = from+i*percStep;
	prc = entry * (1+0.01*step);
	col = colorBlueGrey/*equal to 30*/+i%21;		

	if( step == 0) {
		txt = StrFormat(fmt1, entry);
	} else {
		fmt = WriteIf(step < 0,fmt2,fmt3);
		txt = StrFormat(fmt, step,prc);
	}
	PlotGrid( prc, col, 1, 1, 0 );
	PlotText( txt, x_txt, prc, col);
}
3 Likes