We are looking for a price at which a moving average and a candle bar has intersected, currently it gives us close price of the candle bar, but we need the price at which the intersection happened. This help us to calculate our stop loss precisely.
We are able to enter the trade at intersection of candle bar and moving average, but we are not able to calculate stop loss until the bar close.
Let say I need to add 20 point stop loss from my entry price, here entry is happening at an intersection of candle bar and a moving average, but not able to fetch intersection price instead getting price at candle close.
Yes correct, but the issue here is our trade entry doesn't wait for candle completion and moving average keeps changing until candle closes. The final price it provides is candle completion one and we are looking for the price at the first instance of the intersection irrespective of candle completion.
Moving average will change because on a given candle price won't be constant until it closes.
You need to preserve the state at the time of first Cross and store the MA value.
Unable to suggest anything syntactic, as your question does not clarify all aspect of your logic(s). For instance, what happens when your trade closes on same bar and/or price crosses the MA from both sides multiple times in same bar.
In both scenarios, one critical assumption is made!
However, OP wants exact value of MA when Price crosses it real-time for the first time inside a bar, such that he could:
Some time back I solved a similar problem (slightly varied) as part of a project but @ysharmaa is not sharing any detail of his logic. By far, neither has he shown any effort from his end.
I specifically asked him:
He replied that its all taken care from:
Without logic specifics (keeping all aspects in mind), a precise solution is hard to draw. I doubt OP understands any of this, so let us allow some more absorption time for him to revert. And yes he ignored reading (again):