How to get a Position Price at each moment?

Hi. I am new to AFL.
I am trying to assign current Position Price to an array and plot it out, when the position is closed, position price should be considered as zero.. But don't know how to do that.. Please help..

bought = 0;
boughtPrice = 0;
soldPrice = 0;
posPrice = 0;

shortLine = EMA(C,5);
longLine = EMA(C,10);

Buy = Cross(shortLIne,Longline);
Sell = Cross(LongLine, ShortLine);
Buy = ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Plot(shortline, "Short",colorYellow);
Plot(longline, "Long",colorBlue);
PlotShapes(Sell*shapeDownArrow,colorRed);
PlotShapes(Buy*shapeUpArrow,colorGreen);

bought = Max(Buy,bought);
boughtPrice = ValueWhen(buy,BuyPrice);
posPrice = bought*boughtPrice;
		
Plot(boughtPrice,"POS Price",colorwhite);
Plot(soldprice,"soldPrice",colorRed);
Plot(posPrice,"PosPrice",colorOrange);
	
1 Like

Welcome to the forum @tungtien.

There appear to be a few programming ideas that you are not aware of or using. This will require you to do a bit more reading and testing. As a first hint, I would suggest you search and play with Explorations. The exploration will allow you to see your variables as numbers, not plots. Sometimes seeing how the numbers change can give you a clue to what is not working the way you want.

Second, you need to understand the difference between a State signal and an Impulse signal. The Cross function give you an impulse, but a > (Greater than) gives you a state.

Third, make sure you understand the fact that AFL uses Arrays, and Array processing. It is a bit of a different mindset in programming than sequential processing.

Last, make sure you understand each and every line in your code. When you take code from somewhere/someone else, you need to make sure you understand every line.

You have done a good job posting with code tags, but need to do some more work to be able to explain what you want as a result, and how it differs from what you are getting.

Keep plugging away, and post your code and questions as you can be more specific.

Note: I hated it when I first joined the online list, posted a question, and responders only gave me a vague suggestion. Now I appreciate that they were trying to lead me through the required education and learning to allow me to help myself.

6 Likes

Thank you Snoopy.pa30. I solved the issue. Yes: I am not familiar with array processing - that is the main point..

All the resources are there already.

Absolute MUST READ is this:
http://www.amibroker.com/guide/h_understandafl.html

Followed by this:

And
http://www.amibroker.com/guide/afl/valuewhen.html

and

@tungtien, As a nicety on the forum, we usually ask that when you solve a problem, you post the code (or snippet), in code blocks of course, so that other forum users will be able to see the solution.

Glad you got it solved. Enjoy the AFL learning adventure.

Thank you Snoopy for reminding me to post a solution..
I was hesitating to post my "solution" because I am still thinking that something is not right with this solution. I think there should e a better and more proper way to do this..
Could you please suggest me a better way to get a "LongPosOpen" status rather than take CUMmulative of the whole array of Buy and Sell?

_SECTION_BEGIN("Formula 4");

Buy = 0;
Sell = 0;
Cover = 0;
Short = 0;

function longPosOpened() 
	{
	_longPos = (Cum(Buy)-Cum(Sell));
	return IIf(_longPos>0,1,0);
	}

function shortPosOpened() 
	{
	_shortPos = (Cum(short)-Cum(cover));
	return IIf(_shortPos>0,1,0);
	}
	
function posOpened() {return IIf(longPosOpened() OR shortPosOpened(),True,False);}
function longPosPrice() {return longPosOpened()*ValueWhen(Buy,BuyPrice);}
function shortPosPrice() {return shortPosOpened()*ValueWhen(Short,ShortPrice);}
function posPrice() {return IIf(longPosOpened(),longPosPrice(),IIf(shortPosOPened(),shortPosPrice(),0));}

shortLine = EMA(C,5);
longLine = EMA(C,10);

_Buy = Cross(shortLIne,Longline);
_Sell = Cross(LongLine, ShortLine);
Buy = ExRem(_Buy,_Sell);
_Sell=ExRem(_Sell,_Buy);
longPos = Cum(Buy);
sell = IIf(longPos>0,_sell,False);

Filter = 1;
AddColumn(Buy,"Buy",1.2);
AddColumn(Sell,"Sell",1.2);
AddColumn(BuyPrice,"BuyPrice,1.2");

Plot(shortline, "Short",colorYellow);
Plot(longline, "Long",colorBlue);
PlotShapes(Sell*shapeDownArrow,colorRed);
PlotShapes(Buy*shapeUpArrow,colorGreen);
Plot(longPosOpened(),"longPos",colorGreen);
Plot(longPosPrice(),"longPosPrice",colorred);
	
_SECTION_END();

@tungtien, Posting code and letting others comment on it, or point out better ways to code it is the desired result. There are MANY coders on this forum better than me. I find looking at their code VERY helpful.

I realize that if you don't have a coding background, trying to understand the terms can be frustrating, but as you learn more, it makes it easier to do more of what you want. So, I hope you find this code helpful.
NOTE: State Signal use, not impulse (use > not Cross)

/* Use of State Signal for posLong and posExitLong
  And use of ExRem to control BUY signal */


shortLine = EMA(C,5);
longLine = EMA(C,10);

