After a bunch of trial and error, I've found a combination of queries that work, and a mystery that doesn't, but seems like it should:
This query creates the Moving Averages table, RiskZ_SMAs:
INSERT INTO RiskZ_SMAs ( BarDate, SMA30, SMA10, SMA30_10 )
SELECT b.BarDate, Round((SELECT AVG(close) from VIX_DailyHist WHERE ID BETWEEN b.ID and b.ID-29),6) AS SMA30, Round((SELECT AVG(close) from VIX_DailyHist WHERE ID BETWEEN b.ID and b.ID-9),6) AS SMA10, Round(SMA30/SMA10,6) AS SMA30_10
FROM VIX_DailyHist AS b;
ID BarDate SMA30 SMA10 SMA30_10 2801 11/18/2022 24.182727 24.146 1.001521 2802 11/21/2022 24.030833 23.947 1.003501 2803 11/22/2022 23.82 23.522 1.012669 2804 11/23/2022 23.572143 22.948 1.027198 2805 11/24/2022 23.362 22.637 1.032027 2806 11/25/2022 23.183125 22.435 1.033346 2807 11/28/2022 23.125882 22.283 1.037826
This second query creates the table of all of the Moving Averages plus the calculated values from the Moving Averages table, called RiskZ_All:
SELECT b.ID, b.BarDate, b.SMA30, b.SMA10, b.SMA30_10, (SELECT Round(AVG(SMA30_10), 6) FROM RiskZ_SMAs WHERE ID BETWEEN b.ID AND b.ID-299) AS AVGSMA30_10, (SELECT Round(STDEVP(SMA30_10), 6) FROM RiskZ_SMAs WHERE ID BETWEEN b.ID AND b.ID-299) AS StDevMA30_10, Round((SMA30_10-AVGSMA30_10)/StDevMA30_10,6) AS RiskZ INTO RiskZ_All
FROM RiskZ_SMAs AS b;
ID BarDate SMA30 SMA10 SMA30_10 AVGSMA30_10 StDevMA30_10 RiskZ 2801 11/18/2022 24.182727 24.146 1.001521 1.000138 0.000437 3.16476 2802 11/21/2022 24.030833 23.947 1.003501 1.000419 0.001019 3.024534 2803 11/22/2022 23.82 23.522 1.012669 1.001361 0.003408 3.318075 2804 11/23/2022 23.572143 22.948 1.027198 1.003206 0.00742 3.233423 2805 11/24/2022 23.362 22.637 1.032027 1.005128 0.010153 2.649365 2806 11/25/2022 23.183125 22.435 1.033346 1.006891 0.01197 2.210109 2807 11/28/2022 23.125882 22.283 1.037826 1.008711 0.013706 2.124252
The mystery is when I use essentially the first query as the data source for the second query to try to collect all of the data in one place, the second set of calculations are messed up.
The third query, named qryRiskZ_SMA_v4 is:
SELECT b.ID, b.BarDate, Round((SELECT AVG(close) from VIX_DailyHist WHERE ID BETWEEN b.ID and b.ID-29), 6) AS SMA30, Round((SELECT AVG(close) from VIX_DailyHist WHERE ID BETWEEN b.ID and b.ID-9), 6) AS SMA10, Round(SMA30/SMA10, 6) AS SMA30_10
FROM VIX_DailyHist AS b;
Unsurprisingly, it generates the same data as the RiskZ_SMAs table above:
ID BarDate SMA30 SMA10 SMA30_10 11 11/18/2022 24.182727 24.146 1.001521 12 11/21/2022 24.030833 23.947 1.003501 13 11/22/2022 23.82 23.522 1.012669 14 11/23/2022 23.572143 22.948 1.027198 15 11/24/2022 23.362 22.637 1.032027 16 11/25/2022 23.183125 22.435 1.033346 17 11/28/2022 23.125882 22.283 1.037826
The fourth query that refers to the third:
SELECT b.ID, b.BarDate, b.SMA30, b.SMA10, b.SMA30_10, Round((SELECT AVG(SMA30_10) FROM qryRiskZ_SMA_v4 as c WHERE b.ID BETWEEN c.ID and c.ID-299), 6) AS AVGSMA30_10, Round((SELECT StDevP(SMA30_10) FROM qryRiskZ_SMA_v4 as d WHERE b.ID BETWEEN d.ID and d.ID-299), 6) AS StDevSMA30_10, Round((b.SMA30_10 - AVGSMA30_10)/StDevSMA30_10, 6) AS RiskZ
FROM qryRiskZ_SMA_v4 AS b;
Surprisingly, this query generates the same value in each row for the AVGSMA30_10 and StDevSMA30_10 fields:
ID BarDate SMA30 SMA10 SMA30_10 AVGSMA30_10 StDevSMA30_10 RiskZ 11 11/18/2022 24.182727 24.146 1.001521 1.013988 0.088022 -0.141635 12 11/21/2022 24.030833 23.947 1.003501 1.013988 0.088022 -0.119141 13 11/22/2022 23.82 23.522 1.012669 1.013988 0.088022 -0.014985 14 11/23/2022 23.572143 22.948 1.027198 1.013988 0.088022 0.150076 15 11/24/2022 23.362 22.637 1.032027 1.013988 0.088022 0.204937 16 11/25/2022 23.183125 22.435 1.033346 1.013988 0.088022 0.219922 17 11/28/2022 23.125882 22.283 1.037826 1.013988 0.088022 0.270819
I think the ID's in the where clauses are messed because it's acting as though they aren't even there and calculating the same average for all rows.
Cheers,
Eric