WMA
定义
public static IEnumerable<WmaResult> GetWma<TQuote>(this IEnumerable<TQuote> quotes, int lookbackPeriods) where TQuote : IQuote
描述
WMA(Weighted Moving Average) - 加权移动平均是一种移动平均线计算方法,它给予近期价格更高的权重,而远离当前时间点的价格权重较低。这使得 WMA 比 SMA(简单移动平均)对价格变化的反应更加灵敏。
参数
| 参数名 |
类型 |
描述 |
| lookbackPeriods |
Int |
回顾周期 |
返回值
| 返回值 |
类型 |
描述 |
| Date |
DateTime |
日期 |
| Wma |
double |
计算出的加权移动平均值 |
///指标数据
QuoteHistoryDay(10, (dic) =>
{
if (dic.Count > 0)
{
foreach (var item in dic.Keys)
{
///获取指标结果
var resp = dic[item].GetWma(5);
Console.WriteLine(resp.ToJson());
}
}
});
def init(self):
#初始化T+1 取消时间为120秒
self.SetConfig(1,120)
self.art = {}
self.invested = {}
logger.info("进入MyTestAlgorithm init方法:")
#添加单个指数 分钟为Resolution.Minute
self.index300 = self.AddIndex("000300.XSHG", Resolution.Daily).symbol
#添加指数下的所有 分钟为Resolution.Minute
stock_list=self.FillStocks(["000001.XSHE","000004.XSHE"],"",Resolution.Daily)
self.set_benchmark(self.index300)
# ========== 创建WMA指标 ==========
# 1. 简单移动平均 (SMA) - 确定可用
self.sma_fast = self.SMA(stock_list[0].symbol, 10, Resolution.Daily)
self.sma_slow = self.SMA(stock_list[0].symbol, 30, Resolution.Daily)
# 2. 指数移动平均 (EMA) - 确定可用
self.ema = self.EMA(stock_list[0].symbol, 20, Resolution.Daily)
# 3. 手动实现WMA (加权移动平均)
# 由于没有内置WMA,我们需要手动计算
self.period = 10 # WMA周期
self.price_history = [] # 存储价格历史
def CalculateWMA(self):
"""手动计算加权移动平均"""
if len(self.price_history) < self.period:
return 0
# 获取最近的period个价格
recent_prices = self.price_history[-self.period:]
# 计算权重:最新的权重最大
total_weight = 0
weighted_sum = 0
for i, price in enumerate(recent_prices):
weight = i + 1 # 权重从1到period
weighted_sum += price * weight
total_weight += weight
if total_weight == 0:
return 0
return weighted_sum / total_weight
#数据来了触发事件
def ondata(self,slice):
logger.info(fr"Symbol:{self.Time} {config.SymbolPool[0]} ")
current_price = slice[config.SymbolPool[0].symbol].Price
# 更新价格历史
self.price_history.append(current_price)
# 保持历史长度
if len(self.price_history) > self.period * 2:
self.price_history.pop(0)
# 计算手动WMA
wma_value = self.CalculateWMA()
logger.info(fr"wma_value:{wma_value} ")