关于expma和ema以及macd的问题

2025-04-09 14:50:45
推荐回答(1个)
回答1:

  楼主是不是要自己写程序来计算 EMA 和 MACD 啊?

  粘贴一段我写的 SQL Server 的 函数,用于返回股票的 EMA 数据的,搂主可以参考参考。

  如果楼主不是要写程序自己计算的话……

  只要知道 金叉买入 死叉卖出就好了,呵呵。

  execute dbo.sp_executesql @statement = N'-- =============================================
  -- Author:
  -- Create date: <2010.8.6>
  -- Description: <股票 移动指数平均 函数>
  -- 第一个参数:股票代码
  -- 第二个参数:多少天的均线
  -- 返回:该股票的 指定日期均线数据.
  -- =============================================
  CREATE FUNCTION [dbo].[EmaAllDay]
  (
  @StockCode AS varchar(10),
  @EmaDays AS int
  )
  RETURNS @ema TABLE
  (
  StockCode varchar(10) NOT NULL,
  BusinessDay [datetime] NOT NULL,
  EmaOpenPrice [decimal](10, 3) NOT NULL,
  EmaHighPrice [decimal](10, 3) NOT NULL,
  EmaLowPrice [decimal](10, 3) NOT NULL,
  EmaClosePrice [decimal](10, 3) NOT NULL,
  EmaTransactNumber [decimal](15, 0) NOT NULL,
  EmaTransactAmount [decimal](20, 0) NOT NULL
  )
  AS
  BEGIN

  DECLARE
  @KValue AS [decimal](10, 5),
  @EmaBusinessDay AS [datetime],
  @EmaOpenPrice AS [decimal](10, 3),
  @EmaHighPrice AS [decimal](10, 3),
  @EmaLowPrice AS [decimal](10, 3),
  @EmaClosePrice AS [decimal](10, 3),
  @EmaTransactNumber AS [decimal](15, 0),
  @EmaTransactAmount AS [decimal](20, 0),

  @BusinessDay AS [datetime],
  @OpenPrice AS [decimal](10, 3),
  @HighPrice AS [decimal](10, 3),
  @LowPrice AS [decimal](10, 3),
  @ClosePrice AS [decimal](10, 3),
  @TransactNumber AS [decimal](15, 0),
  @TransactAmount AS [decimal](20, 0);

  DECLARE C CURSOR FAST_FORWARD FOR
  SELECT
  business_day
  ,open_price
  ,high_price
  ,low_price
  ,close_price
  ,transact_number
  ,transact_amount
  FROM
  stock_day
  WHERE
  stock_code = @StockCode
  ORDER BY
  business_day;

  -- 首先计算第一个 简单移动品均值.
  SELECT
  top 1
  @EmaBusinessDay = BusinessDay,
  @EmaOpenPrice = MaOpenPrice,
  @EmaHighPrice = MaHighPrice,
  @EmaLowPrice = MaLowPrice,
  @EmaClosePrice = MaClosePrice,
  @EmaTransactNumber = MaTransactNumber,
  @EmaTransactAmount = MaTransactAmount
  FROM
  MaAllDay(@StockCode, @EmaDays)
  ORDER BY
  BusinessDay;

  -- 第一个 移动指数平均 = 简单移动平均
  INSERT INTO @ema (
  StockCode, BusinessDay, EmaOpenPrice,
  EmaHighPrice, EmaLowPrice, EmaClosePrice,
  EmaTransactNumber, EmaTransactAmount
  ) VALUES (
  @StockCode, @EmaBusinessDay, @EmaOpenPrice,
  @EmaHighPrice, @EmaLowPrice, @EmaClosePrice,
  @EmaTransactNumber, @EmaTransactAmount
  )

  -- 打开游标,开始计算后面的 指数移动平均
  OPEN C;

  -- 填充数据.
  FETCH NEXT FROM C INTO @BusinessDay,
  @OpenPrice,
  @HighPrice,
  @LowPrice,
  @ClosePrice,
  @TransactNumber,
  @TransactAmount;

  -- 指数移动平均 = 今天 * K + 昨天的EMA * (1-K)
  -- K = 2 / (N+1)
  -- N = EMA 天数
  -- 注意:这里要写 @EmaDays + 1.0, 因为 @EmaDays 为整数型, 计算结果会被取整, 1.0 使计算结果为小数.
  SET @KValue = 2 / (@EmaDays + 1.0);

  WHILE @@fetch_status = 0
  BEGIN
  IF @EmaBusinessDay < @BusinessDay
  BEGIN
  -- 当每天的数据的日期,大于 第一个 简单移动品均 的日期后,才开始计算.

  -- 指数移动平均 = 今天 * K + 昨天的EMA * (1-K)
  SET @EmaOpenPrice = @OpenPrice * @KValue + @EmaOpenPrice * (1 - @KValue);
  SET @EmaHighPrice = @HighPrice * @KValue + @EmaHighPrice * (1 - @KValue);
  SET @EmaLowPrice = @LowPrice * @KValue + @EmaLowPrice * (1 - @KValue);
  SET @EmaClosePrice = @ClosePrice * @KValue + @EmaClosePrice * (1 - @KValue);
  SET @EmaTransactNumber = @TransactNumber * @KValue + @EmaTransactNumber * (1 - @KValue);
  SET @EmaTransactAmount = @TransactAmount * @KValue + @EmaTransactAmount * (1 - @KValue);

  -- 插入到返回数据表中.
  INSERT INTO @ema (
  StockCode, BusinessDay, EmaOpenPrice,
  EmaHighPrice, EmaLowPrice, EmaClosePrice,
  EmaTransactNumber, EmaTransactAmount
  ) VALUES (
  @StockCode, @BusinessDay, @EmaOpenPrice,
  @EmaHighPrice, @EmaLowPrice, @EmaClosePrice,
  @EmaTransactNumber, @EmaTransactAmount
  )

  END

  -- 填充 下一条 数据.
  FETCH NEXT FROM C INTO @BusinessDay,
  @OpenPrice,
  @HighPrice,
  @LowPrice,
  @ClosePrice,
  @TransactNumber,
  @TransactAmount;
  END

  -- 关闭游标.
  CLOSE C;
  -- 释放游标.
  DEALLOCATE C;

  RETURN;

  END