Finding way to store the Entry Price for Calculating Exit Level

I am try to write the Code , Take profit after 100pts, or Cut loss after 60pts,
.
Please Help, thanks!!

Buy=cross (Close,ma(Close,15 ));
Short=cross (ma(Close,15),Close);

sell= C > buyprice+100 ;
Cover= C < shortprice-100 ;

BUY=ExRem(BUY,SELL);
SELL=ExRem(SELL,BUY);
COVER=ExRem(COVER,SHORT);
SHORT=ExRem(SHORT,COVER);

BuyPrice=ValueWhen (Buy,O);
ShortPrice=ValueWhen (Short,O);

For creating stops use ApplyStop function but not ValueWhen.

/// Do not remove links from code you have not written
/// It's not been your time spent
/// @link https://www.amibroker.com/guide/afl/applystop.html
/// @link http://www.amibroker.com/kb/index.php?s=applystop
/// Code source:
/// @link https://forum.amibroker.com/t/finding-way-to-store-the-entry-price-for-calculating-exit-level/19045/2
SetOption("ActivateStopsImmediately", 1);
SetOption("ReverseSignalForcesExit", 0);
SetOption("MaxOpenPositions", 2);
SetPositionSize(1, spsPercentOfEquity);
/// In case of wanting to have Buy and Short
/// opened at same time per symbol:
//SetBacktestMode(backtestRegularRawMulti);

BuyPrice = Open;
ShortPrice = Open;

myma = MA(Close, 15);
Buy = Cross(Close, myma);
Short = Cross(myma, Close);
Buy = Ref(Buy, -1);
Short = Ref(Short, -1);

Sell = Cover = 0;
SellPrice = CoverPrice = Close;
ApplyStop(stopTypeLoss, stopModePoint, loss_amount = 60, exitatstop = 0);
ApplyStop(stopTypeProfit, stopModePoint, tgt_amount = 100, exitatstop);

// For chart pane display
if ( Status("action") == actionIndicator ) {
	eq = Equity(1, 0);
	
	intrade_long = Flip(Buy, Sell);
	intrade_short = Flip(Short, Cover);
	
	stoplevel_long = ValueWhen(Buy, BuyPrice-loss_amount);
	tgt_level_long = ValueWhen(Buy, BuyPrice+tgt_amount);
	
	stoplevel_short = ValueWhen(Short, ShortPrice+loss_amount);
	tgt_level_short = ValueWhen(Short, ShortPrice-tgt_amount);

	Plot( C, "Price", colorDefault, styleBar );	
	PlotShapes(Buy*shapesmallUpTriangle,colorGreen,0, L);
	PlotShapes((Sell>0)*shapeDownArrow,colorRed,0,H);	
	PlotShapes(Short*shapesmallDownTriangle,colorRed,0,H);
	PlotShapes((Cover>0)*shapeUpArrow,colorAqua,0, L);
	Plot(IIf(intrade_long, tgt_level_long, Null), "tgt_level_long", colorGreen );
	Plot(IIf(intrade_long, stoplevel_long, Null), "stoplevel_long", colorRed );	
	Plot(IIf(intrade_short, tgt_level_short, Null), "tgt_level_short", colorPaleGreen );
	Plot(IIf(intrade_short, stoplevel_short, Null), "stoplevel_short", colorOrange );
}

13

6 Likes

Many Thanks!

I have tried to use Applystop,
But when I do backtest it showed "No Result"

Would you mind advice me how can show in backtest result too
Thanks thanks!

Please listen, the code works in chart as well as in backtest! I do not post things not working.

16


Just because you can not apply it correctly does not mean it is not working.
You have to check DETAILED LOG in backtest!
15

It highly probable that you get no results in analysis because of insufficient funds, too low initial equity...
Ask your self whether you could enter position in real life if you enter incorrect positions size.
Do calculations!

If you have initial equity of 100k and 1% position size of 1k (see position size setting in upper code!!) but symbol has value of e.g. 2k.... Is that going to work? With that example in mind anyone's brain cells should start working at least now.

SetOption("InitialEquity", 100000);
SetOption("ActivateStopsImmediately", 1);
SetOption("ReverseSignalForcesExit", 0);
SetOption("MaxOpenPositions", 2);
SetPositionSize(5, spsPercentOfEquity);// POSITION SIZE SETTTING HERE

Also you should not create multiple threads on same/similar topic
What's the difference here?
You already got information that to create stops use ApplyStop. Your code in that duplicate thread is incorrect once again.

Not listening and jumping to creating duplicate thread is rather disrespectful.

1 Like

Hahaha.....Thanks Thnaks!

What if I wanna plot a trail that
when buy ,
plot a trail buyprice+500
if H>buyprice+500, plot 1000
if H>buyprcie+1000, plot 1500
until sell
and when short ,
plot a trail shortprice-500
if L<shortprice-500, plot 1000
if L<shortprice-1000, plot 1500
until cover

Would u mind give me some advice, I have tried half day but still failed
Haha....

Sorry, I am a very beginner, I am not mean ur code not work,
but i miss the Backtest report function

I am trying to use following code to make it,
am i on the right track or make it too complex?

for( i = 3; i < BarCount; i++ )
{
if( PositionLong== 0 AND Buy[ i ]==1 )
{
PositionLong = 1;
Holdcount = 0;
bSTOP = BuyStop[i];
bPrice = BuyPrice[i];
SellCheck = ceil((Max (High[i],High[i-1])-bPrice)/500);
SellTri = 0;
continue;
}
else
Buy[ i ] = 0; // remove excess buy signals

if( PositionLong> 0 )
{
Holdcount++;
LowCallProfitCheck[i] = bPrice+SellCheck*500;
LowCallProfitCheckNext = bPrice+(SellCheck+1)*500;
bProfitLevel[i] = SellCheck;

    if (sellcon[i]) {
		Sell[ i ] = 1;
		SellPrice[ i ] = O[i];
 		PositionLong= 0;
		Holdcount = 0;
	}
	

}

}

Plot(LowCallProfitCheck,"",colorGreen,styleThick|styleNoRescale);
_SECTION_END();

Many Thanks!

What you describe is not a trail stop but it is scaling in.

18


A trail stop is below of the price if being long.
And it is above of the price if being short.

19

Oh...I see
Thanks!!!