I have been searching for a long time for the code of TREMA in various languages. I tried using the code in the library. TRSMA seems to work correctly, but TREMA didn’t. I am trying to obtain the SPX price during Regular Trading Hours (RTH) as displayed on SPTRD SP500 charts. In Pine Script, it was easy to extract the EMA from another ticker. Could someone provide a hint on how to correctly implement this?
// TREMA
//
// Time Range Exponential Moving Average
//
DEFPARAM CalculateOnLastBars = 20000
Periods = 200
StartTime = 153000
EndTime = 220000
//
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"
Calculations are made accord to the time range you selected.
What do you think is not working?
Just as I have attached two photos, you can see the difference. Essentially, if the TREMA only reacts within the time range that I have chosen, it should not change outside of this time range, right?”
It changes because you set TREMA=close at line 13, even outside the desired time window.
thanks a lot. without this line looks better. BUT, the ema is still not right… Please check the screenshot. the price was rising but ema not.
An 800-period EMA will take 800 of the selected bars within the desired time range to be aware of that rise.