您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# My语言怎么实现成交量指数加权策略
## 一、策略原理概述
成交量指数加权策略(Volume Weighted Moving Average, VWMA)是一种将成交量信息融入价格均线的技术指标。与传统简单移动平均线(SMA)不同,VWMA通过赋予不同交易日的价格不同的权重(成交量越大权重越高),从而更真实地反映市场实际交易情况。
### 核心公式
VWMA = Σ(Price * Volume) / ΣVolume
其中:
- Price:当前K线价格(可选用收盘价/开盘价等)
- Volume:当前K线成交量
## 二、My语言实现基础版VWMA
### 1. 基础参数设置
```my
//@version=5
strategy("VWMA Strategy", overlay=true)
// 参数输入
length = input.int(20, title="均线周期", minval=1)
src = input(close, title="价格源")
// 计算加权和
sum_price_volume = 0.0
sum_volume = 0.0
for i = 0 to length - 1
sum_price_volume := sum_price_volume + src[i] * volume[i]
sum_volume := sum_volume + volume[i]
// 计算VWMA
vwma = sum_price_volume / sum_volume
plot(vwma, title="VWMA", color=color.blue, linewidth=2)
// 生成交易信号
longCondition = ta.crossover(close, vwma)
shortCondition = ta.crossunder(close, vwma)
// 执行交易
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
// 添加快速线参数
fast_length = input.int(10, title="快速线周期")
// 计算快速VWMA
sum_price_volume_fast = 0.0
sum_volume_fast = 0.0
for i = 0 to fast_length - 1
sum_price_volume_fast := sum_price_volume_fast + src[i] * volume[i]
sum_volume_fast := sum_volume_fast + volume[i]
vwma_fast = sum_price_volume_fast / sum_volume_fast
// 双线交叉策略
dualConditionLong = ta.crossover(vwma_fast, vwma)
dualConditionShort = ta.crossunder(vwma_fast, vwma)
// 根据波动率调整周期
atr_length = input(14, "ATR周期")
atr_value = ta.atr(atr_length)
dynamic_length = math.round(math.max(10, 20 - (atr_value / close * 100)))
// 添加成交量阈值
vol_threshold = input(100000, title="最小成交量")
valid_volume = volume > vol_threshold
// 修改信号条件
filteredLong = longCondition and valid_volume
filteredShort = shortCondition and valid_volume
// 获取更高时间框架数据
higher_vwma = request.security(syminfo.tickerid, "D", vwma)
// 结合多时间框架信号
multiTF_long = longCondition and close > higher_vwma
multiTF_short = shortCondition and close < higher_vwma
//@version=5
strategy("Advanced VWMA Strategy", overlay=true, margin_long=100, margin_short=100)
// 参数设置
length = input.int(20, title="基础周期")
fast_length = input.int(10, title="快速周期")
vol_filter = input(true, title="启用成交量过滤")
vol_threshold = input(100000, title="成交量阈值")
use_multi_tf = input(true, title="启用多时间框架")
// VWMA计算函数
vwma_function(calc_length) =>
sum_pv = 0.0
sum_vol = 0.0
for i = 0 to calc_length - 1
sum_pv := sum_pv + close[i] * volume[i]
sum_vol := sum_vol + volume[i]
sum_pv / sum_vol
// 计算指标
vwma_slow = vwma_function(length)
vwma_fast = vwma_function(fast_length)
higher_vwma = request.security(syminfo.tickerid, "D", vwma_slow)
// 信号生成
basic_long = ta.crossover(close, vwma_slow)
basic_short = ta.crossunder(close, vwma_slow)
dual_long = ta.crossover(vwma_fast, vwma_slow)
dual_short = ta.crossunder(vwma_fast, vwma_slow)
// 条件过滤
valid_volume = volume > vol_threshold or not vol_filter
higher_tf_cond = close > higher_vwma or not use_multi_tf
// 最终信号
enter_long = (basic_long or dual_long) and valid_volume and higher_tf_cond
enter_short = (basic_short or dual_short) and valid_volume and not higher_tf_cond
// 执行交易
if (enter_long)
strategy.entry("Long", strategy.long)
if (enter_short)
strategy.entry("Short", strategy.short)
// 绘制指标
plot(vwma_slow, title="VWMA Slow", color=color.blue)
plot(vwma_fast, title="VWMA Fast", color=color.red)
参数优化建议:
品种适配性:
风险控制:
// 添加止损止盈
stop_loss = input(1.0, title="止损百分比") / 100
take_profit = input(2.0, title="止盈百分比") / 100
strategy.exit("Exit", loss=close * stop_loss, profit=close * take_profit)
优势: - 相比SMA更能反映真实交易成本 - 在放量突破时信号更可靠 - 可有效过滤低成交量下的假突破
局限性: - 在持续缩量行情中可能失效 - 对数据精度要求较高(需准确成交量数据) - 不适合极短线交易(TICK级别)
结合其他指标:
rsi_value = ta.rsi(close, 14)
macd_line = ta.ema(close, 12) - ta.ema(close, 26)
构建多品种对冲系统:
corr_threshold = input(0.7, title="相关性阈值")
机器学习参数优化:
// 可通过外部Python脚本优化参数
提示:实际应用中建议先进行3年以上历史数据回测,并至少包含一次完整牛熊周期。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。