How Do I Detect How Many Times a Moving Average Has Been Touched?

Created at 05 Apr 2023, 16:35
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CT

ctid5083541

Joined 25.05.2022

How Do I Detect How Many Times a Moving Average Has Been Touched?
05 Apr 2023, 16:35


I want to know how many times a moving average has been touched whilst above/below/breakout (can be open/high/low/close) or bounced off of it and even a false breakout of it.

Any ideas would be greatly appreciated.

The code is quite long but I could cut it down and post the bare bones of it if necessary.

Kind regards.


@ctid5083541
Replies

firemyst
06 Apr 2023, 08:23

Are you wanting to check for each "tick", or only the previous bar?

It sounds like you're wanting to check each tick, but am not 100% sure.

If you want to check the previous bar when a new bar is formed, if you are on Renko charts it'll be a bit more complex than any of the other kinds of charts since cTrader doesn't keep track of Renko "tails" for bar related data.

Otherwise, pseudo business logic:

1) get current bar's open

2) check if open is initially above/below the MA

3) if open was below MA, then on each tick:

     a) get the MA's current value

     b) get the Symbol Ask/Bid/Closing price (whatever you want to consider has touched the MA)

    c) check if the above price is >= MA current value

4) if open was above MA, then:

    a) repeat steps 3a/3b

    b) check if the price is <= MA current value

 

Hope that helps and is what you're after?


@firemyst

ctid5083541
06 Apr 2023, 15:03

RE:

Thanks for your input firemyst. I'm only interested in bars, ticks and renko are unlikely to be necessary. I only want to know if the moving average has been tested or a false breakout has recently occurred.

Kind regards.

 

firemyst said:

Are you wanting to check for each "tick", or only the previous bar?

It sounds like you're wanting to check each tick, but am not 100% sure.

If you want to check the previous bar when a new bar is formed, if you are on Renko charts it'll be a bit more complex than any of the other kinds of charts since cTrader doesn't keep track of Renko "tails" for bar related data.

Otherwise, pseudo business logic:

1) get current bar's open

2) check if open is initially above/below the MA

3) if open was below MA, then on each tick:

     a) get the MA's current value

     b) get the Symbol Ask/Bid/Closing price (whatever you want to consider has touched the MA)

    c) check if the above price is >= MA current value

4) if open was above MA, then:

    a) repeat steps 3a/3b

    b) check if the price is <= MA current value

 

Hope that helps and is what you're after?

 


@ctid5083541