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 ;


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
/// @link
/// Code source:
/// @link
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:

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((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 );



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.


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

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

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;
Buy[ i ] = 0; // remove excess buy signals

if( PositionLong> 0 )
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;




Many Thanks!

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


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


Oh...I see