多空线性交叉策略是一种技术分析策略,基于线性回归模型预测股票未来价格走势。策略的基本原理是:股价走势往往遵循一定的线性趋势,通过计算价格的线性回归,可以预测未来价格。当预测价格上穿当前价格时做多,下穿时平仓。
该策略首先计算一段时间内股价的线性回归。线性回归用最小二乘法拟合出一条直线,这条直线代表了价格随时间变化的趋势。策略然后在图表上绘制预测价格线和当前价格。
策略定义了两个信号:
当做多信号出现时,策略开仓做多;当做空信号出现时,平仓。
策略的关键步骤如下:
多空线性交叉策略有以下优点:
尽管多空线性交叉策略有诸多优点,但它也存在一些风险:
多空线性交叉策略以价格线性回归为基础,通过比较预测价格和当前价格产生交易信号。该策略逻辑简单清晰,可以捕捉价格的线性趋势,适用于各类行情。同时,策略易于实现和优化,可以灵活调整参数,结合其他指标,加入风控模块等,不断提高策略性能。但策略也存在识别趋势不准、参数设置不当、过拟合历史数据等风险,实际运用时需谨慎。总的来说,多空线性交叉策略是一个简单有效的量化交易策略,值得进一步探索和优化。
/*backtest start: 2024-02-25 00:00:00 end: 2024-03-26 00:00:00 period: 3h basePeriod: 15m 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 Cross", overlay=true, margin_long=100, margin_short=0) //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 = 'Strategy Length') linearlen=input(defval = 9, title = 'Linear Lookback') [a, b] = linregs(price, vol, len) // Calculate linear regression for stock price based on volume //eps = request.earnings(syminfo.ticker, earnings.actual) //MA For double confirmation out = ta.sma(close, 200) outf = ta.sma(close, 50) outn = ta.sma(close, 90) outt = ta.sma(close, 21) outthree = ta.sma(close, 9) // 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 //MACD //[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9) // Plot predicted stock price plot(predicted_price, color=color.rgb(65, 59, 150), linewidth=2, title="Predicted Price") plot(ta.sma(predicted_price,linearlen), color=color.rgb(199, 43, 64), linewidth=2, title="MA Predicted Price") //offset = input.int(title="Offset", defval=0, minval=-500, maxval=500) plot(out, color=color.blue, title="MA200") [macdLine, signalLine, histLine] = ta.macd(predicted_price, 12, 26, 9) //BUY Signal longCondition=false mafentry =ta.sma(close, 50) > ta.sma(close, 90) //matentry = ta.sma(close, 21) > ta.sma(close, 50) matwohun = close > ta.sma(close, 200) twohunraise = ta.rising(out, 2) twentyrise = ta.rising(outt, 2) macdrise = ta.rising(macdLine,2) macdlong = ta.crossover(predicted_price, ta.wma(predicted_price,linearlen)) and (signalLine < macdLine) if macdlong and macdrise longCondition := true if (longCondition) strategy.entry("My Long Entry Id", strategy.long) //Sell Signal lastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1) daysSinceEntry = len daysSinceEntry := int((time - strategy.opentrades.entry_time(strategy.opentrades - 1)) / (24 * 60 * 60 * 1000)) percentageChange = (close - lastEntryPrice) / lastEntryPrice * 100 //trailChange = (ta.highest(close,daysSinceEntry) - close) / close * 100 //label.new(bar_index, high, color=color.black, textcolor=color.white,text=str.tostring(int(trailChange))) shortCondition=false mafexit =ta.sma(close, 50) < ta.sma(close, 90) matexit = ta.sma(close, 21) < ta.sma(close, 50) matwohund = close < ta.sma(close, 200) twohunfall = ta.falling(out, 3) twentyfall = ta.falling(outt, 2) shortmafall = ta.falling(outthree, 1) macdfall = ta.falling(macdLine,1) macdsell = macdLine < signalLine if macdfall and macdsell and (macdLine < signalLine) and ta.falling(low,2) shortCondition := true if (shortCondition) strategy.entry("My Short Entry Id", strategy.short)