posLong = shortLIne>Longline;
posExitLong = LongLine> ShortLine;
Buy = ExRem(posLong, posExitLong);
Sell=ExRem(posExitLong, posLong);
BuyPrice = C;
longposprice = BuyPrice * posLong;

Filter = 1;
AddColumn(posLong, "Buy Signal", 1.0);
AddColumn(posExitLong, "Exit Long", 1.0);
AddColumn(Buy,"Buy",1.0);
AddColumn(Sell,"Sell",1.0);
AddColumn(BuyPrice,"BuyPrice,1.2");

Plot(shortline, "Short",colorYellow);
Plot(longline, "Long",colorBlue);
PlotShapes(Sell*shapeDownArrow,colorRed);
PlotShapes(Buy*shapeUpArrow,colorGreen);
Plot(posLong,"longPos",colorGreen);
Plot(longPosPrice,"longPosPrice",colorred);

Thanks Snoopy,
but that is not really what I want; I need a function like LongPosOpened() and ShortPosOpened() which return True or False depending on the happenings of Buy and Sell events; The shortLine and LongLine as in my first code is just an example, in reality there are more complicated conditions for trading..

If you look at the Exploration, you should see the "Buy Signal" or posLong array contains the logical value of the STATE of your request. i.e. shortLIne>LongLine

Whether you actually did the buy and have a position is logic beyond what you asked.

Again, I think the Exploration will show you the State or Status of the posLong array, which you can base on any logical test.

If there are a lot of signals that leads to Buy or Sells, it is not easy to have a array posLong ( not simple as "poslong = shortLine > LongLine"); So can we make a code that conduct a array of posLong but not relying on shortLine and LongLine? i.e by calculating from Buy and Sell events?

Take a look at this example:

/* Use of State Signal for posLong and posExitLong
  And use of ExRem to control BUY signal */


shortLine = EMA(C,5);
longLine = EMA(C,10);
cond1 = shortLIne>Longline;
cond2 = MACD()>Signal();
cond3 = RSI()>20 AND RSI()<80;
cond4 = Volume > 10000;
cond5 = C>5;

posLong = cond1 AND cond2 AND cond3 AND cond4 AND cond5;
posExitLong = LongLine> ShortLine;
Buy = ExRem(posLong, posExitLong);
Sell=ExRem(posExitLong, posLong);
BuyPrice = C;
longposprice = BuyPrice * posLong;

Filter = 1;
AddColumn(posLong, "Buy Signal", 1.0);
AddColumn(posExitLong, "Exit Long", 1.0);
AddColumn(Buy,"Buy",1.0);
AddColumn(Sell,"Sell",1.0);
AddColumn(BuyPrice,"BuyPrice,1.2");

Plot(shortline, "Short",colorYellow);
Plot(longline, "Long",colorBlue);
PlotShapes(Sell*shapeDownArrow,colorRed);
PlotShapes(Buy*shapeUpArrow,colorGreen);
Plot(posLong,"longPos",colorGreen);
Plot(longPosPrice,"longPosPrice",colorred);

NOTE: Many Conditions into the posLong STATE signal. Using the State signal we generate the Impulse BUY (array). It has just ONE true value for the entire posLong true state.

Again, try the exploration and show all the conditions that you want to combine.

If I am not leading you in the right direction, then you will need to explain in a different way, as I have not caught your meaning.

Try throwing a more detailed code example up with more inline comments.

Here you go

/// @link https://forum.amibroker.com/t/how-to-get-a-position-price-at-each-moment/12209/12
shortLine = EMA(C,5);
longLine = EMA(C,10);

BuyPrice = SellPrice = Close;

Buy = Cross(shortLIne,Longline);
Sell = Cross(LongLine, ShortLine);

InLongTrade = Flip( Buy, Sell );

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

PriceAtBuy = IIf(InLongTrade, ValueWhen(Buy, BuyPrice), 0);

Filter = 1;
AddColumn(IIf(Buy, 1, Null),"Buy",1);
AddColumn(IIf(Sell, 1, Null),"Sell",1);
AddColumn(PriceAtBuy,"PriceAtBuy", 1.2);
AddColumn(InLongTrade, "InLongTrade", 1 );

27

3 Likes

Thank you fxhrat. That is exactly what I need. You ve saved my day.

And, there is other problem with above function LongPosOPened() is that if I use the function like in above code, just to plot Chart, so it is OK.
But (sometimes(?) if I use that function in the code like below, it cause :

Error 6. Condition in IF, WHILE, FOR statements has to be Numeric or Boolean type. You can not use array here...


function longPosOpened() 
	{
	_longPos = (Cum(Buy)-Cum(Sell));
	return IIf(_longPos>0,1,0);
	}

if (LongPosOpened())
   {
   do_something;
   }

Could some one pls explain me?

@tungtien

http://www.amibroker.com/guide/h_understandafl.html

and
http://www.amibroker.com/guide/a_mistakes.html

and

and
https://forum.amibroker.com/search?q=%22error%206%22%20order%3Alatest

If you open up manual and type "Error 6" in search box there then it explains already what it is about (see picture below).
Arrays are not allowed to be used in if-else statements. Arrays may contain multiple values. E.g. Close is a vector of several close prices (see understanding how AFL works above). If as in picture below Close and Open are arrays then Close > Open becomes array of (multiple) true/false elements too (Of course array may consist of just one element but then type is still not number/boolean. It remains array).

err6

1 Like