Following a request by user discovery2005 (https://www.prorealcode.com/topic/media-mobile-a-tempo/) on the italiam forum, I coded these 3 MA’s to limit calculations to the desired time range, without being affected by data outside that same range.
// TRSMA
//
// Time Range Simple Moving Average
//
DEFPARAM CalculateOnLastBars = 200
//Periods = 20
//StartTime = 090000
//EndTime = 180000
Periods = max(2,min(999,Periods))
StartTime = max(0,min(240000,StartTime))
EndTime = max(0,min(240000,EndTime))
//
i = 0
TRSMA = 0
FOR j = 0 TO 3000
IF opentime[j] >= StartTime AND opentime[j] <= EndTime THEN
TRSMA = TRSMA + close[j]
i = i + 1
IF i = Periods THEN
BREAK
ENDIF
ENDIF
NEXT
TRSMA = (TRSMA / Periods)
IF TRSMA = 0 THEN
TRSMA = close
ENDIF
Return TRSMA AS "TRSMA"
// TREMA
//
// Time Range Exponential Moving Average
//
DEFPARAM CalculateOnLastBars = 200
//Periods = 20
//StartTime = 090000
//EndTime = 175000
Periods = max(2,min(999,Periods))
StartTime = max(0,min(240000,StartTime))
EndTime = max(0,min(240000,EndTime))
//
Alpha = 2 / (Periods + 1)
i = 0
TREMA = close
FOR j = 0 TO 3000
IF opentime[j] >= StartTime AND opentime[j] <= EndTime THEN
i = i + 1
TREMA = ((close[j] - TREMA) * Alpha) + TREMA
IF i = Periods THEN
BREAK
ENDIF
ENDIF
NEXT
IF TREMA = 0 THEN
TREMA = close
ENDIF
Return TREMA AS "TREMA"
// TRWMA
//
// Time Range Weighted Moving Average
//
DEFPARAM CalculateOnLastBars = 200
//Periods = 20
//StartTime = 090000
//EndTime = 175000
Periods = max(2,min(999,Periods))
StartTime = max(0,min(240000,StartTime))
EndTime = max(0,min(240000,EndTime))
//
i = 0
Pond = 0
TRWMA = 0
FOR j = 0 TO 3000
IF opentime[j] >= StartTime AND opentime[j] <= EndTime THEN
i = i + 1
TRWMA = TRWMA + (close[j] * i)
Pond = Pond + i
IF i = Periods THEN
BREAK
ENDIF
ENDIF
NEXT
TRWMA = (TRWMA / Pond)
IF TRWMA = 0 THEN
TRWMA = close
ENDIF
Return TRWMA AS "TRWMA"
You must be logged in to post a comment.
It surely CHANGES even when currently outside time range, as the lookback periods change as well, thus making past calculations different each time.
Thanks Roberto, but shouldnt the values in the outside time range not be different? As it should be the same at whatever the values were at "EndTime" until the next "Starttime" of which then the values calculate on the Bar[1] values which are whatever yesterdays Endtime values were, right? The time outside time range should be a constant = EndTime values ? The lookback period shouldnt be changing outside the time neither, as say Bar[1] at any time between opentime>Endtime and opentime<StartTime should be the EndTime's bar, and thus a lookback period of [14] at any time outside of the market should be the same lookback period of 14 bars before the EndTime ? Or have i confused myself on using this to calculate on only trading time range bars?
Does the EMA calculation have an issue? I can see that on the chart it keeps moving in the non-trade hours range: instead of a straight line between close of yesterday and open of today. Would it more correct as such to change the 'Line 15' ? To something like: IF (opentime >= StartTime and opentime<=EndTime) THEN TREMA = close endif ?
Hi Jan and Roberto, I agree, if you adapt properly the Trading Hours in Plateforme Settings, you are not supposed to calculate Indicators "manually" (I mean using coding). But, how would you do if, for example, you want to calculate the average volume intraday, updated at the end of each 5 min candle? the SMA[n](Volume) doesn't do the job and you need to update the "n" manually if you're scalping. Besides, if you look at IG Markets DAX CFD on 24th Dec. 2020, on a 5 min TF, you can see price changes but Volume always 0, so if you want the SMA[20](Volume) on a 5 min TF, you will get one day of zero volumes included in the SMA. (For some reason, I cannot add a graph with posting a reply to an exsiting post)
Results, while matching when it's SMA compared to TRSMA, are different in the case of WMA and EMA even when using the same custom hours. So, since it was requested on the forum, this must be the reason of its possible use.
Good evening Roberto, I do not understand Italian, so I could not read the background. Why coding the EMA's for a certain time within a day, as you can also adjust the graph for trading hours at Options --> Platform Options at Trading Hours ? I would think it slows down the rebuilding of a graph when you would have more then 1.000 bars KR Jan
Actually it is a constant (I tested them on a 1-hour TF, 9am to 5pm) and outside that time range it's flat.