NexusFi: Find Your Edge


Home Menu

 





Math help needed! Point of intersection of price and fast MA?


Discussion in Platforms and Indicators

Updated
    1. trending_up 5,672 views
    2. thumb_up 3 thanks given
    3. group 3 followers
    1. forum 12 posts
    2. attach_file 0 attachments




 
Search this Thread

Math help needed! Point of intersection of price and fast MA?

  #1 (permalink)
Trader Kevin
Houston, Texas
 
Posts: 13 since Jun 2020
Thanks Given: 3
Thanks Received: 5

I am working on an algorithm that requires that at the open of each 1 minute candle I calculate what price would be required for the price to equal the value of a 5 period EMA. Basically, I need a formula that tells me the point of intersection between the price and a 5 period EMA.

If you watch a 5 period EMA move with price action on a 1 minute chart, you'll note that it moves quite a lot up and down with each tick, so simply using the position of the EMA at the previous candle's close is not an option.

Thank you so much to any math wizard who can help me with this!

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
ZombieSqueeze
Platforms and Indicators
MC PL editor upgrade
MultiCharts
NexusFi Journal Challenge - May 2024
Feedback and Announcements
Cheap historycal L1 data for stocks
Stocks and ETFs
 
  #2 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130


Trader Kevin View Post
I am working on an algorithm that requires that at the open of each 1 minute candle I calculate what price would be required for the price to equal the value of a 5 period EMA. Basically, I need a formula that tells me the point of intersection between the price and a 5 period EMA.

If you watch a 5 period EMA move with price action on a 1 minute chart, you'll note that it moves quite a lot up and down with each tick, so simply using the position of the EMA at the previous candle's close is not an option.

Thank you so much to any math wizard who can help me with this!

You're trying to do what Excel calls a 'Circular Reference.' I said that hoping you'd be familiar with Excel and that term.

You can't know where the EMA will be in advance ... because it moves with price. So you'd have to know where price will be, in advance, to know where EMA will be, in advance.

Also, the final EMA value for each bar is based on the closing bar price. So you'd have to know the closing price of the current bar, in advance, in order to know where the EMA will be.

Reply With Quote
  #3 (permalink)
Trader Kevin
Houston, Texas
 
Posts: 13 since Jun 2020
Thanks Given: 3
Thanks Received: 5


First off, thanks for the reply!

I think that perhaps I didn't explain it in a way you understood correctly. I am not trying to predict where the EMA of a bar will finish before the price has closed, as like you said I'd have to know where the close would be first and that is unpredictable.

What I am trying to do is figure out what the price would have to go to in order to touch the EMA. Whether it does so or not is unpredictable, but what price would be required to go to in order to touch the EMA should be calculable. For example, if we were to use a 500 day EMA, and the value of it on a given day was $100, we would know that price would have to go to basically $100 in order to touch the EMA, since the EMA would move only very, very slightly with each tick, since it is such a large period the EMA references.

