Any way to plot a time based moving average over a tick chart ?
Forums › ProRealTime English forum › ProBuilder support › Any way to plot a time based moving average over a tick chart ?
- This topic has 10 replies, 5 voices, and was last updated 2 months ago by druby.
-
-
03/27/2024 at 3:45 AM #230534
Hi everyone,
I was wondering if there is any simple way to plot a time based indicator over a tick chart.
For example, normally if I plot a 100 period SMA (simple moving average) on a tick chart, it will obviously average the last 100 ticks, for each tick.
I would like to be able to plot, for example, a 10 minutes period SMA over a tick chart, this means the SMA will average the last X ticks that were generated during the 10 last minutes, for each ticks.
Maybe using arrays it could be feasible though I’m not sure how to implement it yet.
(Also maybe there are native methods in ProRealtime that would allow to do that and I am not aware of it )
Thank you very much !
03/27/2024 at 1:40 PM #230569No, different kinds of timeframes cannot be mixed, such as time-based and ticks.
You can only use MTF with the same type. You may use a default 10-tick TF to plot a 100-tick SMA as their types match.
03/27/2024 at 3:50 PM #230589No, different kinds of timeframes cannot be mixed, such as time-based and ticks.
You can only use MTF with the same type. You may use a default 10-tick TF to plot a 100-tick SMA as their types match.
I see, it is a pity as it is a feature available on some other platforms.
So, the only way would be to store tick close in an array and corresponding time in a different array, with same indexes, and then loop through it to calculate the averages..
Or does anyone thinks there can be a simpler way ?
Thank you.
03/27/2024 at 6:19 PM #230600Hello 0309 san !
I think your idea is good. But I would attempt it without arrays. In any event, please keep in mind that you won’t be able to AutoTrade with this. In other words : AutoTrading will require a normal TimeFrame and although you can show tick data in an indicator, you won’t be able to store that data and use it. At least that is what I think.
But for normal visualization of what you ask, this could be the pseudo code, not using arrays :12345678910111213Add tick price to TickPriceIncrease TickCountYourTimeBar = TimeBarPeriod // e.g. 10 minutes.If YourTimeBar <> NewTimeBar then // If TimeBar is e.g. 1 minute this is easy, like you can use the Minute constant (if Minute <> Minute[1]).CalculateAverage from TickData with the help of TickCountTickPrice = 0 // Reset.TickCount = 0 // Reset.YourTimeBar = NewTimeBar // Reset.endifGraph the CalculatedAverageI hope you get the idea ?
Do keep in mind that the code will be called only when a tick (a trade) occurred. So it will need extra attention to properly calculate the average within the time periods; No ticks during the YourTimeBar periods may imply a gap of several (virtual time) bars and the average should stay in-tact. The price won’t be zero during that time, but merely the same as the last one used in your previous average calculation.
I would imagine that others have done such a thing before; hopefully they read this and provide some ready code for you. It won’t be really easy to create, I think.
Regards,
Peter03/28/2024 at 3:39 AM #230620Thank you very much for taking the time to reply.
Yes, I just need it as a visual indicator, I won’t use AutoTrade or anything automatic.
I managed to get somehow desired result using arrays, but because of the looping the loading is a bit slow, and if I want to use several averages on same chart, it might be too heavy.
I am sorry but I am not sure I understand the logic in the “reasoning code” you suggested me, if you may be able to explain it differently ? As it might indeed be less heavy for calculation.
For the info, I want to use it on 1 tick chart, so I wont use it on 100 ticks candles charts for example. 1 tick = 1 bar
Thank you for your help !
03/28/2024 at 5:17 AM #230621Each bar your program is called. So in your case, each tick the program is called.
At each call you must determine whether the time period of your (virtual) time bar has changed. Take one minute for each thinking. So each call you determine12345678If Minute <> PreviousMinute then[...] // Your logic to add to the averagePreviousMinute = Minuteendif[...] // Add to the variable which holds the price for this tick.Return YourAverageSo you are building up the average in real time.
You can of course start with a 1 minute TF chart and do exactly the same, and compare with a normal Average function from PRT to see whether you do it correctly. If it works, then you can change the chart to the TF of 1 Tick and see whether it still works. If not, you should be able to see relatively to the working situation what to change.Your prospect is that it will be fast as lightning.
You could consider why you actually want to start off with a 1 tick chart. Thus, why not 1 second, for example ? Then you would have no problems at all.
Thinking twice myself, I am not really able to envision how your 1 tick chart will look like when a time-period average would show; it would need to stretch the ticks into a time domain and that is not even possible. Anyway, you say you already have something which works. I am curious how that looks like ?03/28/2024 at 6:03 AM #230623Thank you again for your reply.
So, maybe it seems there is a misunderstanding, or I am not understanding what you mean.
At each call you must determine whether the time period of your (virtual) time bar has changed.
I would have the impression that the virtual time bar changes on every tick ? It might be because I am not too used to prorealtime coding yet but I don’t understand on the logic you gave me, how it is able to calculate the number of ticks necessary to get the desired average.
For example, if I want to plot a 30min average on my tick chart, the average of X ticks that occurred during the last 30 minutes is calculated, every new ticks. Sometimes it will be the average of the last 2000 ticks, sometimes 1000.
To be sure we are on the same reasoning : if on the last tick the timestamp was 14h:05min:35sec , I will need to calculate how many ticks there were between 13h:35min:36sec and 14h:05min:35sec, and then average their close.
You can see on my screenshot in attachment an example of the 30 min average on the tick chart. The bottom indicator is to show the number of ticks that occurred during the last 30 minutes, which changes more or less every tick.
Anyway, you say you already have something which works. I am curious how that looks like ?
04/03/2024 at 1:40 PM #230986I would like to be able to plot, for example, a 10 minutes period SMA over a tick chart, this means the SMA will average the last X ticks that were generated during the 10 last minutes, for each ticks.
Hi,
if I understand correctly your request and subsequent posts, I would respectfully disagree with your assessment to average last X ticks that were generated during the 10 last minutes.
What your classic 10mn timeframe SMA with period N is made of, is :
- if for a candle in the past: the sum of latest N 10mn close divided by N. It would therefore look like stairway flat steps with vertical height change at beginning of each new 10mn slot, although arguably assigning to a 10mn slice the value of such step only reached at the end would be some kind of minor repainting (using a future value not known yet in real time to draw a past situation). To avoid repainting it could be plotted « one step late », by this I mean when the close of 10mn is known, draw the SMA value reached as a flat step on the « next » 10mn slice, but graphically not pleasant and not fun for live
- if for a live candle: ( the sum of latest N-1 10mn close + current price ) divided by N. Such latest step wouldn’t be fully flat then, it would be variable until the last tick of the 10mn slot, kept as representative of those 10mn, and a vertical shift occurs at next live first tick of the newest 10mn slot
What you can do, and still call it a classic 10mn timeframe plotted over a tick by tick chart, is to emulate in the past what would have been the live behaviour of the 10mn SMA at each tick. This doesn’t repaint anything, and it is not one 10mn slice late either, as the last tick of each 10mn slot is the final representative of its 10mn in the SMA, and all ticks prior to it in same 10mn slice lead to the “temporary” value the SMA would have had live at such intermediate tick.
If such « stairs with non-flat steps » look is what you want, then the following code for TF10mn should do it, you can customise the period of the SMA with variable N (here N=50 just as an example), and the bigger the N you choose, the more you need to make sure you have loaded enough history to be able to start displaying the SmaN on your tick by tick chart:
1234567891011121314once count=0once N=50once $cumu1[0]=0jcsplit10= floor(openminute/10) // to use for TF 10mnif barindex>0 and (jcsplit10<>jcsplit10[1] or openhour<>openhour[1]) then//éopenhour<>openhour[1]" added in the unlikely but not impossible case of 2 ovn ticks of same 10-minute slice but not same hourcount=count+1$cumu1[count]=$cumu1[count-1]+close[1]endifif count>=N-1 thensmaN=($cumu1[count]-$cumu1[count-N+1]+close)/Nendifreturn smaN as "smaN"Don’t get me wrong, you are of course very free to want each 10mn be represented in the tick by tick chart by the average of all ticks for each 10mn instead of its 10mn close (a little bit like when choosing medianprice or totalprice instead of close)… then it would be coded differently, and be named differently than “a classic 10mn timeframe SMA” because it would never have a classic timeframe 10mn chart equivalent as such chart only has 10mn OHLC data in its history -Open High Low Close- , not the past ticks inside history candles (would only have the live ticks occuring post-launch of indicator).
09/28/2024 at 2:38 PM #238195I want to thank the previous members who tried to help in my request, though I did not manage to find a better solution than using the arrays and looping through it, which is too heavy computation to use on 1 tick chart.
So, I will ask it in a different way, and focus on the number of ticks first :
Is there any better way in prorealtime to know how many ticks there were during the last 60 seconds or the last 10 000 seconds for example, on a tick chart (1 tick chart ) ?
(With currency pairs we have the Tick Volume which I think is reflecting that number of ticks for each bar, and updates on every ticks)
09/28/2024 at 4:53 PM #238211I finally managed to get the tick count and as a consequence I was able to calculate the time based moving average on 1-tick chart, without using arrays, making it way quicker to load. ( Thank you Claude AI for the help ! )
I will share the code here if some people are interesting in it.
09/29/2024 at 9:01 AM #238227Hello, Please post code, I started looking at the problem, in theory how it could be done.
The difference between a time based chart and the tick chart is the timebase.
So gathering up the ticks in each time period ‘bar’ chosen would be one way to go.
If this was done going forward from when bar=0, that would be fast, as the chart has to do that when building, may take a bit extra time initially dependant on units chosen.
At the same time, do a cumulative count of the ticks over the relative time period bars that were being covered as you go.
When enough time period bars reached to be able to calculate the sum for the average , then change technique
Going forward from there, to maintain the sum value, is to add the new time period bars number of ticks and subtract the first time period bars number of ticks.
Adding a value and subtracting a value would be a lot faster than looping through all the values every time one is added.
From additional needed variables point of view, the tick count per period bar and a way to index the bar, keep track of the 1st bar of the summation range.
Other things that came to mind are:
If no ticks fall in a time period bar, how is that treated. In a timebased chart these I believe are ignored, and are not treated as bars.
How would the average line be plotted, from the period bars, or the ticks within that range.
Any way that’s as far as I got with it, apart from the test code to count the ticks every 60seconds.
Regards
1234567891011121314once count=0if openminute[1] <> openminute thencount = 0endifcount = count +1if count = 1 thendrawtext(count[1],barindex[1] ,count[1]+5)endifreturn count -
AuthorPosts
Find exclusive trading pro-tools on