Search code examples
exponentialespermoving-averagecomplex-event-processing

Calculating Exponential Moving Average using Esper


Looking for a way to calculate the Exponential Moving Average over a window of 5 EMA5 and EMA20 using Esper (EPL) statements.

I have a stream of priceEvent (timeStamp , symbol and price) coming in and I wrote a Simple moving avrage SMA over a sliding window of 5 . But being fairly new to Esper , was looking for a way to calculate Exponential Moving Average (EMA) over sliding window.

http://www.iexplain.org/ema-how-to-calculate/

Also it will be great help if someone can help me in writing parabolic SAR function


Solution

  • Ok Guys, I did more learning and research to get to the solution of finding Exponential Moving Average , below are the EPL statements that will help calculating ema5

    //create a named window EMA5 Window
    create window EMA5Window.win:length(1) as select price as ema5 from Quote
    
    //insert the mean of first 5 events 
    insert into EMA5Window select Avg(price) as ema5 from Quote.win:firstlength(5)
    
    //after 5 events calculate todays ema = (today's price)/3 +  (yesterday's ema)*2/3, refer to http://www.iexplain.org/ema-how-to-calculate/ for ema formula 
    
    insert into EMA5Window select ((price)*(1/3)+(2/3)*(select ema5 from EMA5Window)) as ema5 from Quote output after 5 events
    
    // now select the ema5 as below
    select ema5 as ema5 from EMA5Window output after 5 events
    

    Working on calculating parabolic SAR will update once I am done. Thanks