Additionally, lets look at the formula for the ema: EMA = Price * (2 / (period + 1)) + EMA(of the previous period) * (1 - (2 / period + 1))
If you know the previous EMA (which I would), the period (I'm using 5), and you put in any price value, you can calculate where the EMA would be if the price got to that point. If the price also closed at that exact point, the EMA would "finalize" at that point for that period.

I hope this makes sense, and again I appreciate any help anyone can provide!

Reply With Quote
  #4 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130


Trader Kevin View Post
First off, thanks for the reply!

I think that perhaps I didn't explain it in a way you understood correctly. I am not trying to predict where the EMA of a bar will finish before the price has closed, as like you said I'd have to know where the close would be first and that is unpredictable.

What I am trying to do is figure out what the price would have to go to in order to touch the EMA. Whether it does so or not is unpredictable, but what price would be required to go to in order to touch the EMA should be calculable. For example, if we were to use a 500 day EMA, and the value of it on a given day was $100, we would know that price would have to go to basically $100 in order to touch the EMA, since the EMA would move only very, very slightly with each tick, since it is such a large period the EMA references.

Additionally, lets look at the formula for the ema: EMA = Price * (2 / (period + 1)) + EMA(of the previous period) * (1 - (2 / period + 1))
If you know the previous EMA (which I would), the period (I'm using 5), and you put in any price value, you can calculate where the EMA would be if the price got to that point. If the price also closed at that exact point, the EMA would "finalize" at that point for that period.

I hope this makes sense, and again I appreciate any help anyone can provide!

Thanks, I think I understand now. On minute (several minutes), I'll see how the math works out ...

Reply With Quote
  #5 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130


Trader Kevin View Post
First off, thanks for the reply!

I think that perhaps I didn't explain it in a way you understood correctly. I am not trying to predict where the EMA of a bar will finish before the price has closed, as like you said I'd have to know where the close would be first and that is unpredictable.

What I am trying to do is figure out what the price would have to go to in order to touch the EMA. Whether it does so or not is unpredictable, but what price would be required to go to in order to touch the EMA should be calculable. For example, if we were to use a 500 day EMA, and the value of it on a given day was $100, we would know that price would have to go to basically $100 in order to touch the EMA, since the EMA would move only very, very slightly with each tick, since it is such a large period the EMA references.

Additionally, lets look at the formula for the ema: EMA = Price * (2 / (period + 1)) + EMA(of the previous period) * (1 - (2 / period + 1))
If you know the previous EMA (which I would), the period (I'm using 5), and you put in any price value, you can calculate where the EMA would be if the price got to that point. If the price also closed at that exact point, the EMA would "finalize" at that point for that period.

I hope this makes sense, and again I appreciate any help anyone can provide!

 
Code
EMA = Price * (2 / (period + 1)) + EMA(of the previous period) * (1 - (2 / period + 1))
ema=p * (2 / 6) + pema * (1 - (2 / 6))
ema=p * (1/3) + pema * (2/3)

First equation just simplified. Second equation/requirement:

ema=p

We now have two unknowns, and two equations. We should be able to now solve:

Substituting, since ema=p:

p=p * (1/3) + pema * (2/3)

times 3

3p=p+2pema

subtract p from both sides

2p=2pema

divide by 2

p=pema

As it should be: The price where they touch is the EMA of the previous bar.

Reply With Quote
Thanked by:
  #6 (permalink)
Trader Kevin
Houston, Texas
 
Posts: 13 since Jun 2020
Thanks Given: 3
Thanks Received: 5

Excellent work! Thanks for the help. It makes so much sense now that you lay it out.

Since you did so well with that, can you help me with a slightly more difficult one that is similar?

I need to also find the point at which the price would intersect the upper and lower bands of a Keltner Channel. The channel uses a 5 period EMA as before as the middle band, and the distance between the middle EMA and the outer bands is equal to the ATR * KF (Keltner Factor). The ATR is a standard configuration using a Wilders MA, and borrows the length of the MA from the Keltner Channel (5 periods). It seems I can't just add the ATR * KF to the previous EMA in this case.

Thank you again for the help!

Reply With Quote
  #7 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130


Trader Kevin View Post
Excellent work! Thanks for the help. It makes so much sense now that you lay it out.

Since you did so well with that, can you help me with a slightly more difficult one that is similar?

I need to also find the point at which the price would intersect the upper and lower bands of a Keltner Channel. The channel uses a 5 period EMA as before as the middle band, and the distance between the middle EMA and the outer bands is equal to the ATR * KF (Keltner Factor). The ATR is a standard configuration using a Wilders MA, and borrows the length of the MA from the Keltner Channel (5 periods). It seems I can't just add the ATR * KF to the previous EMA in this case.

Thank you again for the help!

You're welcome.

Would you be able to provide all the relevant formulas, like you did for previous one (EMA)?

Reply With Quote
  #8 (permalink)
Trader Kevin
Houston, Texas
 
Posts: 13 since Jun 2020
Thanks Given: 3
Thanks Received: 5


userque View Post
You're welcome.

Would you be able to provide all the relevant formulas, like you did for previous one (EMA)?

Of course. The formula for the middle band is the same EMA formula as before. The formula for the upper band of the Keltner Channel is : KC(upper band) = EMA + (ATR * KF) where KF is a constant that I will provide. ATR = max(high, close(previous)) - min (low, close(previous)). In word form, it is the largest of the: current high - current low, absolute value of the current high - previous close, or absolute value of the current low minus previous close.

Formula for the lower band is KC(lower band) = EMA - (ATR * KF)

I should also explain that I am not looking for the price where the price = the upper/lower bands at the previous 1 minute candle's close, but where they would meet in real time. In other words, what would price have to equal in order for it to close at the same price as the upper/lower bands?

Thanks!

Reply With Quote
  #9 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130


Trader Kevin View Post
Of course. The formula for the middle band is the same EMA formula as before. The formula for the upper band of the Keltner Channel is : KC(upper band) = EMA + (ATR * KF) where KF is a constant that I will provide. ATR = max(high, close(previous)) - min (low, close(previous)). In word form, it is the largest of the: current high - current low, absolute value of the current high - previous close, or absolute value of the current low minus previous close.

Formula for the lower band is KC(lower band) = EMA - (ATR * KF)

I should also explain that I am not looking for the price where the price = the upper/lower bands at the previous 1 minute candle's close, but where they would meet in real time. In other words, what would price have to equal in order for it to close at the same price as the upper/lower bands?

Thanks!

No problem. I'll take a look at this tonight/early morning.

Reply With Quote
  #10 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130



Trader Kevin View Post
Of course. The formula for the middle band is the same EMA formula as before. The formula for the upper band of the Keltner Channel is : KC(upper band) = EMA + (ATR * KF) where KF is a constant that I will provide. ATR = max(high, close(previous)) - min (low, close(previous)). In word form, it is the largest of the: current high - current low, absolute value of the current high - previous close, or absolute value of the current low minus previous close.

Formula for the lower band is KC(lower band) = EMA - (ATR * KF)

I should also explain that I am not looking for the price where the price = the upper/lower bands at the previous 1 minute candle's close, but where they would meet in real time. In other words, what would price have to equal in order for it to close at the same price as the upper/lower bands?

Thanks!

Now working on it. Give me several minutes.

Reply With Quote




Last Updated on August 7, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts