Calculate price with Valuewhen


I'm trying to calculate the price with certain MACD condition. This doesn't seem to return the value I want to find, so I appreciate any help. Thank you in advance!

BP = ValueWhen(Cross(MACD(5,10),Signal(5,10,3)), BuyPrice, 1);
SP = ValueWhen(Cross(Signal(5,10,3),MACD(5,10)), SellPrice, 1);

What value do you want to find?

The below will retrieve the relevant closing prices.

BP = ValueWhen(Cross(MACD(5,10),Signal(5,10,3)), Close, 1);
SP = ValueWhen(Cross(Signal(5,10,3),MACD(5,10)), Close, 1);

I want to find the price value at which MACD signals cross each other

What value though? Open High Low Close???

I want to calculate the price value that results in crossing over in MACD, not the real time data

What price value? LastValue will return the parameter price for the relevant time-frame.

@skymage142 the thing is that you do not explain what you are really after. The code you posted first (and other variant given by @TrendSurfer) return just the value of particular array (say Close) at the bar when given condition (cross) happened (see for more info).

In the second post you change the description to

which is actually totally different thing. MACD can cross in some price between "previous" and "current" bar. See Detect when moving averages are about to cross and Predicting future indicator values without having inverse function and Cross Function is NOT off by 1 bar for already existing discussion about that.

@skymage142 I think you are asking how to predict a price in advance, at which the MACD crossover will occur?

If so, read the articles in Technical Analysis of Stocks and Commodities.

"Reversing MACD" January 2012
"Reversing the MACD: The Sequel" November 2013

There are articles in which the author "Johnny Dough" (in previous correspondence he assures me that is not his real name) discusses how to do this and luckily he is an AmiBroker user and includes afl codes. In fact the Jan 2012 article code is one of the "Trader Tips". Good luck.

@portfoliobuilder I guess I wasn't very clear at first, it's true that I want to find the price point in the future where crossover happens. Thank you for the suggestion, I already found the code for it and testing at the moment.


1 Like

If you are not looking for predictive value but simply the value of the MACD(5,10).
BP = ValueWhen(Cross(MACD(5,10),Signal(5,10,3)), MACD(5,10), 1);