线性MACD解锁交易视图中线性回归的魔力


创建日期: 2023-12-15 10:22:50 最后修改: 2023-12-15 10:22:50
复制: 0 点击次数: 927
avatar of ChaoZhang ChaoZhang
1
关注
1621
关注者

线性MACD解锁交易视图中线性回归的魔力

策略名称:动量驱动的线性MACD策略

概述:这是一个利用线性回归预测股票价格,并与MACD指标结合的量化策略。它利用线性回归分析历史价格和交易量,预测未来价格趋势。在获利机会出现时,它结合MACD指标判断入场时机。

策略原理: 1. 计算价格的线性回归系数:根据历史交易量拟合一条线性回归线,用于预测未来价格。 2. 绘制预测价格:根据步骤1中的回归系数,绘制价格的预测线。 3. 生成买入信号:当预测价格处于开盘价和收盘价之间,且MACD上涨时,产生买入信号。 4. 生成卖出信号:当MACD下降,同时价格低于预测价格时,产生卖出信号。

优势分析: 这是一个结合了统计预测和技术指标判断的策略。它利用线性回归得出价格预测,避免主观臆测。同时,MACD指标可有效判断市场买卖力道,精准捕捉机会。整体来说,这是一个系统化程度高,预测准确,风险可控的策略。

风险分析: 线性回归仅依赖历史数据,对突发事件如重大利空消息反应不敏感,可能产生错误信号。此外,参数设置如回归周期长度等,也会影响策略表现。我们建议采用vwma平滑预测价格,降低曲线抖动对策略的影响。

优化方向: 我们认为该策略可从以下几个方面进行优化: 1. 加入止损机制。当价格突破止损线时平仓,有效控制个别错误信号带来的损失。 2. 引入机器学习模型。使用更高效的模型预测价格趋势,提升策略准确性。 3. 结合情绪指标。引入市场恐惧指数等情绪指标,判断市场买卖氛围,提高策略胜率。 4. 多时框结合。不同时间周期预测可能互相验证,形成组合策略,降低单一时框的局限性。

总结: 本策略通过线性回归预测价格与MACD指标判断,形成系统化的量化交易策略。它具有预测逻辑清晰,风险可控,优化空间广阔等优势。我们相信,通过持续优化与迭代,它的表现将越来越出色。它为我们提供了利用科学预测方法进行量化交易的思路,值得我们深入研究与应用。

策略源码
/*backtest
start: 2023-12-07 00:00:00
end: 2023-12-14 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © stocktechbot

//@version=5
strategy("Linear On MACD", overlay=true, margin_long=100, margin_short=100)



fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
tolerance = input.string(title="Risk tolerance", defval = "LOW", options=["LOW", "HIGH"])

chng = 0
obv = ta.cum(math.sign(ta.change(close)) * volume)
if close < close[1] and (open < close)
    chng := 1
else if close > close[1]
    chng := 1
else
    chng := -1
obvalt = ta.cum(math.sign(chng) * volume)
//src = input(title="Source", defval=close)
src = obvalt
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])

// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
//hline(0, "Zero Line", color=color.new(#787B86, 50))
//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
//plot(macd, title="MACD", color=col_macd)
//plot(signal, title="Signal", color=col_signal)
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

//Linear Regression

vol = volume

// Function to calculate linear regression
linregs(y, x, len) =>
    ybar = math.sum(y, len)/len
    xbar = math.sum(x, len)/len
    b = math.sum((x - xbar)*(y - ybar),len)/math.sum((x - xbar)*(x - xbar),len)
    a = ybar - b*xbar
    [a, b]

// Historical stock price data
price = close

// Length of linear regression
len = input(defval = 21, title = 'Lookback')

// Calculate linear regression for stock price based on volume
[a, b] = linregs(price, vol, len)

// Predicted stock price based on volume
predicted_price = a + b*vol

// Check if predicted price is between open and close
is_between = open < predicted_price and predicted_price < close


// Plot predicted stock price
plot(predicted_price, color=color.rgb(218, 27, 132), linewidth=2, title="Predicted Stock Price")
plot(ta.vwma(predicted_price,len), color=color.rgb(199, 43, 64), linewidth=2, title="Predicted Stock Price")

//BUY Signal
lincrossunder = close > predicted_price
macdrise = ta.rising(macd,2)
//macdvollong = ta.crossover(macd, signal)
//macdlong = ta.crossover(macdLine, signalLine)
macdvollong = macd > signal
macdlong = macdLine > signalLine
longCondition=false
if macdlong and macdvollong and is_between and ta.rising(predicted_price,1)
    longCondition := true

if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)
//Sell Signal
lincrossover = close < predicted_price
macdfall = ta.falling(macd,1)
macdsell = macd < signal
shortCondition = false
risklevel = predicted_price
if (tolerance == "HIGH")
    risklevel := ta.vwma(predicted_price,len)


if macdfall and macdsell and (macdLine < signalLine) and (close < risklevel)
    shortCondition := true


if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)