策略
基于成交量热图与实时价格的买卖策略-Buy-and-Sell-Volume-Heatmap-with-Real-Time-Price-Strategy
ChaoZhang · PineScript · 趋势跟踪 · 2024-05-24 17:16:58
概述
该策略结合了成交量热图和实时价格,通过分析一定时间内的价格和成交量分布,生成买卖信号。策略首先根据当前价格和设定的价格范围百分比,计算出若干个价格水平。然后统计每个价格水平在过去一段时间内的买卖成交量,并计算出累计的买卖成交量。根据累计的买卖成交量,确定标签的颜色。同时,策略还会绘制实时价格曲线。此外,该策略还结合了EMA、VWAP等指标,根据它们与价格、成交量的关系,产生买卖信号。当满足买入条件且之前没有产生过信号时,会产生买入信号;当满足卖出条件或连续两根阴线且之前没有产生过信号时,会产生卖出信号。
策略原理
- 根据当前价格和设定的价格范围百分比,计算出若干个价格水平。
- 统计每个价格水平在过去一段时间内的买卖成交量,并计算出累计的买卖成交量。
- 根据累计的买卖成交量,确定标签的颜色,并显示标签或绘制图形。
- 绘制实时价格曲线。
- 计算EMA、VWAP等指标。
- 根据价格与EMA、VWAP等指标的关系以及成交量条件,判断是否满足买入条件。若满足且之前没有产生过信号,则产生买入信号。
- 根据价格与EMA等指标的关系以及成交量条件,判断是否满足卖出条件。若满足且之前没有产生过信号,则产生卖出信号。若连续两根阴线且之前没有产生过信号,也产生卖出信号。
- 记录当前买卖条件状态,并更新信号产生状态。
优势
- 结合了成交量热图和实时价格,能够直观地展示价格和成交量的分布情况,为交易决策提供参考。
- 引入了EMA、VWAP等指标,丰富了策略的条件判断,提高了策略的可靠性。
- 同时考虑了价格、指标和成交量等多方面因素,使得买卖信号更加全面和稳健。
- 设置了信号产生的限制条件,避免了连续产生重复信号的情况,减少了误导性信号。
风险
- 策略的表现可能受到价格范围百分比、回溯期等参数设置的影响,需要根据具体情况进行调整和优化。
- EMA、VWAP等指标本身也有一定的滞后性和局限性,在某些市场环境下可能失效。
- 该策略主要适用于趋势性较强的市场,在震荡市可能产生较多的虚假信号。
- 策略的风险控制措施相对简单,缺乏止损和仓位管理等风险管理手段。
优化方向
- 引入更多的技术指标和市场情绪指标,如RSI、MACD、布林带等,丰富策略的判断依据。
- 优化买卖信号的产生条件,提高信号的准确性和可靠性。可以考虑引入多个时间框架的分析,确认趋势方向。
- 加入止损和仓位管理等风险控制措施,设置合理的止损位和仓位规模,控制单笔交易的风险敞口。
- 对策略进行参数优化和回测,找出最优的参数组合和市场适用范围。
- 考虑将该策略与其他策略进行组合,发挥不同策略的优势,提高整体的稳定性和收益性。
总结
该策略通过结合成交量热图、实时价格和多个技术指标,生成买卖信号,具有一定的参考价值。策略的优势在于能够直观展示价格和成交量的分布,并综合考虑多方面因素产生信号。但策略也存在一些局限性和风险,如参数设置的影响、指标的滞后性、趋势性市场的依赖等。因此,在实际应用中,需要进一步优化和完善策略,如引入更多指标、优化信号条件、加强风险控制等,以提升策略的稳健性和盈利能力。
源码(126 行)
/*backtest
start: 2024-04-01 00:00:00
end: 2024-04-30 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Buy and Sell Volume Heatmap with Real-Time Price Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// Settings for Volume Heatmap
lookbackPeriod = input.int(100, title="Lookback Period")
baseGreenColor = input.color(color.green, title="Buy Volume Color")
baseRedColor = input.color(color.red, title="Sell Volume Color")
priceLevels = input.int(10, title="Number of Price Levels")
priceRangePct = input.float(0.01, title="Price Range Percentage")
labelSize = input.string("small", title="Label Size", options=["tiny", "small", "normal", "large"])
showLabels = input.bool(true, title="Show Volume Labels")
// Initialize arrays to store price levels, buy volumes, and sell volumes
var float[] priceLevelsArr = array.new_float(priceLevels)
var float[] buyVolumes = array.new_float(priceLevels)
var float[] sellVolumes = array.new_float(priceLevels)
// Calculate price levels around the current price
for i = 0 to priceLevels - 1
priceLevel = close * (1 + (i - priceLevels / 2) * priceRangePct) // Adjust multiplier for desired spacing
array.set(priceLevelsArr, i, priceLevel)
// Calculate buy and sell volumes for each price level
for i = 0 to priceLevels - 1
level = array.get(priceLevelsArr, i)
buyVol = 0.0
sellVol = 0.0
for j = 1 to lookbackPeriod
if close[j] > open[j]
if close[j] >= level and low[j] <= level
buyVol := buyVol + volume[j]
else
if close[j] <= level and high[j] >= level
sellVol := sellVol + volume[j]
array.set(buyVolumes, i, buyVol)
array.set(sellVolumes, i, sellVol)
// Determine the maximum volumes for normalization
maxBuyVolume = array.max(buyVolumes)
maxSellVolume = array.max(sellVolumes)
// Initialize cumulative buy and sell volumes for the current bar
cumulativeBuyVol = 0.0
cumulativeSellVol = 0.0
// Calculate colors based on the volumes and accumulate volumes for the current bar
for i = 0 to priceLevels - 1
buyVol = array.get(buyVolumes, i)
sellVol = array.get(sellVolumes, i)
cumulativeBuyVol := cumulativeBuyVol + buyVol
cumulativeSellVol := cumulativeSellVol + sellVol
// Determine the label color based on which volume is higher
labelColor = cumulativeBuyVol > cumulativeSellVol ? baseGreenColor : baseRedColor
// Initialize variables for plotshape
var float shapePosition = na
var color shapeColor = na
if cumulativeBuyVol > 0 or cumulativeSellVol > 0
if showLabels
labelText = "Buy: " + str.tostring(cumulativeBuyVol) + "\nSell: " + str.tostring(cumulativeSellVol)
label.new(x=bar_index, y=high + (high - low) * 0.02, text=labelText, color=color.new(labelColor, 0), textcolor=color.white, style=label.style_label_down, size=labelSize)
else
shapePosition := high + (high - low) * 0.02
shapeColor := labelColor
// Plot the shape outside the local scope
plotshape(series=showLabels ? na : shapePosition, location=location.absolute, style=shape.circle, size=size.tiny, color=shapeColor)
// Plot the real-time price on the chart
plot(close, title="Real-Time Price", color=color.blue, linewidth=2, style=plot.style_line)
// Mpullback Indicator Settings
a = ta.ema(close, 9)
b = ta.ema(close, 20)
e = ta.vwap(close)
volume_ma = ta.sma(volume, 20)
// Calculate conditions for buy and sell signals
buy_condition = close > a and close > e and volume > volume_ma and close > open and low > a and low > e // Ensure close, low are higher than open, EMA, and VWAP
sell_condition = close < a and close < b and close < e and volume > volume_ma
// Store the previous buy and sell conditions
var bool prev_buy_condition = na
var bool prev_sell_condition = na
// Track if a buy or sell signal has occurred
var bool signal_occurred = false
// Generate buy and sell signals based on conditions
buy_signal = buy_condition and not prev_buy_condition and not signal_occurred
sell_signal = sell_condition and not prev_sell_condition and not signal_occurred
// Determine bearish condition (close lower than the bottom 30% of the candle's range)
bearish = close < low + (high - low) * 0.3
// Add sell signal when there are two consecutive red candles and no signal has occurred
two_consecutive_red_candles = close[1] < open[1] and close < open
sell_signal := sell_signal or (two_consecutive_red_candles and not signal_occurred)
// Remember the current conditions for the next bar
prev_buy_condition := buy_condition
prev_sell_condition := sell_condition
// Update signal occurred status
signal_occurred := buy_signal or sell_signal
// Plot buy and sell signals
plotshape(buy_signal, title="Buy", style=shape.labelup, location=location.belowbar, color=color.green, text="Buy", textcolor=color.white)
plotshape(sell_signal, title="Sell", style=shape.labeldown, location=location.abovebar, color=color.red, text="Sell", textcolor=color.white)
// Strategy entry and exit
if buy_signal
strategy.entry("Buy", strategy.long)
if sell_signal
strategy.entry("Sell", strategy.short)