How to calculate perfect SMA?

chandrak
Hi Team,

I am using historic API for calculation of SMA. I am using following code for the same in C#. Many stocks do not match SMA being displayed on Kite platform. Can you please help? it is extremely urgent please.

DateTime dtFrom = DateTime.Now;
dtFrom=dtFrom.AddDays(-50);
TimeSpan ts = new TimeSpan(9, 0, 0);
dtFrom = dtFrom.Date + ts;

DateTime dtTo = dtFrom.AddDays(50); ;
ts = new TimeSpan(15, 30, 0);
dtTo = dtTo.Date + ts;

foreach (KeyValuePair stock in Globals.Stocks)
{
List historical = Globals.kite.GetHistoricalData(
InstrumentToken: stock.Value.INSTRUMENT_TOKEN,
FromDate: dtFrom, // 2016-01-01 12:50:00 AM
ToDate: dtTo, // 2016-01-01 01:10:00 PM
Interval: Constants.INTERVAL_DAY,
Continuous: false
);
double closePrice = 0;
for (int count = 0; count < historical.Count; count++)
closePrice += (double)historical[count].Close;

stock.Value.FIFTYDMA = closePrice / historical.Count;
  • chandrak
    could figure out the issue.. was the duration calculation. BUT now the 50 SMA is coming exactly as per the NSE and Zerodha But using the same duration 5 mins SMA is still off. (INTERVAL_5MINUTES).
  • chandrak
    so my question is that what is the formula to calculate 50 day simple moving avg over 5 minute candles?
Sign In or Register to comment.