# Price at which a candle bar and a moving average intersect

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.

`price at which a moving average and a candle bar has intersected` = moving average

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.

Yes exactly, If you would be kind enough to tell me how should I do that.
I have handled all of those scenarios, once entry occurs exit logics will start working from next candle.

I'm not a mathematician, and there must be a simpler way to solve the problem without using a loop, but if you are using a simple average you might solve your problem with the afl below

``````MAPeriod = Param("MA Period", 20, 3, 30);

AvgSum = Sum(Close, MAPeriod - 1);
AvgSum = Ref(AvgSum, -1);
MAIntersect = (AvgSum + Ref(Close, -1)) / MAPeriod;

Plot( C, "Close", colorBlack, styleNoTitle | styleCandle );

if( Status("action") == actionIndicator ) {
bi = BarIndex();
fBar = FirstVisibleValue(bi);
lBar = LastVisibleValue(bi) + 1;
} else {
fBar = 0;
lBar = BarCount;
}

for( i = fBar; i < lBar; i++)
{
CurrentLast = Open[i];
LastToIntersect = MAIntersect[i];

if( CurrentLast < LastToIntersect) {
j = 0;
do {
CurrentLast = LastToIntersect;
LastToIntersect = (AvgSum[i] + CurrentLast) / MAPeriod[i];
LastToIntersect = TickSize * round(LastToIntersect / TickSize);
j++;
} while( CurrentLast < LastToIntersect AND j < 50 );
if( j >= 50 )
Error("error CurrentLast < LastToIntersect if( j >= 50 )");
else
MAIntersect[i] = CurrentLast;
} else if( CurrentLast > LastToIntersect) {
j = 0;
do {
CurrentLast = LastToIntersect;
LastToIntersect = (AvgSum[i] + CurrentLast) / MAPeriod[i];
LastToIntersect = TickSize * round(LastToIntersect / TickSize);
j++;
} while( CurrentLast > LastToIntersect AND j < 50 );
if( j >= 50 )
Error("error CurrentLast > LastToIntersect if( j >= 50 )");
else
MAIntersect[i] = CurrentLast;
}
}

Plot(MAIntersect, "MAIntersect", colorRed);
``````

There is general very easy solution already posted on this forum:

IMPORTANT!!! TickSize must be correctly assigned to the symbol, if you use the afl I posted

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.