Hello I’m looking to code a daily moving average(7) on my intraday chart. I know there has been already topics on that in the past, but the particularity here is that I would like to take the closing price of a particular time, ie 17h35 for the Dax for example, corresponding to the cash close. Any idea how to navigate with the past dates, and avoid the problem with the weekend, the new month etc??
So you want a seven period average of the closing price at 1735?
You could just store the last seven values at close of 1735 candle. Each time there is a new one drop one of the old ones and record the new one.
Another alternative is to code a dummy MTF strategy and then use GRAPHONPRICE to draw it on your intraday chart.
I’m not sure what weekend or change of month issues you see happening?
Here is the first idea:
if time = 173500 then
a = b
b = c
c = d
d = e
e = f
f = g
g = close
endif
if a <> 0 then
ave = (a + b + c + d + e + f + g) / 7
else
ave = undefined
endif
return ave
“So you want a seven period average of the closing price at 1735?”
Yes
seems simple but I have some difficulties in my head to work with dates.
If you compare Date in YYYYMMDD, then rolling month complicates a bit the calculation
seems simple but I have some difficulties in my head to work with dates. If you compare Date in YYYYMMDD, then rolling month complicates a bit the calculation
I don’t understand. What do dates and months have to do with it? There is only one 1735 everyday unless you have a time machine!?
Sorry I made my comment before seeing your code idea. I didn’t know that it was that easy and dates weren’t needed.
Idea is good, even though what is missing is to make it more dynamic by taking the value of today into consideration. So the 6 last closes at 17h35, and the seventh value is the most current one
So now I have a bit modified your code, I need to check if this is correct
if time = 173500 and (currenttime<090000 or currenttime>173500) then
a = b
b = c
c = d
d = e
e = f
f = g
g= close
endif
if time = 173500 and (currenttime>=090000 and currenttime< 173500) then
a = b
b = c
c = d
d = e
e = f
f = close
endif
if (currenttime>=090000 and currenttime< 173500) then
g= close
endif
if a <> 0 then
ave = (a + b + c + d + e + f + g) / 7
else
ave = undefined
endif
return ave
Your conditions in lines 1 and 7 can never be met.
Maybe you mean something like this?
if time = 173500 then
a = b
b = c
c = d
d = e
e = f
f = close
endif
if a <> 0 then
ave = (a + b + c + d + e + f + close) / 7
else
ave = undefined
endif
return ave
Indeed;)
the price at 17h35 is the close price of the 17h30 candle on a 5min chart actually, so 17300 it should be for those who wish to use this little code snippet for scalping.
Yes OPENTIME is the time a candle opens and TIME is the time it closes. So if you want the exact price at 1735 then you can use OPEN and OPENTIME or use CLOSE and TIME. Both should return the same value unless there is a gap in price between CLOSE and OPEN.
In a strategy TIME is used more often as decisions are made at the candle close and trades opened at the next candle open. In indicators generally OPENTIME is of more use as on the fly analysis of a candle is carried out from the moment it opens.
I still find that sometimes I have to just put the following indicator on a chart to clarify things in my head when coding stuff:
return time as "time", opentime as "opentime"