Hi,
I am using the moving daily average inidicator (with 200 parameter) in an intraday chart https://www.prorealcode.com/prorealtime-indicators/moving-average-daily/ ; it is working perfectly.
The issue: The intraday chart need to load many intraday timeframes in order to calculate de moving daily average 200.
Is it possible to load the necessary bars in the indicator?
I have tried to use DEFPARAM CalculateOnLastBars = 200 but it is not working.
Here is the code:
// Please note if you're using IG there's a smaller number of days you can use and the smaller the timeframe the small number of days you can use
DEFPARAM CalculateOnLastBars = 200
periods = p // days
LMA=0 // Loop MA
FOR i = 0 TO periods-1 DO // Need -1 so the Result is the number of periods we actaully want
LMA = DClose(i) + LMA // Don't think this is possible CustomClose[(i)]
Next
LMAactual = (LMA / (periods))
RETURN LMAactual AS "Daily MA"
Thanks in advance
Remove the calculateonlastbars, otherwise the loop could not retrieve all the necessary bars to complete the daily MA calculation.
Depending of the intraday timeframe you use, it is possible that you need a lot of units displayed for a correct calculation. So just try to expand a lot the displayed units of the chart first.
This is interesting as I was just trying to code a volatility indicator but it kept giving me the too big loop error. Here is the basics of the code idea:
//defparam calculateonlastbars = 5000
x = 157
a = 0
total = 0
while total <= x
total = total + range[a]
a = a + 1
wend
return 0 - a
It simply counts how many bars it takes to reach or exceed a certain value of added together ranges. The more bars it takes the lower volatility is and the fewer bars it takes the higher volatility is. With the CALCULATEONLASTBARS set to 5000 I could not get it work with values for x any greater than 156 on the EURUSD which is about the total of 3 candles on average! If I remove the CALCULATEONLASTBARS then it works just fine even with massive values for x.
Seems a bit buggy as 5000 candles should be plenty to work with when just looking for candle ranges adding up to 157.
The infinite loop problem occurred more often with a WHILE/WEND loop, I think that you can get rid of the error with this version:
x = 157
//a = 0
total = 0
for a=0 to barindex do
total = total + range[a]
if total>x then
break
endif
next
//while total <= x
//total = total + range[a]
//a = a + 1
//wend
return 0 - a
I tried something like that but it still did not work with CALCULATEONLASTBARS.
Thank you very much,
The code is working properly.
The problem is that I need a lot of units displayed in the intraday chart so it takes a bkit to load the chart. I would like to avoid displaying a lot of units in the chart If I can only charge the units in the indicator code.
Thanks in advance
That’s not how it works, the calculation depends of the loaded history/units.
Another way to use other timeframes indicator on the current chart is to use a backtest with the GRAPHONPRICE instruction, look at these examples: MTF for indicators with GRAPHONPRICE
Thank you very much
I have tried to use it but it is giving an error. I added the GRAPHONPRICE in the Return instruction.
periods = p // days
LMA=0 // Loop MA
FOR i = 0 TO periods-1 DO // Need -1 so the Result is the number of periods we actaully want
LMA = DClose(i) + LMA // Don't think this is possible CustomClose[(i)]
Next
LMAactual = (LMA / (periods))
RETURN graphonprice LMA coloured(200,200,0) as "Daily MA"
Please use the ‘Insert PRT Code’ button when posting code in your posts to make it more readable for others. I have tidied up your post for you 🙂
GRAPHONPRICE is an instruction that can only be used in strategy codes and not in indicator codes. However if we code a dummy strategy as per the link Nicolas provided then we can use it to create MTF indicators on price charts. This can overcome the fact the multi time frame is not currently available in indicator coding but still allows us to create MTF indicators on price charts. Just get rid of the positions window and reduce the equity curve to minimum size and we have MTF indicators on price charts. It is just a work around that makes your indicator much easier to code.
An example. Not tested:
timeframe (daily, updateonclose)
daily200ave = average[200](close) // daily 200 average
timeframe(default)
if average[2] = -1 then // dummy strategy
buy at market
endif
graphonprice daily200ave
Run as a strategy not an indicator.
Thank you very much. Sorry, I am a newbie. I’ll use the button insert proreal code.
I tried it in probacktest and it is working perfectly!
Thanks