pyspark:计算指数移动平均值

dauxcl2d  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(619)

我想计算一下Pypark第9单元的指数移动平均值
下面是我的数据

  1. +---------+-------------------+--------+--------+--------+--------+
  2. | Symbol| DateTime| Open| High| Low| Close|
  3. +---------+-------------------+--------+--------+--------+--------+
  4. |BANKNIFTY|2019-01-01 09:15:00| 27235.5|27239.55| 27087.7| 27119.8|
  5. |BANKNIFTY|2019-01-01 09:30:00| 27120.3| 27123.3|27073.85| 27103.6|
  6. |BANKNIFTY|2019-01-01 09:45:00| 27104.7| 27119.1|27035.25| 27052.7|
  7. |BANKNIFTY|2019-01-01 10:00:00|27051.65|27078.35| 27038.8| 27070.7|
  8. |BANKNIFTY|2019-01-01 10:15:00| 27071|27092.85| 27061.3| 27076.9|
  9. |BANKNIFTY|2019-01-01 10:30:00| 27078.2| 27099.1|27064.95| 27079.1|
  10. |BANKNIFTY|2019-01-01 10:45:00|27077.85| 27094.5|27057.45|27085.65|
  11. |BANKNIFTY|2019-01-01 11:00:00|27082.85| 27116.5|27082.35| 27112.7|
  12. |BANKNIFTY|2019-01-01 11:15:00|27111.75| 27119.1| 27081.6|27083.35|
  13. |BANKNIFTY|2019-01-01 11:30:00|27084.25|27106.65|27080.65| 27099.3|
  14. |BANKNIFTY|2019-01-01 11:45:00| 27100.5| 27114| 27095.3|27109.15|
  15. |BANKNIFTY|2019-01-01 12:00:00|27108.95|27122.85|27105.55| 27111.9|
  16. |BANKNIFTY|2019-01-01 12:15:00| 27114.4| 27121.1|27086.55| 27087.8|
  17. |BANKNIFTY|2019-01-01 12:30:00|27087.95|27088.45| 27060.4| 27074.3|
  18. |BANKNIFTY|2019-01-01 12:45:00| 27072.8| 27081.7|27054.75| 27062.2|
  19. |BANKNIFTY|2019-01-01 13:00:00|27062.35|27094.55| 27059.5|27093.15|
  20. |BANKNIFTY|2019-01-01 13:15:00|27093.45|27094.85|27075.65|27085.05|
  21. |BANKNIFTY|2019-01-01 13:30:00| 27084.8|27087.15|27019.05| 27043.2|
  22. |BANKNIFTY|2019-01-01 13:45:00|27045.75|27068.85|27035.85|27062.35|
  23. |BANKNIFTY|2019-01-01 14:00:00|27062.45|27100.35| 27057|27080.65|
  24. |BANKNIFTY|2019-01-01 14:15:00|27081.05|27188.25| 27076.6|27188.25|
  25. |BANKNIFTY|2019-01-01 14:30:00|27186.85|27309.45| 27179.8|27294.15|
  26. |BANKNIFTY|2019-01-01 14:45:00| 27294.1|27356.85| 27294.1|27346.35|
  27. |BANKNIFTY|2019-01-01 15:00:00| 27346.1|27395.35|27326.05| 27386.8|
  28. |BANKNIFTY|2019-01-01 15:15:00| 27385|27430.55| 27380|27411.15|
  29. +---------+-------------------+--------+--------+--------+--------+

想加上ema列来计算最近9个收盘价周期的指数移动平均值吗
ema计算公式

  1. Multiplier = 2 ÷ (number of time periods + 1) => 2 ÷ (9+ 1) => 2 ÷ 10 => 0.2
  2. EMA: {Price - EMA(previous row)} x Multiplier + EMA(previous row)

