MACD da estratégia de força relativa

Autora:ChaoZhang, Data: 2023-12-21 12:01:01
Tags:

img

Resumo

Esta estratégia é baseada em dois indicadores bem conhecidos: MACD e Relative Strength (RS). Ao acoplá-los, obtemos sinais de compra poderosos. Na verdade, a característica especial desta estratégia é que ela cria um indicador a partir de um indicador. Assim, construímos um MACD cuja fonte é o valor do RS. A estratégia só toma sinais de compra, ignorando sinais de venda, pois eles são principalmente perdedores. Há também um método de gerenciamento de dinheiro que nos permite reinvestir parte dos lucros ou reduzir o tamanho das ordens em caso de perdas substanciais.

Estratégia lógica

RS é um indicador que mede a anomalia entre o impulso e a suposição de eficiência do mercado. É usado por profissionais e é um dos indicadores mais robustos. A ideia é possuir ativos que funcionam melhor do que a média, com base em seu desempenho passado.

RS = Preço corrente / Máximo máximo durante o período de RS Duração

Assim, podemos situar o preço atual em relação ao seu preço mais elevado durante este período definido pelo utilizador.

O MACD é um dos indicadores mais conhecidos, medindo a distância entre duas médias móveis exponenciais: uma rápida e outra mais lenta. Uma distância larga indica impulso rápido e vice-versa. Vamos traçar o valor dessa distância e chamar esta linha de macdline. O MACD usa uma terceira média móvel com um período menor que os dois primeiros. Esta última média móvel dará um sinal quando cruzar a macdline.

É importante notar que os dois primeiros MAs são construídos usando os valores de RS como fonte. Assim, nós construímos um indicador de um indicador.

Análise das vantagens

Esta estratégia combina dois indicadores individualmente muito poderosos: MACD e RS. O MACD é capaz de capturar tendências de curto prazo e mudanças de momento, enquanto o RS reflete a robustez das tendências de médio a longo prazo. Usando-os juntos considera fatores de curto e longo prazo, tornando os sinais de compra mais confiáveis.

Além disso, a estratégia é muito única, derivando o MACD do indicador RS, aumentando de forma criativa o efeito da estratégia.

Por último, a estratégia dispõe de mecanismos de gestão de riscos e de stop loss que controlam eficazmente os riscos e limitam as perdas por transação.

Análise de riscos

O maior risco desta estratégia é a possibilidade de os indicadores RS e MACD darem sinais errados. Embora ambos os indicadores sejam robustos, nenhum indicador técnico pode prever 100% o futuro e os sinais podem ocasionalmente falhar. Além disso, o próprio RS é tendencioso em relação ao julgamento de tendências de médio e longo prazo e pode produzir sinais enganosos no curto prazo.

Para reduzir os riscos, os parâmetros do RS e do MACD podem ser ajustados para se adequarem melhor a instrumentos de negociação específicos e ambientes de mercado. Além disso, um intervalo de stop loss mais rigoroso pode ser imposto.

Orientações para a melhoria

Em primeiro lugar, teste qual mercado (por exemplo, ações, forex, criptomoedas, etc.) dá o melhor efeito desta estratégia, em seguida, concentre-se nesse ativo ideal.

Em segundo lugar, tente utilizar algoritmos de aprendizagem de máquina para otimizar automaticamente os parâmetros RS e MACD em vez de fixá-los manualmente.

Em terceiro lugar, considerar a incorporação de outros indicadores para estabelecer sinais de negociação, formando um modelo multifator para melhorar a precisão do sinal.

Conclusão

Esta estratégia aproveita os indicadores MACD e RS sinergicamente para fornecer sinais de compra fortes. Sua novidade reside em derivar o MACD do indicador RS, realizando acoplamento entre os indicadores para melhorar a eficácia. A estratégia tem mecanismos claros de entrada, stop loss e gerenciamento de dinheiro que controlam efetivamente os riscos. Os próximos passos podem ser melhorar ainda mais a estratégia por meio da otimização de parâmetros, refinamento da geração de sinal, adição de outros fatores, etc.


