Thanks for the help. I am doing fine. I was doing my postgraduation and then job and family – no time to play around with computer. Hope you, your friends and family are doing good.
As you said, I stopped using Query1 inside the QueryApplyMulti and instead stored the previousEMA in the variable and the problem is corrected. The prevEMA for the first record was to be calculated in a different way and it could be done outside the loop.
Now the code is
firstrec = QueryFirst(stockname $ ' where date >= ' $ Display(from ) $ ' and date <= ' $ Display(till) $ ' sort date ')
prevEMA = QueryMean(stockname $ ' where serialno > '$ Display((firstrec.serialno - period)-1) $ ' and serialno <= ' $ Display(firstrec.serialno-1), price)
QueryApply(stockname $ ' where date >= ' $ Display(from ) $ ' sort date ', update:true)
if (QueryCount(stockname $ ' where serialno <=' $ Display(rec.serialno)) > (period + 1))
ema = (((rec[price] - prevEMA) * multiplier) + prevEMA).Round(2)
ema = ""
Print(ema, rec_date: rec.date,rec_price: rec[price],prev_EMA: prevEMA,difference: (rec[price] - prevEMA))
rec[output] = ema
return QueryLast(stockname $ ' where date >= ' $ Display(from ) $ ' sort date').ema
Exponential Moving AVerages are calculated by giving different weightage to different member values. In case of the stock prices, that I am dealing with now, the price for the most recent date is given more weightage. The weightage factor calculated from the period that user supplies is multiplied [for a 5 period ema, the weightage would be 5/(1+2+3+4+5)] by the most recent price and 1-weightage is multiplied with the previous EMA. For the first record, there is no previous EMA and hence a simple average is calculated and is used in place of previous EMA. I was not sure how to deal with these calculations and so I had posted another thread on the forum – “Accessing previous record from current record”. Hope you will guide me on that also.
Thanks once again,
BTW, hello to ferguson and others at Suneido!