Trouble using server-side functions in API

Hi,

I’m having trouble correctly using server-side functions AVG and SUM.

I would like to iterate through dates and get the average price for a preceding number of trading days. The API returns results to me that are not what they should be. Removing the AVG function and just requesting the preceding three days of prices for each date, I i can see it's giving inconsistent results.

My ultimate goal is a dataframe showing a RIC's average price and closing price over several dates, with the average reflecting a set number of preceding trading days, not calendar days.

Can you please tell me what I’m doing wrong?

test_dates = ['2023-06-05', '2023-06-06', '2023-06-07', '2023-06-08']

for d in test_dates:
date = d
print(date)

result, err = ek.get_data('AAPL.O',
[
# f"AVG(TR.PriceClose(SDate={str(date)}, EDate=-2, Frq=D))",
f"TR.PriceClose(SDate={date}, EDate=-4, Frq=D)"
])

print(result)
print('')


2023-06-05
Instrument Price Close
0 AAPL.O +179.58
1 AAPL.O +179.21
2 AAPL.O +177.82

2023-06-06
Instrument Price Close
0 AAPL.O +179.21
1 AAPL.O +177.82

2023-06-07
Instrument Price Close
0 AAPL.O +177.82

2023-06-08
Instrument Price Close
0 AAPL.O +180.57
1 AAPL.O +177.82

Best Answer

  • m.bunkowski
    Answer ✓

    Hi @noel.randewich01,

    Please try:

    f"TR.PriceClose(SDate={date}, EDate={date}-2D)"
    #you can add date to check:
    f"TR.PriceClose(SDate={date}, EDate={date}-2D), TR.PriceClose(SDate={date}, EDate={date}-2D).date"

Answers