/*backtest
start: 2022-12-14 00:00:00
end: 2023-12-20 00:00:00
period: 1d
basePeriod: 1h
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/
// © gsanson66


//This strategy calculates the Relative Strength and plot the MACD of this Relative Strenght
//We take only buy signals send by MACD
//@version=5
strategy("MACD OF RELATIVE STRENGHT STRATEGY", shorttitle="MACD RS STRATEGY", precision=4, overlay=false, initial_capital=1000, default_qty_type=strategy.cash, default_qty_value=950, commission_type=strategy.commission.percent, commission_value=0.18, slippage=3)


//------------------------------TOOL TIPS--------------------------------//

t1 = "Relative Strength length i.e. number of candles back to find the highest high and compare the current price with this high."
t2 = "Relative Strength fast EMA length used to plot the MACD."
t3 = "Relative Strength slow EMA length used to plot the MACD."
t4 = "Macdline SMA length used to plot the MACD."
t5 = "The maximum loss a trade can incur (in percentage of the trade value)"
t6 = "Each gain or losse (relative to the previous reference) in an amount equal to this fixed ratio will change quantity of orders."
t7 = "The amount of money to be added to or subtracted from orders once the fixed ratio has been reached."


//----------------------------------------FUNCTIONS---------------------------------------//

//@function Displays text passed to `txt` when called.
debugLabel(txt, color, loc) =>
    label.new(bar_index, loc, text=txt, color=color, style=label.style_label_lower_right, textcolor=color.black, size=size.small)

//@function which looks if the close date of the current bar falls inside the date range
inBacktestPeriod(start, end) => (time >= start) and (time <= end)


//---------------------------------------USER INPUTS--------------------------------------//

//Technical parameters
rs_lenght = input.int(defval=300, minval=1, title="RS Length", group="Technical parameters", tooltip=t1)
fast_length = input(title="MACD Fast Length", defval=14, group="Technical parameters", tooltip=t2)
slow_length = input(title="MACD Slow Length", defval=26, group="Technical parameters", tooltip=t3)
signal_length = input.int(title="MACD Signal Smoothing",  minval=1, maxval=50, defval=10, group="Technical parameters", tooltip=t4)
//Risk Management
slMax = input.float(8, "Max risk per trade (in %)", minval=0, group="Risk Management", tooltip=t5)
//Money Management
fixedRatio = input.int(defval=400, minval=1, title="Fixed Ratio Value ($)", group="Money Management", tooltip=t6)
increasingOrderAmount = input.int(defval=200, minval=1, title="Increasing Order Amount ($)", group="Money Management", tooltip=t7)
//Backtesting period
startDate = input(title="Start Date", defval=timestamp("1 Jan 2020 00:00:00"), group="Backtesting Period")
endDate = input(title="End Date", defval=timestamp("1 July 2024 00:00:00"), group="Backtesting Period")


//----------------------------------VARIABLES INITIALISATION-----------------------------//
strategy.initial_capital = 50000
//Relative Strenght Calculation
rs = close/ta.highest(high, rs_lenght)
//MACD of RS Calculation
[macdLine, signalLine, histLine] = ta.macd(rs, fast_length, slow_length, signal_length)
//Money management
equity = math.abs(strategy.equity - strategy.openprofit)
var float capital_ref = strategy.initial_capital
var float cashOrder = strategy.initial_capital * 0.95
//Backtesting period
bool inRange = na


//------------------------------CHECKING SOME CONDITIONS ON EACH SCRIPT EXECUTION-------------------------------//

//Checking if the date belong to the range
inRange := true

//Checking performances of the strategy
if equity > capital_ref + fixedRatio
    spread = (equity - capital_ref)/fixedRatio
    nb_level = int(spread)
    increasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder + increasingOrder
    capital_ref := capital_ref + nb_level*fixedRatio
if equity < capital_ref - fixedRatio
    spread = (capital_ref - equity)/fixedRatio
    nb_level = int(spread)
    decreasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder - decreasingOrder
    capital_ref := capital_ref - nb_level*fixedRatio

//Checking if we close all trades in case where we exit the backtesting period
if strategy.position_size!=0 and not inRange
    strategy.close_all()
    debugLabel("END OF BACKTESTING PERIOD : we close the trade", color=color.rgb(116, 116, 116), loc=macdLine)


//-----------------------------------EXIT SIGNAL------------------------------//

if strategy.position_size>0 and histLine<0
    strategy.close("Long")


//-------------------------------BUY CONDITION-------------------------------------//

if histLine>0 and not (strategy.position_size>0) and inRange
    qty = cashOrder/close
    stopLoss = close*(1-slMax/100)
    strategy.entry("Long", strategy.long, qty)
    strategy.exit("Exit Long", "Long", stop=stopLoss)


//---------------------------------PLOTTING ELEMENT----------------------------------//

hline(0, "Zero Line", color=color.new(#787B86, 50))
plot(macdLine, title="MACD", color=color.blue)
plot(signalLine, title="Signal", color=color.orange)
plot(histLine, title="Histogram", style=plot.style_columns, color=(histLine>=0 ? (histLine[1] < histLine ? #26A69A : #B2DFDB) : (histLine[1] < histLine ? #FFCDD2 : #FF5252)))
plotchar(rs, "Relative Strenght", "", location.top, color=color.yellow)

Mais.