How to get the price of entry into the position?

I'm trying to get the entry price for the position with the following formula

LongEntry = 130000 == TimeNum(); 
cvL = ValueWhen(LongEntry, Ref(Open,1)); //entry price
Plot(cvL,"cvL",colorAqua);	//drawing entry price

Buy = LongEntry; 
Sell = DateTime() == _DT("2017-12-15 14:18:00"); 
Buy = ExRem(Buy,Sell); 
Sell = ExRem(Sell,Buy); 

But the problem is the following: every day at 13 o'clock, it updates entry price. How to keep the entry price, while the position of Long is active?

Move cvL code to the END of your formula and replace it with ValueWhen( Buy,...

LongEntry = 130000 == TimeNum(); 

Buy = LongEntry; 
Sell = DateTime() == _DT("2017-12-15 14:18:00"); 
Buy = ExRem(Buy,Sell); 
Sell = ExRem(Sell,Buy);  

cvL = ValueWhen( Buy, Ref(Open,1)); //entry price
Plot(cvL,"cvL",colorAqua);	//drawing entry price
3 Likes

Thank you. This helped to open and close the deal correctly.

But the entry price continues to change every day at 13-00, it shows PLOT (cvL). I need entry price for later use in the formula.

Help me please.
The price changes every day at 13:00
I changed the formula, but it did not help. How to get the entry price (unchanged every day)? Examples in pictures.

Kvhod = 130000;
Buy = Kvhod == TimeNum();
Sell = 0;
trailstop = 0;
trailARRAY = BuyPrice = Null;

for( i = 1; i < BarCount; i++ )
{	BuyPrice[i] = BuyPrice[i-1];

   if( trailstop == 0 AND Buy[i] )  
   {	BuyPrice[i] = Open[i];
		Buy[i] = 1;
   }
   else Buy[i] = 0; // remove excess buy signals
}

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( BuyPrice,"BuyPrice", colorGold );

First day: BuyPrice = 30'481
Second day: the price has changed to 30'501.

How to make that BuyPrice does not change in the next days?

12

Looks like your data source provider is changing (correcting) data after it happen.
Save/export the data right after the open, and next day check if the data has changed.

This is history. The data does not change.

Do you want to plot the price at 1 PM of previous day in next day ?

Kvhod = 130000;
ItsTime = Kvhod == TimeNum();
PriceAtTime = ValueWhen(ItsTime, Open);
DayChange = Day() != Ref(Day(), -1);
PriceAtTimeYesterday = ValueWhen(DayChange, PriceAtTime);

Plot( PriceAtTimeYesterday,"PriceAtTimeYesterday", colorGold );

I want the next one: i buy 1 unit, for example for $ 1100. This price should be Plot by a line, until there is a signal to sell it.

Kvhod = 130000;
Buy = Kvhod == TimeNum();
Sell = 0;
trailstop = 0;
trailARRAY = BuyPrice = Pos = Null;

for( i = 1; i < BarCount; i++ )
{	BuyPrice[i] = BuyPrice[i-1];
	Pos [i]  = Pos [i-1];
	
   if( IsNull(Pos [i]) AND Buy[i] OR Pos [i]==1 AND Buy[i] ) 
   {	BuyPrice[i] = Open[i];
		Buy[i] = 1;
		Pos[i] = 1;
		//trailstop = BuyPrice[i]*(1-1/100); 
   }
   else Buy[i] = 0; // remove excess buy signals
}

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( BuyPrice,"BuyPrice", colorGold );

This is not work. :slightly_frowning_face:

Look up Exrem() function. Basically your code is generating a buy single everyday at your specified time.

The usual code would be ExRem(Buy, Sell), however your formula doesn’t have a sell signal from what you posted.

Hi WKomarav,
Did you find a solution to this? I am having the same problem
I have tried playing around with ExRem and also SetBacktestMode(backtestRegular), but it made no difference

The solution is to use custom backtesting procedure and static variables

@WKomarov

In your FIRST code, the price continues to change on 13:00 because you are ENTERING a NEW trade on 13:00 and closing it on 14:18 so next day on 13:00 there is a NEW OPEN and new

You are doing some major mistake however in usage of variables.

The thing to understand is that BuyPrice is INPUT array for AmIBroker's backtester, not output. BuyPrice is array where you fill the prices you want to use for eventual buys, like this:

BuyPrice = Open; // buys will occur on open IF THEY OCCUR AT ALL

If you want to plot actual Entry price you need to use ValueWhen (ONLY when you have NON-repetive signals):

BuyPrice = Close; // will be buying always on close (IF BUY happens)
Buy = Cross( C, MA( C, 10 ) );
Sell = Cross( MA( C, 10 ), C );

ActualEntryPrice = ValueWhen( Buy, BuyPrice );

Plot( ActualEntryPrice, "ActualEntryPrice", colorRed );

In your SECOND example (the one that you wrote does not work), you've got NO SELL at all, but repeated Buy signals. So if you want to keep price of VERY FIRST buy and hold it forever the code below does that:

Kvhod = 130000; 
Buy = Kvhod == TimeNum(); // raw signal - buy on 13:00 everyday
Sell = 0; // NO SELL at all

Buy = ExRem( Buy, Sell ); // remove excess buys (so ONLY ONE- very first trade is actually made)

ActualEntryPrice = ValueWhen( Buy, BuyPrice );

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( C, "Price", colorDefault, styleCandle );
Plot( ActualEntryPrice,"ActualEntryPrice", colorGold );

And please DO USE the advice given here:

That is crucial to understand your own code. You won't understand it until you follow advice given in "How do I debug my formula". For anyone wanting to write their formula that is basic skill like learning to walk.

2 Likes