期望输出

  1. +---------+----------------+--------+--------+--------+--------+--------+
  2. | Symbol| DateTime| Open| High| Low| Close| EMA|
  3. +---------+----------------+--------+--------+--------+--------+--------+
  4. |BANKNIFTY|01-01-2019 09:15| 27235.5|27239.55| 27087.7| 27119.8| |
  5. |BANKNIFTY|01-01-2019 09:30| 27120.3| 27123.3|27073.85| 27103.6| |
  6. |BANKNIFTY|01-01-2019 09:45| 27104.7| 27119.1|27035.25| 27052.7| |
  7. |BANKNIFTY|01-01-2019 10:00|27051.65|27078.35| 27038.8| 27070.7| |
  8. |BANKNIFTY|01-01-2019 10:15| 27071|27092.85| 27061.3| 27076.9| |
  9. |BANKNIFTY|01-01-2019 10:30| 27078.2| 27099.1|27064.95| 27079.1| |
  10. |BANKNIFTY|01-01-2019 10:45|27077.85| 27094.5|27057.45|27085.65| |
  11. |BANKNIFTY|01-01-2019 11:00|27082.85| 27116.5|27082.35| 27112.7| |
  12. |BANKNIFTY|01-01-2019 11:15|27111.75| 27119.1| 27081.6|27083.35|27087.17|
  13. |BANKNIFTY|01-01-2019 11:30|27084.25|27106.65|27080.65| 27099.3|27089.59|
  14. |BANKNIFTY|01-01-2019 11:45| 27100.5| 27114| 27095.3|27109.15| 27093.5|
  15. |BANKNIFTY|01-01-2019 12:00|27108.95|27122.85|27105.55| 27111.9|27097.18|
  16. |BANKNIFTY|01-01-2019 12:15| 27114.4| 27121.1|27086.55| 27087.8|27095.31|
  17. |BANKNIFTY|01-01-2019 12:30|27087.95|27088.45| 27060.4| 27074.3|27091.11|
  18. |BANKNIFTY|01-01-2019 12:45| 27072.8| 27081.7|27054.75| 27062.2|27085.32|
  19. |BANKNIFTY|01-01-2019 13:00|27062.35|27094.55| 27059.5|27093.15|27086.89|
  20. |BANKNIFTY|01-01-2019 13:15|27093.45|27094.85|27075.65|27085.05|27086.52|
  21. |BANKNIFTY|01-01-2019 13:30| 27084.8|27087.15|27019.05| 27043.2|27077.86|
  22. |BANKNIFTY|01-01-2019 13:45|27045.75|27068.85|27035.85|27062.35|27074.76|
  23. |BANKNIFTY|01-01-2019 14:00|27062.45|27100.35| 27057|27080.65|27075.93|
  24. |BANKNIFTY|01-01-2019 14:15|27081.05|27188.25| 27076.6|27188.25| 27098.4|
  25. |BANKNIFTY|01-01-2019 14:30|27186.85|27309.45| 27179.8|27294.15|27137.55|
  26. |BANKNIFTY|01-01-2019 14:45| 27294.1|27356.85| 27294.1|27346.35|27179.31|
  27. |BANKNIFTY|01-01-2019 15:00| 27346.1|27395.35|27326.05| 27386.8|27220.81|
  28. |BANKNIFTY|01-01-2019 15:15| 27385|27430.55| 27380|27411.15|27258.88|
  29. +---------+-------------------+--------+--------+--------+--------+------+

公式链接:https://sciencing.com/calculate-exponential-moving-averages-8221813.html

e5njpo68

e5njpo681#

你的问题不是很清楚,因为你的测试数据集不包含多天,所以你的ema不能使用你例子中数据的前几天。
但是,要创建数据集,您需要:

  1. from pyspark.sql import SQLContext, Window
  2. from pyspark.sql import functions as F
  3. from pyspark.sql.types import FloatType, StringType, StructField, StructType, TimestampType
  4. schema = StructType(
  5. [
  6. StructField("Symbol", StringType(), nullable=False),
  7. StructField("DateTime", TimestampType(), nullable=False),
  8. StructField("Open", FloatType(), nullable=False),
  9. StructField("High", FloatType(), nullable=False),
  10. StructField("Low", FloatType(), nullable=False),
  11. StructField("Close", FloatType(), nullable=False),
  12. StructField("Close", FloatType(), nullable=False),
  13. ]
  14. )
  15. data = [
  16. ("BANKNIFTY",2019-01-01 09:15:00, 27235.5,27239.55, 27087.7, 27119.8),
  17. ("BANKNIFTY",2019-01-01 09:30:00, 27120.3, 27123.3,27073.85, 27103.6),
  18. ("BANKNIFTY",2019-01-01 09:45:00, 27104.7, 27119.1,27035.25, 27052.7),
  19. ...
  20. ]
  21. sql_context = SQLContext(sc)
  22. df = sql_context.createDataFrame(data, schema)

从那里,你只需要创建一个窗口

  1. w = Window().partitionBy("Close").orderBy(F.col("DateTime"))

然后使用窗口创建新列

  1. df = df.withColumn(
  2. "EMA",
  3. F.avg("Close").over(w)
  4. )

注意:这里我只是用平均数,而不是你要求的均线。为了得到ema,我需要添加一个额外的列来计算每天的ema,然后在计算中使用它。

展开查看全部

相关问题