SQL用于以下等式。今日EMA =(价格*(2/11))+((1 - (2/11))*昨日EMA)

时间:2012-11-07 20:29:55

标签: sql ms-access ms-jet-ace

我可以请求您提供以下帮助吗?

我想创建一个MS Jet / Ace / Access查询,从价格数据表中返回10天指数移动平均线(EMA)。表格格式及其一小部分数据如下......

TableName = Spot
ID (AutoNumber) aDate (DateTime)        Settle (Double)
1               01/10/2007              16.056
2               02/10/2007              15.625
3               03/10/2007              15.655
4               04/10/2007              15.686
5               05/10/2007              15.810
6               08/10/2007              15.665
7               09/10/2007              15.908
8               10/10/2007              16.004
9               11/10/2007              16.319
10              12/10/2007              16.233

如果您不了解指数移动平均线,以下网站可能有助于解释它们......

http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:moving_averages#exponential_moving_a

http://www.pandacash.com/technical-analysis/moving-average/exponential.htm

然而,希望我可以向你解释这个等式。

Todays EMA = (Settle * Exponent) + ((1 - Exponent) * Yesterdays EMA)

其中

Settle = Closing price of the Stock / Asset
Exponent = (2 / (10 Days + 1)) ie 0.1818

我有一个查询返回我需要的一些值,但我无法找到整个事情的解决方案。

我的查询,到目前为止(希望格式化使其更清晰阅读)

SELECT aDate, Settle, 

   (SELECT((SELECT Settle 
            FROM   SPOT AS SubQ 
            WHERE  SubQ.ID = SS.ID)) 
    FROM   SPOT AS SS 
    WHERE  SS.ID = Spot.ID - 1) AS YesterdaySettle,

   ( 2 / 11 ) AS Exponent,

   (SELECT Settle 
    FROM   SPOT AS SS 
    WHERE  SS.ID = Spot.ID) * ( Exponent ) AS [Part A],

   ( 1 - Exponent ) AS [Part B],

   (SELECT ((SELECT Settle 
              FROM   SPOT AS SubQ 
              WHERE  SubQ.ID = SS.ID) * ( 2 / 11 )) 
    FROM   SPOT AS SS 
    WHERE  SS.ID = Spot.ID - 1) AS [Yesterdays Part A],

   (SELECT SUM(Settle) 
    FROM   SPOT AS SubQ 
    WHERE  ID BETWEEN Spot.ID AND Spot.ID -9) / Iif(Spot.id > 10, 10, Spot.id) AS [10DMA] 

FROM   SPOT
ORDER  BY aDate; 

根据我的查询的字段名称,我的等式将是......

Todays EMA = ([Part A])  + ([Part B] * [Part C])

我希望结果与以下相似。

[10DMA] [Exponent]  [Part A]    [Part B]    [Part C] (Previous EMA)     [EMA]
15.896  0.181818182 2.919272727 0.818181818     USE 10DMA ie 15.896     15.925
15.911  0.181818182 2.840909091 0.818181818                 15.925      15.871
15.945  0.181818182 2.846363636 0.818181818                 15.871      15.831
15.985  0.181818182 2.852       0.818181818                 15.831      15.805
16.027  0.181818182 2.874545455 0.818181818                 15.805      15.806
16.037  0.181818182 2.848181818 0.818181818                 15.806      15.780
16.052  0.181818182 2.892363636 0.818181818                 15.780      15.803
16.054  0.181818182 2.909818182 0.818181818                 15.803      15.840
16.039  0.181818182 2.967090909 0.818181818                 15.840      15.927
16.025  0.181818182 2.951454545 0.818181818                 15.927      15.983                  

问题是我无法弄清楚如何创建[Part C]字段。

有些观点......

  • 如果一天没有EMA,那么[Part C]值应该是 [10日线]。例如,第一条记录没有EMA,所以我需要 使用[10DMA]。
  • 如果昨天有EMA,那么今天使用该值[C部分]。
请告诉我一些建议。到目前为止,我偶然发现了我的情况,但我很难接受C部分的价值。

我以为我可以按照...的方式做点什么。

iif (EMA <> 0, EMA, [10DMA]) AS [Part C],
    (([Part A]) + ([Part B] * [Part C])) AS EMA

但是,我收到与

有关的错误
Circular reference caused by alias 'Part C'.

任何建议都会感激不尽。非常感谢你的时间。

约翰。

0 个答案:

没有答案