
A estratégia de ruptura de retorno de linha média é uma estratégia de negociação quantitativa típica de acompanhamento de tendências. A estratégia usa a média móvel e seu canal de desvio padrão para julgar o movimento do mercado e gerar um sinal de negociação quando o preço quebra o canal de desvio padrão.
A estratégia primeiro calcula o SMA de uma média móvel simples de N dias (o padrão é 50 dias) e, em seguida, calcula o diferencial padrão para o preço do ciclo, StdDev, com base no SMA.
Depois de entrar no mercado, a estratégia configura um stop loss. Concretamente, depois de fazer mais, a linha de stop loss é a do preço de fechamento no momento da entrada ((100 - percentual de stop loss); depois de fechar, a linha de stop loss é a do preço de fechamento no momento da entrada ((100 + percentual de stop loss)).
A estratégia tem as seguintes vantagens:
A estratégia também apresenta alguns riscos:
As soluções para os riscos são:
A estratégia ainda tem espaço para ser melhorada:
Verificar a linha média em vários períodos de tempo, evitando que a curva seja muito sensível.
Combinação com outros indicadores como MACD para avaliar tendências e desvios.
Introdução de parâmetros de otimização dinâmica de algoritmos de aprendizagem de máquina.
A estratégia de ruptura de regressão de linha média é uma estratégia de negociação quantitativa muito prática. Ela possui os benefícios de acompanhar a tendência, controlar o recuo, implementar uma estratégia simples e adequada para a necessidade de negociação quantitativa.
/*backtest
start: 2023-02-16 00:00:00
end: 2024-02-22 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Standard Deviation Bands with Buy/Sell Signals", overlay=true)
// Input for the number of standard deviations
deviationMultiplier = input.float(2.0, title="Standard Deviation Multiplier")
// Input for the length of the moving average
maLength = input.int(50, title="Moving Average Length")
// Input for the stop loss percentage
stopLossPercentage = input.float(12, title="Stop Loss Percentage")
// Calculate the moving average
sma = ta.sma(close, maLength)
// Calculate the standard deviation of the price
priceDeviation = ta.stdev(close, maLength)
// Calculate the upper and lower bands
upperBand = sma + (priceDeviation * deviationMultiplier)
lowerBand = sma - (priceDeviation * deviationMultiplier)
// Plot the bands
plot(upperBand, color=color.green, title="Upper Band")
plot(lowerBand, color=color.red, title="Lower Band")
// Plot the moving average
plot(sma, color=color.blue, title="SMA", linewidth=2)
// Buy Signal
buyCondition = ta.crossover(close, lowerBand)
sellCondition = ta.crossunder(close, upperBand)
// Calculate stop loss level
stopLossLevelBuy = close * (1 - stopLossPercentage / 100)
stopLossLevelSell = close * (1 + stopLossPercentage / 100)
// Create Buy and Sell Alerts
alertcondition(buyCondition, title="Buy Signal", message="Buy Signal - Price Crossed Below Lower Band")
alertcondition(sellCondition, title="Sell Signal", message="Sell Signal - Price Crossed Above Upper Band")
// Plot Buy and Sell Arrows on the chart
plotshape(buyCondition, style=shape.triangleup, location=location.belowbar, color=color.green, title="Buy Signal Arrow")
plotshape(sellCondition, style=shape.triangledown, location=location.abovebar, color=color.red, title="Sell Signal Arrow")
// Exit Long and Short Positions
var float stopLossBuy = na
var float stopLossSell = na
if ta.crossover(close, sma)
stopLossBuy := stopLossLevelBuy
if ta.crossunder(close, sma)
stopLossSell := stopLossLevelSell
strategy.entry("Buy", strategy.long, when = buyCondition)
strategy.exit("Stop Loss/Take Profit Buy", from_entry = "Buy", stop = stopLossBuy)
strategy.entry("Sell", strategy.short, when = sellCondition)
strategy.exit("Stop Loss/Take Profit Sell", from_entry = "Sell", stop = stopLossSell)